First some background from the original question:
I'm using the lidR package to make rasters of metrics calculated on point clouds. I have a custom function that calculates 9 metrics outputed as a named list and want to produce one raster per metric.
Outputed rasters should keep the filename of the LAS file but get the name of the metric attached (pasted) to the name (e.g. LASfilename_metric.tif) by using the bylayer and suffix arguments of the writeRaster
function in the raster
package.
I also want to use a slightly customized driver function when writing the raster-files to make sure output rasters had the right CRS. These "adjustments" are introduced by modifying the catalog output_options
(write and param) as per inspiration from https://github.com/r-lidar/lidR/wiki/Modify-the-LAScatalog-drivers.
This have all worked fine using the "old" grid_metrics()
function in the lidR package but has stopped working when updating the lidR package (where pixel_metrics
have replaced grid_metrics
).
An example that reproduce the problem:
library("lidR")
library("raster")
LASfile <- system.file("extdata", "Megaplot.laz", package="lidR")
myMetrics <- function(z){
list(max=max(z), mean=mean(z))
}
Raster_MS <- function(x, filename, crs, ...){
suppressWarnings(crs(x) <- paste0('EPSG:', crs))
suppressWarnings(raster::writeRaster(x, filename, ...))
}
ctg<-readLAScatalog(LASfile)
opt_chunk_buffer(ctg) <- 0
ctg@output_options$drivers$Raster$write <- Raster_MS
ctg@output_options$drivers$Raster$param$bylayer <- T
ctg@output_options$drivers$Raster$param$suffix <- quote(names(x))
ctg@output_options$drivers$Raster$param$crs <- 26917
opt_output_files(ctg) <- paste0("path/to/output/folder/", '{ORIGINALFILENAME}')
metricsrast1 <- grid_metrics(ctg, ~myMetrics(Z), res = 10)
What happens is that I get the following error:
Processing [===========================================================================================================] 100% (1/1) eta: 0s
Error: formal argument "sources" matched by multiple actual arguments
I have seen this related question where the solution was to write multilayer rasters but I would really prefer to have several (2 in my reproducible example) single layer rasters produced (and named accordingly, i.e. Megaplot_max.tif
and Megaplot_mean.tif
).