Skip to content

Commit

Permalink
ENH: Add write_transform to store cache in GDAL location (#141)
Browse files Browse the repository at this point in the history
* ENH: Add write_transform to store cache in GDAL location

* docstring updates

* activate & fix tests
  • Loading branch information
snowman2 authored Jun 24, 2020
1 parent cc2ed4f commit 79a68b2
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 131 deletions.
11 changes: 7 additions & 4 deletions docs/examples/manage_information_loss.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
"\n",
"- [rio.to_raster()](../rioxarray.rst#rioxarray.rioxarray.RasterDataset.to_raster)\n",
"- [rio.write_crs()](../rioxarray.rst#rioxarray.rioxarray.XRasterBase.write_crs)\n",
"- [rio.write_transform()](../rioxarray.rst#rioxarray.rioxarray.XRasterBase.write_transform)\n",
"- [rio.update_attrs()](../rioxarray.rst#rioxarray.rioxarray.XRasterBase.update_attrs)\n",
"- [rio.crs](../rioxarray.rst#rioxarray.rioxarray.XRasterBase.crs)\n",
"- [rio.nodata](../rioxarray.rst#rioxarray.rioxarray.RasterArray.nodata)"
"- [rio.nodata](../rioxarray.rst#rioxarray.rioxarray.RasterArray.nodata)\n",
"\n",
"Note that `write_transform` is only needed if you are not saving the x,y coordinates. It is for\n",
"GDAL to be able to read in the transform without needing the original coordinates and is useful\n",
"if you read in the file with `parse_coordinates=False`."
]
},
{
Expand Down Expand Up @@ -65,7 +70,6 @@
" 'nodata': 0,\n",
" 'units': ('DN', 'DN'),\n",
" '_FillValue': nan,\n",
" 'transform': (3.0, 0.0, 466266.0, 0.0, -3.0, 8084700.0),\n",
" 'scale_factor': 1.0,\n",
" 'add_offset': 0.0},\n",
" CRS.from_epsg(32722),\n",
Expand Down Expand Up @@ -129,7 +133,6 @@
" 'nodata': 0,\n",
" 'units': ('DN', 'DN'),\n",
" '_FillValue': nan,\n",
" 'transform': (3.0, 0.0, 466266.0, 0.0, -3.0, 8084700.0),\n",
" 'scale_factor': 1.0,\n",
" 'add_offset': 0.0},\n",
" CRS.from_epsg(32722),\n",
Expand Down Expand Up @@ -191,7 +194,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.6.10"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ History
Latest
------
- BUG: Fix assigning fill value in `rio.pad_box` (pull #140)

- ENH: Add `rio.write_transform` to store cache in GDAL location (issue #129 & #139)

0.0.29
-------
Expand Down
12 changes: 6 additions & 6 deletions rioxarray/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,6 @@ def _get_rasterio_attrs(riods):
"""
# Add rasterio attributes
attrs = _parse_tags(riods.tags(1))
# Affine transformation matrix (always available)
# This describes coefficients mapping pixel coordinates to CRS
# For serialization store as tuple of 6 floats, the last row being
# always (0, 0, 1) per definition (see
# https://github.com/sgillies/affine)
attrs["transform"] = tuple(_rio_transform(riods))[:6]
if hasattr(riods, "nodata") and riods.nodata is not None:
# The nodata values for the raster bands
attrs["_FillValue"] = riods.nodata
Expand Down Expand Up @@ -789,6 +783,12 @@ def open_rasterio(
result.attrs, result.encoding, "missing_value", name=da_name
)

# Affine transformation matrix (always available)
# This describes coefficients mapping pixel coordinates to CRS
# For serialization store as tuple of 6 floats, the last row being
# always (0, 0, 1) per definition (see
# https://github.com/sgillies/affine)
result.rio.write_transform(riods.transform, inplace=True)
if hasattr(riods, "crs") and riods.crs:
result.rio.write_crs(riods.crs, inplace=True)

Expand Down
2 changes: 1 addition & 1 deletion rioxarray/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ def merge_arrays(
coords = _get_nonspatial_coords(representative_array)

out_attrs = representative_array.attrs
out_attrs["transform"] = tuple(merged_transform)[:6]
xda = DataArray(
name=dataarrays[0].name,
data=merged_data,
Expand All @@ -134,6 +133,7 @@ def merge_arrays(
out_nodata = nodata if nodata is not None else representative_array.rio.nodata
xda.rio.write_nodata(out_nodata, inplace=True)
xda.rio.write_crs(representative_array.rio.crs, inplace=True)
xda.rio.write_transform(merged_transform, inplace=True)
return xda


Expand Down
Loading

0 comments on commit 79a68b2

Please sign in to comment.