paint(xds: Union[str, xarray.core.dataarray.DataArray, xarray.core.dataset.Dataset,], shapes: Union[geopandas.geodataframe.GeoDataFrame, shapely.geometry.polygon.Polygon, list], value: int, invert: bool = False, **kwargs) Union[xarray.core.dataset.Dataset, xarray.core.dataarray.DataArray][source]#

Painting a dataset: setting values inside the given shapes. To set outside the shape, set invert=True. Pay attention that this behavior is the opposite of the rasterio.mask function.

The original nodata is kept. This means if your shapes intersects the original nodata, the value of the pixel will be set to nodata rather than to the wanted value.

Overload of rasterio mask function in order to create a xarray. The mask function docs can be seen here.

>>> raster_path = "path/to/raster.tif"
>>> shape_path = "path/to/shapes.geojson"  # Any vector that geopandas can read
>>> shapes = gpd.read_file(shape_path)
>>> paint1 = paint(raster_path, shapes, value=100)
>>> # or
>>> with as dst:
>>>     paint2 = paint(dst, shapes, value=100)
>>> paint1 == paint2
  • 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)

  • value (int) – Value to set on the shapes.

  • invert (bool) – If invert is True, set value outside the shapes.

  • **kwargs – Other rasterio.mask options


Painted array as a xarray

Return type