rioxarray package
rioxarray.open_rasterio
- rioxarray.open_rasterio(filename: str | PathLike | DatasetReader | WarpedVRT | SingleBandDatasetReader, *, parse_coordinates: bool | None = None, chunks: int | tuple | dict | None = None, cache: bool | None = None, lock: Any | None = None, masked: bool = False, mask_and_scale: bool = False, variable: str | list[str] | tuple[str, ...] | None = None, group: str | list[str] | tuple[str, ...] | None = None, default_name: str | None = None, decode_times: bool = True, decode_timedelta: bool | None = None, band_as_variable: bool = False, **open_kwargs) Dataset | DataArray | list[Dataset] [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 and refer to the center of the pixel.
Added in version 0.13: band_as_variable
- 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 setparse_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 toTrue
or"auto"
to choose sensible chunk sizes according todask.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, default=False) – 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, default=True) – 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.
band_as_variable (bool, default=False) – If True, will load bands in a raster to separate variables.
**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: Sequence[DataArray], *, bounds: tuple | None = None, res: tuple | None = None, nodata: float | None = None, precision: float | None = None, method: str | Callable | None = None, crs: CRS | None = None, parse_coordinates: bool = True) DataArray [source]
Merge data arrays geospatially.
Added in version 0.2: crs
- Parameters:
dataarrays (list[xarray.DataArray]) – List of multiple xarray.DataArray 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.merge.merge()
for details.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:
- rioxarray.merge.merge_datasets(datasets: Sequence[Dataset], *, bounds: tuple | None = None, res: tuple | None = None, nodata: float | None = None, precision: float | None = None, method: str | Callable | None = None, crs: CRS | None = None) Dataset [source]
Merge datasets geospatially.
Added in version 0.2: crs
- Parameters:
datasets (list[xarray.Dataset]) – List of multiple xarray.Dataset 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:
rioxarray.set_options
- class rioxarray.set_options(**kwargs)[source]
Set the global rioxarray option.
Added in version 0.3.0.
Added 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 thecrs_wkt
andspatial_ref
attributes will be written and the program will be faster due to not needing to usepyproj.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 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: DataArray | Dataset)[source]
Bases:
object
This is the base class for the GIS extensions for xarray
- bounds(*, recalc: bool = False) tuple[float, float, float, float] [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
- property count: int
Returns the band count (z dimension size)
- Type:
int
- property crs: CRS | None
rasterio.crs.CRS
: Retrieve projection fromxarray.Dataset
|xarray.DataArray
- estimate_utm_crs(datum_name: str = 'WGS 84') CRS [source]
Returns the estimated UTM CRS based on the bounds of the dataset.
Added 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.
- Return type:
- get_gcps() list[GroundControlPoint] | None [source]
Get the GroundControlPoints from the dataset.
https://rasterio.readthedocs.io/en/latest/topics/georeferencing.html#ground-control-points
- Returns:
The Ground Control Points from the dataset or None if not applicable
- Return type:
list of
rasterio.control.GroundControlPoint
or None
- property grid_mapping: str
The CF grid_mapping attribute. ‘spatial_ref’ is the default.
- Type:
str
- property height: int
Returns the height of the dataset (y dimension size)
- Type:
int
- isel_window(window: Window, *, pad: bool = False) Dataset | DataArray [source]
Use a rasterio.windows.Window to select a subset of the data.
Added in version 0.6.0: pad
Warning
Float indices are converted to integers.
- Parameters:
window (
rasterio.windows.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:
- resolution(recalc: bool = False) tuple[float, float] [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: dict, inplace: bool = False) Dataset | DataArray [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:
- set_crs(input_crs: Any, inplace: bool = True) Dataset | DataArray [source]
Set the CRS value for the Dataset/DataArray without modifying the dataset/data array.
Deprecated since version 0.15.8: It is recommended to use rio.write_crs() instead. This
method will likely be removed in a future release.
- 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:
- set_encoding(new_encoding: dict, inplace: bool = False) Dataset | DataArray [source]
Set the encoding of the dataset/dataarray and reset rioxarray properties to re-search for them.
Added 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:
- set_spatial_dims(x_dim: str, y_dim: str, inplace: bool = True) Dataset | DataArray [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:
- property shape: tuple[int, int]
Returns the shape (height, width)
- Type:
tuple(int, int)
- slice_xy(minx: float, miny: float, maxx: float, maxy: float) Dataset | DataArray [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:
- transform(recalc: bool = False) Affine [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.Affine
- transform_bounds(dst_crs: Any, *, densify_pts: int = 21, recalc: bool = False) tuple[float, float, float, float] [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: dict, inplace: bool = False) Dataset | DataArray [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:
- update_encoding(new_encoding: dict, inplace: bool = False) Dataset | DataArray [source]
Update the encoding of the dataset/dataarray and reset rioxarray properties to re-search for them.
Added 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:
- property width: int
Returns the width of the dataset (x dimension size)
- Type:
int
- write_coordinate_system(inplace: bool = False) Dataset | DataArray [source]
Write the coordinate system CF metadata.
Added 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:
- write_crs(input_crs: Any | None = None, grid_mapping_name: str | None = None, inplace: bool = False) Dataset | DataArray [source]
Write the CRS to the dataset in a CF compliant manner.
Warning
The grid_mapping attribute is written to the encoding.
- Parameters:
input_crs (Any) – 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:
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_gcps(gcps: Iterable[GroundControlPoint], gcp_crs: Any, *, grid_mapping_name: str | None = None, inplace: bool = False) Dataset | DataArray [source]
Write the GroundControlPoints to the dataset.
https://rasterio.readthedocs.io/en/latest/topics/georeferencing.html#ground-control-points
- Parameters:
gcp (list of
rasterio.control.GroundControlPoint
) – The Ground Control Points to integrate to the dataset.gcp_crs (str,
rasterio.crs.CRS
, or dict) – Coordinate reference system for the GCPs.grid_mapping_name (str, optional) – Name of the grid_mapping coordinate to store the GCPs 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 Ground Control Points written.
- Return type:
- write_grid_mapping(grid_mapping_name: str = 'spatial_ref', inplace: bool = False) Dataset | DataArray [source]
Write the CF grid_mapping attribute to the encoding.
- 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:
- write_transform(transform: Affine | None = None, grid_mapping_name: str | None = None, inplace: bool = False) Dataset | DataArray [source]
Added 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:
- property x_dim: Hashable
The dimension for the X-axis.
- Type:
Hashable
- property y_dim: Hashable
The dimension for the Y-axis.
- Type:
Hashable
- class rioxarray.raster_array.RasterArray(xarray_obj: DataArray)[source]
Bases:
XRasterBase
This is the GIS extension for
xarray.DataArray
- clip(geometries: Iterable, crs: Any | None = None, *, all_touched: bool = False, drop: bool = True, invert: bool = False, from_disk: bool = False) DataArray [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)
Added in version 0.2: from_disk
- Parameters:
geometries (Iterable) – A list of geojson geometry dicts or objects with __geo_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 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:
- clip_box(minx: float, miny: float, maxx: float, maxy: float, *, auto_expand: bool | int = False, auto_expand_limit: int = 3, crs: Any | None = None, allow_one_dimensional_raster: bool = False) DataArray [source]
Clip the
xarray.DataArray
by a bounding box.Added in version 0.12: crs
Added in version 0.16: allow_one_dimensional_raster
- 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 (Union[bool, int]) – 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.
crs (
rasterio.crs.CRS
, optional) – The CRS of the bounding box. Default is to assume it is the same as the dataset.allow_one_dimensional_raster (bool, optional) – If True, allow clipping to/from a one dimensional raster.
- Returns:
The clipped object.
- Return type:
- property encoded_nodata: float | None
Return the encoded nodata value for the dataset if encoded.
- interpolate_na(method: Literal['linear', 'nearest', 'cubic'] = 'nearest') DataArray [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:
- property nodata: float | None
Get the nodata value for the dataset.
- pad_box(minx: float, miny: float, maxx: float, maxy: float, *, constant_values: float | tuple[int, int] | Mapping[Any, tuple[int, int]] | None = None) DataArray [source]
Pad the
xarray.DataArray
to a bounding boxAdded 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, tuple or mapping of hashable to tuple) – The value used for padding. If None, nodata will be used if it is set, and numpy.nan otherwise.
- Returns:
The padded object.
- Return type:
- pad_xy(minx: float, miny: float, maxx: float, maxy: float, *, constant_values: float | tuple[int, int] | Mapping[Any, tuple[int, int]] | None = None) DataArray [source]
Pad the array to x,y bounds.
Added 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, tuple or mapping of hashable to tuple) – The value used for padding. If None, nodata will be used if it is set, and numpy.nan otherwise.
- Returns:
The padded object.
- Return type:
- reproject(dst_crs: Any, *, resolution: float | tuple[float, float] | None = None, shape: tuple[int, int] | None = None, transform: Affine | None = None, resampling: Resampling = Resampling.nearest, nodata: float | None = None, **kwargs) DataArray [source]
Reproject
xarray.DataArray
objectsPowered 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.
Note
To re-project with dask, see odc-geo & pyresample.
Added in version 0.0.27: shape
Added in version 0.0.28: transform
Added 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:
- reproject_match(match_data_array: DataArray | Dataset, *, resampling: Resampling = Resampling.nearest, **reproject_kwargs) DataArray [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.
Added in version 0.9: reproject_kwargs
- Parameters:
match_data_array (
xarray.DataArray
|xarray.Dataset
) – DataArray of the target resolution and projection.resampling (rasterio.enums.Resampling, optional) – See
rasterio.warp.reproject()
for more details.**reproject_kwargs – Other options to pass to
rioxarray.raster_array.RasterArray.reproject()
- Returns:
Contains the data from the src_data_array, reprojected to match match_data_array.
- Return type:
- set_nodata(input_nodata: float | None, *, inplace: bool = True) DataArray [source]
Set the nodata value for the DataArray without modifying the data array.
- Parameters:
input_nodata (Optional[float]) – Valid nodata for dtype.
inplace (bool, optional) – If True, it will write to the existing dataset. Default is True.
- Returns:
Dataset with nodata attribute set.
- Return type:
- to_raster(raster_path: str | PathLike, *, driver: str | None = None, dtype: str | dtype | None = None, tags: dict[str, str] | None = None, windowed: bool = False, recalc_transform: bool = True, lock: bool | None = None, compute: bool = True, **profile_kwargs) None [source]
Export the DataArray to a raster file.
..versionadded:: 0.2 lock
- Parameters:
raster_path (Union[str, os.PathLike]) – 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.
recalc_transform (bool, optional) – If False, it will write the raster with the cached transform from the dataarray rather than recalculating it. Default is True.
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: float | None, *, encoded: bool = False, inplace=False) DataArray [source]
Write the nodata to the DataArray in a CF compliant manner.
- Parameters:
input_nodata (Optional[float]) – 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:
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: DataArray | Dataset)[source]
Bases:
XRasterBase
This is the GIS extension for
xarray.Dataset
- clip(geometries: Iterable, crs: Any | None = None, *, all_touched: bool = False, drop: bool = True, invert: bool = False, from_disk: bool = False) Dataset [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)
Added 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:
- clip_box(minx: float, miny: float, maxx: float, maxy: float, *, auto_expand: bool | int = False, auto_expand_limit: int = 3, crs: Any | None = None, allow_one_dimensional_raster: bool = False) Dataset [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.
Added in version 0.12: crs
Added in version 0.16: allow_one_dimensional_raster
- 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.
crs (
rasterio.crs.CRS
, optional) – The CRS of the bounding box. Default is to assume it is the same as the dataset.allow_one_dimensional_raster (bool, optional) – If True, allow clipping to/from a one dimensional raster.
- Returns:
The clipped object.
- Return type:
Dataset
- property crs: CRS | None
rasterio.crs.CRS
: Retrieve projection from xarray.Dataset
- interpolate_na(method: Literal['linear', 'nearest', 'cubic'] = 'nearest') Dataset [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:
- pad_box(minx: float, miny: float, maxx: float, maxy: float, *, constant_values: float | tuple[int, int] | Mapping[Any, tuple[int, int]] | None = None) Dataset [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.
constant_values (scalar, tuple or mapping of hashable to tuple) – The value used for padding. If None, nodata will be used if it is set, and numpy.nan otherwise.
- Returns:
The padded object.
- Return type:
- reproject(dst_crs: Any, *, resolution: float | tuple[float, float] | None = None, shape: tuple[int, int] | None = None, transform: Affine | None = None, resampling: Resampling = Resampling.nearest, nodata: float | None = None, **kwargs) Dataset [source]
Reproject
xarray.Dataset
objectsNote
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.
Note
To re-project with dask, see odc-geo & pyresample.
Added in version 0.0.27: shape
Added in version 0.0.28: transform
Added 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 Dataset.
- Return type:
- reproject_match(match_data_array: DataArray | Dataset, *, resampling: Resampling = Resampling.nearest, **reproject_kwargs) Dataset [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.
Added in version 0.9: reproject_kwargs
- Parameters:
match_data_array (
xarray.DataArray
|xarray.Dataset
) – Dataset with the target resolution and projection.resampling (rasterio.enums.Resampling, optional) – See
rasterio.warp.reproject()
for more details.**reproject_kwargs – Other options to pass to
rioxarray.raster_dataset.RasterDataset.reproject()
- Returns:
Contains the data from the src_data_array, reprojected to match match_data_array.
- Return type:
- to_raster(raster_path: str | PathLike, *, driver: str | None = None, dtype: str | dtype | None = None, tags: dict[str, str] | None = None, windowed: bool = False, recalc_transform: bool = True, lock: bool | None = None, compute: bool = True, **profile_kwargs) None [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.
recalc_transform (bool, optional) – If False, it will write the raster with the cached transform from the dataset rather than recalculating it. Default is True.
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
- property vars: list
Returns non-coordinate varibles
- Type:
list
rioxarray.exceptions module
This contains exceptions for rioxarray.
- exception rioxarray.exceptions.DimensionError[source]
Bases:
RioXarrayError
This is raised when there are more dimensions than is supported by the method
- exception rioxarray.exceptions.DimensionMissingCoordinateError[source]
Bases:
RioXarrayError
This is raised when the dimension does not have the supporting coordinate.
- exception rioxarray.exceptions.InvalidDimensionOrder[source]
Bases:
DimensionError
This is raised when there the dimensions are not ordered correctly.
- exception rioxarray.exceptions.MissingCRS[source]
Bases:
RioXarrayError
Missing the CRS in the dataset.
- exception rioxarray.exceptions.MissingSpatialDimensionError[source]
Bases:
DimensionError
This is raised when the dimension cannot be found
- exception rioxarray.exceptions.NoDataInBounds[source]
Bases:
RioXarrayError
This is for when there are no data in the bounds for clipping a raster.
- exception rioxarray.exceptions.OneDimensionalRaster[source]
Bases:
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:
RioXarrayError
This is for when you have a dataset with a single variable.
- exception rioxarray.exceptions.TooManyDimensions[source]
Bases:
DimensionError
This is raised when there are more dimensions than is supported by the method