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.

library(lidR)
library(terra)
library(here)

Load ndom previously clipped to BI plots in Solling (BI 2023)

ndom_bi <- rast(here("R_session", "data", "ndom_bi_plots_solling_2023.tif"))
mapview(ndom_bi)

Make tiles because data too big for R

tiles <- makeTiles(x=ndom_bi, 
          y=c(3000,3000),
          filename=
            here("R_session", "output","tiles","ndom_bi_.tif"), 
          na.rm=TRUE)

# In case the tiles are already there
tiles <-list.files(here("R_session", "output","tiles"), pattern='*\\.tif', recursive=TRUE, full.names=TRUE)

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.

f <- function(x) {
  y <- 2.6 * (-(exp(-0.08*(x-2)) - 1)) + 3 
  # from https://r-lidar.github.io/lidRbook/itd.html
  y[x < 2] <- 3
  y[x > 20] <- 5
  return(y)
}

heights <- seq(-5,30,0.5)
ws <- f(heights)
plot(heights, ws, type = "l",  ylim = c(0,5))

Tree detection using function for variable window size

out <- sapply(tiles, \(tile) {
        x <- rast(tile)
        ttops <- lidR::locate_trees(x, lidR::lmf(f))
        vect(ttops)
    })

out <- vect(out)

Save tree tops

writeVector(out, here("R_session", "output","ttops_ndom_.gpkg"), overwrite=TRUE)