2014-01-07 58 views
0

我最近開始與R一起工作,所以這個問題可能有一個簡單的解決方案。 我有一些.tif衛星圖像來自不同的場景。我可以用它創建一個測試柵格磚,但由於文件數量巨大,需要自動完成該過程。因此,我一直在嘗試創建一個函數來讀取.tif文件列表並輸出一個柵格列表。 您可以在這裏找到下面的代碼,我一直在使用:R將光柵功能應用於字符列表

# Description: Prepare a raster brick with ordered acquisitions 
# from all the scenes of the study area 

library(raster) 
library(rgdal) 
library(sp) 
library(rtiff) 

rm(list = ls()) 

setwd=getwd() 

# If you want to download the .tif files of the 2 scenes from dropbox: 
dl_from_dropbox <- function(x, key) { 
    require(RCurl) 
    bin <- getBinaryURL(paste0("https://dl.dropboxusercontent.com/s/", key, "/", x), 
         ssl.verifypeer = FALSE) 
    con <- file(x, open = "wb") 
    writeBin(bin, con) 
    close(con) 
    message(noquote(paste(x, "read into", getwd())))       
} 

dl_from_dropbox("lndsr.LT52210611985245CUB00-vi.NDVI.tif", "qb1bap9rghwivwy") 
dl_from_dropbox("lndsr.LT52210611985309CUB00-vi.NDVI.tif", "sbhcffotirwnnc6") 
dl_from_dropbox("lndsr.LT52210611987283CUB00-vi.NDVI.tif", "2zrkoo00ngigfzm") 

dl_from_dropbox("lndsr.LT42240631992198XXX02-vi.NDVI.tif", "gx0ctxn2mca3u5v") 
dl_from_dropbox("lndsr.LT42240631992214XXX02-vi.NDVI.tif", "pqnjw2dpz9beeo5") 
dl_from_dropbox("lndsr.LT52240631986157CUB02-vi.NDVI.tif", "rrka10yaktv8la8") 


# 1- Create a list of .tif files with names ordered chronologically (for time series analysis later on) 
pathdir= # change 
# List all the images from any scene in that folder and 
# make a dataframe with a column for the date 
a <- list.files(path=pathdir,pattern="lndsr.LT", all.files=FALSE,full.names=FALSE) 
a1 <- as.data.frame(a, row.names=NULL, optional=FALSE, stringsAsFactors=FALSE) # class(a1$a) # character 
# Create date column with julean date and order it in ascending order 
a1$date <- substr(a1$a, 16, 22) # class(a1$date) = character 
a1 <- a1[order(a1$date),] 
# Keep only the column with the name of the scene 
a1 <- subset(a1, select=1) # class(a1$a): character 
# retrieve an ordered list from the dataframe 
ord_dates <- as.list(as.data.frame(t(a1$a))) # length(ord_dates): 4 (correct) 
# class(odd_dates) # list 

# 2- Create rasters from elements of a list 
for (i in 1:(length(ord_dates))){ 
    # Point to each individual .tif file 
    tif_file <- ord_dates[i] # Problem: accesses only the first item of ord_dates 
    # Make a raster out of it 
    r <- raster(tif_file) # we cant use here a list as an input. Gives error: 
     # Error in .local(x, ...) : list has no "x" 
    # Give it a standardised name (r1,r2,r3, etc) 
    name <- paste("r", 1:length(ord_dates),sep = "") 
    # Write the raster to file 
    writeRaster (r , filename = name,format = "GTiff", overwrite =T) 
} 

我也曾嘗試()沒有多少成功lapply使用。

r = lapply(ord_dates, raster) 

你能給我一個關於遵循什麼概念的建議嗎?我猜我應該使用矩陣,但我不太明白這裏是他們的優點或者他們需要什麼步驟。

任何幫助真的很感激! 在此先感謝

回答

0

假設ord_dates是文件名(具有完整路徑,或在你getwd())的列表,你可以申請一個(任意)函數來使用lapply這個名單。不幸的是,我沒有測試過。

convertAllToRaster <- function(tif_file) { 
    r <- raster(tif_file) 
    # Give it a standardised name (r1,r2,r3, etc) 
    name <- paste("r", 1:length(ord_dates),sep = "") 
    # Write the raster to file 
    writeRaster (r , filename = name,format = "GTiff", overwrite =T) 
    message("Eeee, maybe it was written successfully.") 
} 

lapply(ord_dates, FUN = convertAllToRaster) 
+0

謝謝你的幫助羅馬! 我試過了,使用lapply()時出現了一個熟悉的錯誤。它返回: (函數(類,fdef,mtable)中的錯誤: 無法找到函數'raster'的簽名''factor''的繼承方法 我不明白簽名因子的創建位置,因爲我想早些時候通過確保列表是類字符,而不是因子來修復它。 – user3127517

+0

您的列表元素可能是因素。在任何情況下,在行的前面插入一個「browser()」(或在一個函數的開始),然後運行你的代碼,你應該探索函數的環境,手動執行代碼並以這種方式追蹤你的bug。 –

0

解決因素和名稱的問題後,這是我工作的代碼。我在你提出的函數Roman中加入了一個for循環。謝謝你的親切幫助!

convertAllToRaster <- function(ord_dates) { 
    for (i in 1:(length(ord_dates))){ 
    tif_file <- ord_dates[i] 
    r <- raster(tif_file) 
    # Keep the original name 
    name <- paste(tif_file, ".grd", sep ="") 
    # Write the raster to file 
    writeRaster (r , filename = name,format = "raster", overwrite =T) # in .grd format 
    } 
} 

lapply(ord_dates, FUN = convertAllToRaster)