Skip to content

Spanner PatchMorph Raster Processing Example

Andrew Sánchez Meador edited this page Oct 9, 2024 · 5 revisions

Spanner PatchMorph Raster Processing Example

This script demonstrates the process of manipulating and visualizing rasters using spanner library's patch morphology functionalities. The example includes setting up suitability levels, gap and spur distances, processing rasters, and visualizing the results.

Import Library

library(spanner)

Set the Input Raster Path

The input raster is set using the system.file function to point to an example raster within the spanner package.

# Set the path to the input raster and read it as a terra::rast
r <- system.file("extdata", "FtValleyClipCHM.tif", package="spanner")
r <- terra::rast(r)

image

Define Parameters

Set the suitability levels, gap distances, and spur distances to define the conditions for patch morphology analysis.

# Define the suitability levels, gap distances, and spur distances
suitList <- c(-Inf, 2, Inf)
gapList <- seq(1, 10, by = 2)
spurList <- seq(1, 10, by = 2)

Explanation of Parameters:

  • suitList: Suitability levels defining thresholds for raster processing.
  • gapList: Range of gap distances in the raster.
  • spurList: Range of spur distances.

Process the Rasters

This function processes the rasters based on the patchmorph parameters defined above.

# Process the rasters based on the patchmorph parameters
rasters <- process_rasters_patchmorph(r, suitList, gapList, spurList)

Visualize Processed Rasters

You can plot a specific raster from the result using its name, as shown below.

# Plot the raster named "suit_2_gap_2_spur_10"
plot_raster_by_name(rasters, "suit_2_gap_2_spur_10")

image

Summing Rasters by Suitability

This function generates a list of summed rasters based on suitability levels.

# Get a list of summed rasters
rSumList <- sum_rasters_by_suitability(rasters, suitList)

Plot Summed Rasters

You can plot a specific summed raster by its name.

# Plot the raster named "suit_2_sum"
plot_raster_by_name(rSumList, "suit_2_sum")

image