# More examples might be avaible in the official lidR vignettes or
# on the github book
## =========================================================================
## Example 1: detect all the tree tops over an entire catalog
## (this is basically a reproduction of the existing function 'locate_trees')
## =========================================================================
# 1. Build the user-defined function that analyzes each chunk of the catalog.
# The function's first argument is a LAScluster object. The other arguments can be freely
# chosen by the users.
my_tree_detection_method <- function(chunk, ws)
{
# The chunk argument is a LAScluster object. The users do not need to know how it works.
# readLAS will load the region of interest (chunk) with a buffer around it, taking advantage of
# point cloud indexation if possible. The filter and select options are propagated automatically
las <- readLAS(chunk)
if (is.empty(las)) return(NULL)
# Find the tree tops using a user-developed method
# (here simply a LMF for the example).
ttops <- locate_trees(las, lmf(ws))
# ttops is an sf object that contains the tree tops in our region of interest
# plus the trees tops in the buffered area. We need to remove the buffer otherwise we will get
# some trees more than once.
bbox <- st_bbox(chunk)
ttops <- sf::st_crop(ttops, bbox)
return(ttops)
}
# 2. Build a collection of file
# (here, a single file LAScatalog for the purposes of this simple example).
LASfile <- system.file("extdata", "MixedConifer.laz", package="lidR")
ctg <- readLAScatalog(LASfile)
plot(ctg)
# 3. Set some processing options.
# For this small single file example, the chunk size is 100 m + 10 m of buffer
opt_chunk_buffer(ctg) <- 10
opt_chunk_size(ctg) <- 100 # Small because this is a dummy example.
opt_chunk_alignment(ctg) <- c(-50, -35) # Align such as it creates 2 chunks only.
opt_select(ctg) <- "xyz" # Read only the coordinates.
opt_filter(ctg) <- "-keep_first" # Read only first returns.
# 4. Apply a user-defined function to take advantage of the internal engine
opt <- list(need_buffer = TRUE, # catalog_apply will throw an error if buffer = 0
automerge = TRUE) # catalog_apply will merge the outputs into a single object
output <- catalog_apply(ctg, my_tree_detection_method, ws = 5, .options = opt)
plot(output)
# \donttest{
## =========================================================================
## Example 1: simplified. There is nothing that requires special data
## manipulation in the previous example. Everything can be handled automatically
##=========================================================================
# 1. Build the user-defined function that analyzes a point cloud.
my_tree_detection_method <- function(las, ws)
{
# Find the tree tops using a user-developed method
# (here simply a LMF for the example).
ttops <- locate_trees(las, lmf(ws))
return(ttops)
}
# 2. Build a project
LASfile <- system.file("extdata", "MixedConifer.laz", package="lidR")
ctg <- readLAScatalog(LASfile)
plot(ctg)
# 3. Set some processing options.
# For this dummy example, the chunk size is 100 m and the buffer is 10 m
opt_chunk_buffer(ctg) <- 10
opt_chunk_size(ctg) <- 100 # small because this is a dummy example.
opt_chunk_alignment(ctg) <- c(-50, -35) # Align such as it creates 2 chunks only.
opt_select(ctg) <- "xyz" # Read only the coordinates.
opt_filter(ctg) <- "-keep_first" # Read only first returns.
# 4. Apply a user-defined function to take advantage of the internal engine
opt <- list(need_buffer = TRUE) # catalog_apply will throw an error if buffer = 0
output <- catalog_map(ctg, my_tree_detection_method, ws = 5, .options = opt)
## ===================================================
## Example 2: compute a rumple index on surface points
## ===================================================
rumple_index_surface = function(las, res)
{
las <- filter_surfacepoints(las, 1)
rumple <- pixel_metrics(las, ~rumple_index(X,Y,Z), res)
return(rumple)
}
LASfile <- system.file("extdata", "Megaplot.laz", package="lidR")
ctg <- readLAScatalog(LASfile)
opt_chunk_buffer(ctg) <- 1
opt_chunk_size(ctg) <- 140 # small because this is a dummy example.
opt_select(ctg) <- "xyz" # read only the coordinates.
opt <- list(raster_alignment = 20) # catalog_apply will adjust the chunks if required
output <- catalog_map(ctg, rumple_index_surface, res = 20, .options = opt)
plot(output, col = height.colors(25))
# }
Run the code above in your browser using DataLab