i具有基于栅格的函数,可以用于许多具有不同模式的栅格对象。对于150个城市,.tif文件模式是:
pattern="_E.tif"
pattern="_ED.tif"
pattern="_SC_t.tif"
pattern="_SH_t.tif"
pattern="_WH_t.tif"
对于一个城市,我这样做了 - 并起作用:
folder <- "D:/Folder_example"
B <- raster("D:/Folder_example/city1 _E.tif")
B1 <- raster("D:/Folder_example/city1 _ED.tif")
B2 <- raster("D:/Folder_example/city1 _SC_t.tif")
B3 <- raster("D:/Folder_example/city1 _SH_t.tif")
B4 <- raster("D:/Folder_example/city1 _WH_t.tif")
当我试图奔跑多个城市时,问题就会出现。所以,我刚刚尝试过:
BB <-list.files(pattern="_E.tif")
BB1<-list.files(pattern="_ED.tif")
BB2<-list.files(pattern="_SC_t.tif")
BB4<-list.files(pattern="_SH_t.tif")
BB6<-list.files(pattern="_WH_t.tif")
B <- lapply(BB, raster) #intead of readTIFF that produced error
B1 <- lapply(BB1, raster)
B2 <- lapply(BB2, raster)
B4 <- lapply(BB4, raster)
B6 <- lapply(BB6, raster)
运行该功能后,我有以下错误
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘cellStats’ for signature ‘"list"’
str(b(给了我 - 在两个城市进行测试
List of 2
$ :Formal class 'RasterLayer' [package "raster"] with 12 slots
...可以,因为我有两个城市作为列表。我的功能需要为这些城市运行。我也尝试了
B <-list.files(path = "D:/Folder_example", pattern="_E.tif", full.names=TRUE)
#same for B1-B4
B <-list.raster.files(path = "folder", pattern = "_E.tif")
#same for B1-B4
B <-stack(pattern="_E.tif", full.names=TRUE)
#same for B1-B4
最近的3个选项都没有。
如何列出一组具有相同模式的栅格对象?非常感谢,迭戈
您是否试图通过目录中的所有tif
文件执行函数?从问题中不太清楚,但这是将目录中的所有图像读取到列表对象中的一个示例。首先创建文件名列表:
> n = 20 # test on a small number before processing all images
> for (i in 1:n) { files[i] <-
paste0("D:/Folder_example/Raster1_pattern",i,".tif") }
> files
[1] "D:/Folder_example/Raster1_pattern1.tif"
[2] "D:/Folder_example/Raster1_pattern2.tif"
[3] "D:/Folder_example/Raster1_pattern3.tif"
[4] "D:/Folder_example/Raster1_pattern4.tif"
[5] "D:/Folder_example/Raster1_pattern5.tif"
... and so on to
[n] "D:/Folder_example/Raster1_patternn.tif"
要将图像放入图像列表中,您将做类似的事情:
> library(rtiff)
> imagelist <- lapply(files, readTIFF)
而不是readTIFF
,您还可以在其位置编写一个函数来做一些不同的事情。这将为您提供包含图像的列表。