Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit 6a20f91

Browse files
gbrenerfmaussion
authored andcommitted
Add attributes to rasterio backend (pydata#1455)
* Add attributes to rasterio backend Add the 'res', 'tiled', and 'transform' attributes to xarray's rasterio backend. * Follow flake8 suggestions, update whats-new.rst * Add period end of line in docs * Change "tiled" attribute to the actual name, "is_tiled"
1 parent 84ee8d7 commit 6a20f91

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ v0.9.7 (unreleased)
2121
Enhancements
2222
~~~~~~~~~~~~
2323

24+
- More attributes available in :py:attr:`~xarray.Dataset.attrs` dictionary when
25+
raster files are opened with :py:func:`~xarray.open_rasterio`.
26+
By `Greg Brener <https://github.com/gbrener>`_
27+
2428
Bug fixes
2529
~~~~~~~~~
2630

xarray/backends/rasterio_.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,17 @@ def open_rasterio(filename, chunks=None, cache=None, lock=None):
141141
# CRS is a dict-like object specific to rasterio
142142
# We convert it back to a PROJ4 string using rasterio itself
143143
attrs['crs'] = riods.crs.to_string()
144-
# Maybe we'd like to parse other attributes here (for later)
144+
if hasattr(riods, 'res'):
145+
# (width, height) tuple of pixels in units of CRS
146+
attrs['res'] = riods.res
147+
if hasattr(riods, 'is_tiled'):
148+
# Is the TIF tiled? (bool)
149+
# We cast it to an int for netCDF compatibility
150+
attrs['is_tiled'] = np.uint8(riods.is_tiled)
151+
if hasattr(riods, 'transform'):
152+
# Affine transformation matrix (tuple of floats)
153+
# Describes coefficients mapping pixel coordinates to CRS
154+
attrs['transform'] = tuple(riods.transform)
145155

146156
data = indexing.LazilyIndexedArray(RasterioArrayWrapper(riods))
147157

xarray/tests/test_backends.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,12 @@ def test_serialization_utm(self):
14731473
assert_allclose(rioda, expected)
14741474
assert 'crs' in rioda.attrs
14751475
assert isinstance(rioda.attrs['crs'], basestring)
1476+
assert 'res' in rioda.attrs
1477+
assert isinstance(rioda.attrs['res'], tuple)
1478+
assert 'is_tiled' in rioda.attrs
1479+
assert isinstance(rioda.attrs['is_tiled'], np.uint8)
1480+
assert 'transform' in rioda.attrs
1481+
assert isinstance(rioda.attrs['transform'], tuple)
14761482

14771483
# Write it to a netcdf and read again (roundtrip)
14781484
with create_tmp_file(suffix='.nc') as tmp_nc_file:
@@ -1510,6 +1516,12 @@ def test_serialization_platecarree(self):
15101516
assert_allclose(rioda, expected)
15111517
assert 'crs' in rioda.attrs
15121518
assert isinstance(rioda.attrs['crs'], basestring)
1519+
assert 'res' in rioda.attrs
1520+
assert isinstance(rioda.attrs['res'], tuple)
1521+
assert 'is_tiled' in rioda.attrs
1522+
assert isinstance(rioda.attrs['is_tiled'], np.uint8)
1523+
assert 'transform' in rioda.attrs
1524+
assert isinstance(rioda.attrs['transform'], tuple)
15131525

15141526
# Write it to a netcdf and read again (roundtrip)
15151527
with create_tmp_file(suffix='.nc') as tmp_nc_file:

0 commit comments

Comments
 (0)