rioxarray

repository·master·Indexed 20 days ago

https://github.com/corteva/rioxarray

A geospatial xarray extension powered by rasterio that provides capabilities for reading and writing geospatial metadata, such as CRS and affine transforms. It enables clipping rasters via bounding boxes or geometries, opening Cloud Optimized GeoTIFFs (COG), and exporting xarray DataArrays or Datasets to raster formats using rio.to_raster(). The library supports memory-efficient operations through windowed writing and Dask-backed parallel processing.

Tokens
24.5K
Snippets
93
Records
126
Agent score
69%

What's inside rioxarray

  1. Overview of rioxarray

    master
    rioxarray is an extension for xarray that provides geospatial capabilities by leveraging rasterio. It allows users to perform common geospatial operations—such as reading/writing geospatial raster data, managing coordinate reference systems (CRS), and reprojecting data—directly on xarray.DataArray and xarray.Dataset objects.
  2. Map Rasterio attributes to rioxarray DataArray attributes

    master

    When switching from rasterio to rioxarray, note that many file-specific attributes (like driver, tiled, compress, or interleave) are unused because rioxarray operates on xarray.DataArray abstractions which are not strictly tied to a single file on disk.

    Profile Equivalences

    rasterio (from ds.profile)rioxarray (from DataArray)
    blockxsize.encoding["preferred_chunks"]["x"]
    blockysize.encoding["preferred_chunks"]["y"]
    countrio.count
    crsrio.crs
    dtype.encoding["rasterio_dtype"]
    heightrio.height
    nodatario.nodata (or encoded_nodata)
    transformrio.transform()
    widthrio.width
  3. Why switch from rasterio to rioxarray

    master

    Switching from rasterio to rioxarray is primarily done to integrate raster data into an xarray workflow.

    Key advantages include:

    • Unified Abstraction: xarray treats the raster dataset and the raster array as a single object, allowing you to use object attributes instead of passing separate arguments to functions.
    • Advanced Computation: xarray provides built-in functions and supports backends like Dask for out-of-memory computation, cluster execution, or GPU acceleration.
    • Dask Support: rioxarray supports basic dask features during I/O operations.

    Important Considerations:

    • Information Loss: Be aware of potential information loss when working with xarray abstractions.
    • Naming Confusion: A rasterio Dataset and an xarray Dataset are distinct objects; be careful when using these overlapping terms in your code.
  4. Pull Request Guidelines for rioxarray

    master

    Before submitting a pull request, ensure the following requirements are met:

    1. Include Tests: Every PR must include corresponding tests.
    2. Update Documentation: If adding functionality, update the docs. This includes adding a docstring to the new function and adding the feature to the list in README.rst.
    3. Python Compatibility: The code must work for Python versions 3.12 through 3.14.
  5. Install rioxarray via conda

    master

    To install rioxarray using conda, it is recommended to use the conda-forge channel. It is best practice to install the package into a new environment rather than your base environment to ensure stability and easier debugging.

    Warning: Avoid using pip install inside a conda environment if possible. If a package is missing from conda-forge, consider submitting a recipe to the conda-forge community instead.

    conda config --prepend channels conda-forge
    conda config --set channel_priority strict
    conda create -n rioxarray_env rioxarray
    conda activate rioxarray_env
  6. Read netCDF files for rioxarray compatibility

    master

    When loading netCDF files using standard xarray methods, it is recommended to use decode_coords="all". This ensures that grid mapping variables are loaded into coordinates, which is necessary for compatibility with rioxarray operations.

    import xarray
    
    xds = xarray.open_dataset("file.nc", decode_coords="all")
  7. Common rioxarray tasks via StackExchange recipes

    master

    For specific, common troubleshooting and implementation questions, rioxarray users often refer to these documented StackExchange solutions:

    • Extracting data within geometry (shape)
    • Converting NetCDF dataset array to GeoTiff
    • Adding projection to a NetCDF file (Satellite)
    • Creating a new raster TIFF file masked based on a GeoJSON file
    • Masking NetCDF time series data from a shapefile
    • Extracting data from a raster at a specific point
    • Converting a raster to CSV with lat, lon, and value columns
  8. Activate the `rio` accessor in xarray

    master

    To extend xarray objects with rioxarray functionality, you must import rioxarray. This activates the .rio accessor on xarray.DataArray and xarray.Dataset objects, allowing you to access geospatial operations like coordinate management, clipping, and reprojecting.

    import rioxarray
    # Now xarray objects have the .rio accessor available