r-lidar / lidR

Airborne LiDAR data manipulation and visualisation for forestry application
https://CRAN.R-project.org/package=lidR
GNU General Public License v3.0
612 stars 133 forks source link

pixel_metrics cannot use a SpatRaster as a template #731

Open candelas762 opened 1 year ago

candelas762 commented 1 year ago

To be able to use a raster as a template given the argument res, this raster can not be in a SpatRaster format. One sollution is simple and is to convert it to a RasterLayer with raster::raster().

This is a reproducible example to use the maximum height raster as a template for minimum heigh calculation:

library(lidR)

LASfile <- system.file("extdata", "Megaplot.laz", package="lidR")
ctg <- readLAScatalog(LASfile, select = "xyz", chunk_size = 140, chunk_buffer = 0)
opt_chunk_alignment(ctg) <- c(0,20)
plot(ctg, chunk = TRUE)

library(future)
plan(multisession, workers = 2)

m = pixel_metrics(ctg, ~max(Z), 20)
# Works
pixel_metrics(ctg, ~min(Z), m)
# Does not work
pixel_metrics(ctg, ~miin(Z), raster(m))
# Works