Skip to content

Spanner LAS Catalog Example

Andrew Sánchez Meador edited this page Oct 7, 2024 · 1 revision

Spanner LAS Catalog Example

This example demonstrates how to process a LAS catalog using lidR, spanner, and the future package for parallel processing in R.

Import Libraries

library(lidR)
library(spanner)
library(future)
library(sf)
library(dplyr)

Setup for Parallel Processing

The following sets the number of threads for lidR and the number of workers for parallel processing.

# Set the number of threads to use in lidR
set_lidr_threads(2)

# Set the number of workers for parallel processing
plan(multisession, workers = 3)

Read and Configure LAS Catalog

The catalog is read and configured with chunk size and buffer size options for processing.

# Read the catalog
LASfile = system.file("extdata", "DensePatchA.laz", package="spanner")
catalog <- readLAScatalog(LASfile)

# Set catalog options
opt_chunk_size(catalog) <- 10
opt_chunk_buffer(catalog) <- 5

Process Each Chunk in the Catalog

This function processes each chunk of the LAS catalog by classifying ground, normalizing points, classifying and removing noise, and finally extracting tree locations.

results <- process_spanner_ctg <- function(chunk) {
    las <- readLAS(chunk)
    if (is.empty(las)) return(NULL)

    # Classify ground
    las <- classify_ground(las, csf())

    # Normalize points
    las <- normalize_height(las, tin())

    # Classify and remove noise
    las <- classify_noise(las, ivf(0.25, 3))
    las <- filter_poi(las, Classification != LASNOISE)
    if (is.empty(las)) return(NULL)

    # Call get_raster_eigen_treelocs on the chunk
    spanner_treeslocs <- get_raster_eigen_treelocs(
      las = las,
      res = 0.05,
      min_density = 0.1
    )
    return(spanner_treeslocs)
}

Run the Processing Over the Catalog

Use the following command to process all the chunks in the catalog using the previously defined function.

output <- catalog_apply(catalog, process_spanner_ctg)

Example Output

The result of catalog_apply will be a list of tree locations derived from each chunk of the LAS catalog.