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
587 stars 132 forks source link

Unnormalization fails when normalized las contains Zref header data #638

Closed mcoghill closed 1 year ago

mcoghill commented 1 year ago

In my workflow, I'm working with a large LAS catalog to segment trees. The process for doing this involves normalizing the LAS tiles, then segmenting the trees, and then unnormalizing the tiles at the end. Because of the steps involved, I have to specify in the normalize_height() function the add_lasattribute argument to be TRUE because if it's not and a LAS tile is saved, then I cannot unnormalize it later on. What I am finding is when the add_lasattribute argument is set to TRUE, and then the tile is unnormalized and then you try to write the tile to a new file, an error occurs due to extra bytes in the header. Here is a reproducible example:

library(lidR)

LASfile <- system.file("extdata", "Topography.laz", package="lidR")
las <- readLAS(LASfile)
nlas <- normalize_height(las, tin(), add_lasattribute = TRUE)
unlas <- unnormalize_height(nlas)
writeLAS(unlas, file.path(tempdir(), "unlas.las"))
# Error: Invalid file: the header describes extra bytes attributes that are not in the data.

My current workaround is to put the unnormalization step for my catalog into a custom catalog_apply function which manually removes the extra bytes from the header after the unnormalization. I am wondering if this behaviour could be modified in the unnormalize_height() function so that the header information is being properly updated to remove the extra bytes that are added due to the add_lasattribute = TRUE argument in the normalize_height() function?

Additionally, would it be possible to add documentation to the normalize_height() function such that it includes the add_lasattribute and Wdegenerated arguments? I noticed these get passed on as ... in the function itself, but they don't show up in the help file when called with ?normalize_height.

Many thanks for your help!

Jean-Romain commented 1 year ago

Fixed. unormalize_height() now cleans the VLRs. For the documentation there is a good reason why it is not documented. It seems I did it on purpose but I don't know why. I will consider documenting it back (it was documented in the past)