rioxarray package

rioxarray.open_rasterio

rioxarray.open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, lock=None, masked=False, mask_and_scale=False, variable=None, group=None, default_name=None, decode_times=True, decode_timedelta=None, **open_kwargs)[source]

Open a file with rasterio (experimental).

This should work with any file that rasterio can open (most often: geoTIFF). The x and y coordinates are generated automatically from the file’s geoinformation, shifted to the center of each pixel (see “PixelIsArea” Raster Space for more information).

Parameters:
  • filename (str, rasterio.io.DatasetReader, or rasterio.vrt.WarpedVRT) – Path to the file to open. Or already open rasterio dataset.
  • parse_coordinates (bool, optional) – Whether to parse the x and y coordinates out of the file’s transform attribute or not. The default is to automatically parse the coordinates only if they are rectilinear (1D). It can be useful to set parse_coordinates=False if your files are very large or if you don’t need the coordinates.
  • chunks (int, tuple or dict, optional) – Chunk sizes along each dimension, e.g., 5, (5, 5) or {'x': 5, 'y': 5}. If chunks is provided, it used to load the new DataArray into a dask array. Chunks can also be set to True or "auto" to choose sensible chunk sizes according to dask.config.get("array.chunk-size").
  • cache (bool, optional) – If True, cache data loaded from the underlying datastore in memory as NumPy arrays when accessed to avoid reading from the underlying data- store multiple times. Defaults to True unless you specify the chunks argument to use dask, in which case it defaults to False.
  • lock (bool or dask.utils.SerializableLock, optional) –

    If chunks is provided, this argument is used to ensure that only one thread per process is reading from a rasterio file object at a time.

    By default and when a lock instance is provided, a xarray.backends.CachingFileManager is used to cache File objects. Since rasterio also caches some data, this will make repeated reads from the same object fast.

    When lock=False, no lock is used, allowing for completely parallel reads from multiple threads or processes. However, a new file handle is opened on each request.

  • masked (bool, optional) – If True, read the mask and set values to NaN. Defaults to False.
  • mask_and_scale (bool, optional) – Lazily scale (using the scales and offsets from rasterio) and mask. If the _Unsigned attribute is present treat integer arrays as unsigned.
  • variable (str or list or tuple, optional) – Variable name or names to use to filter loading.
  • group (str or list or tuple, optional) – Group name or names to use to filter loading.
  • default_name (str, optional) – The name of the data array if none exists. Default is None.
  • decode_times (bool, optional) – If True, decode times encoded in the standard NetCDF datetime format into datetime objects. Otherwise, leave them encoded as numbers.
  • decode_timedelta (bool, optional) – If True, decode variables and coordinates with time units in {“days”, “hours”, “minutes”, “seconds”, “milliseconds”, “microseconds”} into timedelta objects. If False, leave them encoded as numbers. If None (default), assume the same value of decode_time.
  • **open_kwargs (kwargs, optional) – Optional keyword arguments to pass into rasterio.open().
Returns:

The newly created dataset(s).

Return type:

xarray.Dataset | xarray.DataArray | List[xarray.Dataset]

rioxarray.merge module

rioxarray.merge.merge_arrays(dataarrays: Iterable[xarray.core.dataarray.DataArray], bounds: Optional[Tuple] = None, res: Optional[Tuple] = None, nodata: Optional[float] = None, precision: Optional[float] = None, method: Union[str, Callable, None] = None, crs: Optional[rasterio.crs.CRS] = None, parse_coordinates: bool = True) → xarray.core.dataarray.DataArray[source]

Merge data arrays geospatially.

Uses rasterio.merge.merge()

New in version 0.2: crs

Parameters:
  • dataarrays (List[xarray.DataArray]) – List of xarray.DataArray’s with all geo attributes. The first one is assumed to have the same CRS, dtype, and dimensions as the others in the array.
  • bounds (tuple, optional) – Bounds of the output image (left, bottom, right, top). If not set, bounds are determined from bounds of input DataArrays.
  • res (tuple, optional) – Output resolution in units of coordinate reference system. If not set, the resolution of the first DataArray is used. If a single value is passed, output pixels will be square.
  • nodata (float, optional) – nodata value to use in output file. If not set, uses the nodata value in the first input DataArray.
  • precision (float, optional) – Number of decimal points of precision when computing inverse transform.
  • method (str or callable, optional) – See rasterio docs.
  • crs (rasterio.crs.CRS, optional) – Output CRS. If not set, the CRS of the first DataArray is used.
  • parse_coordinates (bool, optional) – If False, it will disable loading spatial coordinates.
