将多个文件读入R
Reading multiple files into R
我有一个包含 365 tif 图像的目录。使用 R,我需要读取它们,然后对它们使用新的投影,然后将它们写为 tif 文件。基本上我有一个充满图像的文件,我需要读入它们,对它们进行某种处理,然后将它们发送到另一个文件位置。
我目前所掌握的是
newproj <- '+init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84
+no_defs +towgs84=0,0,0'
x <- dir(path='c:/users/JDD/desktop/process', pattern='.tif')
for(i in 1:length(x)){
temp_i <- raster(x[i])
temp_i <- projectRaster(temp_i, crs=newproj)
writeRaster(temp_i, '2013_i.tif', GTiff)
}
我知道在 GIS 网站上通常会询问使用栅格,但我的问题是编码,所以我希望这里没问题。任何建议都会很棒。谢谢!
一种方法是创建一个函数并lapply遍历工作目录中的所有文件。
change.proj <- function(x) {
require(rgdal)
temp <- raster(x)
temp <- spTransform(x, crs=CRS(newproj))
writeRaster(temp, paste0("new",x), GTiff)
}
setwd("your folder with all the tif files")
files = list.files(pattern="*.tif")
lapply(files, function(x) change.proj(x))
我认为 rgdal
包中有一个名为 spTransform
的函数也应该可以解决问题。我不熟悉 projectRaster
函数。
我有一个包含 365 tif 图像的目录。使用 R,我需要读取它们,然后对它们使用新的投影,然后将它们写为 tif 文件。基本上我有一个充满图像的文件,我需要读入它们,对它们进行某种处理,然后将它们发送到另一个文件位置。
我目前所掌握的是
newproj <- '+init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84
+no_defs +towgs84=0,0,0'
x <- dir(path='c:/users/JDD/desktop/process', pattern='.tif')
for(i in 1:length(x)){
temp_i <- raster(x[i])
temp_i <- projectRaster(temp_i, crs=newproj)
writeRaster(temp_i, '2013_i.tif', GTiff)
}
我知道在 GIS 网站上通常会询问使用栅格,但我的问题是编码,所以我希望这里没问题。任何建议都会很棒。谢谢!
一种方法是创建一个函数并lapply遍历工作目录中的所有文件。
change.proj <- function(x) {
require(rgdal)
temp <- raster(x)
temp <- spTransform(x, crs=CRS(newproj))
writeRaster(temp, paste0("new",x), GTiff)
}
setwd("your folder with all the tif files")
files = list.files(pattern="*.tif")
lapply(files, function(x) change.proj(x))
我认为 rgdal
包中有一个名为 spTransform
的函数也应该可以解决问题。我不熟悉 projectRaster
函数。