Vector to Raster mask

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

source

generate_mask

 generate_mask (tiff_file, shape_file, output_file, labels_column,
                labels_dict:Dict[str,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

# Get filepaths
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, 117.959...
1 2017.0 mining 71_2017_mining Sofronio Española PH175324000 Palawan PH175300000 Region IV-B PH170000000 MULTIPOLYGON Z (((117.95507 9.03809 0, 117.955...
2 2017.0 mining 70_2017_mining Sofronio Española PH175324000 Palawan PH175300000 Region IV-B PH170000000 MULTIPOLYGON Z (((117.95663 9.03869 0, 117.956...

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

# Generate masks for a file
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,
)

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}