Returns:

The geospatially merged data.

Return type:

xarray.DataArray

rioxarray.merge.merge_datasets(datasets: Iterable[xarray.core.dataset.Dataset], bounds: Optional[Tuple] = None, res: Optional[Tuple] = None, nodata: Optional[float] = None, precision: Optional[float] = None, method: Union[str, Callable, None] = None, crs: Optional[rasterio.crs.CRS] = None) → xarray.core.dataarray.DataArray[source]

Merge datasets geospatially.

Uses rasterio.merge.merge()

New in version 0.2: crs

Parameters:
  • datasets (List[xarray.Dataset]) – List of xarray.Dataset’s with all geo attributes. The first one is assumed to have the same CRS, dtype, dimensions, and data_vars as the others in the array.
  • bounds (tuple, optional) – Bounds of the output image (left, bottom, right, top). If not set, bounds are determined from bounds of input Dataset.
  • res (tuple, optional) – Output resolution in units of coordinate reference system. If not set, the resolution of the first Dataset is used. If a single value is passed, output pixels will be square.
  • nodata (float, optional) – nodata value to use in output file. If not set, uses the nodata value in the first input Dataset.
  • precision (float, optional) – Number of decimal points of precision when computing inverse transform.
  • method (str or callable, optional) – See rasterio docs.
  • crs (rasterio.crs.CRS, optional) – Output CRS. If not set, the CRS of the first DataArray is used.
Returns:

The geospatially merged data.

Return type:

xarray.Dataset

rioxarray.set_options

class rioxarray.set_options(**kwargs)[source]

Set the global rioxarray option.

New in version 0.3.0.

New in version 0.7.0: skip_missing_spatial_dims

Parameters:
  • export_grid_mapping (bool, default=True) – If True, this option will export the full Climate and Forecasts (CF) grid mapping attributes for the CRS. This is useful if you are exporting your file to netCDF using xarray.Dataset.to_netcdf(). When disabled, only the crs_wkt and spatial_ref attributes will be written and the program will be faster due to not needing to use pyproj.CRS.to_cf().
  • skip_missing_spatial_dims (bool, default=False) – If True, it will not perform spatial operations on variables within a xarray.Dataset if the spatial dimensions are not found.

Usage as a context manager:

with rioxarray.set_options(export_grid_mapping=False):
    rds = rioxarray.open_rasterio(...)

Usage for global settings:

rioxarray.set_options(export_grid_mapping=False)

rioxarray.show_versions

rioxarray.show_versions()[source]

New in version 0.0.26.

Print useful debugging information

Example

> python -c “import rioxarray; rioxarray.show_versions()”

rioxarray rio accessors

rioxarray extends xarray with the rio accessor. The rio accessor is activated by importing rioxarray like so:

import rioxarray
class rioxarray.rioxarray.XRasterBase(xarray_obj)[source]

Bases: object

This is the base class for the GIS extensions for xarray

bounds(recalc=False)[source]
Parameters:recalc (bool, optional) – Will force the bounds to be recalculated instead of using the transform attribute.
Returns:left, bottom, right, top – Outermost coordinates of the xarray.DataArray | xarray.Dataset.
Return type:float
count

Returns the band count (z dimension size)

Type:int
crs

Retrieve projection from xarray.Dataset | xarray.DataArray

Type:rasterio.crs.CRS
estimate_utm_crs(datum_name='WGS 84')[source]

Returns the estimated UTM CRS based on the bounds of the dataset.

New in version 0.2.

Note

Requires pyproj 3+

Parameters:datum_name (str, optional) – The name of the datum to use in the query. Default is WGS 84.
Returns:
Return type:rasterio.crs.CRS
grid_mapping

The CF grid_mapping attribute. ‘spatial_ref’ is the default.

Type:str
height

Returns the height of the dataset (y dimension size)

Type:int
isel_window(window, pad=False)[source]

Use a rasterio.window.Window to select a subset of the data.

New in version 0.6.0: pad

Warning

