@@ -18,7 +18,7 @@ pub trait MapCoords<T, NT> {
18
18
/// assert_eq!(p2, Point::new(1010., 40.));
19
19
/// ```
20
20
///
21
- /// You can also change the coordinate precision in this way:
21
+ /// You can convert the coordinate type this way as well
22
22
///
23
23
/// ```
24
24
/// # use geo::Point;
@@ -30,9 +30,27 @@ pub trait MapCoords<T, NT> {
30
30
/// assert_eq!(p2, Point::new(10.0f64, 20.0f64));
31
31
/// ```
32
32
fn map_coords ( & self , func : & Fn ( & ( T , T ) ) -> ( NT , NT ) ) -> Self :: Output
33
- where
34
- T : Float ,
35
- NT : Float ;
33
+ where T : Float , NT : Float ;
34
+
35
+
36
+
37
+ }
38
+
39
+ /// Map all the coordinates in an object in place
40
+ pub trait MapCoordsInplace < T > {
41
+ /// Apply a function to all the coordinates in a geometric object, in place
42
+ ///
43
+ /// ```
44
+ /// use geo::Point;
45
+ /// use geo::algorithm::map_coords::MapCoordsInplace;
46
+ ///
47
+ /// let mut p = Point::new(10., 20.);
48
+ /// p.map_coords_inplace(&|&(x, y)| (x+1000., y*2.));
49
+ ///
50
+ /// assert_eq!(p, Point::new(1010., 40.));
51
+ /// ```
52
+ fn map_coords_inplace ( & mut self , func : & Fn ( & ( T , T ) ) -> ( T , T ) )
53
+ where T : Float ;
36
54
}
37
55
38
56
impl < T : Float , NT : Float > MapCoords < T , NT > for Point < T > {
@@ -44,6 +62,16 @@ impl<T: Float, NT: Float> MapCoords<T, NT> for Point<T> {
44
62
}
45
63
}
46
64
65
+ impl < T : Float > MapCoordsInplace < T > for Point < T > {
66
+
67
+ fn map_coords_inplace ( & mut self , func : & Fn ( & ( T , T ) ) -> ( T , T ) )
68
+ {
69
+ let new_point = func ( & ( self . 0 . x , self . 0 . y ) ) ;
70
+ self . 0 . x = new_point. 0 ;
71
+ self . 0 . y = new_point. 1 ;
72
+ }
73
+ }
74
+
47
75
impl < T : Float , NT : Float > MapCoords < T , NT > for Line < T > {
48
76
type Output = Line < NT > ;
49
77
@@ -52,6 +80,14 @@ impl<T: Float, NT: Float> MapCoords<T, NT> for Line<T> {
52
80
}
53
81
}
54
82
83
+ impl < T : Float > MapCoordsInplace < T > for Line < T > {
84
+ fn map_coords_inplace ( & mut self , func : & Fn ( & ( T , T ) ) -> ( T , T ) )
85
+ {
86
+ self . start . map_coords_inplace ( func) ;
87
+ self . end . map_coords_inplace ( func) ;
88
+ }
89
+ }
90
+
55
91
impl < T : Float , NT : Float > MapCoords < T , NT > for LineString < T > {
56
92
type Output = LineString < NT > ;
57
93
@@ -60,6 +96,15 @@ impl<T: Float, NT: Float> MapCoords<T, NT> for LineString<T> {
60
96
}
61
97
}
62
98
99
+ impl < T : Float > MapCoordsInplace < T > for LineString < T > {
100
+ fn map_coords_inplace ( & mut self , func : & Fn ( & ( T , T ) ) -> ( T , T ) )
101
+ {
102
+ for p in self . 0 . iter_mut ( ) {
103
+ p. map_coords_inplace ( func) ;
104
+ }
105
+ }
106
+ }
107
+
63
108
impl < T : Float , NT : Float > MapCoords < T , NT > for Polygon < T > {
64
109
type Output = Polygon < NT > ;
65
110
@@ -71,6 +116,16 @@ impl<T: Float, NT: Float> MapCoords<T, NT> for Polygon<T> {
71
116
}
72
117
}
73
118
119
+ impl < T : Float > MapCoordsInplace < T > for Polygon < T > {
120
+ fn map_coords_inplace ( & mut self , func : & Fn ( & ( T , T ) ) -> ( T , T ) )
121
+ {
122
+ self . exterior . map_coords_inplace ( func) ;
123
+ for p in self . interiors . iter_mut ( ) {
124
+ p. map_coords_inplace ( func) ;
125
+ }
126
+ }
127
+ }
128
+
74
129
impl < T : Float , NT : Float > MapCoords < T , NT > for MultiPoint < T > {
75
130
type Output = MultiPoint < NT > ;
76
131
@@ -79,6 +134,15 @@ impl<T: Float, NT: Float> MapCoords<T, NT> for MultiPoint<T> {
79
134
}
80
135
}
81
136
137
+ impl < T : Float > MapCoordsInplace < T > for MultiPoint < T > {
138
+ fn map_coords_inplace ( & mut self , func : & Fn ( & ( T , T ) ) -> ( T , T ) )
139
+ {
140
+ for p in self . 0 . iter_mut ( ) {
141
+ p. map_coords_inplace ( func) ;
142
+ }
143
+ }
144
+ }
145
+
82
146
impl < T : Float , NT : Float > MapCoords < T , NT > for MultiLineString < T > {
83
147
type Output = MultiLineString < NT > ;
84
148
@@ -87,6 +151,15 @@ impl<T: Float, NT: Float> MapCoords<T, NT> for MultiLineString<T> {
87
151
}
88
152
}
89
153
154
+ impl < T : Float > MapCoordsInplace < T > for MultiLineString < T > {
155
+ fn map_coords_inplace ( & mut self , func : & Fn ( & ( T , T ) ) -> ( T , T ) )
156
+ {
157
+ for p in self . 0 . iter_mut ( ) {
158
+ p. map_coords_inplace ( func) ;
159
+ }
160
+ }
161
+ }
162
+
90
163
impl < T : Float , NT : Float > MapCoords < T , NT > for MultiPolygon < T > {
91
164
type Output = MultiPolygon < NT > ;
92
165
@@ -95,6 +168,15 @@ impl<T: Float, NT: Float> MapCoords<T, NT> for MultiPolygon<T> {
95
168
}
96
169
}
97
170
171
+ impl < T : Float > MapCoordsInplace < T > for MultiPolygon < T > {
172
+ fn map_coords_inplace ( & mut self , func : & Fn ( & ( T , T ) ) -> ( T , T ) )
173
+ {
174
+ for p in self . 0 . iter_mut ( ) {
175
+ p. map_coords_inplace ( func) ;
176
+ }
177
+ }
178
+ }
179
+
98
180
impl < T : Float , NT : Float > MapCoords < T , NT > for Geometry < T > {
99
181
type Output = Geometry < NT > ;
100
182
@@ -112,6 +194,22 @@ impl<T: Float, NT: Float> MapCoords<T, NT> for Geometry<T> {
112
194
}
113
195
}
114
196
197
+ impl < T : Float > MapCoordsInplace < T > for Geometry < T > {
198
+ fn map_coords_inplace ( & mut self , func : & Fn ( & ( T , T ) ) -> ( T , T ) )
199
+ {
200
+ match * self {
201
+ Geometry :: Point ( ref mut x) => x. map_coords_inplace ( func) ,
202
+ Geometry :: Line ( ref mut x) => x. map_coords_inplace ( func) ,
203
+ Geometry :: LineString ( ref mut x) => x. map_coords_inplace ( func) ,
204
+ Geometry :: Polygon ( ref mut x) => x. map_coords_inplace ( func) ,
205
+ Geometry :: MultiPoint ( ref mut x) => x. map_coords_inplace ( func) ,
206
+ Geometry :: MultiLineString ( ref mut x) => x. map_coords_inplace ( func) ,
207
+ Geometry :: MultiPolygon ( ref mut x) => x. map_coords_inplace ( func) ,
208
+ Geometry :: GeometryCollection ( ref mut x) => x. map_coords_inplace ( func) ,
209
+ }
210
+ }
211
+ }
212
+
115
213
impl < T : Float , NT : Float > MapCoords < T , NT > for GeometryCollection < T > {
116
214
type Output = GeometryCollection < NT > ;
117
215
@@ -120,9 +218,19 @@ impl<T: Float, NT: Float> MapCoords<T, NT> for GeometryCollection<T> {
120
218
}
121
219
}
122
220
123
- mod test {
124
- #[ allow( unused_imports) ]
221
+ impl < T : Float > MapCoordsInplace < T > for GeometryCollection < T > {
222
+ fn map_coords_inplace ( & mut self , func : & Fn ( & ( T , T ) ) -> ( T , T ) )
223
+ {
224
+ for p in self . 0 . iter_mut ( ) {
225
+ p. map_coords_inplace ( func) ;
226
+ }
227
+ }
228
+ }
125
229
230
+
231
+
232
+ #[ cfg( test) ]
233
+ mod test {
126
234
use super :: * ;
127
235
128
236
#[ test]
@@ -133,6 +241,14 @@ mod test {
133
241
assert_eq ! ( new_p. y( ) , 110. ) ;
134
242
}
135
243
244
+ #[ test]
245
+ fn point_inplace ( ) {
246
+ let mut p2 = Point :: new ( 10f32 , 10f32 ) ;
247
+ p2. map_coords_inplace ( & |& ( x, y) | ( x+10. , y+100. ) ) ;
248
+ assert_eq ! ( p2. x( ) , 20. ) ;
249
+ assert_eq ! ( p2. y( ) , 110. ) ;
250
+ }
251
+
136
252
#[ test]
137
253
fn line ( ) {
138
254
let line = Line :: new ( Point :: new ( 0. , 0. ) , Point :: new ( 1. , 2. ) ) ;
0 commit comments