I'm using the R packages lidR
and TreeLS
to process lidar data and I made a function that takes in a single LAS
file and splits out a shapefile, it looks like this:
lasfile = 'tile_1.las'
las = readLAS(lasfile)
stemdetection = function(las, ...)
{
UseMethod("stemdetection", las)
}
stemdetection.LAS = function(las, vox, dens, n, rad)
{
thin = tlsSample(las, voxelize(vox))
map = treeMap(thin, map.hough(hmin = 0.25, hmax = 16, hstep = 0.5, pixel_size = 0.05, max_radius = rad, min_density = dens))
tls = stemPoints(las, map)#classify stem points
seg = stemSegmentation(tls, sgmt.ransac.circle(n))#extract measures
cs <- crs(tls)
thin <- NULL
map <- NULL
coordinates(seg) = cbind(seg$X, seg$Y)
output <- writeOGR(seg, td, paste(tools::file_path_sans_ext(basename(lasfile)), "_stems", sep=""), driver="ESRI Shapefile", overwrite_layer=TRUE)
return(output)
}
I'm trying to change the function to work on a LAScatalog
but having some issues with getting the name of each las file (chunk). Here is one example of what I tried:
stemdetection.LAScluster = function(las, vox, dens, n, rad)
{
las <- readLAS(las, select = 'xyzi')# Read the LAScluster
if (is.empty(las)) return(NULL) # Exit early (see documentation)
thin = tlsSample(las, voxelize(vox))
map = treeMap(thin, map.hough(hmin = 0.25, hmax = 16, hstep = 0.5, pixel_size = 0.05, max_radius = rad, min_density = dens))
tls = stemPoints(las, map)#classify stem points
seg = stemSegmentation(tls, sgmt.ransac.circle(n))#extract measures
cs <- crs(tls)
thin <- NULL; map <- NULL ; tls = NULL
coordinates(seg) = cbind(seg$X, seg$Y)
output <- writeOGR(seg, td, paste(tools::file_path_sans_ext(basename(ctg@data$File)), "_stems", sep=""), driver="ESRI Shapefile", overwrite_layer=TRUE)
return(output)
}
stemdetection.LAScatalog = function(las, vox, dens, n, rad)
{
catalog_apply(las, stemdetection, vox = vox, dens = dens, n = n, rad = rad)
}
Is there a way to append the filename to the LAS object while (or after) performing the readLAS function?
Thanks in advance!