- mask(ds: ~typing.Union[str, tuple, ~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]#
Masking a dataset: setting nodata outside of the given shapes, but without cropping the raster to the shapes extent.
Overload of rasterio mask function in order to create a masked_array.
maskfunction 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.
>>> raster_path = "path/to/raster.tif" >>> shape_path = "path/to/shapes.geojson" # Any vector that geopandas can read >>> shapes = gpd.read_file(shape_path) >>> masked_raster1, meta1 = mask(raster_path, shapes) >>> # or >>> with rasterio.open(raster_path) as ds: >>> masked_raster2, meta2 = mask(ds, shapes) >>> masked_raster1 == masked_raster2 True >>> meta1 == meta2 True
ds (PATH_ARR_DS) – Path to the raster, its dataset, its
xarrayor a tuple containing its array and metadata
shapes (Union[gpd.GeoDataFrame, Polygon, list]) – Shapes with the same CRS as the dataset (except if a
GeoDataFrameis 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
Masked array as a masked array and its metadata
- Return type: