Skip to content
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

Simplify some rasterio tests #1890

Merged
merged 2 commits into from
Feb 7, 2018
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
6 changes: 6 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ Bug fixes
- Fix indexing with lists for arrays loaded from netCDF files with
``engine='h5netcdf`` (:issue:`1864`).
By `Stephan Hoyer <https://github.com/shoyer>`_.
- Corrected a bug with incorrect coordinates for non-georeferenced geotiff
files (:issue:`1686`). Internally, we now use the rasterio coordinate
transform tool instead of doing the computations ourselves. A
``parse_coordinates`` kwarg has beed added to :py:func:`~open_rasterio`
(set to ``True`` per default).
By `Fabien Maussion <https://github.com/fmaussion>`_.

.. _whats-new.0.10.0:

Expand Down
37 changes: 16 additions & 21 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2178,9 +2178,11 @@ class TestPyNioAutocloseTrue(TestPyNio):
@requires_rasterio
@contextlib.contextmanager
def create_tmp_geotiff(nx=4, ny=3, nz=3,
transform=None,
transform_args=[5000, 80000, 1000, 2000.],
crs={'units': 'm', 'no_defs': True, 'ellps': 'WGS84',
'proj': 'utm', 'zone': 18}):
'proj': 'utm', 'zone': 18},
open_kwargs={}):
# yields a temporary geotiff file and a corresponding expected DataArray
import rasterio
from rasterio.transform import from_origin
Expand All @@ -2192,15 +2194,16 @@ def create_tmp_geotiff(nx=4, ny=3, nz=3,
else:
data_shape = nz, ny, nx
write_kwargs = {}
data = np.arange(nz*ny*nx,
dtype=rasterio.float32).reshape(*data_shape)
transform = from_origin(*transform_args)
data = np.arange(nz*ny*nx, dtype=rasterio.float32).reshape(*data_shape)
if transform is None:
transform = from_origin(*transform_args)
with rasterio.open(
tmp_file, 'w',
driver='GTiff', height=ny, width=nx, count=nz,
crs=crs,
transform=transform,
dtype=rasterio.float32) as s:
dtype=rasterio.float32,
**open_kwargs) as s:
s.write(data, **write_kwargs)
dx, dy = s.res[0], -s.res[1]

Expand Down Expand Up @@ -2236,30 +2239,19 @@ def test_utm(self):
assert isinstance(rioda.attrs['res'], tuple)
assert isinstance(rioda.attrs['is_tiled'], np.uint8)
assert isinstance(rioda.attrs['transform'], tuple)
np.testing.assert_array_equal(rioda.attrs['nodatavals'],
[np.NaN, np.NaN, np.NaN])

# Check no parse coords
with xr.open_rasterio(tmp_file, parse_coordinates=False) as rioda:
assert 'x' not in rioda.coords
assert 'y' not in rioda.coords

def test_non_rectilinear(self):
import rasterio
from rasterio.transform import from_origin

# Create a geotiff file with 2d coordinates
with create_tmp_file(suffix='.tif') as tmp_file:
# data
nx, ny, nz = 4, 3, 3
data = np.arange(nx*ny*nz,
dtype=rasterio.float32).reshape(nz, ny, nx)
transform = from_origin(0, 3, 1, 1).rotation(45)
with rasterio.open(
tmp_file, 'w',
driver='GTiff', height=ny, width=nx, count=nz,
transform=transform,
dtype=rasterio.float32) as s:
s.write(data)

with create_tmp_geotiff(transform=from_origin(0, 3, 1, 1).rotation(45),
crs=None) as (tmp_file, _):
# Default is to not parse coords
with xr.open_rasterio(tmp_file) as rioda:
assert 'x' not in rioda.coords
Expand All @@ -2278,14 +2270,17 @@ def test_non_rectilinear(self):

def test_platecarree(self):
with create_tmp_geotiff(8, 10, 1, transform_args=[1, 2, 0.5, 2.],
crs='+proj=latlong') \
crs='+proj=latlong',
open_kwargs={'nodata': -9765}) \
as (tmp_file, expected):
with xr.open_rasterio(tmp_file) as rioda:
assert_allclose(rioda, expected)
assert isinstance(rioda.attrs['crs'], basestring)
assert isinstance(rioda.attrs['res'], tuple)
assert isinstance(rioda.attrs['is_tiled'], np.uint8)
assert isinstance(rioda.attrs['transform'], tuple)
np.testing.assert_array_equal(rioda.attrs['nodatavals'],
[-9765.])

def test_notransform(self):
# regression test for https://github.com/pydata/xarray/issues/1686
Expand Down