crop#

crop(xds: typing.Union[str, xarray.core.dataarray.DataArray, xarray.core.dataset.Dataset, rasterio.io.DatasetReader], shapes: typing.Union[geopandas.geodataframe.GeoDataFrame, shapely.geometry.polygon.Polygon, list], nodata: typing.Optional[int] = None, **kwargs) -> (<class 'numpy.ma.core.MaskedArray'>, <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 rasterio.open(raster_path) as dst:
>>>     xds2 = crop(dst, shapes)
>>> xds1 == xds2
True
Parameters
  • 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

Returns

Cropped array as a xarray

Return type

XDS_TYPE