I am attempting perform individual tree detection on a lidar-derived canopy height model raster using the lidR package in R. The raster is approximately 2.5 Gb. When I attempt to run the locate_trees() function, I get this error: Error: Large on-disk rasters are not supported by locate_tree. Load the raster manually. What is meant by "loading the raster manually?" and how can I do this? I made a reproducible example of my workflow. However, making a fake >3GB raster may crash your R session or take forever. Use caution before running:
library(lidR)
library(terra)
chm<-rast(xmin=-121.5, xmax =-120, ymin=44.5, ymax = 45.5, crs="EPSG:2992", nrows=150000, ncols=200000) #Fake 3 billion cell raster
values(chm)<-rnorm(3000000000, 20, 5) #giving random values to the raster cells
writeRaster(chm, paste(tempdir(), "CHM_Example.tif", sep="/"), filetype="GTiff")#writing the raster to disk
canopy<-rast(paste(tempdir(), "CHM_Example.tif", sep="/")) # reading the raster into R using the terra package framework
taos<-locate_trees(canopy, lmf(ws=5))#Error is thrown here attempting to perform individual tree detection
It dawned on me that I had run into this issue before...and solved it. I dug up and dusted off an old script from a previous project, and voila the answer was there all along! "Loading the raster manually" means actually using the
readStart(),readValues(), andreadStop()functions in terra to manually read all values within predetermined block of the raster to then feed tolidR::locate_trees().I adapted the chunk and buffer scheme that @JRR in lidR into a loop to ensure I am getting full coverage without duplicates. Admittedly, this is probably not the most efficient solution, but it works.N.B. in this reproducible example, I just use the Mixed Conifer dataset that comes with the lidR package