Float indices are converted to integers.

Parameters:
  • window (rasterio.window.Window) – The window of the dataset to read.
  • pad (bool, default=False) – Set to True to expand returned DataArray to dimensions of the window
Returns:

The data in the window.

Return type:

xarray.Dataset | xarray.DataArray

resolution(recalc=False)[source]

Determine if the resolution of the grid. If the transformation has rotation, the sign of the resolution is lost.

Parameters:recalc (bool, optional) – Will force the resolution to be recalculated instead of using the transform attribute.
Returns:x_resolution, y_resolution – The resolution of the xarray.DataArray | xarray.Dataset
Return type:float
set_attrs(new_attrs, inplace=False)[source]

Set the attributes of the dataset/dataarray and reset rioxarray properties to re-search for them.

Parameters:
  • new_attrs (dict) – A dictionary of new attributes.
  • inplace (bool, optional) – If True, it will write to the existing dataset. Default is False.
Returns:

Modified dataset with new attributes.

Return type:

xarray.Dataset | xarray.DataArray

set_crs(input_crs, inplace=True)[source]

Set the CRS value for the Dataset/DataArray without modifying the dataset/data array.

Parameters:
  • input_crs (object) – Anything accepted by rasterio.crs.CRS.from_user_input.
  • inplace (bool, optional) – If True, it will write to the existing dataset. Default is False.
Returns:

Dataset with crs attribute.

Return type:

xarray.Dataset | xarray.DataArray

set_encoding(new_encoding, inplace=False)[source]

Set the encoding of the dataset/dataarray and reset rioxarray properties to re-search for them.

New in version 0.4.

Parameters:
  • new_encoding (dict) – A dictionary for encoding.
  • inplace (bool, optional) – If True, it will write to the existing dataset. Default is False.
Returns:

Modified dataset with new attributes.

Return type:

xarray.Dataset | xarray.DataArray

set_spatial_dims(x_dim, y_dim, inplace=True)[source]

This sets the spatial dimensions of the dataset.

Parameters:
  • x_dim (str) – The name of the x dimension.
  • y_dim (str) – The name of the y dimension.
  • inplace (bool, optional) – If True, it will modify the dataframe in place. Otherwise it will return a modified copy.
Returns:

Dataset with spatial dimensions set.

Return type:

xarray.Dataset | xarray.DataArray

shape

Returns the shape (height, width)

Type:tuple(int, int)
slice_xy(minx, miny, maxx, maxy)[source]

Slice the array by x,y bounds.

Parameters:
  • minx (float) – Minimum bound for x coordinate.
  • miny (float) – Minimum bound for y coordinate.
  • maxx (float) – Maximum bound for x coordinate.
  • maxy (float) – Maximum bound for y coordinate.
Returns:

The data in the slice.

Return type:

xarray.Dataset | xarray.DataArray

transform(recalc=False)[source]
Parameters:recalc (bool, optional) – If True, it will re-calculate the transform instead of using the cached transform.
Returns:The affine of the xarray.Dataset | xarray.DataArray
Return type:affine.Afffine
transform_bounds(dst_crs, densify_pts=21, recalc=False)[source]

Transform bounds from src_crs to dst_crs.

Optionally densifying the edges (to account for nonlinear transformations along these edges) and extracting the outermost bounds.

Note: this does not account for the antimeridian.

Parameters:
  • dst_crs (str, rasterio.crs.CRS, or dict) – Target coordinate reference system.
  • densify_pts (uint, optional) – Number of points to add to each edge to account for nonlinear edges produced by the transform process. Large numbers will produce worse performance. Default: 21 (gdal default).
  • recalc (bool, optional) – Will force the bounds to be recalculated instead of using the transform attribute.
Returns:

left, bottom, right, top – Outermost coordinates in target coordinate reference system.

Return type:

float

update_attrs(new_attrs, inplace=False)[source]

Update the attributes of the dataset/dataarray and reset rioxarray properties to re-search for them.

Parameters:
  • new_attrs (dict) – A dictionary of new attributes to update with.
  • inplace (bool, optional) – If True, it will write to the existing dataset. Default is False.
Returns:

Modified dataset with updated attributes.

Return type:

xarray.Dataset | xarray.DataArray

update_encoding(new_encoding, inplace=False)[source]

