diff --git a/geo-types/CHANGES.md b/geo-types/CHANGES.md index 52ebfdc88..67c26620e 100644 --- a/geo-types/CHANGES.md +++ b/geo-types/CHANGES.md @@ -2,6 +2,9 @@ ## Unreleased +* the `geometry` module now re-exports all the inner geometry variants, so you + can `use geo_types::geometry::*` to concisely include `Point`, `LineString`, etc. + * * You may now specify `Geometry` rather than `Geometry` since we've added a default trait implementation. You may still explicitly declare the numeric type as f64, or any other implementation of `CoordNum`, but this should save diff --git a/geo-types/src/error.rs b/geo-types/src/error.rs index 89b2196b6..fab140eff 100644 --- a/geo-types/src/error.rs +++ b/geo-types/src/error.rs @@ -38,7 +38,7 @@ mod test { let failure = Point::try_from(rect_geometry).unwrap_err(); assert_eq!( failure.to_string(), - "Expected a geo_types::point::Point, but found a geo_types::rect::Rect" + "Expected a geo_types::geometry::point::Point, but found a geo_types::geometry::rect::Rect" ); } } diff --git a/geo-types/src/coordinate.rs b/geo-types/src/geometry/coordinate.rs similarity index 100% rename from geo-types/src/coordinate.rs rename to geo-types/src/geometry/coordinate.rs diff --git a/geo-types/src/geometry_collection.rs b/geo-types/src/geometry/geometry_collection.rs similarity index 100% rename from geo-types/src/geometry_collection.rs rename to geo-types/src/geometry/geometry_collection.rs diff --git a/geo-types/src/line.rs b/geo-types/src/geometry/line.rs similarity index 100% rename from geo-types/src/line.rs rename to geo-types/src/geometry/line.rs diff --git a/geo-types/src/line_string.rs b/geo-types/src/geometry/line_string.rs similarity index 100% rename from geo-types/src/line_string.rs rename to geo-types/src/geometry/line_string.rs diff --git a/geo-types/src/geometry.rs b/geo-types/src/geometry/mod.rs similarity index 92% rename from geo-types/src/geometry.rs rename to geo-types/src/geometry/mod.rs index e2eef308f..5a950fb07 100644 --- a/geo-types/src/geometry.rs +++ b/geo-types/src/geometry/mod.rs @@ -1,7 +1,29 @@ -use crate::{ - CoordNum, Error, GeometryCollection, Line, LineString, MultiLineString, MultiPoint, - MultiPolygon, Point, Polygon, Rect, Triangle, -}; +pub(crate) mod coordinate; +pub(crate) mod geometry_collection; +pub(crate) mod line; +pub(crate) mod line_string; +pub(crate) mod multi_line_string; +pub(crate) mod multi_point; +pub(crate) mod multi_polygon; +pub(crate) mod point; +pub(crate) mod polygon; +pub(crate) mod rect; +pub(crate) mod triangle; + +// re-export all the geometry variants: +pub use coordinate::Coordinate; +pub use geometry_collection::GeometryCollection; +pub use line::Line; +pub use line_string::LineString; +pub use multi_line_string::MultiLineString; +pub use multi_point::MultiPoint; +pub use multi_polygon::MultiPolygon; +pub use point::Point; +pub use polygon::Polygon; +pub use rect::Rect; +pub use triangle::Triangle; + +use crate::{CoordNum, Error}; #[cfg(any(feature = "approx", test))] use approx::{AbsDiffEq, RelativeEq}; @@ -10,9 +32,9 @@ use std::convert::TryFrom; /// An enum representing any possible geometry type. /// -/// All `Geo` types can be converted to a `Geometry` member using `.into()` (as part of the -/// `std::convert::Into` pattern), and `Geo` types implement the `TryFrom` trait in order to -/// convert _back_ from enum members. +/// All geometry variants ([`Point`], [`LineString`], etc.) can be converted to a `Geometry` using +/// [`Into::into`]. Conversely, [`TryFrom::try_from`] can be used to convert a [`Geometry`] +/// _back_ to one of it's specific enum members. /// /// # Example /// diff --git a/geo-types/src/multi_line_string.rs b/geo-types/src/geometry/multi_line_string.rs similarity index 100% rename from geo-types/src/multi_line_string.rs rename to geo-types/src/geometry/multi_line_string.rs diff --git a/geo-types/src/multi_point.rs b/geo-types/src/geometry/multi_point.rs similarity index 100% rename from geo-types/src/multi_point.rs rename to geo-types/src/geometry/multi_point.rs diff --git a/geo-types/src/multi_polygon.rs b/geo-types/src/geometry/multi_polygon.rs similarity index 100% rename from geo-types/src/multi_polygon.rs rename to geo-types/src/geometry/multi_polygon.rs diff --git a/geo-types/src/point.rs b/geo-types/src/geometry/point.rs similarity index 100% rename from geo-types/src/point.rs rename to geo-types/src/geometry/point.rs diff --git a/geo-types/src/polygon.rs b/geo-types/src/geometry/polygon.rs similarity index 100% rename from geo-types/src/polygon.rs rename to geo-types/src/geometry/polygon.rs diff --git a/geo-types/src/rect.rs b/geo-types/src/geometry/rect.rs similarity index 100% rename from geo-types/src/rect.rs rename to geo-types/src/geometry/rect.rs diff --git a/geo-types/src/triangle.rs b/geo-types/src/geometry/triangle.rs similarity index 100% rename from geo-types/src/triangle.rs rename to geo-types/src/geometry/triangle.rs diff --git a/geo-types/src/lib.rs b/geo-types/src/lib.rs index e01b845f6..e91127657 100644 --- a/geo-types/src/lib.rs +++ b/geo-types/src/lib.rs @@ -6,11 +6,28 @@ //! compatibility with other GeoRust crates. Otherwise, the [`geo`](https://crates.io/crates/geo) //! crate re-exports these types and additionally provides geospatial algorithms. //! -//! # Types +//! ## Geometries +//! +//! - **[`Point`]**: A single point represented by one [`Coordinate`] +//! - **[`MultiPoint`]**: A collection of [`Point`]s +//! - **[`Line`]**: A line segment represented by two [`Coordinate`]s +//! - **[`LineString`]**: A series of contiguous line segments represented by two or more +//! [`Coordinate`]s +//! - **[`MultiLineString`]**: A collection of [`LineString`]s +//! - **[`Polygon`]**: A bounded area represented by one [`LineString`] exterior ring, and zero or +//! more [`LineString`] interior rings +//! - **[`MultiPolygon`]**: A collection of [`Polygon`]s +//! - **[`Rect`]**: An axis-aligned bounded rectangle represented by minimum and maximum +//! [`Coordinate`]s +//! - **[`Triangle`]**: A bounded area represented by three [`Coordinate`] vertices +//! - **[`GeometryCollection`]**: A collection of [`Geometry`]s +//! - **[`Geometry`]**: An enumeration of all geometry types, excluding [`Coordinate`] +//! +//! ## Coordinates and Numeric Types //! -//! - **[`Coordinate`]**: A two-dimensional coordinate. All geometry types are composed of [`Coordinate`]s, though [`Coordinate`] itself is not a [`Geometry`] type. +//! - **[`Coordinate`]**: A two-dimensional coordinate. All geometry types are composed of [`Coordinate`]s, though [`Coordinate`] itself is not a [`Geometry`] type. See [`Point`] for a single coordinate geometry. //! -//! By default, coordinates are 64-bit floating point numbers, but this is generic, and you may specify any numeric type that implements [`CoordNum`] or [`CoordFloat`]. As well as f64, this includes common numeric types like f32, i32, i64, etc. +//! By default, coordinates are 64-bit floating point numbers, but this is generic, and you may specify any numeric type that implements [`CoordNum`] or [`CoordFloat`]. As well as [`f64`], this includes common numeric types like [`f32`], [`i32`], [`i64`], etc. //! //! ```rust //! use geo_types::Point; @@ -33,23 +50,6 @@ //! assert_eq!(std::mem::size_of::>(), 32 * 2 / 8); //! ``` //! -//! ## Geometries -//! -//! - **[`Point`]**: A single point represented by one [`Coordinate`] -//! - **[`MultiPoint`]**: A collection of [`Point`]s -//! - **[`Line`]**: A line segment represented by two [`Coordinate`]s -//! - **[`LineString`]**: A series of contiguous line segments represented by two or more -//! [`Coordinate`]s -//! - **[`MultiLineString`]**: A collection of [`LineString`]s -//! - **[`Polygon`]**: A bounded area represented by one [`LineString`] exterior ring, and zero or -//! more [`LineString`] interior rings -//! - **[`MultiPolygon`]**: A collection of [`Polygon`]s -//! - **[`Rect`]**: An axis-aligned bounded rectangle represented by minimum and maximum -//! [`Coordinate`]s -//! - **[`Triangle`]**: A bounded area represented by three [`Coordinate`] vertices -//! - **[`GeometryCollection`]**: A collection of [`Geometry`]s -//! - **[`Geometry`]**: An enumeration of all geometry types, excluding [`Coordinate`] -//! //! # Semantics //! //! The geospatial types provided here aim to adhere to the [OpenGIS Simple feature access][OGC-SFA] @@ -109,42 +109,13 @@ impl CoordNum for T {} pub trait CoordFloat: CoordNum + Float {} impl CoordFloat for T {} -mod coordinate; -pub use crate::coordinate::Coordinate; - -mod point; -pub use crate::point::Point; - -mod multi_point; -pub use crate::multi_point::MultiPoint; - -mod line; -pub use crate::line::Line; - -mod line_string; -pub use crate::line_string::{LineString, PointsIter}; - -mod multi_line_string; -pub use crate::multi_line_string::MultiLineString; - -mod polygon; -pub use crate::polygon::Polygon; - -mod multi_polygon; -pub use crate::multi_polygon::MultiPolygon; - -mod geometry; -pub use crate::geometry::Geometry; - -mod geometry_collection; -pub use crate::geometry_collection::GeometryCollection; +pub mod geometry; +pub use geometry::*; -mod triangle; -pub use crate::triangle::Triangle; +pub use geometry::line_string::PointsIter; -mod rect; #[allow(deprecated)] -pub use crate::rect::{InvalidRectCoordinatesError, Rect}; +pub use geometry::rect::InvalidRectCoordinatesError; mod error; pub use error::Error; diff --git a/geo/CHANGES.md b/geo/CHANGES.md index 031378c5b..b1eb49c6e 100644 --- a/geo/CHANGES.md +++ b/geo/CHANGES.md @@ -4,6 +4,9 @@ * Reexport everything from the `proj` crate * +* Added a `geometry` module which re-exports all the inner geometry variants, so you + can `use geo::geometry::*` to concisely include `Point`, `LineString`, etc. + * * Add densification algorithm for linear geometry components * * Fix fast path euclidean distance diff --git a/geo/src/algorithm/area.rs b/geo/src/algorithm/area.rs index 687f648d0..af34859b2 100644 --- a/geo/src/algorithm/area.rs +++ b/geo/src/algorithm/area.rs @@ -1,7 +1,5 @@ -use crate::{ - CoordFloat, CoordNum, Geometry, GeometryCollection, Line, LineString, MultiLineString, - MultiPoint, MultiPolygon, Point, Polygon, Rect, Triangle, -}; +use crate::geometry::*; +use crate::{CoordFloat, CoordNum}; pub(crate) fn twice_signed_ring_area(linestring: &LineString) -> T where diff --git a/geo/src/algorithm/bounding_rect.rs b/geo/src/algorithm/bounding_rect.rs index d011115d1..df01ca308 100644 --- a/geo/src/algorithm/bounding_rect.rs +++ b/geo/src/algorithm/bounding_rect.rs @@ -1,8 +1,5 @@ use crate::utils::{partial_max, partial_min}; -use crate::{ - coord, CoordNum, Coordinate, Geometry, GeometryCollection, GeometryCow, Line, LineString, - MultiLineString, MultiPoint, MultiPolygon, Point, Polygon, Rect, Triangle, -}; +use crate::{coord, geometry::*, CoordNum, GeometryCow}; use geo_types::private_utils::{get_bounding_rect, line_string_bounding_rect}; /// Calculation of the bounding rectangle of a geometry. diff --git a/geo/src/algorithm/centroid.rs b/geo/src/algorithm/centroid.rs index 93bc219a3..4b1ff070b 100644 --- a/geo/src/algorithm/centroid.rs +++ b/geo/src/algorithm/centroid.rs @@ -2,11 +2,9 @@ use std::cmp::Ordering; use crate::area::{get_linestring_area, Area}; use crate::dimensions::{Dimensions, Dimensions::*, HasDimensions}; +use crate::geometry::*; use crate::EuclideanLength; -use crate::{ - Coordinate, GeoFloat, Geometry, GeometryCollection, Line, LineString, MultiLineString, - MultiPoint, MultiPolygon, Point, Polygon, Rect, Triangle, -}; +use crate::GeoFloat; /// Calculation of the centroid. /// The centroid is the arithmetic mean position of all points in the shape. diff --git a/geo/src/algorithm/chamberlain_duquette_area.rs b/geo/src/algorithm/chamberlain_duquette_area.rs index f6a855049..255cfd2d4 100644 --- a/geo/src/algorithm/chamberlain_duquette_area.rs +++ b/geo/src/algorithm/chamberlain_duquette_area.rs @@ -1,8 +1,5 @@ -use crate::{ - CoordNum, Geometry, GeometryCollection, Line, LineString, MultiLineString, MultiPoint, - MultiPolygon, Point, Polygon, Rect, Triangle, EQUATORIAL_EARTH_RADIUS, -}; -use num_traits::Float; +use crate::geometry::*; +use crate::{CoordFloat, EQUATORIAL_EARTH_RADIUS}; /// Calculate the signed approximate geodesic area of a `Geometry`. /// @@ -49,7 +46,7 @@ use num_traits::Float; /// ``` pub trait ChamberlainDuquetteArea where - T: Float + CoordNum, + T: CoordFloat, { fn chamberlain_duquette_signed_area(&self) -> T; @@ -58,7 +55,7 @@ where impl ChamberlainDuquetteArea for Polygon where - T: Float + CoordNum, + T: CoordFloat, { fn chamberlain_duquette_signed_area(&self) -> T { self.interiors() @@ -75,7 +72,7 @@ where fn ring_area(coords: &LineString) -> T where - T: Float + CoordNum, + T: CoordFloat, { let mut total = T::zero(); let coords_len = coords.0.len(); @@ -111,7 +108,7 @@ macro_rules! zero_impl { ($type:ident) => { impl ChamberlainDuquetteArea for $type where - T: Float + CoordNum, + T: CoordFloat, { fn chamberlain_duquette_signed_area(&self) -> T { T::zero() @@ -130,7 +127,7 @@ macro_rules! to_polygon_impl { ($type:ident) => { impl ChamberlainDuquetteArea for $type where - T: Float + CoordNum, + T: CoordFloat, { fn chamberlain_duquette_signed_area(&self) -> T { self.to_polygon().chamberlain_duquette_signed_area() @@ -149,7 +146,7 @@ macro_rules! sum_impl { ($type:ident) => { impl ChamberlainDuquetteArea for $type where - T: Float + CoordNum, + T: CoordFloat, { fn chamberlain_duquette_signed_area(&self) -> T { self.iter().fold(T::zero(), |total, next| { @@ -178,7 +175,7 @@ sum_impl!(MultiPolygon); impl ChamberlainDuquetteArea for Geometry where - T: Float + CoordNum, + T: CoordFloat, { crate::geometry_delegate_impl! { fn chamberlain_duquette_signed_area(&self) -> T; diff --git a/geo/src/algorithm/closest_point.rs b/geo/src/algorithm/closest_point.rs index ea038dbc5..ea09f0579 100644 --- a/geo/src/algorithm/closest_point.rs +++ b/geo/src/algorithm/closest_point.rs @@ -1,8 +1,8 @@ -use crate::prelude::*; -use crate::{ - Closest, Coordinate, GeoFloat, Geometry, GeometryCollection, Line, LineString, MultiLineString, - MultiPoint, MultiPolygon, Point, Polygon, Rect, Triangle, -}; +use crate::algorithm::{EuclideanLength, Intersects}; +use crate::geometry::*; +use crate::Closest; +use crate::GeoFloat; + use std::iter; /// Find the closest `Point` between a given geometry and an input `Point`. @@ -185,6 +185,7 @@ impl ClosestPoint for Geometry { #[cfg(test)] mod tests { use super::*; + use crate::algorithm::{Contains, Translate}; use crate::{point, polygon}; /// Create a test which checks that we get `$should_be` when trying to find diff --git a/geo/src/algorithm/contains/geometry.rs b/geo/src/algorithm/contains/geometry.rs index 1f9302581..a1515a77b 100644 --- a/geo/src/algorithm/contains/geometry.rs +++ b/geo/src/algorithm/contains/geometry.rs @@ -1,5 +1,7 @@ use super::Contains; -use crate::*; +use crate::geometry::{Coordinate, Geometry, GeometryCollection, Point}; +use crate::geometry_delegate_impl; +use crate::GeoNum; // ┌──────────────────────────────┐ // │ Implementations for Geometry │ diff --git a/geo/src/algorithm/coordinate_position.rs b/geo/src/algorithm/coordinate_position.rs index 16ef525a3..803cca8d6 100644 --- a/geo/src/algorithm/coordinate_position.rs +++ b/geo/src/algorithm/coordinate_position.rs @@ -1,7 +1,5 @@ -use crate::{ - coord, Coordinate, GeoNum, Geometry, GeometryCollection, GeometryCow, Line, LineString, - MultiLineString, MultiPoint, MultiPolygon, Point, Polygon, Rect, Triangle, -}; +use crate::geometry::*; +use crate::{coord, GeoNum, GeometryCow}; use crate::{BoundingRect, HasDimensions, Intersects}; /// The position of a `Coordinate` relative to a `Geometry` diff --git a/geo/src/algorithm/coords_iter.rs b/geo/src/algorithm/coords_iter.rs index 0d3853585..503eed6b3 100644 --- a/geo/src/algorithm/coords_iter.rs +++ b/geo/src/algorithm/coords_iter.rs @@ -1,9 +1,7 @@ use std::fmt::Debug; -use crate::{ - coord, CoordNum, Coordinate, Geometry, GeometryCollection, Line, LineString, MultiLineString, - MultiPoint, MultiPolygon, Point, Polygon, Rect, Triangle, -}; +use crate::geometry::*; +use crate::{coord, CoordNum}; use std::{fmt, iter, marker, slice}; diff --git a/geo/src/algorithm/dimensions.rs b/geo/src/algorithm/dimensions.rs index a920f8164..ea5632d41 100644 --- a/geo/src/algorithm/dimensions.rs +++ b/geo/src/algorithm/dimensions.rs @@ -1,8 +1,6 @@ +use crate::geometry::*; use crate::Orientation::Collinear; -use crate::{ - CoordNum, GeoNum, Geometry, GeometryCollection, GeometryCow, Line, LineString, MultiLineString, - MultiPoint, MultiPolygon, Point, Polygon, Rect, Triangle, -}; +use crate::{CoordNum, GeoNum, GeometryCow}; /// Geometries can have 0, 1, or two dimensions. Or, in the case of an [`empty`](#is_empty) /// geometry, a special `Empty` dimensionality. diff --git a/geo/src/algorithm/intersects/collections.rs b/geo/src/algorithm/intersects/collections.rs index 74c1e39a3..459a6a3fd 100644 --- a/geo/src/algorithm/intersects/collections.rs +++ b/geo/src/algorithm/intersects/collections.rs @@ -1,6 +1,8 @@ use super::{has_disjoint_bboxes, Intersects}; +use crate::geometry::*; +use crate::geometry_delegate_impl; use crate::BoundingRect; -use crate::*; +use crate::CoordNum; impl Intersects for Geometry where diff --git a/geo/src/algorithm/map_coords.rs b/geo/src/algorithm/map_coords.rs index b8f69bfac..f12714ae4 100644 --- a/geo/src/algorithm/map_coords.rs +++ b/geo/src/algorithm/map_coords.rs @@ -26,10 +26,8 @@ pub use modern::*; mod modern { - pub(crate) use crate::{ - CoordNum, Coordinate, Geometry, GeometryCollection, Line, LineString, MultiLineString, - MultiPoint, MultiPolygon, Point, Polygon, Rect, Triangle, - }; + pub(crate) use crate::geometry::*; + pub(crate) use crate::CoordNum; /// Map a function over all the coordinates in an object, returning a new one pub trait MapCoords { diff --git a/geo/src/algorithm/relate/mod.rs b/geo/src/algorithm/relate/mod.rs index bb3c4b56e..12151514e 100644 --- a/geo/src/algorithm/relate/mod.rs +++ b/geo/src/algorithm/relate/mod.rs @@ -1,10 +1,8 @@ pub(crate) use edge_end_builder::EdgeEndBuilder; pub use geomgraph::intersection_matrix::IntersectionMatrix; -use crate::{ - GeoFloat, Geometry, GeometryCollection, GeometryCow, Line, LineString, MultiLineString, - MultiPoint, MultiPolygon, Point, Polygon, Rect, Triangle, -}; +use crate::geometry::*; +use crate::{GeoFloat, GeometryCow}; mod edge_end_builder; mod geomgraph; diff --git a/geo/src/geometry.rs b/geo/src/geometry.rs new file mode 100644 index 000000000..71bcaeb41 --- /dev/null +++ b/geo/src/geometry.rs @@ -0,0 +1 @@ +pub use geo_types::geometry::*; diff --git a/geo/src/lib.rs b/geo/src/lib.rs index d1789597a..4917b591b 100644 --- a/geo/src/lib.rs +++ b/geo/src/lib.rs @@ -189,11 +189,10 @@ pub use crate::algorithm::*; pub use crate::traits::ToGeo; pub use crate::types::Closest; -pub use geo_types::{ - coord, line_string, point, polygon, CoordFloat, CoordNum, Coordinate, Geometry, - GeometryCollection, Line, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, - Polygon, Rect, Triangle, -}; +pub use geo_types::{coord, line_string, point, polygon, CoordFloat, CoordNum}; + +pub mod geometry; +pub use geometry::*; /// This module includes all the functions of geometric calculations pub mod algorithm;