Skip to content

Commit c0708c5

Browse files
committed
FIX: Fix rasters(_rio).get_window function (with None and with xarray input)
1 parent b47bf60 commit c0708c5

5 files changed

Lines changed: 34 additions & 24 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.54.2 (2026-mm-dd)
44

5+
- FIX: Fix `rasters(_rio).get_window` function (with `None` and with xarray input)
56
- DEPS: Add an `arcpy` optional dependency to install `python-json-logger` needed for the backend logger
67

78
## 1.54.1 (2026-06-02)

ci/test_rasters.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,3 +1535,8 @@ def test_reproject(tmp_path, raster_path):
15351535
ci.assert_val(shape_arr.rio.count, 1, "Reprojected count")
15361536
with pytest.raises(AssertionError):
15371537
ci.assert_val(xda.rio.shape, (1, 161, 232), "Native shape")
1538+
1539+
1540+
def test_none_window(raster_path):
1541+
ds = get_xda(raster_path)
1542+
ci.assert_val(rasters.get_window(ds, window=None), None, "None window")

ci/test_rasters_rio.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,8 @@ def _test_idx(idx_list):
425425
_test_idx([1])
426426
_test_idx([1, 2])
427427
_test_idx(1)
428+
429+
430+
def test_none_window(raster_path):
431+
with rasterio.open(str(raster_path)) as ds:
432+
ci.assert_val(rasters_rio.get_window(ds, None), None, "None window")

sertit/rasters.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,20 +1005,19 @@ def wrapper(xda: xr.DataArray, *_args, **_kwargs):
10051005
return wrapper
10061006

10071007

1008-
@__read__any_raster_to_rio_ds
1009-
def get_window(ds: AnyRasterType, window: Any):
1008+
def get_window(xda: AnyRasterType, window: Any):
10101009
"""
10111010
Get a window from any type of input
10121011
10131012
Args:
1014-
ds (AnyRasterType): Path to the raster, its dataset, its :code:`xarray` or a tuple containing its array and metadata
1013+
xda (AnyRasterType): Path to the raster, its dataset, its :code:`xarray` or a tuple containing its array and metadata
10151014
window (Any): Anything that can be returned as a window. In case of iterable, assumption is made it's geographic bounds. For pixel, please provide a Window directly.
10161015
10171016
Returns:
10181017
Window: Rasterio window
10191018
10201019
"""
1021-
return rasters_rio.get_window(ds, window)
1020+
return rasters_rio.get_window(xda, window)
10221021

10231022

10241023
@__read__any_raster_to_rio_ds

sertit/rasters_rio.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -932,26 +932,26 @@ def get_window(ds: AnyRasterType, window: Any):
932932
"Window should either be a GeoDataFrame, tuple, list, Window, readable as a vector or set to None"
933933
) from exc
934934

935-
# Use rioxarray way to convert window to integer
936-
try:
937-
(row_start, row_stop), (col_start, col_stop) = window.toranges()
938-
row_start = 0 if row_start < 0 else np.floor(row_start)
939-
row_stop = 0 if row_stop < 0 else np.ceil(row_stop)
940-
col_start = 0 if col_start < 0 else np.floor(col_start)
941-
col_stop = 0 if col_stop < 0 else np.ceil(col_stop)
942-
row_slice = slice(int(row_start), int(row_stop))
943-
col_slice = slice(int(col_start), int(col_stop))
944-
945-
window = Window.from_slices(
946-
rows=row_slice,
947-
cols=col_slice,
948-
width=int(window.width),
949-
height=int(window.height),
950-
)
951-
except ValueError as exc:
952-
raise ValueError(
953-
"Impossible to create a window on your dataset! Make sure the two are overlapping."
954-
) from exc
935+
# Use rioxarray way to convert window to integer
936+
try:
937+
(row_start, row_stop), (col_start, col_stop) = window.toranges()
938+
row_start = 0 if row_start < 0 else np.floor(row_start)
939+
row_stop = 0 if row_stop < 0 else np.ceil(row_stop)
940+
col_start = 0 if col_start < 0 else np.floor(col_start)
941+
col_stop = 0 if col_stop < 0 else np.ceil(col_stop)
942+
row_slice = slice(int(row_start), int(row_stop))
943+
col_slice = slice(int(col_start), int(col_stop))
944+
945+
window = Window.from_slices(
946+
rows=row_slice,
947+
cols=col_slice,
948+
width=int(window.width),
949+
height=int(window.height),
950+
)
951+
except ValueError as exc:
952+
raise ValueError(
953+
"Impossible to create a window on your dataset! Make sure the two are overlapping."
954+
) from exc
955955

956956
return window
957957

0 commit comments

Comments
 (0)