Update the encoding of the dataset/dataarray and reset rioxarray properties to re-search for them.

New in version 0.4.

Parameters:
  • new_encoding (dict) – A dictionary with encoding values to update with.
  • inplace (bool, optional) – If True, it will write to the existing dataset. Default is False.
Returns:

Modified dataset with updated attributes.

Return type:

xarray.Dataset | xarray.DataArray

width

Returns the width of the dataset (x dimension size)

Type:int
write_coordinate_system(inplace=False)[source]

Write the coordinate system CF metadata.

New in version 0.0.30.

Parameters:inplace (bool, optional) – If True, it will write to the existing dataset. Default is False.
Returns:The dataset with the CF coordinate system attributes added.
Return type:xarray.Dataset | xarray.DataArray
write_crs(input_crs=None, grid_mapping_name=None, inplace=False)[source]

Write the CRS to the dataset in a CF compliant manner.

Parameters:
  • input_crs (object) – Anything accepted by rasterio.crs.CRS.from_user_input.
  • grid_mapping_name (str, optional) – Name of the grid_mapping coordinate to store the CRS information in. Default is the grid_mapping name of the dataset.
  • inplace (bool, optional) – If True, it will write to the existing dataset. Default is False.
Returns:

Modified dataset with CF compliant CRS information.

Return type:

xarray.Dataset | xarray.DataArray

Examples

Write the CRS of the current xarray object:

>>> raster.rio.write_crs("epsg:4326", inplace=True)

Write the CRS on a copy:

>>> raster = raster.rio.write_crs("epsg:4326")
write_grid_mapping(grid_mapping_name='spatial_ref', inplace=False)[source]

Write the CF grid_mapping attribute.

Parameters:
  • grid_mapping_name (str, optional) – Name of the grid_mapping coordinate.
  • inplace (bool, optional) – If True, it will write to the existing dataset. Default is False.
Returns:

Modified dataset with CF compliant CRS information.

Return type:

xarray.Dataset | xarray.DataArray

write_transform(transform=None, grid_mapping_name=None, inplace=False)[source]

New in version 0.0.30.

Write the GeoTransform to the dataset where GDAL can read it in.

https://gdal.org/drivers/raster/netcdf.html#georeference

Parameters:
  • transform (affine.Affine, optional) – The transform of the dataset. If not provided, it will be calculated.
  • grid_mapping_name (str, optional) – Name of the grid_mapping coordinate to store the transform information in. Default is the grid_mapping name of the dataset.
  • inplace (bool, optional) – If True, it will write to the existing dataset. Default is False.
Returns:

Modified dataset with Geo Transform written.

Return type:

xarray.Dataset | xarray.DataArray

x_dim

The dimension for the X-axis.

Type:str
y_dim

The dimension for the Y-axis.

Type:str
class rioxarray.raster_array.RasterArray(xarray_obj)[source]

Bases: rioxarray.rioxarray.XRasterBase

This is the GIS extension for xarray.DataArray

clip(geometries, crs=None, all_touched=False, drop=True, invert=False, from_disk=False)[source]

Crops a xarray.DataArray by geojson like geometry dicts.

Powered by rasterio.features.geometry_mask.

Examples

>>> geometry = ''' {"type": "Polygon",
...                 "coordinates": [
...                 [[-94.07955380199459, 41.69085871273774],
...                 [-94.06082436942204, 41.69103313774798],
...                 [-94.06063203899649, 41.67932439500822],
...                 [-94.07935807746362, 41.679150041277325],
...                 [-94.07955380199459, 41.69085871273774]]]}'''
>>> cropping_geometries = [geojson.loads(geometry)]
>>> xds = xarray.open_rasterio('cool_raster.tif')
>>> cropped = xds.rio.clip(geometries=cropping_geometries, crs=4326)

New in version 0.2: from_disk

Parameters:
  • geometries (list) – A list of geojson geometry dicts or objects with __geom_interface__ with if you have rasterio 1.2+.
  • crs (rasterio.crs.CRS, optional) – The CRS of the input geometries. Default is to assume it is the same as the dataset.
  • all_touched (bool, optional) – If True, all pixels touched by geometries will be burned in. If false, only pixels whose center is within the polygon or that are selected by Bresenham’s line algorithm will be burned in.
  • drop (bool, optional) – If True, drop the data outside of the extent of the mask geoemtries Otherwise, it will return the same raster with the data masked. Default is True.
  • invert (boolean, optional) – If False, pixels that do not overlap shapes will be set as nodata. Otherwise, pixels that overlap the shapes will be set as nodata. False by default.
  • from_disk (boolean, optional) – If True, it will clip from disk using rasterio.mask.mask if possible. This is beneficial when the size of the data is larger than memory. Default is False.
