Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

The main reason to want to use the ForestForesight package is to create your own datasets and use them to improve the model. Below we give instructions on how to do this.

First, let's set up our environment:

library(terra)

ff_folder <- "/path/to/ff_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])

Reprojecting a raster dataset:

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.

Example: Reprojecting a land cover raster

# Load a land cover raster in a different CRS
land_cover <- rast("landcover_wgs84.tif")

# 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")

Selecting a single layer from a multi-layer raster:

This is common when working with satellite imagery or time series data.

Example: Selecting a single band from a Landsat image

# Load a multi-band Landsat image
landsat <- rast("landsat_image.tif")

# 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")

Reclassifying categorical data:

This is useful for simplifying land cover classes or creating binary masks.

Example: Reclassifying a land cover raster

# Load a land cover raster
lulc <- rast("land_use_land_cover.tif")

# 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")

Rasterizing a vector dataset (presence/absence):

This is useful for creating binary masks from vector data.

Example: Creating a forest/non-forest mask from polygon data

# Load forest cover polygons
forest_polygons <- vect("forest_cover.shp")

# 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")

Rasterizing a vector dataset with a specific attribute:

This is useful when you want to preserve numerical or categorical information from the vector data.

Example: Rasterizing administrative boundaries with population data

# Load administrative boundaries
admin_boundaries <- vect("admin_boundaries.shp")

# 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")

Creating a distance raster from vector data:

This is useful for proximity analysis, such as distance to roads or water bodies.

Example: Calculating distance to roads

# Load road network
roads <- vect("road_network.shp")

# 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)")

Writing the new dataset to disk

# Function to generate the output filename
generate_output_filename <- function(template_path, availability_date, feature_name) {
  # Extract the base parts of the template filename
  template_basename <- basename(template_path)
  parts <- strsplit(template_basename, "_")[[1]]
  
  # Construct the new filename
  new_filename <- paste(parts[1], parts[2], availability_date, feature_name, sep="_")
  
  # Ensure the filename ends with .tif
  if (!endsWith(new_filename, ".tif")) {
    new_filename <- paste0(new_filename, ".tif")
  }
  
  return(new_filename)
}

# Get user input for availability date and feature name
availability_date <- readline(prompt = "Enter the availability date (YYYY-MM-DD): ")
feature_name <- readline(prompt = "Enter the name of your new feature: ")

# Generate the output filename
output_filename <- generate_output_filename(template_raster, availability_date, feature_name)

# Construct the full output path
output_path <- file.path(dirname(template_raster), output_filename)

# Write the result to a file
writeRaster(final_raster, output_path)

cat("Raster saved as:", output_path, "\n")

This script does the following:

  1. Defines a function generate_output_filename that takes the template path, availability date, and feature name as inputs.

  2. Extracts the base parts of the template filename (the tile identifier, e.g., "00N_010E").

  3. Constructs a new filename using the tile identifier, user-specified availability date, and feature name.

  4. Ensures the filename ends with ".tif".

  5. Prompts the user to input the availability date and feature name.

  6. Generates the output filename using the function.

  7. Constructs the full output path in the same directory as the template raster.

  8. Writes the raster to the generated filename.

  9. Prints the path where the raster was saved.

Example usage:

If the template file is "00N_010E_2021-01-01_forestfire.tif", and the user inputs:

  • Availability date: 2023-06-30

  • Feature name: vegetation_density

The output file would be named: "00N_010E_2023-06-30_vegetation_density.tif"

This approach ensures that the output filename maintains the structure of the template filename while allowing for user-specified dates and feature names. It also places the new file in the same directory as the template, maintaining the file organization.

  • No labels