From 2b1800371a39206b27ed053d78589f60e310844c Mon Sep 17 00:00:00 2001 From: Fabien Maussion Date: Tue, 6 Feb 2018 15:22:34 +0100 Subject: [PATCH 1/2] Simplify some rasterio tests --- doc/whats-new.rst | 6 ++++++ xarray/tests/test_backends.py | 37 +++++++++++++++-------------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index ecd9877c496..f17bb8f0d49 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -129,6 +129,12 @@ Bug fixes - Fix indexing with lists for arrays loaded from netCDF files with ``engine='h5netcdf`` (:issue:`1864`). By `Stephan Hoyer `_. +- 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 `_. .. _whats-new.0.10.0: diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 73eb49b863b..fd61cf4b177 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -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 @@ -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] @@ -2236,6 +2239,8 @@ 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: @@ -2243,23 +2248,10 @@ def test_utm(self): 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 @@ -2278,7 +2270,8 @@ 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) @@ -2286,6 +2279,8 @@ def test_platecarree(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'], + [-9765.]) def test_notransform(self): # regression test for https://github.com/pydata/xarray/issues/1686 From b92e516d4611aef6928173b2e485290a99d191ce Mon Sep 17 00:00:00 2001 From: Fabien Maussion Date: Tue, 6 Feb 2018 20:34:01 +0100 Subject: [PATCH 2/2] pep8 --- xarray/tests/test_backends.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index fd61cf4b177..85b6bdea346 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -2271,7 +2271,7 @@ 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', - open_kwargs={'nodata':-9765}) \ + open_kwargs={'nodata': -9765}) \ as (tmp_file, expected): with xr.open_rasterio(tmp_file) as rioda: assert_allclose(rioda, expected)