mask
mask#
- mask(xds: Union[str, xarray.core.dataarray.DataArray, xarray.core.dataset.Dataset, rasterio.io.DatasetReader], shapes: Union[geopandas.geodataframe.GeoDataFrame, shapely.geometry.polygon.Polygon, list], nodata: Optional[int] = None, **kwargs) Union[xarray.core.dataset.Dataset, xarray.core.dataarray.DataArray] [source]#
Masking a dataset: setting nodata outside of the given shapes, but without cropping the raster to the shapes extent.
The original nodata is kept and completed with the nodata provided by the shapes.
Overload of rasterio mask function in order to create a
xarray
.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.>>> raster_path = "path/to/raster.tif" >>> shape_path = "path/to/shapes.geojson" # Any vector that geopandas can read >>> shapes = gpd.read_file(shape_path) >>> mask1 = mask(raster_path, shapes) >>> # or >>> with rasterio.open(raster_path) as dst: >>> mask2 = mask(dst, shapes) >>> mask1 == mask2 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 rasterio.mask options
- Returns
Masked array as a xarray
- Return type
XDS_TYPE