Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
taeyoonlee87 opened this issue Aug 17, 2023 · 10 comments
Closed
Assignees
Labels
Bug A bug in the package

Comments

@taeyoonlee87
Copy link

taeyoonlee87 commented Aug 17, 2023

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 Jean-Romain changed the title crown_metrics crown_metrics: scan() expected 'a real', got 'IllegalArgumentException: Aug 18, 2023
@r-lidar r-lidar deleted a comment from taeyoonlee87 Aug 18, 2023
@Jean-Romain
Copy link
Collaborator

Jean-Romain commented Aug 21, 2023

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.

@Jean-Romain Jean-Romain self-assigned this Aug 21, 2023
@Jean-Romain Jean-Romain added the Bug A bug in the package label Aug 21, 2023
@taeyoonlee87
Copy link
Author

taeyoonlee87 commented Aug 21, 2023

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
Copy link

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
Copy link
Collaborator

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
Copy link
Collaborator

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
Copy link
Collaborator

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
Copy link

Thank you!

Jean-Romain added a commit that referenced this issue Aug 22, 2023
@taeyoonlee87
Copy link
Author

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
Copy link

flottsam commented Aug 23, 2023

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
Copy link
Author

I really appreciate it. I confirmed it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in the package
Projects
None yet
Development

No branches or pull requests

3 participants