geo/algorithm/contains/
point.rs

1use super::{impl_contains_from_relate, impl_contains_geometry_for, Contains};
2use crate::algorithm::{CoordsIter, HasDimensions};
3use crate::geometry::*;
4use crate::{CoordNum, GeoFloat};
5
6// ┌────────────────────────────────┐
7// │ Implementations for Point      │
8// └────────────────────────────────┘
9
10impl<T> Contains<Coord<T>> for Point<T>
11where
12    T: CoordNum,
13{
14    fn contains(&self, coord: &Coord<T>) -> bool {
15        &self.0 == coord
16    }
17}
18
19impl<T> Contains<Point<T>> for Point<T>
20where
21    T: CoordNum,
22{
23    fn contains(&self, p: &Point<T>) -> bool {
24        self.contains(&p.0)
25    }
26}
27
28impl<T> Contains<Line<T>> for Point<T>
29where
30    T: CoordNum,
31{
32    fn contains(&self, line: &Line<T>) -> bool {
33        if line.start == line.end {
34            // degenerate line is a point
35            line.start == self.0
36        } else {
37            false
38        }
39    }
40}
41
42impl<T> Contains<LineString<T>> for Point<T>
43where
44    T: CoordNum,
45{
46    fn contains(&self, line_string: &LineString<T>) -> bool {
47        if line_string.is_empty() {
48            return false;
49        }
50        // only a degenerate LineString could be within a point
51        line_string.coords().all(|c| c == &self.0)
52    }
53}
54
55impl<T> Contains<Polygon<T>> for Point<T>
56where
57    T: CoordNum,
58{
59    fn contains(&self, polygon: &Polygon<T>) -> bool {
60        if polygon.is_empty() {
61            return false;
62        }
63        // only a degenerate Polygon could be within a point
64        polygon.coords_iter().all(|coord| coord == self.0)
65    }
66}
67
68impl<T> Contains<MultiPoint<T>> for Point<T>
69where
70    T: CoordNum,
71{
72    fn contains(&self, multi_point: &MultiPoint<T>) -> bool {
73        if multi_point.is_empty() {
74            return false;
75        }
76        multi_point.iter().all(|point| self.contains(point))
77    }
78}
79
80impl<T> Contains<MultiLineString<T>> for Point<T>
81where
82    T: CoordNum,
83{
84    fn contains(&self, multi_line_string: &MultiLineString<T>) -> bool {
85        if multi_line_string.is_empty() {
86            return false;
87        }
88        // only a degenerate MultiLineString could be within a point
89        multi_line_string
90            .iter()
91            .all(|line_string| self.contains(line_string))
92    }
93}
94
95impl<T> Contains<MultiPolygon<T>> for Point<T>
96where
97    T: CoordNum,
98{
99    fn contains(&self, multi_polygon: &MultiPolygon<T>) -> bool {
100        if multi_polygon.is_empty() {
101            return false;
102        }
103        // only a degenerate MultiPolygon could be within a point
104        multi_polygon.iter().all(|polygon| self.contains(polygon))
105    }
106}
107
108impl<T> Contains<GeometryCollection<T>> for Point<T>
109where
110    T: GeoFloat,
111{
112    fn contains(&self, geometry_collection: &GeometryCollection<T>) -> bool {
113        if geometry_collection.is_empty() {
114            return false;
115        }
116        geometry_collection
117            .iter()
118            .all(|geometry| self.contains(geometry))
119    }
120}
121
122impl<T> Contains<Rect<T>> for Point<T>
123where
124    T: CoordNum,
125{
126    fn contains(&self, rect: &Rect<T>) -> bool {
127        // only a degenerate Rect could be within a point
128        rect.min() == rect.max() && rect.min() == self.0
129    }
130}
131
132impl<T> Contains<Triangle<T>> for Point<T>
133where
134    T: CoordNum,
135{
136    fn contains(&self, triangle: &Triangle<T>) -> bool {
137        // only a degenerate Triangle could be within a point
138        triangle.0 == triangle.1 && triangle.0 == triangle.2 && triangle.0 == self.0
139    }
140}
141
142impl_contains_geometry_for!(Point<T>);
143
144// ┌────────────────────────────────┐
145// │ Implementations for MultiPoint │
146// └────────────────────────────────┘
147
148impl_contains_from_relate!(MultiPoint<T>, [Line<T>, LineString<T>, Polygon<T>, MultiLineString<T>, MultiPolygon<T>, MultiPoint<T>, GeometryCollection<T>, Rect<T>, Triangle<T>]);
149
150impl<T> Contains<Coord<T>> for MultiPoint<T>
151where
152    T: CoordNum,
153{
154    fn contains(&self, coord: &Coord<T>) -> bool {
155        self.iter().any(|c| &c.0 == coord)
156    }
157}
158
159impl<T> Contains<Point<T>> for MultiPoint<T>
160where
161    T: CoordNum,
162{
163    fn contains(&self, point: &Point<T>) -> bool {
164        self.iter().any(|c| c == point)
165    }
166}