crop(xds: ~typing.Union[str, ~xarray.core.dataarray.DataArray, ~xarray.core.dataset.Dataset,], shapes: ~typing.Union[~geopandas.geodataframe.GeoDataFrame, ~shapely.geometry.polygon.Polygon, list], nodata: ~typing.Optional[int] = None, **kwargs) -> (<class ''>, <class 'dict'>)[source]#

Cropping a dataset: setting nodata outside of the given shapes AND cropping the raster to the shapes extent.

Overload of rioxarray.clip function in order to create a masked_array.

>>> raster_path = "path/to/raster.tif"
>>> shape_path = "path/to/shapes.geojson"  # Any vector that geopandas can read
>>> shapes = gpd.read_file(shape_path)
>>> xds2 = crop(raster_path, shapes)
>>> # or
>>> with as ds:
>>>     xds2 = crop(ds, shapes)
>>> xds1 == xds2
  • xds (PATH_XARR_DS) – Path to the raster or a rasterio dataset or a xarray

  • shapes (Union[gpd.GeoDataFrame, Polygon, list]) – Shapes with the same CRS as the dataset (except if a GeoDataFrame is passed, in which case it will automatically be converted)

  • nodata (int) – Nodata value. If not set, uses the ds.nodata. If doesnt exist, set to 0.

  • **kwargs – Other rioxarray.clip options


Cropped array as a xarray

Return type: