R lidR – Retrieve File Names with catalog_apply and lascatalog

lidrr

I'm using catalog_apply with a las catalog to compute some simple cloud metrics, which returns a list.

The following code, for example, will process 45 point clouds and return a list of 45 items. But how do I relate the values in the list to their respective plot IDs? Is it possible to keep track of the file name with catalog apply?

# read catalog
ctg <- readLAScatalog(here())

# define function for catalog apply
my_func <- function(chunk)
{
  las <- readLAS(chunk)                
  if (is.empty(las)) return(NULL)
  metrics <- cloud_metrics(las, ~max(Z)) # calculate max Z
  return(metrics)
}

# set options --------------------------
plan(strategy = multisession, workers = 12)
opt_independent_files(ctg) <- TRUE

# run function 
(max_z <- catalog_apply(ctg, my_func))

Best Answer

First, maybe you need the function plot_metrics() (new addition in lidR 3.2.0 released yesterday).

Second, if plot_metrics() does not do the job you can do

res <- catalog_apply(ctg, cloud_metrics, func = ~max(Z))
res <- data.table::rbindlist(res)

Last, even in parallel, order is preserved. So the file names are the ones from the LAScatalog.

res$filenames <- ctg$filenames
Related Question