Skip to content

Commit

Permalink
Relate only works for GeoFloat, not GeoNum
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkirk committed Jan 26, 2021
1 parent b78d093 commit f36196a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
8 changes: 4 additions & 4 deletions geo/src/algorithm/contains/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::*;

impl<T> Contains<Coordinate<T>> for Geometry<T>
where
T: GeoNum,
T: GeoFloat,
{
fn contains(&self, coord: &Coordinate<T>) -> bool {
match self {
Expand All @@ -27,7 +27,7 @@ where

impl<T> Contains<Point<T>> for Geometry<T>
where
T: GeoNum,
T: GeoFloat,
{
fn contains(&self, point: &Point<T>) -> bool {
self.contains(&point.0)
Expand All @@ -40,7 +40,7 @@ where

impl<T> Contains<Coordinate<T>> for GeometryCollection<T>
where
T: GeoNum,
T: GeoFloat,
{
fn contains(&self, coord: &Coordinate<T>) -> bool {
self.iter().any(|geometry| geometry.contains(coord))
Expand All @@ -49,7 +49,7 @@ where

impl<T> Contains<Point<T>> for GeometryCollection<T>
where
T: GeoNum,
T: GeoFloat,
{
fn contains(&self, point: &Point<T>) -> bool {
self.contains(&point.0)
Expand Down
33 changes: 25 additions & 8 deletions geo/src/algorithm/contains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,29 +370,46 @@ mod test {
#[test]
fn line_in_polygon_test() {
let c = |x, y| Coordinate { x, y };
let line = Line::new(c(0, 10), c(30, 40));
let linestring0 = line_string![c(-10, 0), c(50, 0), c(50, 50), c(0, 50), c(-10, 0)];
let line = Line::new(c(0.0, 10.0), c(30.0, 40.0));
let linestring0 = line_string![
c(-10.0, 0.0),
c(50.0, 0.0),
c(50.0, 50.0),
c(0.0, 50.0),
c(-10.0, 0.0)
];
let poly0 = Polygon::new(linestring0, Vec::new());
let linestring1 = line_string![c(0, 0), c(0, 20), c(20, 20), c(20, 0), c(0, 0)];
let linestring1 = line_string![
c(0.0, 0.0),
c(0.0, 20.0),
c(20.0, 20.0),
c(20.0, 0.0),
c(0.0, 0.0)
];
let poly1 = Polygon::new(linestring1, Vec::new());
assert!(poly0.contains(&line));
assert!(!poly1.contains(&line));
}
#[test]
#[ignore]
fn line_in_polygon_edgecases_test() {
// Some DE-9IM edge cases for checking line is
// inside polygon The end points of the line can be
// on the boundary of the polygon but we don't allow
// that yet.
let c = |x, y| Coordinate { x, y };
// A non-convex polygon
let linestring0 = line_string![c(0, 0), c(1, 1), c(1, -1), c(-1, -1), c(-1, 1)];
let linestring0 = line_string![
c(0.0, 0.0),
c(1.0, 1.0),
c(1.0, -1.0),
c(-1.0, -1.0),
c(-1.0, 1.0)
];
let poly = Polygon::new(linestring0, Vec::new());

assert!(poly.contains(&Line::new(c(0, 0), c(1, -1))));
assert!(poly.contains(&Line::new(c(-1, 1), c(1, -1))));
assert!(!poly.contains(&Line::new(c(-1, 1), c(1, 1))));
assert!(poly.contains(&Line::new(c(0.0, 0.0), c(1.0, -1.0))));
assert!(poly.contains(&Line::new(c(-1.0, 1.0), c(1.0, -1.0))));
assert!(!poly.contains(&Line::new(c(-1.0, 1.0), c(1.0, 1.0))));
}
#[test]
fn line_in_linestring_edgecases() {
Expand Down
13 changes: 6 additions & 7 deletions geo/src/algorithm/contains/polygon.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use super::Contains;
use crate::intersects::Intersects;
use crate::{CoordNum, Coordinate, GeoNum, Line, LineString, MultiPolygon, Point, Polygon};
use crate::{CoordNum, Coordinate, GeoFloat, Line, LineString, MultiPolygon, Point, Polygon};

// ┌─────────────────────────────┐
// │ Implementations for Polygon │
// └─────────────────────────────┘

impl<T> Contains<Coordinate<T>> for Polygon<T>
where
T: GeoNum,
T: GeoFloat,
{
fn contains(&self, coord: &Coordinate<T>) -> bool {
use crate::algorithm::coordinate_position::{CoordPos, CoordinatePosition};
Expand All @@ -19,7 +18,7 @@ where

impl<T> Contains<Point<T>> for Polygon<T>
where
T: GeoNum,
T: GeoFloat,
{
fn contains(&self, p: &Point<T>) -> bool {
self.contains(&p.0)
Expand All @@ -30,7 +29,7 @@ where
// line.start and line.end is on the boundaries
impl<T> Contains<Line<T>> for Polygon<T>
where
T: GeoNum,
T: GeoFloat,
{
fn contains(&self, line: &Line<T>) -> bool {
// both endpoints are contained in the polygon and the line
Expand All @@ -45,7 +44,7 @@ where
// TODO: also check interiors
impl<T> Contains<Polygon<T>> for Polygon<T>
where
T: GeoNum,
T: GeoFloat,
{
fn contains(&self, poly: &Polygon<T>) -> bool {
// decompose poly's exterior ring into Lines, and check each for containment
Expand All @@ -56,7 +55,7 @@ where
// TODO: ensure DE-9IM compliance
impl<T> Contains<LineString<T>> for Polygon<T>
where
T: GeoNum,
T: GeoFloat,
{
fn contains(&self, linestring: &LineString<T>) -> bool {
// All LineString points must be inside the Polygon
Expand Down

0 comments on commit f36196a

Please sign in to comment.