Rioxarray

0.22.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

Demonstrates how to create a simple xarray DataArray, assign geospatial metadata using the `.rio` accessor, and retrieve basic spatial properties like CRS, bounds, and resolution. It also includes a commented-out example of reprojection, a common geospatial operation.

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}")

view raw JSON →