grid_metrics
is designed in a way that your expression is evaluated within the frame of the data.table
that contains the point cloud. Your function is found within the loaded R packages. Thus your code cannot work for two reasons:
f
is definedefined within the frame ofget_layer_counts
breaks
does not exists within the frame of the point cloud
This version works:
# Main package function
get_layer_counts <- function(las, res = 10)
{
stats <- lidR::grid_metrics(las, func = layer_count_metrics(Z, breaks = seq(0,20,2)), res = res)
lidR:::as.raster.lasmetrics(stats)
}
# Metrics function to count points within height intervals
layer_count_metrics <- function(Z, breaks)
{
zcat <- findInterval(Z, breaks, left.open = TRUE)
tcount = as.list(table(factor(zcat, levels = 1:length(breaks))))
names(tcount) <- paste0("layer", 1:length(breaks))
tcount
}
library(lidR)
LASfile <- system.file("extdata", "Megaplot.laz", package="lidR")
las = readLAS(LASfile)
stats = get_layer_counts(las)
You can notice one important drawback. You can't propagate the argument breaks
through the get_layer_count
function because it looks for breaks
within the frame of the point cloud. When explicitly passed there is no name to look for and thus it just works. I hope to improve that in future releases.
Also notice that I added factor
before table
. Indeed the function must returns always the same number of metrics for each pixel. Otherwise the pixel aggregation fails.