-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Round up Intersects implementations #516
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,17 @@ | |
//! with other `GeoRust` crates. Otherwise, the [`geo`](https://crates.io/crates/geo) crate re-exports these types and | ||
//! provides geospatial algorithms, while the [`geojson`](https://crates.io/crates/geojson) crate allows serialising | ||
//! and de-serialising `geo-types` primitives to GeoJSON. | ||
//! | ||
//! # Semantics | ||
//! | ||
//! The geospatial types provided here aim to adhere to the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we dare make this claim just yet? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, we just "aim to"; nothing wrong in aiming high! |
||
//! [OpenGIS Simple feature access][OGC-SFA] standards. | ||
//! Thus, the types here are inter-operable with other | ||
//! implementations of the standards: [JTS], [geos], etc. | ||
//! | ||
//! [OGC-SFA]: //www.ogc.org/standards/sfa | ||
//! [JTS]: //github.com/locationtech/jts | ||
//! [geos]: //trac.osgeo.org/geos | ||
extern crate num_traits; | ||
use num_traits::{Num, NumCast}; | ||
|
||
|
@@ -21,8 +32,9 @@ extern crate approx; | |
|
||
/// The type of an x or y value of a point/coordinate. | ||
/// | ||
/// Floats (`f32` and `f64`) and Integers (`u8`, `i32` etc.) implement this. Many algorithms only | ||
/// make sense for Float types (like area, or length calculations). | ||
/// Floats (`f32` and `f64`) and Integers (`u8`, `i32` etc.) | ||
/// implement this. Many algorithms only make sense for | ||
/// Float types (like area, or length calculations). | ||
pub trait CoordinateType: Num + Copy + NumCast + PartialOrd {} | ||
// Little bit of a hack to make to make this work | ||
impl<T: Num + Copy + NumCast + PartialOrd> CoordinateType for T {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,11 @@ use std::ops::{Add, Div, Mul, Neg, Sub}; | |
/// tuples, or arrays – see the `From` impl section for a | ||
/// complete list. | ||
/// | ||
/// A point is _valid_ iff neither coordinate is `NaN`. The | ||
/// _interior_ of the point is itself (a singleton set), and | ||
/// its _boundary_ is empty. | ||
/// # Semantics | ||
/// | ||
/// The _interior_ of the point is itself (a singleton set), | ||
/// and its _boundary_ is empty. A point is _valid_ if and | ||
/// only if the `Coordinate` is valid. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we mean only that the Coordinate cannot have an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm referring to validity requirement of Coordinate (which is vague too :) ) |
||
/// | ||
/// # Examples | ||
/// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use super::Intersects; | ||
use crate::*; | ||
|
||
impl<T, G> Intersects<G> for Geometry<T> | ||
where | ||
T: CoordinateType, | ||
Point<T>: Intersects<G>, | ||
MultiPoint<T>: Intersects<G>, | ||
Line<T>: Intersects<G>, | ||
LineString<T>: Intersects<G>, | ||
MultiLineString<T>: Intersects<G>, | ||
Triangle<T>: Intersects<G>, | ||
Rect<T>: Intersects<G>, | ||
Polygon<T>: Intersects<G>, | ||
MultiPolygon<T>: Intersects<G>, | ||
{ | ||
fn intersects(&self, rhs: &G) -> bool { | ||
match self { | ||
Geometry::Point(geom) => geom.intersects(rhs), | ||
Geometry::MultiPoint(geom) => geom.intersects(rhs), | ||
Geometry::Line(geom) => geom.intersects(rhs), | ||
Geometry::LineString(geom) => geom.intersects(rhs), | ||
Geometry::MultiLineString(geom) => geom.intersects(rhs), | ||
Geometry::Triangle(geom) => geom.intersects(rhs), | ||
Geometry::Rect(geom) => geom.intersects(rhs), | ||
Geometry::Polygon(geom) => geom.intersects(rhs), | ||
Geometry::MultiPolygon(geom) => geom.intersects(rhs), | ||
Geometry::GeometryCollection(geom) => geom.intersects(rhs), | ||
} | ||
} | ||
} | ||
symmetric_intersects_impl!(Coordinate<T>, Geometry<T>); | ||
symmetric_intersects_impl!(Line<T>, Geometry<T>); | ||
symmetric_intersects_impl!(Rect<T>, Geometry<T>); | ||
symmetric_intersects_impl!(Polygon<T>, Geometry<T>); | ||
|
||
impl<T, G> Intersects<G> for GeometryCollection<T> | ||
where | ||
T: CoordinateType, | ||
Geometry<T>: Intersects<G>, | ||
{ | ||
fn intersects(&self, rhs: &G) -> bool { | ||
self.0.iter().any(|geom| geom.intersects(rhs)) | ||
} | ||
} | ||
symmetric_intersects_impl!(Coordinate<T>, GeometryCollection<T>); | ||
symmetric_intersects_impl!(Line<T>, GeometryCollection<T>); | ||
symmetric_intersects_impl!(Rect<T>, GeometryCollection<T>); | ||
symmetric_intersects_impl!(Polygon<T>, GeometryCollection<T>); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By "requirements" are you speaking to our concept of "validity"?
i.e. our geometric primitives have different rules for validity, but the validity rules all pre-suppose that their coordinates are not NAN?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left this intentionally vague, as I also wasn't sure of the exact requirements. For instance, inf - inf is nan, so any operation that computes diffs (would end up producing nan coordinates if starting with inf).