Skip to content

Commit 696418f

Browse files
Add crop test, allow for non-integer crops
1 parent 6d9a9e8 commit 696418f

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

sarxarray/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ def crop(data: xr.Dataset | xr.DataArray, geom: sg.Polygon) -> xr.Dataset:
181181

182182
bounding_box = geom.bounds # returns (min_az, min_r, max_az, max_r)
183183
data = data.sel(
184-
azimuth=range(bounding_box[0], bounding_box[2]+1),
185-
range=range(bounding_box[1], bounding_box[3]+1)
184+
azimuth=range(int(bounding_box[0]), int(np.ceil(bounding_box[2]))+1),
185+
range=range(int(bounding_box[1]), int(np.ceil(bounding_box[3]))+1)
186186
)
187187

188188
return data

tests/test_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import pytest
33
import xarray as xr
44
from dask.delayed import Delayed
5+
from shapely.geometry import Polygon
56

67
from sarxarray.utils import (
78
_get_chunks,
89
_validate_multi_look_inputs,
910
complex_coherence,
11+
crop,
1012
multi_look,
1113
)
1214

@@ -27,6 +29,17 @@ 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+
[602, 1405]
42+
])
3043

3144
# this class tests multi_look with dataarray. For testing with dataset, see
3245
# test_stack.py
@@ -276,3 +289,13 @@ def test_complex_coherence_bad_args(
276289
complex_coherence(reference, other1, window_size=(2, 2), compute=True)
277290
with pytest.raises(ValueError):
278291
complex_coherence(reference, other2, window_size=(2, 2), compute=True)
292+
293+
294+
class TestUtilsCrop:
295+
def test_crop(self, synthetic_dataarray, crop_geometry):
296+
da = synthetic_dataarray
297+
geom = crop_geometry
298+
da_crop = crop(da, geom)
299+
assert da_crop.azimuth.size == 6
300+
assert da_crop.range.size == 5
301+
assert da_crop.time.size == da.time.size

0 commit comments

Comments
 (0)