I have a big point cloud (3.5 GB) and want to create a big DTM from it using R. In the "old" lidR
version this was possible by turning on automerge = TRUE
. If the tiles could not be merged, a virtual raster was returned. If they could, a raster object was returned. I might be wrong, because I am new to the terra
package, but this did not work for me anymore. I tried making my DTM with the following code:
# load data
ctg <- readTLSLAScatalog(ctg_path)
# set options
opt_output_files(ctg) <- paste0(dtm_path, "/tile_{ID}")
opt_chunk_size(ctg) <- 20
opt_chunk_buffer(ctg) <- 5
opt_merge(ctg) <- TRUE
# my function
make_dtm <- function(chunk){
las <- readLAS(chunk)
if (is.empty(las)) return(NULL)
las <- classify_ground(las, csf(class_threshold = 0.3, cloth_resolution = 0.1))
dtm <- rasterize_terrain(las, res = 0.01, algorithm = tin())
dtm <- crop(dtm, ext(chunk)) # remove buffer
return(dtm)
}
# execution
dtm <- catalog_apply(ctg, make_dtm)
writeRaster(dtm, file.path(dtm_path, "final.tif"), overwrite = T)
But the return is always a list with the filenames. I guess I am just not up-to-date anymore on lidR
, but how can I use catalog_apply()
to obtain a SpatRaster
which I can save as a single tif?
Edit: The new working solution:
# load data
ctg <- readTLSLAScatalog(ctg_path, chunk_size = 20, chunk_buffer = 5)
# my functions
make_dtm <- function(chunk){
las <- readLAS(chunk)
if (is.empty(las)) return(NULL)
las <- classify_ground(las, csf(class_threshold = 0.3, cloth_resolution = 0.3))
dtm <- rasterize_terrain(las, res = 0.01, algorithm = tin())
dtm <- crop(dtm, ext(chunk)) # remove buffer
return(dtm)
}
make_dtm_ctg <- function(las) {
options <- list(
need_buffer = TRUE, # buffer necessary
automerge = TRUE) # combine outputs
output <- catalog_apply(las, make_dtm, .options = options)
return(output)
}
# execution
dtm <- make_dtm_ctg(ctg)
writeRaster(dtm, file.path(dtm_path, "DTM.tif"))