2017-12-16 1 views
-1

Rで複数の.tifファイルを処理しようとしましたが、.tifファイルを再作成したいと思っていました。私はそれに取り組んでいる間、私は次のコードを実行しました。私は次のようなエラーにループ機能を使用して複数のラスターファイルを再投影する方法

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([email protected]@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 

誰もがこの点で私を助けることができるのだコードを実行した後

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

? ありがとう

+0

は 'raster'を呼び出す前に、ファイル名をプリントアウトしてみます。あなたの 'list.files'に' full = TRUE'を追加して、名前だけでなく*フルパス*を返すようにしてください。 '1:4'をハードコーディングするのではなく、' mylist'の長さをループしてみてください。ここで質問する前にいくつかの基本的なデバッグを試してみてください。 – Spacedman

+0

ありがとうございます。しかし、私はこのアイデアを試しても同じエラーが発生しました。私は完全なパス名を追加し、ファイルの長さにわたってループを試しました。同じエラーが出ています。 – user2968058

+0

ループを読む前に 'mylist [i]'を表示してください。それが失敗したファイルを確認してください - 最初のファイルですか?二番目?コマンドラインで 'raster("/whatever/is/printed/out.tif ")'を実行してみてください。ファイルが破損している可能性がありますか? – Spacedman

答えて

0

fnにファイル名のベクトルfn[i]の代わりに1つのファイル名を書き込もうとすると、警告が表示されます。つまり、最初の要素fn[1]に4回書き込みます。私には、エラーメッセージがどこから来たのかは不明ですが、それも消えてしまうことが予想されます。

以下はクリーンアップ版です。私はmylistffに変更しました。mylistは、Rの意味で "リスト"ではないため、悪い名前です(list.filesは文字ベクトルを返します)。また、*は無意味でした。そして私はsubstr(, 1,20)より一般的なアプローチを使用しています。さらに、writeRasterと別の手順で行うのではなく、projectRasterというファイルに書き込むほうが効率的です。ラスタが同程度と解像度を持っている場合は

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') 
+0

ありがとう@RobertH。それはうまくいく。どうもありがとうございました。 – user2968058

関連する問題