geo_types/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![warn(missing_debug_implementations)]
3#![doc(html_logo_url = "https://raw.githubusercontent.com/georust/meta/master/logo/logo.png")]
4//! The `geo-types` library defines geometric types for the [GeoRust] ecosystem.
5//!
6//! In most cases, you will only need to use this crate if you’re a crate author and want
7//! compatibility with other GeoRust crates. Otherwise, the [`geo`](https://crates.io/crates/geo)
8//! crate re-exports these types and additionally provides geospatial algorithms.
9//!
10//! ## Geometries
11//!
12//! - **[`Point`]**: A single point represented by one [`Coord`]
13//! - **[`MultiPoint`]**: A collection of [`Point`]s
14//! - **[`Line`]**: A line segment represented by two [`Coord`]s
15//! - **[`LineString`]**: A series of contiguous line segments represented by two or more
16//!   [`Coord`]s
17//! - **[`MultiLineString`]**: A collection of [`LineString`]s
18//! - **[`Polygon`]**: A bounded area represented by one [`LineString`] exterior ring, and zero or
19//!   more [`LineString`] interior rings
20//! - **[`MultiPolygon`]**: A collection of [`Polygon`]s
21//! - **[`Rect`]**: An axis-aligned bounded rectangle represented by minimum and maximum
22//!   [`Coord`]s
23//! - **[`Triangle`]**: A bounded area represented by three [`Coord`] vertices
24//! - **[`GeometryCollection`]**: A collection of [`Geometry`]s
25//! - **[`Geometry`]**: An enumeration of all geometry types, excluding [`Coord`]
26//!
27//! ## Coordinates and Numeric Types
28//!
29//! - **[`Coord`]**: A two-dimensional coordinate. All geometry types are composed of [`Coord`]s, though [`Coord`] itself is not a [`Geometry`] type. See [`Point`] for a single coordinate geometry.
30//!
31//! 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.
32//!
33//! ```rust
34//! use geo_types::Point;
35//!
36//! // Geometries are f64 by default
37//! let point: Point = Point::new(1.0, 2.0);
38//! assert_eq!(std::mem::size_of::<Point>(), 64 * 2 / 8);
39//!
40//! // You can be explicit about the numeric type.
41//! let f64_point: Point<f64> = Point::new(1.0, 2.0);
42//! assert_eq!(std::mem::size_of::<Point<f64>>(), 64 * 2 / 8);
43//!
44//! // Or specify some non-default numeric type
45//! let f32_point: Point<f32> = Point::new(1.0, 2.0);
46//! assert_eq!(std::mem::size_of::<Point<f32>>(), 32 * 2 / 8);
47//!
48//! // Integer geometries are supported too, though not all
49//! // algorithms will be implemented for all numeric types.
50//! let i32_point: Point<i32> = Point::new(1, 2);
51//! assert_eq!(std::mem::size_of::<Point<i32>>(), 32 * 2 / 8);
52//! ```
53//!
54//! # Semantics
55//!
56//! The geospatial types provided here aim to adhere to the [OpenGIS Simple feature access][OGC-SFA]
57//! standards. Thus, the types here are inter-operable with other implementations of the standards:
58//! [JTS], [GEOS], etc.
59//!
60//! # Features
61//!
62//! The following optional [Cargo features] are available:
63//!
64//! - `std`: Enables use of the full `std` library. Enabled by default.
65//! - `multithreading`: Enables multi-threaded iteration over `Multi*` geometries. **Disabled**
66//!   by default but **enabled** by `geo`'s default features.
67//! - `approx`: Allows geometry types to be checked for approximate equality with [approx]
68//! - `arbitrary`: Allows geometry types to be created from unstructured input with [arbitrary]
69//! - `serde`: Allows geometry types to be serialized and deserialized with [Serde]
70//! - `use-rstar_0_8`: Allows geometry types to be inserted into [rstar] R*-trees (`rstar v0.8`)
71//! - `use-rstar_0_9`: Allows geometry types to be inserted into [rstar] R*-trees (`rstar v0.9`)
72//! - `use-rstar_0_10`: Allows geometry types to be inserted into [rstar] R*-trees (`rstar v0.10`)
73//! - `use-rstar_0_11`: Allows geometry types to be inserted into [rstar] R*-trees (`rstar v0.11`)
74//! - `use-rstar_0_12`: Allows geometry types to be inserted into [rstar] R*-trees (`rstar v0.12`)
75//!
76//! This library can be used in `#![no_std]` environments if the default `std` feature is disabled. At
77//! the moment, the `arbitrary` and `use-rstar_0_8` features require `std`. This may change in a
78//! future release.
79//!
80//! [approx]: https://github.com/brendanzab/approx
81//! [arbitrary]: https://github.com/rust-fuzz/arbitrary
82//! [Cargo features]: https://doc.rust-lang.org/cargo/reference/features.html
83//! [GeoRust]: https://georust.org
84//! [GEOS]: https://trac.osgeo.org/geos
85//! [JTS]: https://github.com/locationtech/jts
86//! [OGC-SFA]: https://www.ogc.org/standards/sfa
87//! [rstar]: https://github.com/Stoeoef/rstar
88//! [Serde]: https://serde.rs/
89extern crate alloc;
90
91use core::fmt::Debug;
92use num_traits::{Float, Num, NumCast};
93
94#[cfg(feature = "serde")]
95#[macro_use]
96extern crate serde;
97
98#[cfg(test)]
99#[macro_use]
100extern crate approx;
101
102#[deprecated(since = "0.7.0", note = "use `CoordFloat` or `CoordNum` instead")]
103pub trait CoordinateType: Num + Copy + NumCast + PartialOrd + Debug {}
104#[allow(deprecated)]
105impl<T: Num + Copy + NumCast + PartialOrd + Debug> CoordinateType for T {}
106
107/// For algorithms which can use both integer **and** floating point `Point`s/`Coord`s
108///
109/// Floats (`f32` and `f64`) and Integers (`u8`, `i32` etc.) implement this.
110///
111/// For algorithms which only make sense for floating point, like area or length calculations,
112/// see [CoordFloat](trait.CoordFloat.html).
113#[allow(deprecated)]
114pub trait CoordNum: CoordinateType + Debug {}
115#[allow(deprecated)]
116impl<T: CoordinateType + Debug> CoordNum for T {}
117
118/// For algorithms which can only use floating point `Point`s/`Coord`s, like area or length calculations
119pub trait CoordFloat: CoordNum + Float {}
120impl<T: CoordNum + Float> CoordFloat for T {}
121
122pub mod geometry;
123pub use geometry::*;
124
125pub use geometry::line_string::PointsIter;
126
127#[allow(deprecated)]
128pub use geometry::rect::InvalidRectCoordinatesError;
129
130mod error;
131pub use error::Error;
132
133#[macro_use]
134mod macros;
135
136#[macro_use]
137mod wkt_macro;
138
139#[cfg(feature = "arbitrary")]
140mod arbitrary;
141
142#[cfg(any(
143    feature = "rstar_0_8",
144    feature = "rstar_0_9",
145    feature = "rstar_0_10",
146    feature = "rstar_0_11",
147    feature = "rstar_0_12"
148))]
149#[doc(hidden)]
150pub mod private_utils;
151
152mod debug;
153
154#[doc(hidden)]
155pub mod _alloc {
156    //! Needed to access these types from `alloc` in macros when the std feature is
157    //! disabled and the calling context is missing `extern crate alloc`. These are
158    //! _not_ meant for public use.
159    pub use ::alloc::vec;
160}
161
162// Some gymnastics to help migrate people off our old feature flag naming conventions
163#[allow(unused, non_camel_case_types, missing_debug_implementations)]
164mod deprecated_feature_flags {
165    #[cfg_attr(
166        not(feature = "__allow_deprecated_features"),
167        deprecated(
168            since = "0.7.18",
169            note = "The `use-rstar` feature has been renamed to simply `rstar`. Use the `rstar` feature instead."
170        )
171    )]
172    pub struct UseRstar;
173
174    #[cfg_attr(
175        not(feature = "__allow_deprecated_features"),
176        deprecated(
177            since = "0.7.18",
178            note = "The `use-rstar_0_8` feature has been renamed to simply `rstar_0_8`. Use the `rstar_0_8` feature instead."
179        )
180    )]
181    pub struct UseRstar_0_8;
182
183    #[cfg_attr(
184        not(feature = "__allow_deprecated_features"),
185        deprecated(
186            since = "0.7.18",
187            note = "The `use-rstar_0_9` feature has been renamed to simply `rstar_0_9`. Use the `rstar_0_9` feature instead."
188        )
189    )]
190    pub struct UseRstar_0_9;
191
192    #[cfg_attr(
193        not(feature = "__allow_deprecated_features"),
194        deprecated(
195            since = "0.7.18",
196            note = "The `use-rstar_0_10` feature has been renamed to simply `rstar_0_10`. Use the `rstar_0_10` feature instead."
197        )
198    )]
199    pub struct UseRstar_0_10;
200
201    #[cfg_attr(
202        not(feature = "__allow_deprecated_features"),
203        deprecated(
204            since = "0.7.18",
205            note = "The `use-rstar_0_11` feature has been renamed to simply `rstar_0_11`. Use the `rstar_0_11` feature instead."
206        )
207    )]
208    pub struct UseRstar_0_11;
209
210    #[cfg_attr(
211        not(feature = "__allow_deprecated_features"),
212        deprecated(
213            since = "0.7.18",
214            note = "The `use-rstar_0_12` feature has been renamed to simply `rstar_0_12`. Use the `rstar_0_12` feature instead."
215        )
216    )]
217    pub struct UseRstar_0_12;
218}
219
220#[cfg(feature = "use-rstar")]
221pub use deprecated_feature_flags::UseRstar;
222
223#[cfg(feature = "use-rstar_0_8")]
224pub use deprecated_feature_flags::UseRstar_0_8;
225
226#[cfg(feature = "use-rstar_0_9")]
227pub use deprecated_feature_flags::UseRstar_0_9;
228
229#[cfg(feature = "use-rstar_0_10")]
230pub use deprecated_feature_flags::UseRstar_0_10;
231
232#[cfg(feature = "use-rstar_0_11")]
233pub use deprecated_feature_flags::UseRstar_0_11;
234
235#[cfg(feature = "use-rstar_0_12")]
236pub use deprecated_feature_flags::UseRstar_0_12;
237
238#[cfg(test)]
239mod tests {
240    use alloc::vec;
241
242    use super::*;
243    use core::convert::TryFrom;
244
245    #[test]
246    fn type_test() {
247        let c = coord! {
248            x: 40.02f64,
249            y: 116.34,
250        };
251
252        let p = Point::from(c);
253
254        let Point(c2) = p;
255        assert_eq!(c, c2);
256        assert_relative_eq!(c.x, c2.x);
257        assert_relative_eq!(c.y, c2.y);
258
259        let p: Point<f32> = (0f32, 1f32).into();
260        assert_relative_eq!(p.x(), 0.);
261        assert_relative_eq!(p.y(), 1.);
262    }
263
264    #[test]
265    fn convert_types() {
266        let p: Point<f32> = Point::new(0., 0.);
267        let p1 = p;
268        let g: Geometry<f32> = p.into();
269        let p2 = Point::try_from(g).unwrap();
270        assert_eq!(p1, p2);
271    }
272
273    #[test]
274    fn polygon_new_test() {
275        let exterior = LineString::new(vec![
276            coord! { x: 0., y: 0. },
277            coord! { x: 1., y: 1. },
278            coord! { x: 1., y: 0. },
279            coord! { x: 0., y: 0. },
280        ]);
281        let interiors = vec![LineString::new(vec![
282            coord! { x: 0.1, y: 0.1 },
283            coord! { x: 0.9, y: 0.9 },
284            coord! { x: 0.9, y: 0.1 },
285            coord! { x: 0.1, y: 0.1 },
286        ])];
287        let p = Polygon::new(exterior.clone(), interiors.clone());
288
289        assert_eq!(p.exterior(), &exterior);
290        assert_eq!(p.interiors(), &interiors[..]);
291    }
292
293    #[test]
294    fn iters() {
295        let _: MultiPoint<_> = vec![(0., 0.), (1., 2.)].into();
296        let _: MultiPoint<_> = vec![(0., 0.), (1., 2.)].into_iter().collect();
297
298        let mut l1: LineString<_> = vec![(0., 0.), (1., 2.)].into();
299        assert_eq!(l1[1], coord! { x: 1., y: 2. }); // index into linestring
300        let _: LineString<_> = vec![(0., 0.), (1., 2.)].into_iter().collect();
301
302        // index mutably into a linestring
303        l1[0] = coord! { x: 1., y: 1. };
304        assert_eq!(l1, vec![(1., 1.), (1., 2.)].into());
305    }
306
307    #[test]
308    fn test_coordinate_types() {
309        let p: Point<u8> = Point::new(0, 0);
310        assert_eq!(p.x(), 0u8);
311
312        let p: Point<i64> = Point::new(1_000_000, 0);
313        assert_eq!(p.x(), 1_000_000i64);
314    }
315
316    #[cfg(feature = "rstar_0_8")]
317    #[test]
318    /// ensure Line's SpatialObject impl is correct
319    fn line_test() {
320        use rstar_0_8::primitives::Line as RStarLine;
321        use rstar_0_8::{PointDistance, RTreeObject};
322
323        let rl = RStarLine::new(Point::new(0.0, 0.0), Point::new(5.0, 5.0));
324        let l = Line::new(coord! { x: 0.0, y: 0.0 }, coord! { x: 5., y: 5. });
325        assert_eq!(rl.envelope(), l.envelope());
326        // difference in 15th decimal place
327        assert_relative_eq!(26.0, rl.distance_2(&Point::new(4.0, 10.0)));
328        assert_relative_eq!(25.999999999999996, l.distance_2(&Point::new(4.0, 10.0)));
329    }
330
331    #[cfg(feature = "rstar_0_9")]
332    #[test]
333    /// ensure Line's SpatialObject impl is correct
334    fn line_test_0_9() {
335        use rstar_0_9::primitives::Line as RStarLine;
336        use rstar_0_9::{PointDistance, RTreeObject};
337
338        let rl = RStarLine::new(Point::new(0.0, 0.0), Point::new(5.0, 5.0));
339        let l = Line::new(coord! { x: 0.0, y: 0.0 }, coord! { x: 5., y: 5. });
340        assert_eq!(rl.envelope(), l.envelope());
341        // difference in 15th decimal place
342        assert_relative_eq!(26.0, rl.distance_2(&Point::new(4.0, 10.0)));
343        assert_relative_eq!(25.999999999999996, l.distance_2(&Point::new(4.0, 10.0)));
344    }
345
346    #[cfg(feature = "rstar_0_10")]
347    #[test]
348    /// ensure Line's SpatialObject impl is correct
349    fn line_test_0_10() {
350        use rstar_0_10::primitives::Line as RStarLine;
351        use rstar_0_10::{PointDistance, RTreeObject};
352
353        let rl = RStarLine::new(Point::new(0.0, 0.0), Point::new(5.0, 5.0));
354        let l = Line::new(coord! { x: 0.0, y: 0.0 }, coord! { x: 5., y: 5. });
355        assert_eq!(rl.envelope(), l.envelope());
356        // difference in 15th decimal place
357        assert_relative_eq!(26.0, rl.distance_2(&Point::new(4.0, 10.0)));
358        assert_relative_eq!(25.999999999999996, l.distance_2(&Point::new(4.0, 10.0)));
359    }
360
361    #[cfg(feature = "rstar_0_11")]
362    #[test]
363    /// ensure Line's SpatialObject impl is correct
364    fn line_test_0_11() {
365        use rstar_0_11::primitives::Line as RStarLine;
366        use rstar_0_11::{PointDistance, RTreeObject};
367
368        let rl = RStarLine::new(Point::new(0.0, 0.0), Point::new(5.0, 5.0));
369        let l = Line::new(coord! { x: 0.0, y: 0.0 }, coord! { x: 5., y: 5. });
370        assert_eq!(rl.envelope(), l.envelope());
371        // difference in 15th decimal place
372        assert_relative_eq!(26.0, rl.distance_2(&Point::new(4.0, 10.0)));
373        assert_relative_eq!(25.999999999999996, l.distance_2(&Point::new(4.0, 10.0)));
374    }
375
376    #[cfg(feature = "rstar_0_12")]
377    #[test]
378    /// ensure Line's SpatialObject impl is correct
379    fn line_test_0_12() {
380        use rstar_0_12::primitives::Line as RStarLine;
381        use rstar_0_12::{PointDistance, RTreeObject};
382
383        let rl = RStarLine::new(Point::new(0.0, 0.0), Point::new(5.0, 5.0));
384        let l = Line::new(coord! { x: 0.0, y: 0.0 }, coord! { x: 5., y: 5. });
385        assert_eq!(rl.envelope(), l.envelope());
386        // difference in 15th decimal place
387        assert_relative_eq!(26.0, rl.distance_2(&Point::new(4.0, 10.0)));
388        assert_relative_eq!(25.999999999999996, l.distance_2(&Point::new(4.0, 10.0)));
389    }
390
391    #[test]
392    fn test_rects() {
393        let r = Rect::new(coord! { x: -1., y: -1. }, coord! { x: 1., y: 1. });
394        let p: Polygon<_> = r.into();
395        assert_eq!(
396            p,
397            Polygon::new(
398                vec![(-1., -1.), (1., -1.), (1., 1.), (-1., 1.), (-1., -1.)].into(),
399                vec![]
400            )
401        );
402    }
403}