Returns:

The clipped object.

Return type:

xarray.DataArray

clip_box(minx, miny, maxx, maxy, auto_expand=False, auto_expand_limit=3)[source]

Clip the xarray.DataArray by a bounding box.

Parameters:
  • minx (float) – Minimum bound for x coordinate.
  • miny (float) – Minimum bound for y coordinate.
  • maxx (float) – Maximum bound for x coordinate.
  • maxy (float) – Maximum bound for y coordinate.
  • auto_expand (bool) – If True, it will expand clip search if only 1D raster found with clip.
  • auto_expand_limit (int) – maximum number of times the clip will be retried before raising an exception.
Returns:

The clipped object.

Return type:

xarray.DataArray

encoded_nodata

Return the encoded nodata value for the dataset if encoded.

interpolate_na(method='nearest')[source]

This method uses scipy.interpolate.griddata to interpolate missing data.

Warning

scipy is an optional dependency.

Parameters:method ({‘linear’, ‘nearest’, ‘cubic’}, optional) – The method to use for interpolation in scipy.interpolate.griddata.
Returns:An interpolated xarray.DataArray object.
Return type:xarray.DataArray
nodata

Get the nodata value for the dataset.

pad_box(minx, miny, maxx, maxy, constant_values=None)[source]

Pad the xarray.DataArray to a bounding box

New in version 0.0.29.

Parameters:
  • minx (float) – Minimum bound for x coordinate.
  • miny (float) – Minimum bound for y coordinate.
  • maxx (float) – Maximum bound for x coordinate.
  • maxy (float) – Maximum bound for y coordinate.
  • constant_values (scalar) – The value used for padding. If None, nodata will be used if it is set, and np.nan otherwise.
Returns:

The padded object.

Return type:

xarray.DataArray

pad_xy(minx, miny, maxx, maxy, constant_values)[source]

Pad the array to x,y bounds.

New in version 0.0.29.

Parameters:
  • minx (float) – Minimum bound for x coordinate.
  • miny (float) – Minimum bound for y coordinate.
  • maxx (float) – Maximum bound for x coordinate.
  • maxy (float) – Maximum bound for y coordinate.
  • constant_values (scalar) – The value used for padding. If None, nodata will be used if it is set, and np.nan otherwise.
Returns:

The padded object.

Return type:

xarray.DataArray

reproject(dst_crs, resolution=None, shape=None, transform=None, resampling=<Resampling.nearest: 0>, nodata=None, **kwargs)[source]

Reproject xarray.DataArray objects

Powered by rasterio.warp.reproject()

Note

Only 2D/3D arrays with dimensions ‘x’/’y’ are currently supported. Requires either a grid mapping variable with ‘spatial_ref’ or a ‘crs’ attribute to be set containing a valid CRS. If using a WKT (e.g. from spatiareference.org), make sure it is an OGC WKT.

New in version 0.0.27: shape

New in version 0.0.28: transform

New in version 0.5.0: nodata, kwargs

Parameters:
  • dst_crs (str) – OGC WKT string or Proj.4 string.
  • resolution (float or tuple(float, float), optional) – Size of a destination pixel in destination projection units (e.g. degrees or metres).
  • shape (tuple(int, int), optional) – Shape of the destination in pixels (dst_height, dst_width). Cannot be used together with resolution.
  • transform (Affine, optional) – The destination transform.
  • resampling (rasterio.enums.Resampling, optional) – See rasterio.warp.reproject() for more details.
  • nodata (float, optional) – The nodata value used to initialize the destination; it will remain in all areas not covered by the reprojected source. Defaults to the nodata value of the source image if none provided and exists or attempts to find an appropriate value by dtype.
  • **kwargs (dict) – Additional keyword arguments to pass into rasterio.warp.reproject(). To override: - src_transform: rio.write_transform - src_crs: rio.write_crs - src_nodata: rio.write_nodata
