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

crown_metrics: scan() expected 'a real', got 'IllegalArgumentException: #701

Closed taeyoonlee87 closed 1 year ago

taeyoonlee87 commented 1 year ago

I confirmed that the error occurred regardless of number of las files in las catalog. This error was confirmed. But I am not sure whether the latest version of lidR fixed it. I don't know how I can download version 4.0.4 so, I used version 4.0.3.

Here is my code.

las_cat<-readLAScatalog("D:/Lidar_2022/R/segment tree/missing_silva/folder1")

chm<-rasterize_canopy(las_cat, 0.5, pitfree(c(0,2,5,10,15), c(3,1.5), subcircle = 0.2))

ttops <- locate_trees(las_cat, lmf(5), uniqueness = "bitmerge")
ttops2<- st_as_sf(ttops)

opt_output_files(las_cat) <- "D:/Lidar_2022/R/segment tree/missing_silva/folder1/f1segmented_{XLEFT}_{YBOTTOM}"
algo <- silva2016(chm, ttops2)
ctg_segmented <- segment_trees(las_cat, algo)

opt_output_files(ctg_segmented) <- "D:/Lidar_2022/R/segment tree/missing_silva/folder1/f1canpoy_{XLEFT}_{YBOTTOM}"
segmentedtree<-crown_metrics(ctg_segmented, NULL, geom = "convex")

It gave me an error below.

Processing [=====================] 100% (1/1) eta:  0s
Error: scan() expected 'a real', got 'IllegalArgumentException:'
In addition: Warning messages:
1: There are 304 points flagged 'withheld'. 
2: 1 invalid polygons created. They likely correspond to trees with aligned points.

Here is a single las file that I used for.

Jean-Romain commented 1 year ago

Please sent a reproducible that takes less time.

I try to reproduce with

chm <- rasterize_canopy(las_cat, 1, p2r(0.2))

But it worked.

Using pitfree is excessively long. Please send me the CHM and the shapefile of tree tops.

taeyoonlee87 commented 1 year ago
ctg_segmented <- readLAS("D:/Lidar_2022/R/segment tree/missing_silva/folder1/f1segmented_589500_3690000.las")

##f1segmented_589500_3690000.las  is a result of these codes 
#opt_output_files(las_cat) <- "D:/Lidar_2022/R/segment tree/missing_silva/folder1/f1segmented_{XLEFT}_{YBOTTOM}"
#algo <- silva2016(chm, ttops2)
#ctg_segmented <- segment_trees(las_cat, algo)

opt_output_files(ctg_segmented) <- "D:/Lidar_2022/R/segment tree/missing_silva/folder1/f1canpoy_{XLEFT}_{YBOTTOM}"
#>Error in opt_chunk_size(ctg) :   no slot of name "chunk_options" for this object of class "LAS"
segmentedtree<-crown_metrics(ctg_segmented, NULL, geom = "convex")
#> Warning message: 1 invalid polygons created. They likely correspond to trees with aligned points. 

I uploaded Shapefile of ttops and CHM. you can also find the denoised las file and segemented las file.

https://www.dropbox.com/scl/fo/v7i6a1p27yto9fo6t7vcd/h?rlkey=dk6q77kpiazxetvnlor1s37qw&dl=0

Thanks!

flottsam commented 1 year ago

I encountered the same issue. I was able to work around it by getting the crowns directly from the CHM:

(algo <- dalponte2016(chm, trees_lmfauto))
(dalponte_lmfauto <- algo())

And then converting the crowns to polygons, something like:

(dalponte_lmfauto <- dalponte_lmfauto %>% rast() %>% 
    as.polygons(values=TRUE) %>% 
    st_as_sf() %>% st_convex_hull() %>% 
    # spatially join trees
    st_join(trees_lmf))

Oddly, sometimes crowns are returned as raster, sometimes as spatrast.

Jean-Romain commented 1 year ago

I encountered the same issue. I was able to work around it by getting the crowns directly from the CHM:

Which is definitively a different output. You get the contour of the raster while crown_metrics make the hull of the point in the point cloud.

Oddly, sometimes crowns are returned as raster, sometimes as spatrast.

Please report an issue with a reproducible example. Thanks.

Jean-Romain commented 1 year ago

So I reproduced the error with the following MRE:

ctg_segmented = readLAScatalog("issue 701/f1segmented_589500_3690000.las")
segmentedtree<-crown_metrics(ctg_segmented, NULL, geom = "convex")

But this works

ctg_segmented = readLAS("issue701/f1segmented_589500_3690000.las")
segmentedtree<-crown_metrics(ctg_segmented, NULL, geom = "convex")

Let's investigate. It is a very weird behavior

Jean-Romain commented 1 year ago

The issue was the following:

  1. f1segmented_589500_3690000.las contains a tree which generates an invalid polygon
#> Warning message: 1 invalid polygons created. They likely correspond to trees with aligned points. 
  1. when reading the las file and applying crown_metrics() it returns an sf object with one invalid polygons. The functions worked as expected with a warning and so on...
  2. when using a las catalog crown_metrics is called internally and works but then, for each chunk, there is a post processing step to ensure the different outputs from different files merge properly. This post processing step failed because of the invalid polygon.

I fixed the issue by always removing the invalid polygons in crown_metrics.

flottsam commented 1 year ago

Thank you!

taeyoonlee87 commented 1 year ago

Then, do you know if I need to download version 4.0.4? Could you please tell me how I can download 4.0.4. I only see the 4.0.3 from the "Release" tab.

flottsam commented 1 year ago

You need to get the development version from Github (or wait until it is released to cran).

install.packages("devtools")
library(devtools)
install_github("r-lidar/lidR")
library(lidR)
taeyoonlee87 commented 1 year ago

I really appreciate it. I confirmed it works!