from rasterio import features
from rasterio.plot import show
from rasterio.windows import Window, transform
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)
And a dictionary with your desired labels and assigned values 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,
)
masks
grids
values
Choose raster images to convert into a dataframe and use the generated raster mask to add the labels the converted dataframe.
tiff_files = ["../data/vector_to_raster_mask_sample/cabanglasan.tif"]
mask_file = "../data/vector_to_raster_mask_sample/labels_20220816.tiff"
data = read_bands(tiff_files, mask_file)
data