Skip to content

BUG: Fix #2864 by adding the missing vrt parameters #2865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Bug fixes
By `Andras Gefferth <https://github.com/kefirbandi>`_.
- ``swap_dims`` would create incorrect ``indexes`` (:issue:`2842`).
By `Stephan Hoyer <https://github.com/shoyer>`_.
- open_rasterio() now supports rasterio.vrt.WarpedVRT with custom transform, width and height (:issue:`2864`).
By `Julien Michel <https://github.com/jmichel-otb>`_.

.. _whats-new.0.12.0:

Expand Down
3 changes: 3 additions & 0 deletions xarray/backends/rasterio_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3350,6 +3350,33 @@ 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
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 = 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
with xr.open_rasterio(vrt) as da:
actual_shape = (da.sizes['x'], da.sizes['y'])
actual_res = da.res
actual_transform = Affine(*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
Expand Down