Skip to content

Commit

Permalink
rasterio backend: added nodatavals attribute (#1740)
Browse files Browse the repository at this point in the history
* Added nodatavals attribute

Connected with issue #1736

* Replace None nodatavals with np.nan

Fixes the type error with serialization: https://travis-ci.org/pydata/xarray/jobs/306107679

* Fix typo

* Added nodatavals open_rasterio information.

* added issue and author information

* added tests for nodatavals attr

* moved back to nan

* added separate test for missing nodataval
  • Loading branch information
snowman2 authored and fmaussion committed Jan 19, 2018
1 parent f2ea7b6 commit 74d8318
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Documentation

Enhancements
~~~~~~~~~~~~
- Added nodatavals attribute to DataArray when using :py:func:`~xarray.open_rasterio`. (:issue:`1736`).
By `Alan Snow <https://github.com/snowman2>`_.

- :py:func:`~plot.contourf()` learned to contour 2D variables that have both a
1D co-ordinate (e.g. time) and a 2D co-ordinate (e.g. depth as a function of
Expand Down
4 changes: 4 additions & 0 deletions xarray/backends/rasterio_.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ def open_rasterio(filename, chunks=None, cache=None, lock=None):
# Affine transformation matrix (tuple of floats)
# Describes coefficients mapping pixel coordinates to CRS
attrs['transform'] = tuple(riods.transform)
if hasattr(riods, 'nodatavals'):
# The nodata values for the raster bands
attrs['nodatavals'] = tuple([np.nan if nodataval is None else nodataval
for nodataval in riods.nodatavals])

# Parse extra metadata from tags, if supported
parsers = {'ENVI': _parse_envi}
Expand Down
62 changes: 62 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,68 @@ def test_serialization(self):
with xr.open_dataarray(tmp_nc_file) as ncds:
assert_identical(rioda, ncds)

@requires_scipy_or_netCDF4
def test_nodata(self):
import rasterio
from rasterio.transform import from_origin

# Create a geotiff file in utm proj
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(5000, 80000, 1000, 2000.)
with rasterio.open(
tmp_file, 'w',
driver='GTiff', height=ny, width=nx, count=nz,
crs={'units': 'm', 'no_defs': True, 'ellps': 'WGS84',
'proj': 'utm', 'zone': 18},
transform=transform,
nodata=-9765,
dtype=rasterio.float32) as s:
s.write(data)
expected_nodatavals = [-9765, -9765, -9765]
with xr.open_rasterio(tmp_file) as rioda:
np.testing.assert_array_equal(rioda.attrs['nodatavals'],
expected_nodatavals)
with create_tmp_file(suffix='.nc') as tmp_nc_file:
rioda.to_netcdf(tmp_nc_file)
with xr.open_dataarray(tmp_nc_file) as ncds:
np.testing.assert_array_equal(ncds.attrs['nodatavals'],
expected_nodatavals)

@requires_scipy_or_netCDF4
def test_nodata_missing(self):
import rasterio
from rasterio.transform import from_origin

# Create a geotiff file in utm proj
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(5000, 80000, 1000, 2000.)
with rasterio.open(
tmp_file, 'w',
driver='GTiff', height=ny, width=nx, count=nz,
crs={'units': 'm', 'no_defs': True, 'ellps': 'WGS84',
'proj': 'utm', 'zone': 18},
transform=transform,
dtype=rasterio.float32) as s:
s.write(data)

expected_nodatavals = [np.nan, np.nan, np.nan]
with xr.open_rasterio(tmp_file) as rioda:
np.testing.assert_array_equal(rioda.attrs['nodatavals'],
expected_nodatavals)
with create_tmp_file(suffix='.nc') as tmp_nc_file:
rioda.to_netcdf(tmp_nc_file)
with xr.open_dataarray(tmp_nc_file) as ncds:
np.testing.assert_array_equal(ncds.attrs['nodatavals'],
expected_nodatavals)

def test_utm(self):
import rasterio
from rasterio.transform import from_origin
Expand Down

0 comments on commit 74d8318

Please sign in to comment.