R语言 自动执行焦点函数,以便将其应用于文件夹中的每个栅格

lb3vh1jj  于 2023-03-15  发布在  其他
关注(0)|答案(2)|浏览(107)

在R中,我使用焦点函数来模糊一些光栅。到目前为止,我正在单独阅读每个光栅,应用焦点(请参见下面的代码),然后移动到下一个光栅等。单独读取和应用函数到每个光栅的过程非常耗时,因为我有许多研究地点的多个光栅。
我想做的是读取位于文件夹中的光栅,并以自动化的方式应用焦点函数。也就是说,该函数应该读取文件夹中的每个光栅,然后将准确地应用我对每个光栅使用的代码。这样,我唯一需要设置的参数将是工作目录。
这就是我目前所做的:

library(terra)

wd = "C:/Users/nikos/OneDrive/Desktop/psf_paris2/"

# read the rasters
pop = rast(paste0(wd, "pop.tif"))

# focal for every raster
# raster named pop
for (i in seq(from = 0.2, to = 0.8, by = 0.2)) {
  
  print(i)
  
  gf <- focalMat(pop, i * 400, "Gauss")
  r_gf <- focal(pop, w = gf, na.rm = TRUE)

  r_gf = aggregate(r_gf, fact = 4, fun = "mean", cores = 8)
  
  stringedi = gsub("\\.", "", toString(format(i, nsmall = 2)))
  
  writeRaster(r_gf, 
              paste0("C:/Users/nikos/OneDrive/Desktop/psf_paris2/pop", 
                                  stringedi, ".tif"), 
              overwrite=TRUE)
}

下面是一个光栅:

pop = raster(new("RasterLayer", file = new(".RasterFile", name = "C:\\Users\\Geography\\Desktop\\focal\\pop.tif", 
    datanotation = "FLT4S", byteorder = "little", nodatavalue = -Inf, 
    NAchanged = FALSE, nbands = 1L, bandorder = "BIL", offset = 0L, 
    toptobottom = TRUE, blockrows = c(rows = 5L), blockcols = c(cols = 358L), 
    driver = "gdal", open = FALSE), data = new(".SingleLayerData", 
    values = logical(0), offset = 0, gain = 1, inmemory = FALSE, 
    fromdisk = TRUE, isfactor = FALSE, attributes = list(), haveminmax = TRUE, 
    min = 0.43411433696747, max = 355.74725341797, band = 1L, 
    unit = "", names = "pop"), legend = new(".RasterLegend", 
    type = character(0), values = logical(0), color = logical(0), 
    names = logical(0), colortable = logical(0)), title = character(0), 
    extent = new("Extent", xmin = 165700, xmax = 201500, ymin = 5735500, 
        ymax = 5769600), rotated = FALSE, rotation = new(".Rotation", 
        geotrans = numeric(0), transfun = function () 
        NULL), ncols = 358L, nrows = 341L, crs = new("CRS", projargs = "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"), 
    srs = "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs", 
    history = list(), z = list()))
ivqmmu1c

ivqmmu1c1#

文件名问题的解决方案:

library(terra)
library(purrr)
library(fs)

wd = "C:/Users/Geography/Desktop/focal/"

# read the rasters
pop = rast(paste0(wd, "pop.tif"))

doStuff <- function(file){
  
  pic = rast(file)
  
  for (i in seq(from = 0.2, to = 0.8, by = 0.2)) {
    
    print(i)
    
    gf <- focalMat(pic, i * 400, "Gauss")
    r_gf <- focal(pic, w = gf, na.rm = TRUE)
    
    r_gf = aggregate(r_gf, fact = 4, fun = "mean", cores = 4)

    (stringedi = gsub("\\.", "", toString(format(i, nsmall = 2))))
    
    writeRaster(r_gf, 
                paste0("C:/Users/Geography/Desktop/focal/", 
                       basename(fs::path_ext_remove(file)),
                       stringedi, ".tif"), 
                overwrite=TRUE)
  }
  
}

list.files(wd, pattern = "tif$", full.names = TRUE) |>
  purrr::walk(doStuff)
j5fpnvbx

j5fpnvbx2#

创建一个函数,你想做的每一个图片,然后只是循环通过所有的文件

doStuff <- function(file){
  
  pic = rast(pic)
  
  for (i in seq(from = 0.2, to = 0.8, by = 0.2)) {
    
    print(i)
    
    gf <- focalMat(pic, i * 400, "Gauss")
    r_gf <- focal(pic, w = gf, na.rm = TRUE)
    
    r_gf = aggregate(r_gf, fact = 4, fun = "mean", cores = 8)
    
    r_gf <- crop(r_gf, ext(v))
    r_gf <- mask(r_gf, v)
    
    stringedi = gsub("\\.", "", toString(format(i, nsmall = 2)))
    
    writeRaster(r_gf, 
                paste0("C:/Users/nikos/OneDrive/Desktop/psf_paris2/", basename(file),
                       stringedi, ".tif"), 
                overwrite=TRUE)
  }
  
}

list.files(wd, pattern = "tif$", full.names = TRUE) |>
  purrr::walk(doStuff)

相关问题