@@ -33,6 +33,24 @@ pub trait CoordsIter<'a, T: CoordinateType> {
3333 /// assert_eq!(None, iter.next());
3434 /// ```
3535 fn coords_iter ( & ' a self ) -> Self :: Iter ;
36+
37+ /// Return the number of coordinates in a geometry.
38+ ///
39+ /// # Examples
40+ ///
41+ /// ```
42+ /// use geo::coords_iter::CoordsIter;
43+ /// use geo::line_string;
44+ ///
45+ /// let ls = line_string![
46+ /// (x: 1., y: 2.),
47+ /// (x: 23., y: 82.),
48+ /// (x: -1., y: 0.),
49+ /// ];
50+ ///
51+ /// assert_eq!(3, ls.coords_count());
52+ /// ```
53+ fn coords_count ( & ' a self ) -> usize ;
3654}
3755
3856// ┌──────────────────────────┐
@@ -45,6 +63,11 @@ impl<'a, T: CoordinateType> CoordsIter<'a, T> for Point<T> {
4563 fn coords_iter ( & ' a self ) -> Self :: Iter {
4664 iter:: once ( self . 0 )
4765 }
66+
67+ /// Return the number of coordinates in the `Point`.
68+ fn coords_count ( & ' a self ) -> usize {
69+ 1
70+ }
4871}
4972
5073// ┌─────────────────────────┐
@@ -57,6 +80,11 @@ impl<'a, T: CoordinateType> CoordsIter<'a, T> for Line<T> {
5780 fn coords_iter ( & ' a self ) -> Self :: Iter {
5881 iter:: once ( self . start ) . chain ( iter:: once ( self . end ) )
5982 }
83+
84+ /// Return the number of coordinates in the `Line`.
85+ fn coords_count ( & ' a self ) -> usize {
86+ 2
87+ }
6088}
6189
6290// ┌───────────────────────────────┐
@@ -69,6 +97,11 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for LineString<T> {
6997 fn coords_iter ( & ' a self ) -> Self :: Iter {
7098 self . 0 . iter ( ) . copied ( )
7199 }
100+
101+ /// Return the number of coordinates in the `LineString`.
102+ fn coords_count ( & ' a self ) -> usize {
103+ self . 0 . len ( )
104+ }
72105}
73106
74107// ┌────────────────────────────┐
@@ -87,6 +120,12 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for Polygon<T> {
87120 . coords_iter ( )
88121 . chain ( MapCoordsIter ( self . interiors ( ) . iter ( ) , marker:: PhantomData ) . flatten ( ) )
89122 }
123+
124+ /// Return the number of coordinates in the `Polygon`.
125+ fn coords_count ( & ' a self ) -> usize {
126+ self . exterior ( ) . coords_count ( ) +
127+ self . interiors ( ) . iter ( ) . map ( |i| i. coords_count ( ) ) . sum :: < usize > ( )
128+ }
90129}
91130
92131// ┌───────────────────────────────┐
@@ -99,6 +138,11 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for MultiPoint<T> {
99138 fn coords_iter ( & ' a self ) -> Self :: Iter {
100139 MapCoordsIter ( self . 0 . iter ( ) , marker:: PhantomData ) . flatten ( )
101140 }
141+
142+ /// Return the number of coordinates in the `MultiPoint`.
143+ fn coords_count ( & ' a self ) -> usize {
144+ self . 0 . len ( )
145+ }
102146}
103147
104148// ┌────────────────────────────────────┐
@@ -111,6 +155,11 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for MultiLineString<T> {
111155 fn coords_iter ( & ' a self ) -> Self :: Iter {
112156 MapCoordsIter ( self . 0 . iter ( ) , marker:: PhantomData ) . flatten ( )
113157 }
158+
159+ /// Return the number of coordinates in the `MultiLineString`.
160+ fn coords_count ( & ' a self ) -> usize {
161+ self . 0 . iter ( ) . map ( |line_string| line_string. coords_count ( ) ) . sum ( )
162+ }
114163}
115164
116165// ┌─────────────────────────────────┐
@@ -123,6 +172,11 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for MultiPolygon<T> {
123172 fn coords_iter ( & ' a self ) -> Self :: Iter {
124173 MapCoordsIter ( self . 0 . iter ( ) , marker:: PhantomData ) . flatten ( )
125174 }
175+
176+ /// Return the number of coordinates in the `MultiPolygon`.
177+ fn coords_count ( & ' a self ) -> usize {
178+ self . 0 . iter ( ) . map ( |polygon| polygon. coords_count ( ) ) . sum ( )
179+ }
126180}
127181
128182// ┌───────────────────────────────────────┐
@@ -135,6 +189,11 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for GeometryCollection<T> {
135189 fn coords_iter ( & ' a self ) -> Self :: Iter {
136190 Box :: new ( self . 0 . iter ( ) . flat_map ( |geometry| geometry. coords_iter ( ) ) )
137191 }
192+
193+ /// Return the number of coordinates in the `GeometryCollection`.
194+ fn coords_count ( & ' a self ) -> usize {
195+ self . 0 . iter ( ) . map ( |geometry| geometry. coords_count ( ) ) . sum ( )
196+ }
138197}
139198
140199// ┌─────────────────────────┐
@@ -167,6 +226,14 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for Rect<T> {
167226 y : self . min ( ) . y ,
168227 } ) )
169228 }
229+
230+ /// Return the number of coordinates in the `Rect`.
231+ ///
232+ /// Note: Although a `Rect` is represented by two coordinates, it is
233+ /// spatially represented by four, so this method returns `4`.
234+ fn coords_count ( & ' a self ) -> usize {
235+ 4
236+ }
170237}
171238
172239// ┌─────────────────────────────┐
@@ -181,6 +248,11 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for Triangle<T> {
181248 . chain ( iter:: once ( self . 1 ) )
182249 . chain ( iter:: once ( self . 2 ) )
183250 }
251+
252+ /// Return the number of coordinates in the `Triangle`.
253+ fn coords_count ( & ' a self ) -> usize {
254+ 3
255+ }
184256}
185257
186258// ┌─────────────────────────────┐
@@ -206,6 +278,22 @@ impl<'a, T: CoordinateType + 'a> CoordsIter<'a, T> for Geometry<T> {
206278 Geometry :: Triangle ( g) => GeometryCoordsIter :: Triangle ( g. coords_iter ( ) ) ,
207279 }
208280 }
281+
282+ /// Return the number of coordinates in the `Geometry`.
283+ fn coords_count ( & ' a self ) -> usize {
284+ match self {
285+ Geometry :: Point ( g) => g. coords_count ( ) ,
286+ Geometry :: Line ( g) => g. coords_count ( ) ,
287+ Geometry :: LineString ( g) => g. coords_count ( ) ,
288+ Geometry :: Polygon ( g) => g. coords_count ( ) ,
289+ Geometry :: MultiPoint ( g) => g. coords_count ( ) ,
290+ Geometry :: MultiLineString ( g) => g. coords_count ( ) ,
291+ Geometry :: MultiPolygon ( g) => g. coords_count ( ) ,
292+ Geometry :: GeometryCollection ( g) => g. coords_count ( ) ,
293+ Geometry :: Rect ( g) => g. coords_count ( ) ,
294+ Geometry :: Triangle ( g) => g. coords_count ( ) ,
295+ }
296+ }
209297}
210298
211299// ┌───────────┐
0 commit comments