ff_polygonize: creating actionable data from predictions
ff_polygonize is used to create focused, discrete polygons from the prediction raster that comes out of ff_predict or ff_run. It creates vector data that can be written as a Shapefile, GeoJSON or other spatial data format.
Exercise
# Load required libraries
library(ForestForesight)
# Let's assume we have downloaded predictions for Suriname into our forestforesight_data folder
prediction_file <- "forestforesight_data/predictions/SUR/SUR_2024-01-01_predictions.tif"
# Basic usage with automatic thresholding for "high" risk areas
high_risk_areas <- ff_polygonize(
input_raster = prediction_file,
threshold = "high", # Automatically determine threshold for high-risk areas
verbose = TRUE # Show the chosen threshold value
)
# Save the polygons to a shapefile
ff_polygonize(
input_raster = prediction_file,
output_file = "high_risk_areas.shp",
threshold = "high",
minimum_pixel_count = 10, # Slightly larger minimum area
smoothness = 3 # Increased smoothing
)
# Create a series of risk levels to compare
risk_levels <- c("medium", "high", "very high")
risk_polygons <- lapply(risk_levels, function(level) {
ff_polygonize(
input_raster = prediction_file,
threshold = level,
verbose = TRUE
)
})
names(risk_polygons) <- risk_levels
Key points about ff_polygonize:
Automatic Thresholding:
"medium" - Uses a balanced threshold suitable for general risk areas
"high" - More selective, focusing on higher probability areas
"very high" - Most selective, only highest probability areas
Important Parameters:
minimum_pixel_count
: Controls minimum size of risk areas (default=5)window_size
: Controls initial smoothing (default=7)smoothness
: Controls polygon smoothing (default=2)
Output Attributes:
risk
: Average risk value within the polygonsize
: Area in hectaresriskfactor
: risk × sizethreshold
: The threshold value useddate
: Generation date
Tips:
Start with
threshold = "high"
for a balanced resultIncrease
minimum_pixel_count
to remove small polygonsAdjust
smoothness
higher for smoother boundariesUse
verbose = TRUE
to see the automatically chosen threshold