crop(xds: Union[str, xarray.core.dataarray.DataArray, xarray.core.dataset.Dataset,], shapes: Union[geopandas.geodataframe.GeoDataFrame, shapely.geometry.polygon.Polygon, list], nodata: 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 dst:
>>>     xds2 = crop(dst, 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