R – Using Shapefile Attribute as Filename in lidR LAScatalog clip_roi

lidrr

I am trying to use the LAScatalog engine and clip out several thousand point clouds using a shapefile. I have already run this successfully but I specified my output names using {XCENTER}_{YCENTER} which as I have come to find out isn't the best option for me. What I want is something very similar to the documentation where {_LAKENAME1} was used, in my case this will be {shotnumber} as per my file. Unfortunately, it does not recognize this name within the file and thus cannot use it as a naming convention.

enter image description here

enter image description here

Best Answer

What you are reporting is simply a little bug. When plotting with chunk = TRUE lidR tries to build the filenames but in this context it cannot. I opened an issue with a reproducible example here.

However clipping works as expected:

library(lidR)

LASfile <- system.file("extdata", "Megaplot.laz", package="lidR")
ctg = readLAScatalog(LASfile)

# Generate random spatial points
x <- runif(5, ctg$Min.X,  ctg$Max.X)
y <- runif(5, ctg$Min.Y,  ctg$Max.Y)
shotnumber <- paste0(letters[1:5], letters[10:14])
loc <- data.frame(x,y,shotnumber)
loc <- sf::st_as_sf(loc, coords = c("x", "y"))

opt_output_files(ctg) <- "clip_{shotnumber}"
loc_ctg = clip_roi(ctg, loc, radius = 10)

plot(ctg)
plot(sf::st_geometry(loc), add = T, pch = 19 )
plot(loc_ctg, add = T)

loc_ctg$filename
#> [1] "clip_aj.las" "clip_bk.las" "clip_cl.las"  "clip_dm.las"
#> [5] "clip_en.las"

Created on 2022-02-22 by the reprex package (v2.0.1)

Related Question