From 6cae63c1354ac7236e07b3a1464f73f8051cd76f Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 24 Apr 2019 08:26:49 -0400 Subject: [PATCH] Move new macros to geo-types; reexport in geo. --- geo-types/src/lib.rs | 3 +++ {geo => geo-types}/src/macros.rs | 28 ++++++++++---------- geo/src/algorithm/convexhull.rs | 37 +++++++++++++++++---------- geo/src/algorithm/rotate.rs | 44 ++++++++++++++++++++------------ geo/src/lib.rs | 7 ++--- 5 files changed, 69 insertions(+), 50 deletions(-) rename {geo => geo-types}/src/macros.rs (87%) diff --git a/geo-types/src/lib.rs b/geo-types/src/lib.rs index f52f8b820..b777db5a0 100644 --- a/geo-types/src/lib.rs +++ b/geo-types/src/lib.rs @@ -53,6 +53,9 @@ pub use crate::triangle::Triangle; mod rect; pub use crate::rect::Rect; +#[macro_use] +mod macros; + #[doc(hidden)] pub mod private_utils; diff --git a/geo/src/macros.rs b/geo-types/src/macros.rs similarity index 87% rename from geo/src/macros.rs rename to geo-types/src/macros.rs index 939c7e144..674b47ff6 100644 --- a/geo/src/macros.rs +++ b/geo-types/src/macros.rs @@ -9,11 +9,11 @@ /// Creating a [`Point`], supplying x/y values: /// /// ``` -/// use geo::point; +/// use geo_types::point; /// /// let p = point!(x: 181.2, y: 51.79); /// -/// assert_eq!(p, geo::Point(geo::Coordinate { +/// assert_eq!(p, geo_types::Point(geo_types::Coordinate { /// x: 181.2, /// y: 51.79, /// })); @@ -38,7 +38,7 @@ macro_rules! point { /// Creating a [`LineString`], supplying x/y values: /// /// ``` -/// use geo::line_string; +/// use geo_types::line_string; /// /// let ls = line_string![ /// (x: -21.95156, y: 64.1446), @@ -47,7 +47,7 @@ macro_rules! point { /// (x: -21.951445, y: 64.145508) /// ]; /// -/// assert_eq!(ls[1], geo::Coordinate { +/// assert_eq!(ls[1], geo_types::Coordinate { /// x: -21.951, /// y: 64.14479 /// }); @@ -56,16 +56,16 @@ macro_rules! point { /// Creating a [`LineString`], supplying [`Coordinate`]s: /// /// ``` -/// use geo::line_string; +/// use geo_types::line_string; /// -/// let coord1 = geo::Coordinate { x: -21.95156, y: 64.1446 }; -/// let coord2 = geo::Coordinate { x: -21.951, y: 64.14479 }; -/// let coord3 = geo::Coordinate { x: -21.95044, y: 64.14527 }; -/// let coord4 = geo::Coordinate { x: -21.951445, y: 64.145508 }; +/// let coord1 = geo_types::Coordinate { x: -21.95156, y: 64.1446 }; +/// let coord2 = geo_types::Coordinate { x: -21.951, y: 64.14479 }; +/// let coord3 = geo_types::Coordinate { x: -21.95044, y: 64.14527 }; +/// let coord4 = geo_types::Coordinate { x: -21.951445, y: 64.145508 }; /// /// let ls = line_string![coord1, coord2, coord3, coord4]; /// -/// assert_eq!(ls[1], geo::Coordinate { +/// assert_eq!(ls[1], geo_types::Coordinate { /// x: -21.951, /// y: 64.14479 /// }); @@ -119,7 +119,7 @@ macro_rules! line_string { /// Creating a [`Polygon`] without interior rings, supplying x/y values: /// /// ``` -/// use geo::polygon; +/// use geo_types::polygon; /// /// let poly = polygon![ /// (x: -111., y: 45.), @@ -130,14 +130,14 @@ macro_rules! line_string { /// /// assert_eq!( /// poly.exterior()[1], -/// geo::Coordinate { x: -111., y: 41. }, +/// geo_types::Coordinate { x: -111., y: 41. }, /// ); /// ``` /// /// Creating a [`Polygon`], supplying x/y values: /// /// ``` -/// use geo::polygon; +/// use geo_types::polygon; /// /// let poly = polygon!( /// exterior: [ @@ -158,7 +158,7 @@ macro_rules! line_string { /// /// assert_eq!( /// poly.exterior()[1], -/// geo::Coordinate { x: -111., y: 41. }, +/// geo_types::Coordinate { x: -111., y: 41. }, /// ); /// ``` /// diff --git a/geo/src/algorithm/convexhull.rs b/geo/src/algorithm/convexhull.rs index 60730eb26..f1355cc2e 100644 --- a/geo/src/algorithm/convexhull.rs +++ b/geo/src/algorithm/convexhull.rs @@ -114,16 +114,29 @@ pub trait ConvexHull { /// # Examples /// /// ``` - /// use geo::{Point, LineString, Polygon}; + /// use geo::{line_string, polygon}; /// use geo::convexhull::ConvexHull; + /// /// // an L shape - /// let coords = vec![(0.0, 0.0), (4.0, 0.0), (4.0, 1.0), (1.0, 1.0), (1.0, 4.0), (0.0, 4.0), (0.0, 0.0)]; - /// let ls: LineString<_> = coords.iter().map(|e| Point::new(e.0, e.1)).collect(); - /// let poly = Polygon::new(ls, vec![]); + /// let poly = polygon![ + /// (x: 0.0, y: 0.0), + /// (x: 4.0, y: 0.0), + /// (x: 4.0, y: 1.0), + /// (x: 1.0, y: 1.0), + /// (x: 1.0, y: 4.0), + /// (x: 0.0, y: 4.0), + /// (x: 0.0, y: 0.0), + /// ]; /// /// // The correct convex hull coordinates - /// let hull_coords = vec![(4.0, 0.0), (4.0, 1.0), (1.0, 4.0), (0.0, 4.0), (0.0, 0.0), (4.0, 0.0)]; - /// let correct_hull: LineString<_> = hull_coords.iter().map(|e| Point::new(e.0, e.1)).collect(); + /// let correct_hull = line_string![ + /// (x: 4.0, y: 0.0), + /// (x: 4.0, y: 1.0), + /// (x: 1.0, y: 4.0), + /// (x: 0.0, y: 4.0), + /// (x: 0.0, y: 0.0), + /// (x: 4.0, y: 0.0), + /// ]; /// /// let res = poly.convex_hull(); /// assert_eq!(res.exterior(), &correct_hull); @@ -197,7 +210,7 @@ where #[cfg(test)] mod test { use super::*; - use crate::{line_string, Coordinate, Point}; + use crate::{line_string, polygon, Coordinate, Point}; #[test] fn quick_hull_test1() { @@ -341,7 +354,7 @@ mod test { (x: -1.0, y: -1.0), (x: -10.0, y: 0.0), (x: -1.0, y: 1.0), - (x: 0.0, y: 10.0) + (x: 0.0, y: 10.0), ]; let correct = vec![ Coordinate::from((0.0, -10.0)), @@ -370,12 +383,8 @@ mod test { } #[test] fn quick_hull_multipolygon_test() { - let ls1 = - line_string![(x: 0.0, y: 0.0), (x: 1.0, y: 10.0), (x: 2.0, y: 0.0), (x: 0.0, y: 0.0)]; - let ls2 = - line_string![(x: 3.0, y: 0.0), (x: 4.0, y: 10.0), (x: 5.0, y: 0.0), (x: 3.0, y: 0.0)]; - let p1 = Polygon::new(ls1, vec![]); - let p2 = Polygon::new(ls2, vec![]); + let p1 = polygon![(x: 0.0, y: 0.0), (x: 1.0, y: 10.0), (x: 2.0, y: 0.0), (x: 0.0, y: 0.0)]; + let p2 = polygon![(x: 3.0, y: 0.0), (x: 4.0, y: 10.0), (x: 5.0, y: 0.0), (x: 3.0, y: 0.0)]; let mp = MultiPolygon(vec![p1, p2]); let correct = vec![ Coordinate::from((5.0, 0.0)), diff --git a/geo/src/algorithm/rotate.rs b/geo/src/algorithm/rotate.rs index dc3f47bbd..f5662401d 100644 --- a/geo/src/algorithm/rotate.rs +++ b/geo/src/algorithm/rotate.rs @@ -217,7 +217,7 @@ where #[cfg(test)] mod test { use super::*; - use crate::{line_string, point, polygon, Coordinate, LineString, Point, Polygon}; + use crate::{line_string, point, polygon, Coordinate, Point}; #[test] fn test_rotate_around_point() { @@ -274,23 +274,33 @@ mod test { } #[test] fn test_rotate_polygon_holes() { - let ls1 = line_string![ - (x: 5.0, y: 1.0), - (x: 4.0, y: 2.0), - (x: 4.0, y: 3.0), - (x: 5.0, y: 4.0), - (x: 6.0, y: 4.0), - (x: 7.0, y: 3.0), - (x: 7.0, y: 2.0), - (x: 6.0, y: 1.0), - (x: 5.0, y: 1.0) + let poly1 = polygon![ + exterior: [ + (x: 5.0, y: 1.0), + (x: 4.0, y: 2.0), + (x: 4.0, y: 3.0), + (x: 5.0, y: 4.0), + (x: 6.0, y: 4.0), + (x: 7.0, y: 3.0), + (x: 7.0, y: 2.0), + (x: 6.0, y: 1.0), + (x: 5.0, y: 1.0) + ], + interiors: [ + [ + (x: 5.0, y: 1.3), + (x: 5.5, y: 2.0), + (x: 6.0, y: 1.3), + (x: 5.0, y: 1.3), + ], + [ + (x: 5., y: 2.3), + (x: 5.5, y: 3.0), + (x: 6., y: 2.3), + (x: 5., y: 2.3), + ], + ], ]; - - let ls2 = LineString::from(vec![(5.0, 1.3), (5.5, 2.0), (6.0, 1.3), (5.0, 1.3)]); - - let ls3 = LineString::from(vec![(5., 2.3), (5.5, 3.0), (6., 2.3), (5., 2.3)]); - - let poly1 = Polygon::new(ls1, vec![ls2, ls3]); let rotated = poly1.rotate(-15.0); let correct_outside = vec![ Coordinate::from((4.628808519201685, 1.180520783117658)), diff --git a/geo/src/lib.rs b/geo/src/lib.rs index 9e1596274..4b6498a55 100644 --- a/geo/src/lib.rs +++ b/geo/src/lib.rs @@ -16,8 +16,8 @@ pub use crate::traits::ToGeo; pub use crate::types::*; pub use geo_types::{ - Coordinate, CoordinateType, Geometry, GeometryCollection, Line, LineString, MultiLineString, - MultiPoint, MultiPolygon, Point, Polygon, Rect, Triangle, + line_string, point, polygon, Coordinate, CoordinateType, Geometry, GeometryCollection, Line, + LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon, Rect, Triangle, }; /// This module includes all the functions of geometric calculations @@ -26,9 +26,6 @@ mod traits; mod types; mod utils; -#[macro_use] -mod macros; - #[cfg(test)] #[macro_use] extern crate approx;