使用R中的“lapply”从包含激光雷达数据的文件夹创建多个栅格文件

gt0wga4j  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(167)

我如何读取一个文件夹中的所有文件,执行一个脚本,并从包含原始名称的所有文件创建单独的输出?我有一个文件夹与.las文件,我需要从他们创建相应的.asc文件。我的脚本如下:

library(lidR)
# Path to data  
LASfile <- ("path/1234.las") 
# Sorting out points in point cloud data, keeping vegetation and ground point classes.
las <- readLAS(LASfile, filter="-keep_class 1 2") # Keep high vegetation and ground point classes
# Normalizing ground points to 0 elevation (idwinterpolation), instead of meters above sea level.
dtm <- grid_terrain(las, algorithm = knnidw(k = 8, p = 2))
las_normalized <- normalize_height(las, dtm)
# Create a filter to remove points above 95th percentile of height
lasfilternoise = function(las, sensitivity)
{
  p95 <- grid_metrics(las, ~quantile(Z, probs = 0.95), 10)
  las <- merge_spatial(las, p95, "p95")
  las <- filter_poi(las, Z < p95*sensitivity)
  las$p95 <- NULL
  return(las)
}
# Generating a pitfree canopy height modela model without null values (Khosravipour et al., 2014)
las_denoised <- lasfilternoise(las_normalized, sensitivity = 1.2)
chm <- grid_canopy(las_denoised, 0.32, pitfree(c(0,2,5,10,15), c(3,1.5), subcircle = 0.2))
# Applying a median filter, 5x5 moving window to smooth the image and remove noise
ker <- matrix(1,3,3)
chms <- raster::focal(chm, w = ker, fun = median) 
plot(chms)
library(raster)
# Writing output file
writeRaster(chms, filename="path/1234.asc", format="ascii", overwrite=TRUE) # Ändra till relevant för varje körning
citation("lidR")

我尝试使用lapply,但我不知道如何使用它在正确的方式。必须是这样的东西来读取文件夹中的所有文件:list.files("path", pattern = "*.las", full.names = TRUE)
写输出文件的代码如下:lapply(r, writeRaster, filename = paste0(f, ".asc"), format = "ascii")
但我做不好

ia2d9nvy

ia2d9nvy1#

LAZ到LAS+索引转换的示例:

convertLAZ <- function(lazfile, outdir = "") {
  if(!dir.exists({{outdir}})) { dir.create({{outdir}}, recursive = TRUE)}
  print(lazfile)
  las <- lidR::readLAS(files = {{lazfile}}, filter = "-keep_class 2 9")
  .file <- stringi::stri_replace_all_regex({{lazfile}}, "^.*/", "")
  lidR::writeLAS(las, file = paste0({{outdir}}, "/", stringi::stri_replace_all_fixed(.file, "laz", "las")), index = TRUE)
}

f <- list.files("data/laz", pattern = "*.laz", full.names = TRUE)
lapply(f, convertLAZ, outdir = "data/las22")

你可以将它扩展到光栅化、标准化等,并保存为.asc。但我建议你看看https://r-lidar.github.io/lidRbook/engine.html。简而言之:将LAZ/LAS文件处理为LAScatalog,然后平铺结果栅格并保存为.asc
以及一个如何使用并行处理的示例(在下面的示例3+1进程中-请注意,它可能会占用大量内存,因此要注意工作线程数/处理参数,如opt_chunk_buffer

library(future)
options(parallelly.availableCores.methods = "mc.cores")
options(mc.cores = 3)
plan(multisession)
parallelly::availableWorkers()

library(lidR)

myPath <- "data/las"
ctg <- readLAScatalog(myPath)
crs(ctg) <- "EPSG:2180"

ctg@output_options$drivers$SpatRaster$param$overwrite <- TRUE
opt_output_files(ctg) <- "data/dtm2/barycz__{XLEFT}_{YBOTTOM}"
opt_chunk_size(ctg) <- 500
opt_chunk_buffer(ctg) <- 600
opt_filter(ctg) <- "-keep_class 2 9"
summary(ctg)

vr <- rasterize_terrain(ctg, 0.25, tin())
plot(vr)
rdlzhqv9

rdlzhqv92#

LasCatalog似乎是一个很好的控制工作流的唤醒!我一定会看看LasCatalog。我会使用瓦片来机器预测这个特定的情况,所以我会解决与lapply来解决这个更紧迫的任务。我几乎得到了它的解决,但仍然不是100%。
我得到“乐趣中的错误(Xi,...):未使用的参数(文件名= c(“C:/Lasdata/125000_6410000.las.asc”,...”
我的代码如下:

library(lidR)
createASCI <- function(lasfile, outdir = "") {
  if(!dir.exists({{outdir}})) { dir.create({{outdir}}, recursive = TRUE)}
  print(lasfile)
las <- readLAS(files = {{lasfile}}, filter="-keep_class 2 1")
.file <- stringi::stri_replace_all_regex({{lasfile}}, "^.*/", "")
# Normalizing ground points to 0 elevation (idwinterpolation), instead of meters above sea level.
dtm <- grid_terrain(las, algorithm = knnidw(k = 8, p = 2))
las_normalized <- normalize_height(las, dtm)
# Create a filter to remove points above 95th percentile of height
lasfilternoise = function(las, sensitivity)
{
  p95 <- grid_metrics(las, ~quantile(Z, probs = 0.95), 10)
  las <- merge_spatial(las, p95, "p95")
  las <- filter_poi(las, Z < p95*sensitivity)
  las$p95 <- NULL
  return(las)
}
# Generating a pitfree canopy height modela model without null values (Khosravipour et al., 2014)
las_denoised <- lasfilternoise(las_normalized, sensitivity = 1.2)
chm <- grid_canopy(las_denoised, 0.32, pitfree(c(0,2,5,10,15), c(3,1.5), subcircle = 0.2))
# Applying a median filter, 5x5 moving window to smooth the image and remove noise
ker <- matrix(1,3,3)
chms <- raster::focal(chm, w = ker, fun = median) 
library(raster)
# Writing output file
writeRaster(chms, file = paste0({{outdir}},index = TRUE)

}
f <- list.files("C:/Lasdata", pattern = "*.las", full.names = TRUE)
lapply(f, createASCI, filename = paste0(f, ".asc"), format = "ascii")

相关问题