gistools/geometry/tools/points/
nearest.rs1use s2json::{
2 VectorFeature, VectorGeometry, VectorMultiLineString, VectorMultiLineStringGeometry,
3 VectorMultiPoint, VectorMultiPointGeometry, VectorMultiPolygon, VectorMultiPolygonGeometry,
4 VectorPoint, VectorPointGeometry,
5};
6
7pub trait NearestPoint<M: Clone + Default> {
22 fn nearest_point<M2: Clone + Default>(&self, b: &VectorPoint<M2>) -> Option<&VectorPoint<M>>;
24 fn nearest_point_mut<M2: Clone + Default>(
26 &mut self,
27 b: &VectorPoint<M2>,
28 ) -> Option<&mut VectorPoint<M>>;
29}
30
31impl<M, P: Clone + Default, D: Clone + Default> NearestPoint<D> for VectorFeature<M, P, D> {
34 fn nearest_point<M2: Clone + Default>(&self, b: &VectorPoint<M2>) -> Option<&VectorPoint<D>> {
35 self.geometry.nearest_point(b)
36 }
37 fn nearest_point_mut<M2: Clone + Default>(
38 &mut self,
39 b: &VectorPoint<M2>,
40 ) -> Option<&mut VectorPoint<D>> {
41 self.geometry.nearest_point_mut(b)
42 }
43}
44impl<M: Clone + Default> NearestPoint<M> for VectorGeometry<M> {
45 fn nearest_point<M2: Clone + Default>(&self, b: &VectorPoint<M2>) -> Option<&VectorPoint<M>> {
46 match self {
47 VectorGeometry::Point(g) => g.nearest_point(b),
48 VectorGeometry::MultiPoint(g) => g.nearest_point(b),
49 VectorGeometry::LineString(g) => g.nearest_point(b),
50 VectorGeometry::MultiLineString(g) => g.nearest_point(b),
51 VectorGeometry::Polygon(g) => g.nearest_point(b),
52 VectorGeometry::MultiPolygon(g) => g.nearest_point(b),
53 }
54 }
55 fn nearest_point_mut<M2: Clone + Default>(
56 &mut self,
57 b: &VectorPoint<M2>,
58 ) -> Option<&mut VectorPoint<M>> {
59 match self {
60 VectorGeometry::Point(g) => g.nearest_point_mut(b),
61 VectorGeometry::MultiPoint(g) => g.nearest_point_mut(b),
62 VectorGeometry::LineString(g) => g.nearest_point_mut(b),
63 VectorGeometry::MultiLineString(g) => g.nearest_point_mut(b),
64 VectorGeometry::Polygon(g) => g.nearest_point_mut(b),
65 VectorGeometry::MultiPolygon(g) => g.nearest_point_mut(b),
66 }
67 }
68}
69impl<M: Clone + Default> NearestPoint<M> for VectorPointGeometry<M> {
70 fn nearest_point<M2: Clone + Default>(&self, b: &VectorPoint<M2>) -> Option<&VectorPoint<M>> {
71 self.coordinates.nearest_point(b)
72 }
73 fn nearest_point_mut<M2: Clone + Default>(
74 &mut self,
75 b: &VectorPoint<M2>,
76 ) -> Option<&mut VectorPoint<M>> {
77 self.coordinates.nearest_point_mut(b)
78 }
79}
80impl<M: Clone + Default> NearestPoint<M> for VectorMultiPointGeometry<M> {
81 fn nearest_point<M2: Clone + Default>(&self, b: &VectorPoint<M2>) -> Option<&VectorPoint<M>> {
82 self.coordinates.nearest_point(b)
83 }
84 fn nearest_point_mut<M2: Clone + Default>(
85 &mut self,
86 b: &VectorPoint<M2>,
87 ) -> Option<&mut VectorPoint<M>> {
88 self.coordinates.nearest_point_mut(b)
89 }
90}
91impl<M: Clone + Default> NearestPoint<M> for VectorMultiLineStringGeometry<M> {
92 fn nearest_point<M2: Clone + Default>(&self, b: &VectorPoint<M2>) -> Option<&VectorPoint<M>> {
93 self.coordinates.nearest_point(b)
94 }
95 fn nearest_point_mut<M2: Clone + Default>(
96 &mut self,
97 b: &VectorPoint<M2>,
98 ) -> Option<&mut VectorPoint<M>> {
99 self.coordinates.nearest_point_mut(b)
100 }
101}
102impl<M: Clone + Default> NearestPoint<M> for VectorMultiPolygonGeometry<M> {
103 fn nearest_point<M2: Clone + Default>(&self, b: &VectorPoint<M2>) -> Option<&VectorPoint<M>> {
104 self.coordinates.nearest_point(b)
105 }
106 fn nearest_point_mut<M2: Clone + Default>(
107 &mut self,
108 b: &VectorPoint<M2>,
109 ) -> Option<&mut VectorPoint<M>> {
110 self.coordinates.nearest_point_mut(b)
111 }
112}
113
114impl<M: Clone + Default> NearestPoint<M> for VectorPoint<M> {
117 fn nearest_point<M2: Clone + Default>(&self, _b: &VectorPoint<M2>) -> Option<&VectorPoint<M>> {
118 Some(self)
119 }
120 fn nearest_point_mut<M2: Clone + Default>(
121 &mut self,
122 _b: &VectorPoint<M2>,
123 ) -> Option<&mut VectorPoint<M>> {
124 Some(self)
125 }
126}
127impl<M: Clone + Default> NearestPoint<M> for VectorMultiPoint<M> {
128 fn nearest_point<M2: Clone + Default>(&self, b: &VectorPoint<M2>) -> Option<&VectorPoint<M>> {
129 let mut res = None;
130 let mut min_dist = f64::MAX;
131 for p in self {
132 let dist = p.distance(b);
133 if dist < min_dist {
134 min_dist = dist;
135 res = Some(p);
136 }
137 }
138 res
139 }
140 fn nearest_point_mut<M2: Clone + Default>(
141 &mut self,
142 b: &VectorPoint<M2>,
143 ) -> Option<&mut VectorPoint<M>> {
144 let mut res = None;
145 let mut min_dist = f64::MAX;
146 for p in self {
147 let dist = p.distance(b);
148 if dist < min_dist {
149 min_dist = dist;
150 res = Some(p);
151 }
152 }
153 res
154 }
155}
156impl<M: Clone + Default> NearestPoint<M> for VectorMultiLineString<M> {
157 fn nearest_point<M2: Clone + Default>(&self, b: &VectorPoint<M2>) -> Option<&VectorPoint<M>> {
158 let mut res = None;
159 let mut min_dist = f64::MAX;
160 for line in self {
161 for p in line {
162 let dist = p.distance(b);
163 if dist < min_dist {
164 min_dist = dist;
165 res = Some(p);
166 }
167 }
168 }
169 res
170 }
171 fn nearest_point_mut<M2: Clone + Default>(
172 &mut self,
173 b: &VectorPoint<M2>,
174 ) -> Option<&mut VectorPoint<M>> {
175 let mut res = None;
176 let mut min_dist = f64::MAX;
177 for line in self {
178 for p in line {
179 let dist = p.distance(b);
180 if dist < min_dist {
181 min_dist = dist;
182 res = Some(p);
183 }
184 }
185 }
186 res
187 }
188}
189impl<M: Clone + Default> NearestPoint<M> for VectorMultiPolygon<M> {
190 fn nearest_point<M2: Clone + Default>(&self, b: &VectorPoint<M2>) -> Option<&VectorPoint<M>> {
191 let mut res = None;
192 let mut min_dist = f64::MAX;
193 for poly in self {
194 for line in poly {
195 for p in line {
196 let dist = p.distance(b);
197 if dist < min_dist {
198 min_dist = dist;
199 res = Some(p);
200 }
201 }
202 }
203 }
204 res
205 }
206 fn nearest_point_mut<M2: Clone + Default>(
207 &mut self,
208 b: &VectorPoint<M2>,
209 ) -> Option<&mut VectorPoint<M>> {
210 let mut res = None;
211 let mut min_dist = f64::MAX;
212 for poly in self {
213 for line in poly {
214 for p in line {
215 let dist = p.distance(b);
216 if dist < min_dist {
217 min_dist = dist;
218 res = Some(p);
219 }
220 }
221 }
222 }
223 res
224 }
225}