Returns:

The reprojected DataArray.

Return type:

xarray.DataArray

reproject_match(match_data_array, resampling=<Resampling.nearest: 0>)[source]

Reproject a DataArray object to match the resolution, projection, and region of another DataArray.

Powered by rasterio.warp.reproject()

Note

Only 2D/3D arrays with dimensions ‘x’/’y’ are currently supported. Requires either a grid mapping variable with ‘spatial_ref’ or a ‘crs’ attribute to be set containing a valid CRS. If using a WKT (e.g. from spatiareference.org), make sure it is an OGC WKT.

Parameters:
Returns:

Contains the data from the src_data_array, reprojected to match match_data_array.

Return type:

xarray.DataArray

set_nodata(input_nodata, inplace=True)[source]

Set the nodata value for the DataArray without modifying the data array.

Parameters:
  • input_nodata (object) – Valid nodata for dtype.
  • inplace (bool, optional) – If True, it will write to the existing dataset. Default is False.
Returns:

Dataset with nodata attribute set.

Return type:

xarray.DataArray

to_raster(raster_path, driver=None, dtype=None, tags=None, windowed=False, recalc_transform=True, lock=None, compute=True, **profile_kwargs)[source]

Export the DataArray to a raster file.

..versionadded:: 0.2 lock

Parameters:
  • raster_path (str) – The path to output the raster to.
  • driver (str, optional) – The name of the GDAL/rasterio driver to use to export the raster. Default is “GTiff” if rasterio < 1.2 otherwise it will autodetect.
  • dtype (str, optional) – The data type to write the raster to. Default is the datasets dtype.
  • tags (dict, optional) – A dictionary of tags to write to the raster.
  • windowed (bool, optional) – If True, it will write using the windows of the output raster. This is useful for loading data in chunks when writing. Does not do anything when writing with dask. Default is False.
  • lock (boolean or Lock, optional) – Lock to use to write data using dask. If not supplied, it will use a single process for writing.
  • compute (bool, optional) – If True and data is a dask array, then compute and save the data immediately. If False, return a dask Delayed object. Call “.compute()” on the Delayed object to compute the result later. Call dask.compute(delayed1, delayed2) to save multiple delayed files at once. Default is True.
  • **profile_kwargs – Additional keyword arguments to pass into writing the raster. The nodata, transform, crs, count, width, and height attributes are ignored.
Returns:

If the data array is a dask array and compute is True. Otherwise None is returned.

Return type:

dask.Delayed

write_nodata(input_nodata, encoded=False, inplace=False)[source]

Write the nodata to the DataArray in a CF compliant manner.

Parameters:
  • input_nodata (object) – Nodata value for the DataArray. If input_nodata is None, it will remove the _FillValue attribute.
  • encoded (bool, optional) – If True, it will write the nodata value in the encoding and remove the fill value from the attributes. This is useful for masking with nodata. Default is False.
  • inplace (bool, optional) – If True, it will write to the existing DataArray. Default is False.
Returns:

Modified DataArray with CF compliant nodata information.

Return type:

xarray.DataArray

Examples

To write the nodata value if it is missing:

>>> raster.rio.write_nodata(-9999, inplace=True)

To write the nodata value on a copy:

>>> raster = raster.rio.write_nodata(-9999)

To mask with nodata:

>>> nodata = raster.rio.nodata
>>> raster = raster.where(raster != nodata)
>>> raster.rio.write_nodata(nodata, encoded=True, inplace=True)
class rioxarray.raster_dataset.RasterDataset(xarray_obj)[source]

Bases: rioxarray.rioxarray.XRasterBase

This is the GIS extension for xarray.Dataset

clip(geometries, crs=None, all_touched=False, drop=True, invert=False, from_disk=False)[source]

Crops a xarray.Dataset by geojson like geometry dicts in dimensions ‘x’/’y’.

Warning

Clips variables that have dimensions ‘x’/’y’. Others are appended as is.

Powered by rasterio.features.geometry_mask.

Examples

