library(lidR)
library(terra)
library(here)
Tree detection
Tree detection
Goal is to detect tree tops using a variable window size and using the ndom raster in the BI plots in Solling 2023.
Load ndom previously clipped to BI plots in Solling (BI 2023)
<- rast(here("R_session", "data", "ndom_bi_plots_solling_2023.tif"))
ndom_bi mapview(ndom_bi)
Make tiles because data too big for R
<- makeTiles(x=ndom_bi,
tiles y=c(3000,3000),
filename=
here("R_session", "output","tiles","ndom_bi_.tif"),
na.rm=TRUE)
# In case the tiles are already there
<-list.files(here("R_session", "output","tiles"), pattern='*\\.tif', recursive=TRUE, full.names=TRUE) tiles
Tree detection function with variable window size. Any points below 2 m will equate to a window size of 3 m, while points above 20 meters equate to a window size of 5 m. Anything between 2 and 20 meter will have a non-linear relationship.
<- function(x) {
f <- 2.6 * (-(exp(-0.08*(x-2)) - 1)) + 3
y # from https://r-lidar.github.io/lidRbook/itd.html
< 2] <- 3
y[x > 20] <- 5
y[x return(y)
}
<- seq(-5,30,0.5)
heights <- f(heights)
ws plot(heights, ws, type = "l", ylim = c(0,5))
Tree detection using function for variable window size
<- sapply(tiles, \(tile) {
out <- rast(tile)
x <- lidR::locate_trees(x, lidR::lmf(f))
ttops vect(ttops)
})
<- vect(out) out
Save tree tops
writeVector(out, here("R_session", "output","ttops_ndom_.gpkg"), overwrite=TRUE)