2014-01-07 33 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)