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

segment_trees throws out a crs error #627

Closed alekszaf closed 1 year ago

alekszaf commented 1 year ago

When using segment_trees with any of the methods the function throws out and error:

Error in st_crs.character(terra::crs(.x)) : invalid crs:

The error started appearing after update of lidR to version 4.0.2, previously there were no issues with segmentation. I tested the code with different point clouds but I keep getting the same error.



# Load the normalized LAS file
las = readLAS("ALS_2017_plot_normZ_fltr.las", filter = "-drop_z_below 0")

#Create Canopy Height Model
#chm <- rasterize_canopy(las, res = 1, algorithm = p2r())
chm <- rasterize_canopy(las, res = 0.5, pitfree(max_edge = c(0, 2.5)))

# Plot the canopy height model
col <- height.colors(25)
plot(chm, col = col)

#Detect the tree tops
ttops <- locate_trees(chm, lmf(ws = 1, hmin = 5))

#Plot the tree tops
plot(chm, col = height.colors(50))
plot(sf::st_geometry(ttops), add = TRUE, pch = 3)

# Segmentation

las <- segment_trees(las, silva2016(chm, ttops))
[ALS_2017_plot_normZ_fltr.zip](https://github.com/r-lidar/lidR/files/9631838/ALS_2017_plot_normZ_fltr.zip)
alekszaf commented 1 year ago

The point cloud coordinate system was not explicitly written into the LAS header. Code now works after assigning correct CRS with st_crs().