使用R堆叠各种文件夹的文件

时间:2018-04-20 18:53:12

标签: r stack landsat

我堆叠了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]” (条件2)

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) 
  }
}

2 个答案:

答案 0 :(得分:0)

我没有检查过这个,但希望这有效:

FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("users").get().addOnCompleteListener(appExecutors
                     .networkIO(),
             task -> {});

答案 1 :(得分:0)

谢谢@anup。我终于用这段代码解决了这个问题。它返回按文件夹堆叠的TIF图像列表。

setwd("C:/Users/Landsat")    
a<-list.dirs(getwd(),recursive = FALSE )

landsat<- apply(a,function (dir){  
    img<-stack(list.files(path=dir,ignore.case= TRUE, 
    pattern="*B[123457]\\.tif$", full.names= TRUE))
})