tiagodc / TreeLS

R functions for processing individual tree TLS point clouds
GNU General Public License v3.0
82 stars 27 forks source link

tlsNormalize failing to normalize TLS data #55

Open munachau opened 6 months ago

munachau commented 6 months ago

I used the TLS data available in package pine_plot.laz. but it is unable to normalize as it is related with function raster

tls = tlsNormalize(tls, keep_ground = T) no ground points found, performing ground segmentation Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'raster': unable to find an inherited method for function ‘extent’ for signature ‘"LAS"’

ashlynolah commented 4 months ago

I am also having this issue.

green512 commented 4 months ago

Me too

bi0m3trics commented 4 months ago

It's line 545 of methods.R - grid = las %>% extent %>% raster which is trying to apply extent to an object of class LAS and instead lidR uses ext from terra since lidR no longer uses raster(as no one should, since the "package has been superseded by the "terra" package")

In short, I'd be careful even using this package at all as it's no longer supported, but if you do I suggest rewriting functions (like tlsNormalize) to use them locally (or fork the repo and make your on version of the package) where objects like rasters produced from function in lidRare converted to raste::raster prior to being used. That or undertake the epic task of reworking the entire package to use terrainstead of raster.

For example:

tlsNormalize = function(las, min_res=.25, keep_ground=TRUE){

  isLAS(las)

  if(min_res <= 0)
    stop('res must be a positive number')

  if(!any(las$Classification == 2)){
    message('no ground points found, performing ground segmentation')
    las = classify_ground(las, csf(class_threshold = 0.05, cloth_resolution = 0.05), last_returns = F)
  }

  res = lidR::st_area(las) / length(las@data[Classification == 2])
  res = ifelse(res < min_res, min_res, res)

  ext = lidR::ext(las)
  grid = terra::rast(ext, res=res)

  dtm = grid_terrain(las, res = grid, algorithm = knnidw(), full_raster=TRUE)
  las = normalize_height(las, dtm, na.rm=TRUE, Wdegenerated = TRUE)

  if(!keep_ground) las = filter_poi(las, Classification != 2)

  return(las)
}
Estrada-Gene commented 4 months ago

thank you @bi0m3trics for the helpful response. I tried using your updated tlsNormalize() function as you've written it out above, but I get this error: could not find function "isLAS". Is this function in a different package that I should load or is this just example code that I should adjust myself for my own needs?

bi0m3trics commented 4 months ago

@Estrada-Gene isLAS is part of TreeLS, so if you know it's a las you can delete it...

spono commented 3 months ago

@bi0m3trics I forked the project and moved sp/raster functions to sf/terra...it still may need some proof-check but it runs the examples pretty smoothly :) If any of you can test it a bit against the previous version, I can then push a pull request to the main repo and let TreeLS roll again

radt0005 commented 1 month ago

Thanks @spono and @bi0m3trics.

crespelp commented 3 weeks ago

Same problem, and I´m too neophyte to try the suggested fixes. Is this library current?

bi0m3trics commented 3 weeks ago

@crespelp It was current to R 4.3 about a year ago. @spono has a fork that was current a few months ago (correct me if I'm wrong, Nic) and my fork was current to somewhere in between (but definitely pre-4.4)... so this repo - maybe no, other forks - maybe yes. Not a great answer, but the truth.

I suggest installing Nic's fork, and seeing if it works... remotes::install_github('spono/TreeLS')