2017-12-16 1 views
-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([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 

이 사람이 나를이 점에서 도울 수

다음과 같은 오류가있어? 감사합니다

+0

가'raster'를 호출하기 전에 파일 이름을 인쇄 해보십시오. 'full = TRUE'를'list.files'에 추가하여 이름 만이 아닌 * 전체 경로 *를 반환하십시오. '1 : 4'를 하드 코딩하는 대신'mylist'의 길이를 반복 해보십시오. 여기서 물어보기 전에 기본 디버깅을 시도해보십시오. – Spacedman

+0

제안 해 주셔서 감사합니다. 그러나, 나는이 아이디어를 시도한 후에 같은 오류가 발생했습니다. 전체 경로 이름을 추가하고 파일 길이에 걸쳐 루프를 시도했습니다. 같은 오류가 나타납니다. – user2968058

+0

'mylist [i]'를 루프에서 인쇄하기 전에 인쇄하십시오. 내용을 확인하십시오. 그것이 실패한 파일을 확인하십시오 - 첫 번째? 두번째? 그런 다음 명령 줄에서'raster ("/ whatever/is/printed/out.tif")'를 실행 해보십시오. 파일이 손상되었을 수 있습니까? – Spacedman

답변

0

fn, fn[i] 대신 하나의 파일 이름 대신 파일 이름의 벡터를 써서 경고가 발생합니다. 즉, 첫 번째 요소 인 fn[1]에 네 번 쓸 수 있습니다. 오류 메시지의 출처는 저에게 불분명하지만 그 오류 메시지도 사라질 것으로 예상됩니다.

다음은 정리 된 버전입니다. 나는 R 감각 (list.files 문자 벡터를 반환)의 "목록"이 아니기 때문에 mylistff으로 변경했습니다. 따라서 mylist은 나쁜 이름입니다. 또한 *은 의미가 없었습니다. 그리고 substr(, 1,20)보다 일반적인 접근 방식을 사용하고 있습니다. 또한 writeRaster으로 별도의 단계를 거치지 않고 projectRaster에 파일에 쓰는 것이 더 효율적입니다. Raster로이 같은 범위와 해상도가있는 경우

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

고맙습니다 @ 로버트. 잘 작동한다. 고맙습니다. – user2968058

관련 문제