如何使用循环函数重新投影多个光栅文件[关闭]

问题描述 投票:-1回答:1

我试图在R中处理多个.tif文件。我想重新投影.tif文件。在处理它时,我执行了以下代码。

#convert into raster and reproject
mylist <- list.files(path='mydir', pattern = "*.tif$")
fname <- substr(mylist, 1,20)
fn <- paste0('prj',fname, '.tif')
newproj <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
for (i in 1:4){
  temprast <- raster(mylist[i])
  prj<-projectRaster(temprast, crs=newproj, method = 'bilinear')
  writeRaster(prj, filename = fn, format='GTiff', overwrite=T)
}

运行代码后,我收到以下错误

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  : 
  Cannot create a RasterLayer object from this file.
In addition: There were 20 warnings (use warnings() to see them)
Warning messages:
1: In if (filename == "") { ... :
  the condition has length > 1 and only the first element will be used
2: In if (!file.exists(dirname(filename))) { ... :
  the condition has length > 1 and only the first element will be used
3: In if (toupper(x@file@name) == toupper(filename)) { ... :
  the condition has length > 1 and only the first element will be used
4: In if (trim(filename) == "") { ... :
  the condition has length > 1 and only the first element will be used
5: In if (!file.exists(dirname(filename))) { ... :
  the condition has length > 1 and only the first element will be used
6: In if (filename == "") { ... :
  the condition has length > 1 and only the first element will be used
7: In if (file.exists(filename)) { ... :
  the condition has length > 1 and only the first element will be used
8: In if (nchar(filename) == 0) stop("empty file name") :
  the condition has length > 1 and only the first element will be used
9: In if (x == "" | x == ".") { ... :
  the condition has length > 1 and only the first element will be used
10: In if (!start %in% c("http", "ftp")) { ... :
  the condition has length > 1 and only the first element will be used
11: In if (fileext %in% c(".GRD", ".GRI")) { ... :
  the condition has length > 1 and only the first element will be used
12: In if (!file.exists(x)) { ... :
  the condition has length > 1 and only the first element will be used
13: In if ((fileext %in% c(".HE5", ".NC", ".NCF", ".NC4",  ... :
  the condition has length > 1 and only the first element will be used
14: In if (fileext == ".GRD") { ... :
  the condition has length > 1 and only the first element will be used
15: In if (fileext == ".BIG" | fileext == ".BRD") { ... :
  the condition has length > 1 and only the first element will be used
16: In if (fileext %in% c(".BIN")) { ... :
  the condition has length > 1 and only the first element will be used
17: In if (fileext == ".DOC") { ... :
  the condition has length > 1 and only the first element will be used
18: In if (fileext %in% c(".SGRD", ".SDAT")) { ... :
  the condition has length > 1 and only the first element will be used
19: In if (nchar(filename) == 0) stop("empty file name") :
  the condition has length > 1 and only the first element will be used
20: In if (!file.exists(x)) { ... :
  the condition has length > 1 and only the first element will be used

在这方面有人可以帮助我吗?谢谢

r loops
1个回答
0
投票

出现警告是因为你试图写入fn,一个文件名向量而不是fn[i],一个文件名。这意味着你要写第一个元素fn[1]四次。我不清楚错误信息的来源,但我预计它也会消失。

以下是清理版本。我将mylist改为ff,因为mylist是一个坏名字,因为它不是R意义上的“列表”(list.files返回一个字符向量)。 *也毫无意义。而且我使用的是比substr( , 1,20)更通用的方法。此外,在projectRaster中写入文件应该更有效,而不是在与writeRaster分开的步骤中执行此操作。

ff <- list.files(path='mydir', pattern = "\\.tif$")
fn <- gsub("\\.tif$", "_prj.tif", ff)
newproj <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"

for (i in 1:4){
    r <- raster(ff[i])
    prj <- projectRaster(r, crs=newproj, method = 'bilinear', filename = fn[i], overwrite=TRUE)
}

如果栅格具有相同的范围和分辨率,您还可以:

ff <- list.files(path='mydir', pattern = "\\.tif$")
s <- stack(ff)
newproj <- "+proj=longlat +datum=WGS84"
prj <- projectRaster(s, crs=newproj, method ='bilinear', filename='prj.tif')
© www.soinside.com 2019 - 2024. All rights reserved.