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
582 stars 130 forks source link

Unexpected behaviour classify_roi() #732

Open zoeschindler opened 9 months ago

zoeschindler commented 9 months ago

Hello, I have an issue with classify_roi(). I want to classify points within some polygons (derived from buffered lines) as a different class than the remaining points. But when I read my data in using readTLSLAS() instead of readLAS(), I get weird results. I made some dummy data: https://drive.google.com/file/d/1OOl4_vRYCIRSmx-Ey7cflgZv_s-y9l07/view?usp=sharing

Here is the code:

# load packages
library(lidR)
library(sf)

# read pointcloud
las <- readTLSLAS("test.las")
crs(las) <- "EPSG:32632"

# load shapes
shp_lines <- st_read("test.shp")
shp_polys <- st_buffer(shp_lines, 0.025)

# show pointcloud & shapes
plot(las)
plot(shp_polys)

# classify using lines
las$Classification <- LASUNCLASSIFIED
las <- classify_poi(las, class = LASGROUND, roi = shp_polys)

# show results
plot(las, color = "Classification")
plot(filter_poi(las, Classification == LASGROUND))

Polygons: grafik

Result when using readLAS() (expected result): grafik

Result when using readTLSLAS() (unexpected result): grafik

Jean-Romain commented 9 months ago

Thank you for the nice and easily reproducible example. I confirm the bug.

Jean-Romain commented 9 months ago

Minimal Reproducible example

las <- readLAS("issue 732/test.las")
st_crs(las) <- "EPSG:32632"
shp_lines <- st_read("~/Téléchargements/lidR issues/issue 732/test.shp")
shp_polys <- st_buffer(shp_lines, 0.025)
sfc <- sf::st_geometry(shp_polys)
sum(lidR:::point_in_polygons(las, sfc), na.rm = T)
#> TLS: 101111
#> ALS: 469881

By the way @zoeschindler do not use crs(). Use st_crs(). crs() will be removed in next version to avoid problem on CRAN with old geospatial package (raster, sp)

Jean-Romain commented 9 months ago

Fixed with a temporary fix. My octree has a problem, so I disabled the octree and used another spatial indexing structure by default. I keep the issue open because I must repair the octree.