3

I am just wondering whether lasfilter() is able to filter the las data with more than two criteria such as z value between 5 and 10 m and Number of point records greater than 1. I have tried the following code but it didn't work. I don't know how to filter the datasets with multiple criteria. There are 1173 files in my folder but when I used the following code it could only write 67 files because after 67th file there is another file which doesn't have point fitting with this criteria. So I want to write all those files which do meet the above criteria.

plotlist16 <- list.files(path = "./data/LidarData/2016_random_plot/AllTogether/normalized", pattern = ".las", full.names = T, recursive = FALSE)

for (i in 1:length(plotlist16))
{
   Rplot <- readLAS(plotlist16[i])
   ht_1.3_5m <- lasfilter(Rplot, Z>=1.3 & Z<5) # with this could only write 67 files 
   ht_1.3_5m <- lasfilter(Rplot, Z>=1.3, Z<5, Rplot@header@PHB$Number of point records>=1) # so want to filter with third criteria which has at least one point in this height range

   file_name <- gsub('.*/','',plotlist16[i])
   file_name <- gsub('.las','',file_name)
   file_name <- print(file_name)

   outname <- paste0('./data/LidarData/2016_random_plot/AllTogether/stratawise/strata_1.3_5m/', file_name, "_ht_1.3_5m.las")
   writeLAS(ht_1.3_5m, outname)
}
1

Many options here:

Use LAStools

lidR is good for some tasks but weaker for some other. This is a typical case where las2las from LAStools is more suitable for what you want to achieve.

Use streaming filter

If you want to work within R anyway, do not load the full point cloud in R. Read only the points of interest. Your filter is very simple here.

Rplot <- readLAS("file.las", filter = "-drop_z_below 1.5 -drop_z_above 5")

This way you don't need a full copy of your files. You only read what matters for you

Use LAScatalog capabilities

If you want a copy of the files anyway, use the LAScatalog capabilities. Don't use custom for loop

plotlist16 = readLAScatalog("./data/LidarData/2016_random_plot/AllTogether/normalized")
opt_filter(plotlist16) <- "-drop_z_below 1.5 -drop_z_above 5"
opt_chunk_buffer(plotlist16) <- 0
opt_chunk_size(plotlist16) <- 0
opt_output_files(plotlist16) <- "/new/folder/{ORIGINALFILENAME}_ht_1.3_5m"
plotlist16_ht <- catalog_retile(plotlist16)

Then you can easily select the files with the number of points you want.

plotlist16_ht <- plotlist16_ht[plotlist16_ht$Number.of.point.records > 1, ]

Notice that files with 0 points will be deleted automatically so you won't have files with 0 points

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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