>>> geometry = ''' {"type": "Polygon",
...                 "coordinates": [
...                 [[-94.07955380199459, 41.69085871273774],
...                 [-94.06082436942204, 41.69103313774798],
...                 [-94.06063203899649, 41.67932439500822],
...                 [-94.07935807746362, 41.679150041277325],
...                 [-94.07955380199459, 41.69085871273774]]]}'''
>>> cropping_geometries = [geojson.loads(geometry)]
>>> xds = xarray.open_rasterio('cool_raster.tif')
>>> cropped = xds.rio.clip(geometries=cropping_geometries, crs=4326)

New in version 0.2: from_disk

Parameters:
  • geometries (list) – A list of geojson geometry dicts.
  • crs (rasterio.crs.CRS, optional) – The CRS of the input geometries. Default is to assume it is the same as the dataset.
  • all_touched (boolean, optional) – If True, all pixels touched by geometries will be burned in. If false, only pixels whose center is within the polygon or that are selected by Bresenham’s line algorithm will be burned in.
  • drop (bool, optional) – If True, drop the data outside of the extent of the mask geometries Otherwise, it will return the same raster with the data masked. Default is True.
  • invert (boolean, optional) – If False, pixels that do not overlap shapes will be set as nodata. Otherwise, pixels that overlap the shapes will be set as nodata. False by default.
  • from_disk (boolean, optional) – If True, it will clip from disk using rasterio.mask.mask if possible. This is beneficial when the size of the data is larger than memory. Default is False.
Returns:

The clipped object.

Return type:

xarray.Dataset

clip_box(minx, miny, maxx, maxy, auto_expand=False, auto_expand_limit=3)[source]

Clip the xarray.Dataset by a bounding box in dimensions ‘x’/’y’.

Warning

Clips variables that have dimensions ‘x’/’y’. Others are appended as is.

Parameters:
  • minx (float) – Minimum bound for x coordinate.
  • miny (float) – Minimum bound for y coordinate.
  • maxx (float) – Maximum bound for x coordinate.
  • maxy (float) – Maximum bound for y coordinate.
  • auto_expand (bool) – If True, it will expand clip search if only 1D raster found with clip.
  • auto_expand_limit (int) – maximum number of times the clip will be retried before raising an exception.
Returns:

The clipped object.

Return type:

Dataset

crs

Retrieve projection from xarray.Dataset

Type:rasterio.crs.CRS
interpolate_na(method='nearest')[source]

This method uses scipy.interpolate.griddata to interpolate missing data.

Warning

scipy is an optional dependency.

Warning

Interpolates variables that have dimensions ‘x’/’y’. Others are appended as is.

Parameters:method ({‘linear’, ‘nearest’, ‘cubic’}, optional) – The method to use for interpolation in scipy.interpolate.griddata.
Returns:The interpolated object.
Return type:xarray.DataArray
pad_box(minx, miny, maxx, maxy)[source]

Pad the xarray.Dataset to a bounding box.

Warning

Only works if all variables in the dataset have the same coordinates.

Warning

Pads variables that have dimensions ‘x’/’y’. Others are appended as is.

Parameters:
  • minx (float) – Minimum bound for x coordinate.
  • miny (float) – Minimum bound for y coordinate.
  • maxx (float) – Maximum bound for x coordinate.
  • maxy (float) – Maximum bound for y coordinate.
Returns:

The padded object.

Return type:

xarray.Dataset

reproject(dst_crs, resolution=None, shape=None, transform=None, resampling=<Resampling.nearest: 0>, nodata=None, **kwargs)[source]

Reproject xarray.Dataset objects

Note

Only 2D/3D arrays with dimensions ‘x’/’y’ are currently supported. Others are appended as is. Requires either a grid mapping variable with ‘spatial_ref’ or a ‘crs’ attribute to be set containing a valid CRS. If using a WKT (e.g. from spatiareference.org), make sure it is an OGC WKT.

New in version 0.0.27: shape

New in version 0.0.28: transform

New in version 0.5.0: nodata, kwargs

Parameters:
  • dst_crs (str) – OGC WKT string or Proj.4 string.
  • resolution (float or tuple(float, float), optional) – Size of a destination pixel in destination projection units (e.g. degrees or metres).
  • shape (tuple(int, int), optional) – Shape of the destination in pixels (dst_height, dst_width). Cannot be used together with resolution.
  • transform (optional) – The destination transform.
  • resampling (rasterio.enums.Resampling, optional) – See rasterio.warp.reproject() for more details.
  • nodata (float, optional) – The nodata value used to initialize the destination; it will remain in all areas not covered by the reprojected source. Defaults to the nodata value of the source image if none provided and exists or attempts to find an appropriate value by dtype.
  • **kwargs (dict) – Additional keyword arguments to pass into rasterio.warp.reproject(). To override: - src_transform: rio.write_transform - src_crs: rio.write_crs - src_nodata: rio.write_nodata
