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
607 stars 132 forks source link

Delineate Crowns #438

Closed Saadi4469 closed 3 years ago

Saadi4469 commented 3 years ago

Good day Jean,

Thank you for responding to that question on gis.stackexchange. I tried sending you this via email, but your email server keeps on rejecting my email both from Outlook and Gmail. The code that I used is below:

# Bring in the ALS file
TP1 = readLAS("~/Sample.las")

# Then, normalize the data so that ground points are centered on 0.
TP1_DTM = grid_terrain(TP1.pmf, algorithm = knnidw(k = 8, p = 2))
TP1.normalized = normalize_height(TP1.pmf, TP1_DTM)

# Create a filter to remove points above 95th percentile of height
filter_noise = function(las, sensitivity)
{
  p99 = grid_metrics(las, ~quantile(Z, probs = 0.99), 10)
  las = merge_spatial(las, p99, "p99")
  las = filter_poi(las, Z < p99*sensitivity)
  las$p99 <- NULL
  return(las)
}

TP1_denoised = filter_noise(TP1.normalized, sensitivity = 1.2)

# Generate the Canopy Height Model (CHM)
TP1.CHM = grid_canopy(TP1_denoised, 0.5, pitfree(c(0,2,5,10,20), c(3,1.5), subcircle = 0.2))
plot_dtm3d(TP1.CHM)

# Here a single 5x5 moving window is used to apply a median filter:
win = matrix(1,5,5)
TP1.chm_s = focal(TP1.CHM, w = win, fun = median)

TP1.wtrshd = watershed(TP1.chm_s, th = 20)
TP1.ITD.wtrshd  = segment_trees(TP1_denoised, TP1.wtrshd)

# Hulls from the Watershed method
TP1_hulls_wtrshd  = delineate_crowns(TP1.ITD.wtrshd, func = .stdmetrics)

Error in sp::SpatialPolygons(hulls[["poly"]]) : is.list(Srl) is not TRUE

Kind Regards,

Jean-Romain commented 3 years ago

I answered on gis.stackexchange. I keep the issue opened to improve the code and handle this edge case with a more informative error. Btw you should not be that scared of sharing a microscopic 1000 m² of data containing shurbs lost in the desert. Anyway I deleted the link after I downloaded the file in your message. because github is also a public place. You can remove the file from were it is strored. Thank you for reporting the issue.

Saadi4469 commented 3 years ago

Can I remove the file link though?

Jean-Romain commented 3 years ago

Yes

Jean-Romain commented 3 years ago

segment_trees() now prints a message and delineate_crowns() throws a warning and returns NULL. No longer failure. Thanks