...
Code Block |
---|
library(ForestForesight)
# Make sure you have DATArun ff_FOLDERenvironment inpreviously, config.yml
setif tonot where you savehave your data, and make sure that the config.yml file is in your working directory
to assign the ff_folder yourself
ff_folder <- Sys.getenv("DATA_FOLDER")
template_folder <- list.files(file.path(ff_folder, "preprocessed", "input"),
pattern = "^[0-9]{2}[NS]_[0-9]{3}[EW]$", full.names = TRUE)[1]
template_raster <- rast(list.files(template_folder,
pattern = "\\.tif$", full.names = TRUE)[1]) |
...
This is useful when your input raster is in a different coordinate reference system (CRS) than your template or has a different resolution or extent.
Below we give different example scripts on how to preprocess your own dataset. Please make sure you enter your own datasets when you load your dataset using rast or vect
Example: Reprojecting a land cover raster
Code Block |
---|
|
# Load a land cover raster in a different CRS
land_cover <- rast("landcover_wgs84path/to/your/own/dataset.tif") #this is an example! Enter your own dataset
# Check CRS
print(crs(land_cover))
print(crs(template_raster))
# Reproject using nearest neighbor (best for categorical data)
land_cover_nearest <- project(land_cover, template_raster, method = "near")
# Reproject using cubic (often better for continuous data)
land_cover_cubic <- project(land_cover, template_raster, method = "cubic")
# Compare results
par(mfrow = c(1, 2))
plot(land_cover_nearest, main = "Nearest Neighbor")
plot(land_cover_cubic, main = "Cubic") |
...
Code Block |
---|
|
# Load a multi-band Landsat image
landsat <- rast("landsat_image.tif") #this is an example! Enter your own dataset
# Check number of layers
nlyr(landsat)
# Select the Near-Infrared band (usually band 5 in Landsat 8)
nir_band <- landsat[[5]]
# Standardize to template
nir_standardized <- project(nir_band, template_raster, method = "cubic")
# Plot
plot(nir_standardized, main = "Near-Infrared Band") |
...
Code Block |
---|
|
# Load a land cover raster
lulc <- rast("land_use_land_cover.tif") #this is an example! Enter your own dataset
# Create a reclassification matrix
# Let's say we want to simplify to Forest (1), Agriculture (2), and Other (3)
rcl_matrix <- matrix(c(
1, 5, 1, # Classes 1-5 become Forest (1)
6, 10, 2, # Classes 6-10 become Agriculture (2)
11, 20, 3 # Classes 11-20 become Other (3)
), ncol = 3, byrow = TRUE)
# Reclassify
lulc_reclass <- classify(lulc, rcl_matrix)
# Standardize to template
lulc_standardized <- project(lulc_reclass, template_raster, method = "near")
# Plot
plot(lulc_standardized, main = "Reclassified Land Cover") |
...
Code Block |
---|
|
# Load forest cover polygons
forest_polygons <- vect("forest_cover.shp") #this is an example! Enter your own dataset
# Rasterize (1 for forest, 0 for non-forest)
forest_raster <- rasterize(forest_polygons, template_raster, field = 1, background = 0)
# Plot
plot(forest_raster, main = "Forest Cover Mask") |
...
Code Block |
---|
|
# Load administrative boundaries
admin_boundaries <- vect("admin_boundaries.shp") #this is an example! Enter your own dataset
# Assuming there's a 'population' column in the vector data
population_raster <- rasterize(admin_boundaries, template_raster, field = "population", background = 0)
# Plot
plot(population_raster, main = "Population Density") |
...
Code Block |
---|
|
# Load road network
roads <- vect("road_network.shp") #this is an example! Enter your own dataset
# Create distance raster (in meters)
distance_to_roads <- distance(template_raster, roads)
# Convert to kilometers
distance_to_roads_km <- distance_to_roads / 1000
# Plot
plot(distance_to_roads_km, main = "Distance to Roads (km)") |
...