1

I'm using LAScatalog with an area based approach for tree detection. Ultimately, I want to merge the trees and tree height information into a single data frame for subsequent analysis. But I did not find a convenient way to relate trees and tree height values to the plots that they belong to (e.g., treeID 1 has Z value of 10, but which plot does it belong to?).

Is there a simple way to add .las/.laz filename (or some intuitive ID) as attribute in the shapefile output?

My current solution is as follows:

library(lidR)
library(sf)
library(tidyverse)

# read catalog
ctg <- readLAScatalog("folder/")

# set las catalog options 
opt_independent_files(ctg) <- TRUE
opt_output_files(ctg) <- "output/trees/plot_{ID}"

# run tree detection (outputs shapefiles)
ttops <- find_trees(ctg, lmf(ws = 5))

# create a list of shapefiles
file_list <- list.files(here("output/trees/"), pattern = "*shp", full.names = TRUE)
# read in the list of shapefiles 
shapefile_list <- lapply(file_list, read_sf)
# convert shapefile list to single data frame
trees <- 
  shapefile_list %>% 
  lapply(st_drop_geometry) %>% # converts to dataframe
  bind_rows(.id="plot")

1 Answer 1

1

I'm not sure to understand what you are looking for. To my understanding you want to retrieve the plot (i.e. the file) where the trees were found. I'd go for something like that:

opt_output_files(ctg) <- here("output/trees/{ORIGINALFILENAME}")

Then you can extract the name from the filename:

read_and_add_name <- function(path) {
  name <- tools::file_path_sans_ext(basename((path))
  trees <- read_sf(path)
  trees$PLOTNAME <- name
  return(trees)
}
  
file_list <- list.files(here("output/trees/"), pattern = "*shp", full.names = TRUE)
shapefile_list <- lapply(file_list, read_and_add_name)

#[...]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.