crop#
- crop(ds: str | ~cloudpathlib.cloudpath.CloudPath | ~pathlib.Path | ~typing.Tuple[~numpy.ndarray | ~numpy.ma.MaskedArray, dict] | ~xarray.core.dataarray.DataArray | ~xarray.core.dataset.Dataset | ~rasterio.io.DatasetReader | ~rasterio.io.DatasetWriter, shapes: ~geopandas.geodataframe.GeoDataFrame | ~shapely.geometry.polygon.Polygon | list, nodata: int | None = None, **kwargs) -> (<class 'numpy.ma.MaskedArray'>, <class 'dict'>)[source]#
Cropping a dataset: setting nodata outside of the given shapes AND cropping the raster to the shapes extent.
HOW:
Overload of rasterio mask function in order to create a masked_array.
The
mask
function docs can be seen here. It basically masks a raster with a vector mask, with the possibility to crop the raster to the vector’s extent.- Parameters:
ds (AnyRasterType) – Path to the raster, its dataset, its
xarray
or a tuple containing its array and metadatashapes (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 rasterio.mask options
- Returns:
Cropped array as a masked array and its metadata
- Return type:
(np.ma.masked_array, dict)
Example
>>> raster_path = "path/to/raster.tif" >>> shape_path = "path/to/shapes.geojson" # Any vector that geopandas can read >>> shapes = gpd.read_file(shape_path) >>> cropped_raster1, meta1 = crop(raster_path, shapes) >>> # or >>> with rasterio.open(raster_path) as ds: >>> cropped_raster2, meta2 = crop(ds, shapes) >>> >>> # Assert those two approaches give the same result >>> cropped_raster1 == cropped_raster2 True >>> meta1 == meta2 True