crop

Contents

crop#

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

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

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

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 doesn’t exist, set to 0.

  • **kwargs – Other rioxarray.clip options

Returns:

Cropped array as a xarray

Return type:

AnyXrDataStructure

Examples

>>> 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 ds:
>>>     xds2 = crop(ds, shapes)
>>>
>>> # Assert those two approaches give the same result
>>> xds1 == xds2
True