22import pytest
33import xarray as xr
44from dask .delayed import Delayed
5+ from shapely .geometry import Polygon
56
67from sarxarray .utils import (
78 _get_chunks ,
89 _validate_multi_look_inputs ,
910 complex_coherence ,
11+ crop ,
1012 multi_look ,
1113)
1214
@@ -27,6 +29,22 @@ def synthetic_dataarray():
2729 },
2830 )
2931
32+ # Create a polygon for cropping
33+ @pytest .fixture
34+ def crop_geometry ():
35+ return Polygon ([
36+ [601.9 , 1405 ],
37+ [605 , 1407 ],
38+ [606 , 1408 ],
39+ [606 , 1409 ],
40+ [603 , 1409 ],
41+ [601.9 , 1405 ]
42+ ])
43+
44+ # Create a bounding box for cropping
45+ @pytest .fixture
46+ def crop_geometry_bbox ():
47+ return (601.9 , 1405 , 606 , 1409 )
3048
3149# this class tests multi_look with dataarray. For testing with dataset, see
3250# test_stack.py
@@ -276,3 +294,62 @@ def test_complex_coherence_bad_args(
276294 complex_coherence (reference , other1 , window_size = (2 , 2 ), compute = True )
277295 with pytest .raises (ValueError ):
278296 complex_coherence (reference , other2 , window_size = (2 , 2 ), compute = True )
297+
298+
299+ class TestUtilsCrop :
300+ def test_crop_poly (self , synthetic_dataarray , crop_geometry ):
301+ da = synthetic_dataarray
302+ geom = crop_geometry
303+ da_crop = crop (da , geom )
304+ assert da_crop .azimuth .size == 6
305+ assert da_crop .range .size == 5
306+ assert da_crop .time .size == da .time .size
307+
308+ def test_crop_bbox (self , synthetic_dataarray , crop_geometry_bbox ):
309+ da = synthetic_dataarray
310+ geom = crop_geometry_bbox
311+ da_crop = crop (da , geom )
312+ assert da_crop .azimuth .size == 6
313+ assert da_crop .range .size == 5
314+ assert da_crop .time .size == da .time .size
315+
316+ def test_crop_wrong_dimname (self , synthetic_dataarray , crop_geometry ):
317+ da = synthetic_dataarray
318+ da = da .rename ({"azimuth" : "az" }) # rename azimuth to a wrong name
319+ geom = crop_geometry
320+ with pytest .raises (ValueError ):
321+ _ = crop (da , geom )
322+
323+ def test_crop_bbox_wrong_length (self , synthetic_dataarray , crop_geometry_bbox ):
324+ da = synthetic_dataarray
325+ geom = crop_geometry_bbox [:3 ]
326+ with pytest .raises (AssertionError ):
327+ _ = crop (da , geom )
328+
329+ def test_crop_bbox_wrong_az (self , synthetic_dataarray , crop_geometry_bbox ):
330+ da = synthetic_dataarray
331+ geom = ( # swap min and max azimuth
332+ crop_geometry_bbox [2 ],
333+ crop_geometry_bbox [1 ],
334+ crop_geometry_bbox [0 ],
335+ crop_geometry_bbox [3 ]
336+ )
337+ with pytest .raises (AssertionError ):
338+ _ = crop (da , geom )
339+
340+ def test_crop_bbox_wrong_r (self , synthetic_dataarray , crop_geometry_bbox ):
341+ da = synthetic_dataarray
342+ geom = ( # swap min and max range
343+ crop_geometry_bbox [0 ],
344+ crop_geometry_bbox [3 ],
345+ crop_geometry_bbox [2 ],
346+ crop_geometry_bbox [1 ]
347+ )
348+ with pytest .raises (AssertionError ):
349+ _ = crop (da , geom )
350+
351+ def test_crop_bbox_wrong_geom_type (self , synthetic_dataarray , crop_geometry_bbox ):
352+ da = synthetic_dataarray
353+ geom = list (crop_geometry_bbox )
354+ with pytest .raises (ValueError ):
355+ _ = crop (da , geom )
0 commit comments