我堆叠了一个 Landsat 图像的某些 tif 文件,如图所示:
setwd("C:/Users/Landsat/L5__002072-09MAY-2006")
may2006<-list.files(".",pattern="*B[123457]\\.tif$", ignore.case=TRUE)
[1] "LT05_L1TP_002072_20060509_20161121_01_T1_B1.TIF"
[2] "LT05_L1TP_002072_20060509_20161121_01_T1_B2.TIF"
[3] "LT05_L1TP_002072_20060509_20161121_01_T1_B3.TIF"
[4] "LT05_L1TP_002072_20060509_20161121_01_T1_B4.TIF"
[5] "LT05_L1TP_002072_20060509_20161121_01_T1_B5.TIF"
[7] "LT05_L1TP_002072_20060509_20161121_01_T1_B7.TIF"
landsat_stack <- stack(may2006)
我想做同样的事情,但是对于文件夹 Landsat 的所有图像(每个文件夹都是一个单独的堆栈)
setwd("C:/Users/Landsat")
foldersList <- normalizePath(list.dirs(full.names = TRUE, recursive = FALSE))
[1] "C:\\Users\\Landsat\\L5__002072-09MAY-2006"
[2] "C:\\Users\\Landsat\\L5_001073_02MAY-2006"
[3] "C:\\Users\\Landsat\\L5_001073_14MAY-1987"
[4] "C:\\Users\\Landsat\\L8__002072-7MAY-2017"
是否可以同时对所有图像执行此操作?我想首先对所有 tif 文件(无论文件夹)做一个列表,然后使用循环仅堆叠名称匹配的文件(条件 1),但以这种模式“B [123457]”结束(条件二)
all_Landsat<-list.files(".",pattern="*B[123457]\\.tif$", ignore.case=TRUE, recursive= TRUE)
all_Landsat
[1] "L5__002072-09MAY-2006/LT05_L1TP_002072_20060509_20161121_01_T1_B1.TIF"
[2] "L5__002072-09MAY-2006/LT05_L1TP_002072_20060509_20161121_01_T1_B2.TIF"
[3] "L5__002072-09MAY-2006/LT05_L1TP_002072_20060509_20161121_01_T1_B3.TIF"
[4] "L5__002072-09MAY-2006/LT05_L1TP_002072_20060509_20161121_01_T1_B4.TIF"
[5] "L5__002072-09MAY-2006/LT05_L1TP_002072_20060509_20161121_01_T1_B5.TIF"
[6] "L5__002072-09MAY-2006/LT05_L1TP_002072_20060509_20161121_01_T1_B7.TIF"
[7] "L5_001073_02MAY-2006/LT05_L1TP_001073_20060502_20161122_01_T1_B1.TIF"
[8] "L5_001073_02MAY-2006/LT05_L1TP_001073_20060502_20161122_01_T1_B2.TIF"
[9] "L5_001073_02MAY-2006/LT05_L1TP_001073_20060502_20161122_01_T1_B3.TIF"
[10]"L5_001073_02MAY-2006/LT05_L1TP_001073_20060502_20161122_01_T1_B4.TIF"
[11]"L5_001073_02MAY-2006/LT05_L1TP_001073_20060502_20161122_01_T1_B5.TIF"
[12]"L5_001073_02MAY-2006/LT05_L1TP_001073_20060502_20161122_01_T1_B7.TIF"
[13]"L5_001073_14MAY-1987/LM50010731987134AAA03_B1.TIF"
[14]"L5_001073_14MAY-1987/LM50010731987134AAA03_B2.TIF"
[15]"L5_001073_14MAY-1987/LM50010731987134AAA03_B3.TIF"
[16]"L5_001073_14MAY-1987/LM50010731987134AAA03_B4.TIF"
[17]"L8__002072-7MAY-2017/LC08_L1TP_002072_20170507_20170515_01_T1_B1.TIF"
[18]"L8__002072-7MAY-2017/LC08_L1TP_002072_20170507_20170515_01_T1_B2.TIF"
[19]"L8__002072-7MAY-2017/LC08_L1TP_002072_20170507_20170515_01_T1_B3.TIF"
[20]"L8__002072-7MAY-2017/LC08_L1TP_002072_20170507_20170515_01_T1_B4.TIF"
[21]"L8__002072-7MAY-2017/LC08_L1TP_002072_20170507_20170515_01_T1_B5.TIF"
[22]"L8__002072-7MAY-2017/LC08_L1TP_002072_20170507_20170515_01_T1_B7.TIF"
但我找不到这两个条件的正确代码:
for (i in all_Landsat){
if (grep(pattern="+B[123457]\\.tif$", ignore.case=FALSE)){
stack(i)
}
}