【发布时间】:2021-09-17 12:21:14
【问题描述】:
我有 16 个单独的光栅文件,分别以 ID 和波段命名,用于 8 个波段和 2 个 ID。如何合并每个 ID 的波段以创建两个多波段 TIFF?这是我的数据:
dput(rastlist)
c("7398_b10_new.tif", "7398_b2_clip.tif", "7398_b3_clip.tif",
"7398_b4_clip.tif", "7398_b5_clip.tif", "7398_b6_clip.tif", "7398_b7_clip.tif",
"7398_pan_new.tif", "9609_b10_new.tif", "9609_b2_clip.tif", "9609_b3_clip.tif",
"9609_b4_clip.tif", "9609_b5_clip.tif", "9609_b6_clip.tif", "9609_b7_clip.tif",
"9609_pan_new.tif")
我设法为列表的前 8 个元素做到了这一点。方法如下:
setwd("mydir")
#first import all files in a single folder as a list
rlist <- list.files(path = "mydir", pattern='.tif$', all.files=T, full.names=F)
#substract the first 8 raster of the list
n = tail(rlist,8)
#stack layers
rstack = stack(n)
#substract the first element of the list
one = tail(n, 1)
#get the first 4 letters of the first element of a list (to be used as raster name of the raster stack)
rexport = substr(one, 1, 4)
writeRaster(rstack, filename = rexport, options = "INTERLEAVE=BAND", overwrite = T, format = "GTiff")
基本上,我想要的是创建一个函数,每 8 个栅格执行一次上述代码。
【问题讨论】:
-
如果光栅 ID 是每个光栅文件名的前缀,你能在一个循环中使用你现有的代码吗?
-
试了很多次还是搞不定。