From 2560f820232f4a7c06cf04b4c2f24addf670370c Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Tue, 3 Sep 2024 12:16:16 +0200 Subject: [PATCH] test ordered bounds --- rio_tiler/io/xarray.py | 5 +++-- tests/test_io_xarray.py | 44 +++++++++++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/rio_tiler/io/xarray.py b/rio_tiler/io/xarray.py index 98023e77..24c9292c 100644 --- a/rio_tiler/io/xarray.py +++ b/rio_tiler/io/xarray.py @@ -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: @@ -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}") diff --git a/tests/test_io_xarray.py b/tests/test_io_xarray.py index cdefbd08..57515a1f 100644 --- a/tests/test_io_xarray.py +++ b/tests/test_io_xarray.py @@ -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)], }, ) @@ -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)], }, ) @@ -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)], }, ) @@ -363,8 +377,8 @@ 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)], }, ) @@ -372,3 +386,17 @@ def test_xarray_reader_invalid_bounds_crs(): 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