From af9af808b4c56c3d3fd0fff4ea89050279cfb304 Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Thu, 24 Oct 2024 14:48:26 +0200 Subject: [PATCH 1/3] check if the Y bounds are inverted and flip the image on the Y axis --- rio_tiler/io/xarray.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rio_tiler/io/xarray.py b/rio_tiler/io/xarray.py index 5cfb0e49..dfaf90d4 100644 --- a/rio_tiler/io/xarray.py +++ b/rio_tiler/io/xarray.py @@ -223,6 +223,10 @@ def tile( arr = ds.to_masked_array() arr.mask |= arr.data == ds.rio.nodata + output_bounds = ds.rio._unordered_bounds() + if output_bounds[1] > output_bounds[3] and ds.rio.transform().e > 0: + arr = arr[..., ::-1, :] + return ImageData( arr, bounds=tile_bounds, @@ -305,6 +309,10 @@ def part( arr = ds.to_masked_array() arr.mask |= arr.data == ds.rio.nodata + output_bounds = ds.rio._unordered_bounds() + if output_bounds[1] > output_bounds[3] and ds.rio.transform().e > 0: + arr = arr[..., ::-1, :] + img = ImageData( arr, bounds=ds.rio.bounds(), @@ -387,6 +395,10 @@ def preview( arr = ds.to_masked_array() arr.mask |= arr.data == ds.rio.nodata + output_bounds = ds.rio._unordered_bounds() + if output_bounds[1] > output_bounds[3] and ds.rio.transform().e > 0: + arr = arr[..., ::-1, :] + img = ImageData( arr, bounds=ds.rio.bounds(), @@ -523,6 +535,10 @@ def feature( arr = ds.to_masked_array() arr.mask |= arr.data == ds.rio.nodata + output_bounds = ds.rio._unordered_bounds() + if output_bounds[1] > output_bounds[3] and ds.rio.transform().e > 0: + arr = arr[..., ::-1, :] + img = ImageData( arr, bounds=ds.rio.bounds(), From cda3d468341225a989fc2d3601fb7a85607fc516 Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Fri, 25 Oct 2024 21:13:47 +0200 Subject: [PATCH 2/3] Update tests/test_io_xarray.py Co-authored-by: Henry Rodman --- tests/test_io_xarray.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_io_xarray.py b/tests/test_io_xarray.py index 4ea34e75..410231b9 100644 --- a/tests/test_io_xarray.py +++ b/tests/test_io_xarray.py @@ -536,6 +536,10 @@ def test_xarray_reader_no_dims(): def test_xarray_reader_Y_axis(): """test XarrayReader with 2D dataset.""" + # Create a DataArray where the y coordinates are in increasing order + # (this is the opposite of typical raster data) + # This array will have a positive y resolution in the affine transform + # and the data values increase with the y coordinates arr = numpy.arange(0, 33 * 35).reshape(1, 33, 35) data = xarray.DataArray( arr, From 246ed0427726219233cf08ccb4cd278983567c7d Mon Sep 17 00:00:00 2001 From: Vincent Sarago Date: Fri, 25 Oct 2024 21:13:55 +0200 Subject: [PATCH 3/3] Update tests/test_io_xarray.py Co-authored-by: Henry Rodman --- tests/test_io_xarray.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_io_xarray.py b/tests/test_io_xarray.py index 410231b9..d05eaca3 100644 --- a/tests/test_io_xarray.py +++ b/tests/test_io_xarray.py @@ -568,6 +568,10 @@ def test_xarray_reader_Y_axis(): img = dst.tile(1, 1, 2) assert img.array[0, 0, 0] > img.array[0, -1, -1] + # Create a DataArray where the y coordinates are in decreasing order + # (this is typical for raster data) + # This array will have a negative y resolution in the affine transform + # and the data values decrease with the y coordinates arr = numpy.arange(0, 33 * 35).reshape(1, 33, 35) data = xarray.DataArray( arr,