I have to transform the list of polygons that is in vector form into a raster mask that should be geographically aligned with the NDVI image that I have. I was advised to use
rasterio.features.rasterize. But I am not sure how to use the function.
shapes : iterable of (geometry, value) pairs or iterable over geometries. `geometry` can either be an object that implements the geo interface or GeoJSON-like object.
A list, tuple, or generator of shapely geometries (or other GeoJSON-y objects as described, but shapely is easy). If you want them to be burnt in with the same
default_value, then this is all you need in the list. If you have a specific value to burn for each geometry, then your list needs to pair the value with the geometry. e.g.
[(geom0, 10), (geom1, 20), (geom2, 33)]. Sounds like you want a boolean raster, so just supply the geometries here (no pairs).
out_shape : tuple or list with 2 integers Shape of output numpy ndarray.
This should be the shape of the numpy array to be created. If
source_raster is the handle returned by
rasterio.open(), then the shape will be
(source_raster.height, source_raster.width) (row-major since it's a numpy array).
fill : int or float, optional Used as fill value for all areas not covered by input geometries.
Whatever background/nodata value you want. Sounds like you either want 1 or 0.
out : numpy ndarray, optional Array of same shape and data type as `source` in which to store results.
You don't need this unless you want to store the results in an existing array to save memory
transform : Affine transformation object, optional Transformation from pixel coordinates of `source` to the coordinate system of the input `shapes`. See the `transform` property of dataset objects.
This should be
all_touched : boolean, optional If True, all pixels touched by geometries will be burned in. If false, only pixels whose center is within the polygon or that are selected by Bresenham's line algorithm will be burned in.
merge_alg : MergeAlg, optional Merge algorithm to use. One of: MergeAlg.replace (default): the new value will overwrite the existing value. MergeAlg.add: the new value will be added to the existing raster.
As described, for a mask you'll want the default
default_value : int or float, optional Used as value for all geometries, if not provided in `shapes`.
If you have a single value to burn in, supply it here. Otherwise you'll have to have specified this within
shapes. Since you want a boolean raster, use 1 or 0.
dtype : rasterio or numpy data type, optional Used as data type for results, if `out` is not provided.
I think this should default to something appropriate based on the burn value(s). If not, use the smallest numpy dtype appropriate for what you've supplied. gdal doesn't like bitmasks, though, so for a boolean raster use
See the official examples here: https://rasterio.readthedocs.io/en/stable/topics/features.html