gistools/geometry/tools/points/
to_points.rs

1use alloc::vec;
2use s2json::{
3    Feature, Geometry, MultiLineString, MultiLineString3D, MultiLineString3DGeometry,
4    MultiLineStringGeometry, MultiPoint, MultiPoint3D, MultiPoint3DGeometry, MultiPointGeometry,
5    MultiPolygon, MultiPolygon3D, MultiPolygon3DGeometry, MultiPolygonGeometry, Point, Point3D,
6    Point3DGeometry, PointGeometry, VectorFeature, VectorGeometry, VectorMultiLineString,
7    VectorMultiLineStringGeometry, VectorMultiPoint, VectorMultiPointGeometry, VectorMultiPolygon,
8    VectorMultiPolygonGeometry, VectorPoint, VectorPointGeometry,
9};
10
11/// Convert any geometry shape to a [`VectorMultiPoint`]
12///
13/// This trait is implemented for:
14/// - [`Feature`]
15/// - [`Geometry`]
16/// - [`PointGeometry`]
17/// - [`MultiPointGeometry`]
18/// - [`MultiLineStringGeometry`]
19/// - [`MultiPolygonGeometry`]
20/// - [`Point3DGeometry`]
21/// - [`MultiPoint3DGeometry`]
22/// - [`MultiLineString3DGeometry`]
23/// - [`MultiPolygon3DGeometry`]
24/// - [`VectorFeature`]
25/// - [`VectorGeometry`]
26/// - [`VectorPointGeometry`]
27/// - [`VectorMultiPointGeometry`]
28/// - [`VectorMultiLineStringGeometry`]
29/// - [`VectorMultiPolygonGeometry`]
30/// - [`VectorMultiPoint`]
31/// - [`VectorMultiLineString`]
32/// - [`VectorMultiPolygon`]
33///
34/// And all specific geometries of the above enums
35pub trait ToPoints<M: Clone + Default> {
36    /// Convert any geometry shape to a [`VectorMultiPoint`]
37    fn to_points(&self) -> VectorMultiPoint<M>;
38}
39
40// Feature and below
41
42impl<M, P: Clone + Default, D: Clone + Default> ToPoints<D> for Feature<M, P, D> {
43    fn to_points(&self) -> VectorMultiPoint<D> {
44        self.geometry.to_points()
45    }
46}
47impl<M: Clone + Default> ToPoints<M> for Geometry<M> {
48    fn to_points(&self) -> VectorMultiPoint<M> {
49        match self {
50            Geometry::Point(g) => g.to_points(),
51            Geometry::MultiPoint(g) => g.to_points(),
52            Geometry::LineString(g) => g.to_points(),
53            Geometry::MultiLineString(g) => g.to_points(),
54            Geometry::Polygon(g) => g.to_points(),
55            Geometry::MultiPolygon(g) => g.to_points(),
56            Geometry::Point3D(g) => g.to_points(),
57            Geometry::MultiPoint3D(g) => g.to_points(),
58            Geometry::LineString3D(g) => g.to_points(),
59            Geometry::MultiLineString3D(g) => g.to_points(),
60            Geometry::Polygon3D(g) => g.to_points(),
61            Geometry::MultiPolygon3D(g) => g.to_points(),
62        }
63    }
64}
65impl<M: Clone + Default> ToPoints<M> for PointGeometry<M> {
66    fn to_points(&self) -> VectorMultiPoint<M> {
67        self.coordinates.to_points()
68    }
69}
70impl<M: Clone + Default> ToPoints<M> for MultiPointGeometry<M> {
71    fn to_points(&self) -> VectorMultiPoint<M> {
72        self.coordinates.to_points()
73    }
74}
75impl<M: Clone + Default> ToPoints<M> for MultiLineStringGeometry<M> {
76    fn to_points(&self) -> VectorMultiPoint<M> {
77        self.coordinates.to_points()
78    }
79}
80impl<M: Clone + Default> ToPoints<M> for MultiPolygonGeometry<M> {
81    fn to_points(&self) -> VectorMultiPoint<M> {
82        self.coordinates.to_points()
83    }
84}
85impl<M: Clone + Default> ToPoints<M> for Point3DGeometry<M> {
86    fn to_points(&self) -> VectorMultiPoint<M> {
87        self.coordinates.to_points()
88    }
89}
90impl<M: Clone + Default> ToPoints<M> for MultiPoint3DGeometry<M> {
91    fn to_points(&self) -> VectorMultiPoint<M> {
92        self.coordinates.to_points()
93    }
94}
95impl<M: Clone + Default> ToPoints<M> for MultiLineString3DGeometry<M> {
96    fn to_points(&self) -> VectorMultiPoint<M> {
97        self.coordinates.to_points()
98    }
99}
100impl<M: Clone + Default> ToPoints<M> for MultiPolygon3DGeometry<M> {
101    fn to_points(&self) -> VectorMultiPoint<M> {
102        self.coordinates.to_points()
103    }
104}
105
106// Feature Point types
107
108impl<M: Clone + Default> ToPoints<M> for Point {
109    fn to_points(&self) -> VectorMultiPoint<M> {
110        vec![VectorPoint::from(self)]
111    }
112}
113impl<M: Clone + Default> ToPoints<M> for MultiPoint {
114    fn to_points(&self) -> VectorMultiPoint<M> {
115        let mut points = vec![];
116        for p in self {
117            points.push(VectorPoint::from(p));
118        }
119        points
120    }
121}
122impl<M: Clone + Default> ToPoints<M> for MultiLineString {
123    fn to_points(&self) -> VectorMultiPoint<M> {
124        let mut points = vec![];
125        for line in self {
126            points.extend(line.to_points());
127        }
128        points
129    }
130}
131impl<M: Clone + Default> ToPoints<M> for MultiPolygon {
132    fn to_points(&self) -> VectorMultiPoint<M> {
133        let mut points = vec![];
134        for poly in self {
135            points.extend(poly.to_points());
136        }
137        points
138    }
139}
140impl<M: Clone + Default> ToPoints<M> for Point3D {
141    fn to_points(&self) -> VectorMultiPoint<M> {
142        vec![VectorPoint::from(self)]
143    }
144}
145impl<M: Clone + Default> ToPoints<M> for MultiPoint3D {
146    fn to_points(&self) -> VectorMultiPoint<M> {
147        let mut points = vec![];
148        for p in self {
149            points.push(VectorPoint::from(p));
150        }
151        points
152    }
153}
154impl<M: Clone + Default> ToPoints<M> for MultiLineString3D {
155    fn to_points(&self) -> VectorMultiPoint<M> {
156        let mut points = vec![];
157        for line in self {
158            points.extend(line.to_points());
159        }
160        points
161    }
162}
163impl<M: Clone + Default> ToPoints<M> for MultiPolygon3D {
164    fn to_points(&self) -> VectorMultiPoint<M> {
165        let mut points = vec![];
166        for poly in self {
167            points.extend(poly.to_points());
168        }
169        points
170    }
171}
172
173// Vector Feature and below
174
175impl<M, P: Clone + Default, D: Clone + Default> ToPoints<D> for VectorFeature<M, P, D> {
176    fn to_points(&self) -> VectorMultiPoint<D> {
177        self.geometry.to_points()
178    }
179}
180impl<M: Clone + Default> ToPoints<M> for VectorGeometry<M> {
181    fn to_points(&self) -> VectorMultiPoint<M> {
182        match self {
183            VectorGeometry::Point(g) => g.to_points(),
184            VectorGeometry::MultiPoint(g) => g.to_points(),
185            VectorGeometry::LineString(g) => g.to_points(),
186            VectorGeometry::MultiLineString(g) => g.to_points(),
187            VectorGeometry::Polygon(g) => g.to_points(),
188            VectorGeometry::MultiPolygon(g) => g.to_points(),
189        }
190    }
191}
192impl<M: Clone + Default> ToPoints<M> for VectorPointGeometry<M> {
193    fn to_points(&self) -> VectorMultiPoint<M> {
194        self.coordinates.to_points()
195    }
196}
197impl<M: Clone + Default> ToPoints<M> for VectorMultiPointGeometry<M> {
198    fn to_points(&self) -> VectorMultiPoint<M> {
199        self.coordinates.to_points()
200    }
201}
202impl<M: Clone + Default> ToPoints<M> for VectorMultiLineStringGeometry<M> {
203    fn to_points(&self) -> VectorMultiPoint<M> {
204        self.coordinates.to_points()
205    }
206}
207impl<M: Clone + Default> ToPoints<M> for VectorMultiPolygonGeometry<M> {
208    fn to_points(&self) -> VectorMultiPoint<M> {
209        self.coordinates.to_points()
210    }
211}
212
213// Vector Point Types
214
215impl<M: Clone + Default> ToPoints<M> for VectorPoint<M> {
216    fn to_points(&self) -> VectorMultiPoint<M> {
217        vec![self.clone()]
218    }
219}
220impl<M: Clone + Default> ToPoints<M> for VectorMultiPoint<M> {
221    fn to_points(&self) -> VectorMultiPoint<M> {
222        self.clone()
223    }
224}
225impl<M: Clone + Default> ToPoints<M> for VectorMultiLineString<M> {
226    fn to_points(&self) -> VectorMultiPoint<M> {
227        let mut points = vec![];
228
229        for line in self {
230            points.extend(line.to_points());
231        }
232
233        points
234    }
235}
236impl<M: Clone + Default> ToPoints<M> for VectorMultiPolygon<M> {
237    fn to_points(&self) -> VectorMultiPoint<M> {
238        let mut points = vec![];
239
240        for polygon in self {
241            points.extend(polygon.to_points());
242        }
243
244        points
245    }
246}