from rasterio import features
from rasterio.plot import show
from rasterio.windows import Window, transform
Vector to Raster mask
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
= "../data/vector_to_raster_mask_sample/cabanglasan.tif"
tiff_file = "../data/vector_to_raster_mask_sample/labels_20220816.gpkg"
shape_file = shape_file.replace("gpkg", "tiff") target_file
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.
3) gpd.read_file(shape_file).head(
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
= generate_mask(
masks, grids, values =tiff_file,
tiff_file=shape_file,
shape_file=target_file,
output_file="label",
labels_column=labels,
labels_dict=True,
plot )
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}