Solved: How to "merge" lidr classes back into las file?

97 Views Asked by At

I have an airborne lidar las file that I am reading using readLAS where I only keep a certain class (drop class = 6). I then perform a ground classification on these subset of points. I would like to "merge" or "replace" the old ground points with the newly classified ground points such that the new file would contain the newly classified points and the class 6 points from my original las file.

I cant figure out how to do this with the lidR package. Can someone provide some insight?

Here is my code to read only certain classes and write out the subset to a new las file:

# read in buildings and ground + all other classes as 2 separate objects
in_las = "C:/temp/in_tst.las" 
las_buildings <- readLAS(in_las, filter = "-keep_class 6")
las_no_buildig <- readLAS(in_las, filter = "-drop_class 6")

# run CSF classification
mycsf <- csf(sloop_smooth = FALSE, class_threshold = 0.25, cloth_resolution = 1, time_step = 0.65, rigidness = 1) 
las <- classify_ground(las_no_buildig, mycsf)

# write out las file
out_las = "C:/temp/out_tst.las" 
writeLAS(las_tst , out_las, index = FALSE)

I tried this thinking the objects could be appended but of course, the object gets overwritten

las_tst <- las_buildings
las_tst <- las_ground

I am not sure if I am approaching this correctly or if there is a better way to achieve this.

Edit with solution and code I used: I realized adding new data to a las file is not a trivial task and since the las file format is quite strict to prevent errors, I ended up taking the building class from the original las file and replacing the building class for points in the newly classified points.

# read in all points and buildings only as 2 separate objects
in_las = "C:/temp/in_tst.las" 
las_all <- readLAS(in_las)
las_buildings <- readLAS(in_las, filter = "-keep_class 6")

# run CSF classification
mycsf <- csf(sloop_smooth = TRUE, class_threshold = 0.25, cloth_resolution = 1, time_step = 0.65, rigidness = 1) 
las_classified <- classify_ground(las_all, mycsf)

# get index of old points
ind_grd_orig = las_all@data$Classification==2
ind_buildings_orig = las_all@data$Classification==6

# merge orig ground points with new classification
las_classified@data[ind_grd_orig]$Classification = 2

# merge orig building points to fix buildings classed incorrectly by lidR
las_classified@data[ind_buildings_orig]$Classification = 6

# write out las file
out_las = "C:/temp/out_tst.las" 
writeLAS(las_classified, out_las, index = FALSE)
0

There are 0 best solutions below