Skip to content

Commit a4305e7

Browse files
jmichel-otbdcherian
authored andcommitted
BUG: Fix #2864 by adding the missing vrt parameters (#2865)
* BUG: Fix #2864 by adding the missing vrt parameters (transform, width, height) * TEST: Add a test to demonstrate that #2864 is actually fixed by patch * DOC: Add an entry corresponding to the patch in whats-new.rst * STY: Conform to PEP 8 * TEST: Fix test imports and assert for the affine transform check
1 parent b9a920e commit a4305e7

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Bug fixes
4545
(e.g., ``swap_dims``, ``isel``, ``reindex``, ``[]``) (:issue:`2842`,
4646
:issue:`2856`).
4747
By `Stephan Hoyer <https://github.com/shoyer>`_.
48+
- open_rasterio() now supports rasterio.vrt.WarpedVRT with custom transform, width and height (:issue:`2864`).
49+
By `Julien Michel <https://github.com/jmichel-otb>`_.
4850

4951
.. _whats-new.0.12.0:
5052

xarray/backends/rasterio_.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None,
224224
src_nodata=vrt.src_nodata,
225225
dst_nodata=vrt.dst_nodata,
226226
tolerance=vrt.tolerance,
227+
transform=vrt.transform,
228+
width=vrt.width,
229+
height=vrt.height,
227230
warp_extras=vrt.warp_extras)
228231

229232
if lock is None:

xarray/tests/test_backends.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,6 +3350,33 @@ def test_rasterio_vrt(self):
33503350
assert actual_shape == expected_shape
33513351
assert expected_val.all() == actual_val.all()
33523352

3353+
def test_rasterio_vrt_with_transform_and_size(self):
3354+
# Test open_rasterio() support of WarpedVRT with transform, width and
3355+
# height (issue #2864)
3356+
import rasterio
3357+
from rasterio.warp import calculate_default_transform
3358+
from affine import Affine
3359+
with create_tmp_geotiff() as (tmp_file, expected):
3360+
with rasterio.open(tmp_file) as src:
3361+
# Estimate the transform, width and height
3362+
# for a change of resolution
3363+
# tmp_file initial res is (1000,2000) (default values)
3364+
trans, w, h = calculate_default_transform(
3365+
src.crs, src.crs, src.width, src.height,
3366+
resolution=500, *src.bounds)
3367+
with rasterio.vrt.WarpedVRT(src, transform=trans,
3368+
width=w, height=h) as vrt:
3369+
expected_shape = (vrt.width, vrt.height)
3370+
expected_res = vrt.res
3371+
expected_transform = vrt.transform
3372+
with xr.open_rasterio(vrt) as da:
3373+
actual_shape = (da.sizes['x'], da.sizes['y'])
3374+
actual_res = da.res
3375+
actual_transform = Affine(*da.transform)
3376+
assert actual_res == expected_res
3377+
assert actual_shape == expected_shape
3378+
assert actual_transform == expected_transform
3379+
33533380
@network
33543381
def test_rasterio_vrt_network(self):
33553382
import rasterio

0 commit comments

Comments
 (0)