From efa8fe6715d40b874d92328dbf24574acc1e8026 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Tue, 2 Apr 2019 20:17:44 +0200 Subject: [PATCH 1/5] BUG: Fix #2864 by adding the missing vrt parameters (transform, width, height) --- xarray/backends/rasterio_.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xarray/backends/rasterio_.py b/xarray/backends/rasterio_.py index b629eb51241..2f00702f854 100644 --- a/xarray/backends/rasterio_.py +++ b/xarray/backends/rasterio_.py @@ -224,6 +224,9 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, src_nodata=vrt.src_nodata, dst_nodata=vrt.dst_nodata, tolerance=vrt.tolerance, + transform=vrt.transform, + width=vrt.width, + height=vrt.height, warp_extras=vrt.warp_extras) if lock is None: From e16b4c39b9f71a3ac0ce581100c537a4078ffed4 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 4 Apr 2019 08:49:06 +0000 Subject: [PATCH 2/5] TEST: Add a test to demonstrate that #2864 is actually fixed by patch --- xarray/tests/test_backends.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index a20ba2df229..15400b846e6 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -3350,6 +3350,28 @@ def test_rasterio_vrt(self): assert actual_shape == expected_shape assert expected_val.all() == actual_val.all() + def test_rasterio_vrt_with_transform_and_size(self): + # Test open_rasterio() support of WarpedVRT with transform, width and + # height (issue #2864) + import rasterio + with create_tmp_geotiff() as (tmp_file, expected): + with rasterio.open(tmp_file) as src: + # Estimate the transform, width and height for a change of resolution + # tmp_file initial res is (1000,2000) (default values) + transform, width, height = rasterio.warp.calculate_default_transform( + src.crs, src.crs, src.width, src.height, resolution=500, *src.bounds) + with rasterio.vrt.WarpedVRT(src, transform=transform, width=width, height=height) as vrt: + expected_shape = (vrt.width, vrt.height) + expected_res = vrt.res + expected_transform = vrt.transform + with xr.open_rasterio(vrt) as da: + actual_shape = (da.sizes['x'], da.sizes['y']) + actual_res = da.res + actual_transform = da.transform + assert actual_res == expected_res + assert actual_shape == expected_shape + assert actual_transform == expected_transform + @network def test_rasterio_vrt_network(self): import rasterio From f8de48c7a606e4b0209388bac4ec01e343dfc3d2 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 4 Apr 2019 09:06:01 +0000 Subject: [PATCH 3/5] DOC: Add an entry corresponding to the patch in whats-new.rst --- doc/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 9c88445b5ba..2bdedb1a505 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -33,6 +33,8 @@ Bug fixes By `Andras Gefferth `_. - ``swap_dims`` would create incorrect ``indexes`` (:issue:`2842`). By `Stephan Hoyer `_. +- open_rasterio() now supports rasterio.vrt.WarpedVRT with custom transform, width and height (:issue:`2864`). + By `Julien Michel `_. .. _whats-new.0.12.0: From 18dc6d8ee68e3369ffed3656da733de6d20bf3cb Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 4 Apr 2019 09:19:10 +0000 Subject: [PATCH 4/5] STY: Conform to PEP 8 --- xarray/tests/test_backends.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 15400b846e6..c9e1101d528 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -3356,11 +3356,14 @@ def test_rasterio_vrt_with_transform_and_size(self): import rasterio with create_tmp_geotiff() as (tmp_file, expected): with rasterio.open(tmp_file) as src: - # Estimate the transform, width and height for a change of resolution + # Estimate the transform, width and height + # for a change of resolution # tmp_file initial res is (1000,2000) (default values) - transform, width, height = rasterio.warp.calculate_default_transform( - src.crs, src.crs, src.width, src.height, resolution=500, *src.bounds) - with rasterio.vrt.WarpedVRT(src, transform=transform, width=width, height=height) as vrt: + trans, w, h = rasterio.warp.calculate_default_transform( + src.crs, src.crs, src.width, src.height, + resolution=500, *src.bounds) + with rasterio.vrt.WarpedVRT(src, transform=trans, + width=w, height=h) as vrt: expected_shape = (vrt.width, vrt.height) expected_res = vrt.res expected_transform = vrt.transform From fea6780dc6fba7fe4d36938d1141473aa7939e77 Mon Sep 17 00:00:00 2001 From: Julien Michel Date: Thu, 4 Apr 2019 09:40:35 +0000 Subject: [PATCH 5/5] TEST: Fix test imports and assert for the affine transform check --- xarray/tests/test_backends.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index c9e1101d528..f76d1927210 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -3354,12 +3354,14 @@ def test_rasterio_vrt_with_transform_and_size(self): # Test open_rasterio() support of WarpedVRT with transform, width and # height (issue #2864) import rasterio + from rasterio.warp import calculate_default_transform + from affine import Affine with create_tmp_geotiff() as (tmp_file, expected): with rasterio.open(tmp_file) as src: # Estimate the transform, width and height # for a change of resolution # tmp_file initial res is (1000,2000) (default values) - trans, w, h = rasterio.warp.calculate_default_transform( + trans, w, h = calculate_default_transform( src.crs, src.crs, src.width, src.height, resolution=500, *src.bounds) with rasterio.vrt.WarpedVRT(src, transform=trans, @@ -3370,7 +3372,7 @@ def test_rasterio_vrt_with_transform_and_size(self): with xr.open_rasterio(vrt) as da: actual_shape = (da.sizes['x'], da.sizes['y']) actual_res = da.res - actual_transform = da.transform + actual_transform = Affine(*da.transform) assert actual_res == expected_res assert actual_shape == expected_shape assert actual_transform == expected_transform