Returns:

The reprojected Dataset.

Return type:

xarray.Dataset

reproject_match(match_data_array, resampling=<Resampling.nearest: 0>)[source]

Reproject a Dataset object to match the resolution, projection, and region of another DataArray.

Note

Only 2D/3D arrays with dimensions ‘x’/’y’ are currently supported. Others are appended as is. Requires either a grid mapping variable with ‘spatial_ref’ or a ‘crs’ attribute to be set containing a valid CRS. If using a WKT (e.g. from spatiareference.org), make sure it is an OGC WKT.

Parameters:
Returns:

Contains the data from the src_data_array, reprojected to match match_data_array.

Return type:

xarray.Dataset

to_raster(raster_path, driver=None, dtype=None, tags=None, windowed=False, recalc_transform=True, lock=None, compute=True, **profile_kwargs)[source]

Export the Dataset to a raster file. Only works with 2D data.

..versionadded:: 0.2 lock

Parameters:
  • raster_path (str) – The path to output the raster to.
  • driver (str, optional) – The name of the GDAL/rasterio driver to use to export the raster. Default is “GTiff” if rasterio < 1.2 otherwise it will autodetect.
  • dtype (str, optional) – The data type to write the raster to. Default is the datasets dtype.
  • tags (dict, optional) – A dictionary of tags to write to the raster.
  • windowed (bool, optional) – If True, it will write using the windows of the output raster. This is useful for loading data in chunks when writing. Does not do anything when writing with dask. Default is False.
  • lock (boolean or Lock, optional) – Lock to use to write data using dask. If not supplied, it will use a single process for writing.
  • compute (bool, optional) – If True and data is a dask array, then compute and save the data immediately. If False, return a dask Delayed object. Call “.compute()” on the Delayed object to compute the result later. Call dask.compute(delayed1, delayed2) to save multiple delayed files at once. Default is True.
  • **profile_kwargs – Additional keyword arguments to pass into writing the raster. The nodata, transform, crs, count, width, and height attributes are ignored.
Returns:

If the data array is a dask array and compute is True. Otherwise None is returned.

Return type:

dask.Delayed

vars

Returns non-coordinate varibles

Type:list

rioxarray.exceptions module

This contains exceptions for rioxarray.

exception rioxarray.exceptions.DimensionError[source]

Bases: rioxarray.exceptions.RioXarrayError

This is raised when there are more dimensions than is supported by the method

exception rioxarray.exceptions.DimensionMissingCoordinateError[source]

Bases: rioxarray.exceptions.RioXarrayError

This is raised when the dimension does not have the supporting coordinate.

exception rioxarray.exceptions.InvalidDimensionOrder[source]

Bases: rioxarray.exceptions.DimensionError

This is raised when there the dimensions are not ordered correctly.

exception rioxarray.exceptions.MissingCRS[source]

Bases: rioxarray.exceptions.RioXarrayError

Missing the CRS in the dataset.

exception rioxarray.exceptions.MissingSpatialDimensionError[source]

Bases: rioxarray.exceptions.DimensionError

This is raised when the dimension cannot be found

exception rioxarray.exceptions.NoDataInBounds[source]

Bases: rioxarray.exceptions.RioXarrayError

This is for when there are no data in the bounds for clipping a raster.

exception rioxarray.exceptions.OneDimensionalRaster[source]

Bases: rioxarray.exceptions.DimensionError

This is an error when you have a 1 dimensional raster.

exception rioxarray.exceptions.RioXarrayError[source]

Bases: RuntimeError

This is the base exception for errors in the rioxarray extension.

exception rioxarray.exceptions.SingleVariableDataset[source]

Bases: rioxarray.exceptions.RioXarrayError

This is for when you have a dataset with a single variable.

exception rioxarray.exceptions.TooManyDimensions[source]

Bases: rioxarray.exceptions.DimensionError

This is raised when there are more dimensions than is supported by the method