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

Filter options in readLAScatalog are superseded by opt_filter(). #675

Closed rs806 closed 1 year ago

rs806 commented 1 year ago

Can the filter in readLAScatalog be made to be persistent? For example, I apply a filter when reading in a catalog, and then I am lazy and don't repeat that filter after adding a new one with opt_filter(), and then I lose the first filter.

library(sf)
library(lidR)

smallCat <- readLAScatalog(system.file("extdata", "example.laz", package = "rlas"),
  filter = "-drop_z_below 975")
plotDF <- data.frame(place = "plot_1", x = 339008, y = 5248001)
sf_SAMPLE <- st_as_sf(x = plotDF, coords = c("x", "y"), crs = st_crs(smallCat))

plotRadius <- 11.35

myMetrics = function(z)
{
  res = list(
    zmin = min(z),
    zmax = max(z),
    zmean = mean(z)
  )
  return(res)
}

# -------------------------------------------------------------------------------------------------
# problem happens here.
# This line will supersede filter set in readLAScatalog.
# i.e., if it is not run, minimum value is 975.182. If it is run, minimum value is 973.145  * edited. I had thse values backwards
# -------------------------------------------------------------------------------------------------
opt_filter(smallCat) <- "-drop_withheld"
# -------------------------------------------------------------------------------------------------

plotTreeVolAndType <- plot_metrics(smallCat,func = ~myMetrics(Z), sf_SAMPLE, radius = plotRadius)
print(plotTreeVolAndType$zmin)
print(plotTreeVolAndType$zmax)
Jean-Romain commented 1 year ago

In summary you want a new function opt_add_filter(). I'm I correct ?

rs806 commented 1 year ago

Initially I thought it would be better to always maintain the filter set up when running readLAScatalog(). In my actual project I drop withheld right away knowing that I'll never want to include those. However I can see your suggestion of opt_add_filter() will provide flexibility in other cases. But now that I'm thinking about this, maybe it would just be better to warn the user that the previous filters will be lost when using opt_filter(). This would remind me to always explicitly choose filter options. I see either of those options. Thanks.

Jean-Romain commented 1 year ago

For backward compatibility I cannot change the current behavior anyway. I can only add a new convenient function

rs806 commented 1 year ago

I see now from the LAS catalog vignette that readLAS() behavior is different in that it does discard filtered hits. So I needed to be more careful with readLAScatalog(). For that reason it's probably better for me to just state all the filters I want each time I use opt_filter(). My feeling is that adding a new function opt_add_filter() wouldn't be necessary. Thanks for taking the time to look at this!