@@ -156,19 +156,187 @@ def test_filter_bbox_args_and_kwargs_conflict(con100: Connection, args, kwargs,
156
156
con100 .load_collection ("S2" ).filter_bbox (* args , ** kwargs )
157
157
158
158
159
- def test_mask_polygon (con100 : Connection ):
159
+ def test_aggregate_spatial_basic (con100 : Connection ):
160
160
img = con100 .load_collection ("S2" )
161
161
polygon = shapely .geometry .box (0 , 0 , 1 , 1 )
162
+ masked = img .aggregate_spatial (geometries = polygon , reducer = "mean" )
163
+ assert sorted (masked .graph .keys ()) == ["aggregatespatial1" , "loadcollection1" ]
164
+ assert masked .graph ["aggregatespatial1" ] == {
165
+ "process_id" : "aggregate_spatial" ,
166
+ "arguments" : {
167
+ "data" : {"from_node" : "loadcollection1" },
168
+ "geometries" : {
169
+ "type" : "Polygon" ,
170
+ "coordinates" : (((1.0 , 0.0 ), (1.0 , 1.0 ), (0.0 , 1.0 ), (0.0 , 0.0 ), (1.0 , 0.0 )),),
171
+ },
172
+ "reducer" : {"process_graph" : {
173
+ "mean1" : {"process_id" : "mean" , "arguments" : {"data" : {"from_parameter" : "data" }}, "result" : True }
174
+ }}
175
+ },
176
+ "result" : True
177
+ }
178
+
179
+
180
+ @pytest .mark .parametrize (["polygon" , "expected_geometries" ], [
181
+ (
182
+ shapely .geometry .box (0 , 0 , 1 , 1 ),
183
+ {"type" : "Polygon" , "coordinates" : (((1.0 , 0.0 ), (1.0 , 1.0 ), (0.0 , 1.0 ), (0.0 , 0.0 ), (1.0 , 0.0 )),)},
184
+ ),
185
+ (
186
+ {"type" : "Polygon" , "coordinates" : (((1 , 0 ), (1 , 1 ), (0 , 1 ), (0 , 0 ), (1 , 0 )),)},
187
+ {"type" : "Polygon" , "coordinates" : (((1 , 0 ), (1 , 1 ), (0 , 1 ), (0 , 0 ), (1 , 0 )),)},
188
+ ),
189
+ (
190
+ shapely .geometry .MultiPolygon ([shapely .geometry .box (0 , 0 , 1 , 1 )]),
191
+ {"type" : "MultiPolygon" , "coordinates" : [(((1.0 , 0.0 ), (1.0 , 1.0 ), (0.0 , 1.0 ), (0.0 , 0.0 ), (1.0 , 0.0 )),)]},
192
+ ),
193
+ (
194
+ shapely .geometry .GeometryCollection ([shapely .geometry .box (0 , 0 , 1 , 1 )]),
195
+ {"type" : "GeometryCollection" , "geometries" : [
196
+ {"type" : "Polygon" , "coordinates" : (((1.0 , 0.0 ), (1.0 , 1.0 ), (0.0 , 1.0 ), (0.0 , 0.0 ), (1.0 , 0.0 )),)}
197
+ ]},
198
+ ),
199
+ (
200
+ {
201
+ "type" : "FeatureCollection" ,
202
+ "features" : [
203
+ {
204
+ "type" : "Feature" , "properties" : {},
205
+ "geometry" : {"type" : "Polygon" , "coordinates" : (((1 , 0 ), (1 , 1 ), (0 , 1 ), (0 , 0 ), (1 , 0 )),)},
206
+ },
207
+
208
+ ]
209
+ },
210
+ {
211
+ "type" : "FeatureCollection" ,
212
+ "features" : [
213
+ {
214
+ "type" : "Feature" , "properties" : {},
215
+ "geometry" : {"type" : "Polygon" , "coordinates" : (((1 , 0 ), (1 , 1 ), (0 , 1 ), (0 , 0 ), (1 , 0 )),)},
216
+ },
217
+
218
+ ]
219
+ },
220
+ ),
221
+ ])
222
+ def test_aggregate_spatial_types (con100 : Connection , polygon , expected_geometries ):
223
+ img = con100 .load_collection ("S2" )
224
+ masked = img .aggregate_spatial (geometries = polygon , reducer = "mean" )
225
+ assert sorted (masked .graph .keys ()) == ["aggregatespatial1" , "loadcollection1" ]
226
+ assert masked .graph ["aggregatespatial1" ] == {
227
+ "process_id" : "aggregate_spatial" ,
228
+ "arguments" : {
229
+ "data" : {"from_node" : "loadcollection1" },
230
+ "geometries" : expected_geometries ,
231
+ "reducer" : {"process_graph" : {
232
+ "mean1" : {"process_id" : "mean" , "arguments" : {"data" : {"from_parameter" : "data" }}, "result" : True }
233
+ }}
234
+ },
235
+ "result" : True
236
+ }
237
+
238
+
239
+ def test_aggregate_spatial_with_crs (con100 : Connection , recwarn ):
240
+ img = con100 .load_collection ("S2" )
241
+ polygon = shapely .geometry .box (0 , 0 , 1 , 1 )
242
+ masked = img .aggregate_spatial (geometries = polygon , reducer = "mean" , crs = "EPSG:32631" )
243
+ warnings = [str (w .message ) for w in recwarn ]
244
+ assert "Geometry with non-Lon-Lat CRS 'EPSG:32631' is only supported by specific back-ends." in warnings
245
+ assert sorted (masked .graph .keys ()) == ["aggregatespatial1" , "loadcollection1" ]
246
+ assert masked .graph ["aggregatespatial1" ] == {
247
+ "process_id" : "aggregate_spatial" ,
248
+ "arguments" : {
249
+ "data" : {"from_node" : "loadcollection1" },
250
+ "geometries" : {
251
+ "type" : "Polygon" ,
252
+ "coordinates" : (((1.0 , 0.0 ), (1.0 , 1.0 ), (0.0 , 1.0 ), (0.0 , 0.0 ), (1.0 , 0.0 )),),
253
+ "crs" : {"properties" : {"name" : "EPSG:32631" }, "type" : "name" },
254
+ },
255
+ "reducer" : {"process_graph" : {
256
+ "mean1" : {"process_id" : "mean" , "arguments" : {"data" : {"from_parameter" : "data" }}, "result" : True }
257
+ }}
258
+ },
259
+ "result" : True
260
+ }
261
+
262
+
263
+ def test_mask_polygon_basic (con100 : Connection ):
264
+ img = con100 .load_collection ("S2" )
265
+ polygon = shapely .geometry .box (0 , 0 , 1 , 1 )
266
+ masked = img .mask_polygon (mask = polygon )
267
+ assert sorted (masked .graph .keys ()) == ["loadcollection1" , "maskpolygon1" ]
268
+ assert masked .graph ["maskpolygon1" ] == {
269
+ "process_id" : "mask_polygon" ,
270
+ "arguments" : {
271
+ "data" : {"from_node" : "loadcollection1" },
272
+ "mask" : {
273
+ "type" : "Polygon" ,
274
+ "coordinates" : (((1.0 , 0.0 ), (1.0 , 1.0 ), (0.0 , 1.0 ), (0.0 , 0.0 ), (1.0 , 0.0 )),),
275
+ }
276
+ },
277
+ "result" : True
278
+ }
279
+
280
+
281
+ @pytest .mark .parametrize (["polygon" , "expected_mask" ], [
282
+ (
283
+ shapely .geometry .box (0 , 0 , 1 , 1 ),
284
+ {"type" : "Polygon" , "coordinates" : (((1.0 , 0.0 ), (1.0 , 1.0 ), (0.0 , 1.0 ), (0.0 , 0.0 ), (1.0 , 0.0 )),)},
285
+ ),
286
+ (
287
+ {"type" : "Polygon" , "coordinates" : (((1 , 0 ), (1 , 1 ), (0 , 1 ), (0 , 0 ), (1 , 0 )),)},
288
+ {"type" : "Polygon" , "coordinates" : (((1 , 0 ), (1 , 1 ), (0 , 1 ), (0 , 0 ), (1 , 0 )),)},
289
+ ),
290
+ (
291
+ shapely .geometry .MultiPolygon ([shapely .geometry .box (0 , 0 , 1 , 1 )]),
292
+ {"type" : "MultiPolygon" , "coordinates" : [(((1.0 , 0.0 ), (1.0 , 1.0 ), (0.0 , 1.0 ), (0.0 , 0.0 ), (1.0 , 0.0 )),)]},
293
+ ),
294
+ (
295
+ shapely .geometry .GeometryCollection ([shapely .geometry .box (0 , 0 , 1 , 1 )]),
296
+ {"type" : "GeometryCollection" , "geometries" : [
297
+ {"type" : "Polygon" , "coordinates" : (((1.0 , 0.0 ), (1.0 , 1.0 ), (0.0 , 1.0 ), (0.0 , 0.0 ), (1.0 , 0.0 )),)}
298
+ ]},
299
+ ),
300
+ (
301
+ {
302
+ "type" : "Feature" , "properties" : {},
303
+ "geometry" : {"type" : "Polygon" , "coordinates" : (((1 , 0 ), (1 , 1 ), (0 , 1 ), (0 , 0 ), (1 , 0 )),)},
304
+ },
305
+ {
306
+ "type" : "Feature" , "properties" : {},
307
+ "geometry" : {"type" : "Polygon" , "coordinates" : (((1 , 0 ), (1 , 1 ), (0 , 1 ), (0 , 0 ), (1 , 0 )),)},
308
+ },
309
+ ),
310
+ ])
311
+ def test_mask_polygon_types (con100 : Connection , polygon , expected_mask ):
312
+ img = con100 .load_collection ("S2" )
162
313
masked = img .mask_polygon (mask = polygon )
163
314
assert sorted (masked .graph .keys ()) == ["loadcollection1" , "maskpolygon1" ]
164
315
assert masked .graph ["maskpolygon1" ] == {
165
316
"process_id" : "mask_polygon" ,
166
317
"arguments" : {
167
318
"data" : {"from_node" : "loadcollection1" },
168
- 'mask' : {
169
- 'coordinates' : (((1.0 , 0.0 ), (1.0 , 1.0 ), (0.0 , 1.0 ), (0.0 , 0.0 ), (1.0 , 0.0 )),),
170
- 'crs' : {'properties' : {'name' : 'EPSG:4326' }, 'type' : 'name' },
171
- 'type' : 'Polygon' }
319
+ "mask" : expected_mask
320
+ },
321
+ "result" : True
322
+ }
323
+
324
+
325
+ def test_mask_polygon_with_crs (con100 : Connection , recwarn ):
326
+ img = con100 .load_collection ("S2" )
327
+ polygon = shapely .geometry .box (0 , 0 , 1 , 1 )
328
+ masked = img .mask_polygon (mask = polygon , srs = "EPSG:32631" )
329
+ warnings = [str (w .message ) for w in recwarn ]
330
+ assert "Geometry with non-Lon-Lat CRS 'EPSG:32631' is only supported by specific back-ends." in warnings
331
+ assert sorted (masked .graph .keys ()) == ["loadcollection1" , "maskpolygon1" ]
332
+ assert masked .graph ["maskpolygon1" ] == {
333
+ "process_id" : "mask_polygon" ,
334
+ "arguments" : {
335
+ "data" : {"from_node" : "loadcollection1" },
336
+ "mask" : {
337
+ "type" : "Polygon" , "coordinates" : (((1.0 , 0.0 ), (1.0 , 1.0 ), (0.0 , 1.0 ), (0.0 , 0.0 ), (1.0 , 0.0 )),),
338
+ "crs" : {"type" : "name" , "properties" : {"name" : "EPSG:32631" }},
339
+ },
172
340
},
173
341
"result" : True
174
342
}
0 commit comments