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
601 stars 131 forks source link

Chained functions bug with las catalog and piping? #649

Closed flottsam closed 1 year ago

flottsam commented 1 year ago

I'm trying to understand why this chain of functions would write two files instead of a single file as expected.

# read clipped files (24 total)
(ctg <- readLAScatalog(here("plot_clips")))

opt_output_files(ctg) <- here("plot_clips_voxelized/{ORIGINALFILENAME}_5cm")
opt_independent_files <- TRUE
library(future)
# specify number of cores for parallel proc. 
plan(multisession, workers = 12L)

ctg %>%
  classify_ground(csf(rigidness=3L)) %>% 
  normalize_height(knnidw()) %>% 
  filter_poi(Z >= 0) %>% 
  voxelize_points(res=0.05)

plan(sequential)

The expected output is: plot_A_1_5cm.las But I'm getting two files: plot_A_1_5cm.las plot_A_1_5cm_5cm.las

Jean-Romain commented 1 year ago

lidR does not include smart pipe that optimally combine commands. What you wrote actually is equivalent to:

opt_output_files(ctg) <- here("plot_clips_voxelized/{ORIGINALFILENAME}_5cm")
opt_independent_files <- TRUE
library(future)
plan(multisession, workers = 12L)

# creates plot_clips_voxelized/plot_A_1_5cm
gnd = classify_ground(ctg, csf(rigidness=3L))

# creates plot_clips_voxelized/plot_A_1_5cm_5cm
# because the file template has been inherited from `ctg`
nor = normalize_height(gnd,  knnidw())

# `nor` being a LAScatalog this should not work 
# because filter_poi() does not apply to a LAScatalog
pos = filter_poi(nor, Z >= 0) 
vox = voxelize_points(pos, res=0.05)
flottsam commented 1 year ago

thnk you