Skip to content

Commit

Permalink
test ordered bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Sep 3, 2024
1 parent 490d461 commit 2560f82
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
5 changes: 3 additions & 2 deletions rio_tiler/io/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def __attrs_post_init__(self):
assert xarray is not None, "xarray must be installed to use XarrayReader"
assert rioxarray is not None, "rioxarray must be installed to use XarrayReader"

# NOTE: rioxarray returns **ordered** bounds in form of (minx, miny, maxx, maxx)
self.bounds = tuple(self.input.rio.bounds())
self.crs = self.input.rio.crs
if not self.crs:
Expand All @@ -84,9 +85,9 @@ def __attrs_post_init__(self):

if self.crs == WGS84_CRS and (
self.bounds[0] < -180
or min(self.bounds[1], self.bounds[3]) < -90
or self.bounds[1] < -90
or self.bounds[2] > 180
or max(self.bounds[1], self.bounds[3]) > 90
or self.bounds[3] > 90
):
raise InvalidGeographicBounds(f"Invalid geographic bounds: {self.bounds}")

Expand Down
44 changes: 36 additions & 8 deletions tests/test_io_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def test_xarray_reader():
arr,
dims=("time", "y", "x"),
coords={
"x": list(range(-170, 180, 10)),
"y": list(range(-80, 85, 5)),
"x": numpy.arange(-170, 180, 10),
"y": numpy.arange(-80, 85, 5),
"time": [datetime(2022, 1, 1)],
},
)
Expand Down Expand Up @@ -269,8 +269,8 @@ def test_xarray_reader_resampling():
arr,
dims=("time", "y", "x"),
coords={
"x": list(range(-170, 180, 10)),
"y": list(range(-80, 85, 5)),
"x": numpy.arange(-170, 180, 10),
"y": numpy.arange(-80, 85, 5),
"time": [datetime(2022, 1, 1)],
},
)
Expand Down Expand Up @@ -349,8 +349,22 @@ def test_xarray_reader_invalid_bounds_crs():
arr,
dims=("time", "y", "x"),
coords={
"x": list(range(10, 360, 10)),
"y": list(range(-80, 85, 5)),
"x": numpy.arange(10, 360, 10),
"y": numpy.arange(-80, 85, 5),
"time": [datetime(2022, 1, 1)],
},
)
data.rio.write_crs("epsg:4326", inplace=True)
with pytest.raises(InvalidGeographicBounds):
with XarrayReader(data):
pass

data = xarray.DataArray(
arr,
dims=("time", "y", "x"),
coords={
"x": numpy.arange(-170, 180, 10),
"y": numpy.arange(15, 180, 5),
"time": [datetime(2022, 1, 1)],
},
)
Expand All @@ -363,12 +377,26 @@ def test_xarray_reader_invalid_bounds_crs():
arr,
dims=("time", "y", "x"),
coords={
"x": list(range(-170, 180, 10)),
"y": list(range(15, 180, 5)),
"x": numpy.arange(-170, 180, 10),
"y": numpy.arange(15, 180, 5),
"time": [datetime(2022, 1, 1)],
},
)
data.rio.write_crs("epsg:4326", inplace=True)
with pytest.raises(InvalidGeographicBounds):
with XarrayReader(data):
pass

# Inverted bounds are still ok because rioxarray reorder the bounds
data = xarray.DataArray(
arr,
dims=("time", "y", "x"),
coords={
"x": numpy.flip(numpy.arange(-170, 180, 10)),
"y": numpy.flip(numpy.arange(-80, 85, 5)),
"time": [datetime(2022, 1, 1)],
},
)
data.rio.write_crs("epsg:4326", inplace=True)
with XarrayReader(data):
pass

0 comments on commit 2560f82

Please sign in to comment.