首页 > 解决方案 > 如何有效地将多个光栅(.tif)文件导入 R

问题描述

我是 R 新手,尤其是在空间数据方面。我正在尝试找到一种方法将多个(~600)单波段光栅(.tif)文件有效地导入 R,所有文件都存储在同一个文件夹中。不确定这是否重要,但请注意,在我的 Mac 和 Windows Parallel VM 上的文件夹中查看时,每个 .tif = .TIF 有以下五 (5) 种文件格式;.tfw; .TIF.aux.xml;.TIF.ovr; .TIF.xml。无论如何,以下代码(以及我尝试过的其他类似变体)似乎不起作用:

library(sp)
library(rgdal)
library(raster)

#path to where all .tif files are located
setwd("/path/to/workingdirectory")

#my attempt to create a list of my .tif files for lapply
temp = list.files(pattern="*.tif")
temp #returns 'character(0)'

#trying to use the raster function to read all .tif files
myfiles = lapply(temp, raster)
myfiles #returns 'list()'

有没有办法使用某种形式的循环来有效地导入所有光栅文件?

标签: rlapplyspatialrastertiff

解决方案


如果栅格具有相同的范围,您可以简单地将它们加载到堆栈中

#first import all files in a single folder as a list 
rastlist <- list.files(path = "/path/to/wd", pattern='.TIF$', all.files=TRUE, full.names=FALSE)

library(raster)
allrasters <- stack(rastlist)

推荐阅读