paint#
- paint(xds: str | CloudPath | Path | tuple[ndarray | MaskedArray, dict] | DataArray | Dataset | DatasetReader | DatasetWriter, shapes: GeoDataFrame | Polygon | list, value: int, invert: bool = False, **kwargs) DataArray | Dataset [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
. Themask
function docs can be seen here.- Parameters:
xds (AnyRasterType) – 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
- Returns:
Painted array as a xarray
- Return type:
AnyXrDataStructure
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) >>> paint1 = paint(raster_path, shapes, value=100) >>> >>> # or >>> with rasterio.open(raster_path) as ds: >>> paint2 = paint(ds, shapes, value=100) >>> >>> # Assert those two approaches give the same result >>> paint1 == paint2 True