Rioxarray
Rioxarray is an open-source Python library that extends xarray with geospatial capabilities, powered by rasterio. It provides a `.rio` accessor for xarray DataArrays and Datasets, enabling easy manipulation, reprojecting, and analysis of geospatial raster data. It is actively maintained with regular releases, typically following `xarray` and `rasterio` updates.
Warnings
- gotcha Many `.rio` operations (e.g., `reproject`, `bounds`, `resolution`) explicitly require a Coordinate Reference System (CRS) to be set on the DataArray. If the CRS is missing, these operations will raise an error.
- breaking The `rasterio.crs.CRS` object, which `rioxarray` uses, became immutable in `rasterio` version 1.2.x (a dependency for `rioxarray >= 0.20.0`). This means direct in-place modification of CRS attributes (e.g., `da.rio.crs['init'] = 'epsg:4326'`) will now raise an error.
- gotcha There can be nuances in handling `_FillValue` from NetCDF/HDF and `nodata` from GeoTIFFs. `rioxarray` attempts to unify these under `da.rio.nodata`, but inconsistencies or unexpected behavior can arise, especially when merging datasets or after reprojection where new `NaN` values might be introduced.
- deprecated The `da.rio.set_crs()` method is deprecated. While it may still function in current versions, it is recommended to use `da.rio.write_crs()` for better clarity and future compatibility.
- gotcha When opening non-geospatial-specific file formats (e.g., generic NetCDF, HDF files without explicit geospatial metadata), you must explicitly specify `engine='rasterio'` with `xr.open_dataarray` or `xr.open_dataset` for `rioxarray` to process them. Otherwise, `xarray`'s default engine will be used, and the `.rio` accessor might not be available or fully functional.
Install
-
pip install rioxarray -
pip install 'rioxarray[tools]' # For CLI tools, requires GDAL
Imports
- rioxarray
import rioxarray
Quickstart
import xarray as xr
import rioxarray # Enables the .rio accessor
import numpy as np
# Create a dummy DataArray for demonstration
# In a real scenario, you'd typically open a GeoTIFF like this:
# da = xr.open_dataarray("path/to/your/file.tif", engine="rasterio")
# Dummy data: a 2x2 grid representing a small geographic area
data = np.array([[10.1, 20.2], [30.3, 40.4]], dtype=np.float32)
coords = {
"y": [45.5, 45.0], # Example latitude (y-coordinate, usually decreasing)
"x": [-120.0, -119.5], # Example longitude (x-coordinate, usually increasing)
}
da = xr.DataArray(data, coords=coords, dims=("y", "x"), name="temperature")
# Assign geospatial metadata using the .rio accessor
# These properties are typically inferred automatically when opening a geospatial file.
da = da.rio.write_crs("EPSG:4326") # WGS84 Geographic CRS
da = da.rio.set_spatial_dims(x_dim="x", y_dim="y") # Explicitly set spatial dimensions
print(f"Original CRS: {da.rio.crs}")
print(f"Original Bounds: {da.rio.bounds()}")
print(f"Original Resolution: {da.rio.resolution()}")
print(f"Original Width: {da.rio.width}")
print(f"Original Height: {da.rio.height}")
# Example of a common operation: Reproject to a different CRS
# Note: This operation requires 'pyproj' to be installed. Some systems
# might also need GDAL for full functionality and performance.
# try:
# # Reproject to Web Mercator (EPSG:3857)
# reprojected_da = da.rio.reproject("EPSG:3857")
# print(f"\nReprojected CRS: {reprojected_da.rio.crs}")
# print(f"Reprojected Bounds: {reprojected_da.rio.bounds()}")
# print(f"Reprojected Resolution: {reprojected_da.rio.resolution()}")
# except ImportError:
# print("\nSkipping reprojection: 'pyproj' not installed. Install with 'pip install pyproj'.")
# except Exception as e:
# print(f"\nCould not reproject: {e}")