【问题标题】:stack files of various folders using R使用R堆栈各种文件夹的文件
【发布时间】:2018-10-01 13:10:02
【问题描述】:

我堆叠了一个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) 
  }
}

【问题讨论】:

  • 您的意思是希望每个文件夹成为一个单独的堆栈,还是希望所有 tif 都在一个堆栈中?
  • all_Landsat&lt;-list.files("C:/Users/Landsat",pattern="*B[123457]\\.tif$", ignore.case=TRUE, recursive= TRUE)
  • 是的,我想要的是为每个文件夹创建一个单独的堆栈。有可能吗?

标签: r stack landsat


【解决方案1】:

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

setwd("C:/Users/Landsat")    
a<-list.dirs(getwd(),recursive = FALSE )
flist <- list()
stackfile <- list()
for (i in 1:length(a)){  
    flist[[i]] <-  list.files(a[i], recursive = TRUE, full.names = TRUE, pattern = "tif$")  

    stackfile[[i]] <- stack(flist[[i]])
}

【讨论】:

  • 谢谢 ANUP,但我正在尝试为每个文件夹创建一个单独的堆栈
  • 一共有多少个文件夹?如果它只有 3 或 4,您可以重新使用相同的读取文件和堆栈代码。 @Cotimaass
  • 我总共有很多文件夹(至少 25 个)
  • @Cotimaass 我已经更新了我的答案。那样有用吗 ?我无法检查这个,因为我没有你的设置。
  • 感谢@anup 我在运行循环时遇到错误:.local(.Object, ...) 中的错误:此外:警告消息:在 flist[i]
【解决方案2】:

感谢@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))
}) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 2012-08-27
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    相关资源
    最近更新 更多