简体   繁体   English

将多个文件读入R

[英]Reading multiple files into R

I have a directory with 365 tif images. 我有一个包含365个tif图像的目录。 Using R, I need to read them in then use a new projection on them then write them as tif files. 使用R,我需要读入它们,然后在它们上使用新的投影,然后将它们写为tif文件。 Basically I have a file full of images and I need to read them in, do some kind of process on them then send them to another file location. 基本上,我有一个充满图像的文件,我需要读入它们,对它们进行某种处理,然后将它们发送到另一个文件位置。

What I have so far is 我到目前为止所拥有的是

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)

}

I know working with rasters would normally be asked on the GIS site but my question is with the coding so I hope it is fine here. 我知道通常会在GIS网站上询问使用栅格的问题,但是我的问题是编码,所以我希望在这里很好。 Any suggestions would be great. 任何建议都很好。 Thanks! 谢谢!

One approach is to make a function and lapply through all the files in your working directory. 一种方法是创建一个函数并遍历工作目录中的所有文件。

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

I think there is a function called spTransform from the rgdal package that should also do the trick. 我认为rgdal包中有一个名为spTransform的函数也可以解决这个问题。 I am not familar with the projectRaster function. 我不熟悉projectRaster函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM