convert vectors into rasters where each pixel is a numerical representation of a feature in the vector data
from rasterio import features
from rasterio.plot import show
from rasterio.windows import Window, transform

generate_mask[source]

generate_mask(tiff_file, shape_file, output_file, labels_column, labels_dict:Dict[str, typing.Any], plot=False)

Generates a segmentation mask for one TIFF image. Arguments: tiff_file (str): Path to reference TIFF file || shape_file (str): Path to shapefile || output_file (str): Path to output file || labels_column (str): Feature in the shapefile that contains labels/categories || labels_dict (dict): Dictionary of desired labels and assigned values for the mask || Returns: image (np.array): A binary mask as a numpy array

Test data

Generating a raster mask

tiff_file = "../data/vector_to_raster_mask_sample/cabanglasan.tif"
shape_file = "../data/vector_to_raster_mask_sample/labels_20220816.gpkg"
target_file = shape_file.replace("gpkg", "tiff")

Given a raster image of a certain area that will be masked to use as a reference and a shape file that contains that area. Note that the shape file must include a column that contains labels/categories.

gpd.read_file(shape_file).head(3)
year label uid ADM3_EN ADM3_PCODE ADM2_EN ADM2_PCODE ADM1_EN ADM1_PCODE geometry
0 2017.0 mining 72_2017_mining Sofronio Española PH175324000 Palawan PH175300000 Region IV-B PH170000000 MULTIPOLYGON Z (((117.95961 9.03303 0.00000, 1...
1 2017.0 mining 71_2017_mining Sofronio Española PH175324000 Palawan PH175300000 Region IV-B PH170000000 MULTIPOLYGON Z (((117.95507 9.03809 0.00000, 1...
2 2017.0 mining 70_2017_mining Sofronio Española PH175324000 Palawan PH175300000 Region IV-B PH170000000 MULTIPOLYGON Z (((117.95663 9.03869 0.00000, 1...

And a dictionary with the labels and assigned values to be used in creating a mask

labels = {
    "mining": 1,
    "neg": 2,
    "agriculture": 3,
    "product_extraction": 4,
    "kaingin": 5,
    "biophysical": 6,
}

Input them in the generate_mask function to create a raster mask of the same dimension as the reference raster image

masks, grids, values = generate_mask(
    tiff_file=tiff_file,
    shape_file=shape_file,
    output_file=target_file,
    labels_column="label",
    labels_dict=labels,
    plot=True,
)
/tmp/ipykernel_263/3832611824.py:14: FutureWarning: Currently, index_parts defaults to True, but in the future, it will default to False to be consistent with Pandas. Use `index_parts=True` to keep the current behavior and True/False to silence the warning.
  gs = gdf.explode()
No description has been provided for this image
masks
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint16)
grids
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint16)
values
{'mining': 1,
 'neg': 2,
 'agriculture': 3,
 'product_extraction': 4,
 'kaingin': 5,
 'biophysical': 6}