4

When executing grid_metrics for a normalized LAScatalog the final results are GTiff files where each band corresponds to a metric. The problem is that the band names are "band 01", "band 02", "band 03"... and I can not identify which band is which metric. Is there a way to keep the name of the metric as the band name? Here is the part of the script that I use:

ctg <- readLAScatalog("C:/JAIME/dataLaS_test/", progress = TRUE)

# Load an extra buffer around the processed file to properly process the catalog without edge artifacts
opt_chunk_buffer(ctg) <- 10

# Write files to disc
opt_output_files(ctg) <- "C:/JAIME/00_tests/test2/{ORIGINALFILENAME}_normalized"

#Normalize LAS files
las <- lasnormalize(ctg, tin())

opt_filter(las) <- "-drop_z_below 0"

#export metrics
opt_output_files(las) <- "C:/JAIME/00_tests/test2/{ORIGINALFILENAME}_metrics"

#Create functionn to get metrics
get_metrics <- grid_metrics(las, .stdmetrics_z, 15.8114, filter = ~ReturnNumber == 1)

1 Answer 1

5

The problem you are describing is more or less an unexpected behavior. First raster::writeRaster does not preserve the names of the layers when writing in a GTiff format (see this question). So anyway you must change the default format (see this question).

Then, once all the rasters are computed and written on disk, lidR uses gdalUtils::gdalbuildvrt to return a single object instead of a vector of written filenames. But at this step the layer names are lost too because gdalbuildvrt does not preserve the names.

So this is the workarounds you could use

Rename manually

attrnames = c("zmax", "zmean", "zsd", "zskew", "zkurt", "zentropy", "pzabovezmean", "pzabove2", "zq5", "zq10", "zq15", "zq20", "zq25", "zq30", "zq35", "zq40", "zq45", "zq50", "zq55", "zq60", "zq65", "zq70", "zq75", "zq80", "zq85", "zq90", "zq95", "zpcum1", "zpcum2", "zpcum3", "zpcum4", "zpcum5", "zpcum6", "zpcum7", "zpcum8", "zpcum9")
names(get_metrics) <- attrnames

Change the format and retrieve the names

# Change the default format
ctg@output_options$drivers$Raster$param$format <- "raster"
ctg@output_options$drivers$Raster$extension <- ".grd"

# Compute
get_metrics <- grid_metrics(las, .stdmetrics_z, 15)

# Use the first file to read the names
first <- brick("C:/JAIME/00_tests/test2/firstfilename_metrics.grd")

# Rename
names(get_metrics) <- names(first)

Also please note that the following:

opt_filter(las) <- "-drop_z_below 0 -keep_first"
get_metrics <- grid_metrics(las, .stdmetrics_z, 15.8114)

is equivalent but better than:

opt_filter(las) <- "-drop_z_below 0"
get_metrics <- grid_metrics(las, .stdmetrics_z, 15.8114, filter = ~ReturnNumber == 1)
1
  • Thank you! Renaming it manually worked like a charm by just inserting the scrypt at the end after "get_metrics" command. The other approach I could not make it work as I am pretty new in R and do not know how to fit it in my code Jan 10, 2020 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.