R将栅格函数应用于字符列表 [英] R apply raster function to a list of characters
问题描述
我有一些来自不同场景的.tif卫星图像。我可以用它创建一个测试栅格砖,但由于文件数量巨大,需要自动完成该过程。因此,我一直在尝试创建一个函数来读取.tif文件列表并输出一个栅格列表。
您可以在下面找到我已经使用的代码:
#描述:准备一个有序采集的栅格砖
#从学习区域的所有场景中
库(栅格)
库(rgdal)
库(sp)
库(rtiff)
rm(list = ls())
setwd = getwd()
#如果你想下载2个场景的.tif文件from dropbox:
dl_from_dropbox< - function(x,key){
require(RCurl)
bin< - getBinaryURL(paste0(https://dl.dropboxusercontent.com/s / bin,/,键,/,x),
ssl.verifypeer = FALSE)
con< - file(x,open =wb)
writeBin
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.LT5221061 1987283CUB00-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-创建一个.tif文件的名称按时间顺序排列(用于时间序列分析)
pathdir =#change
#列出该文件夹中任何场景的所有图像,以及
#将数据帧与日期的列
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
#创建包含julean日期的日期列并以升序排列
a1 $ date< - substr(a1 $ a,16,22)#class(a1 $ date)=字符
a1 < - a1 [order(a1 $ date),]
#仅保留具有场景名称的列
a1< - subset(a1,select = 1)#class(a1 $ a):character
#从数据框中检索一个有序列表
ord_dates< - as.list(as .data.frame(t(a1 $ a)))#length(ord_dates):4(correct)
#class(odd_dates)#list
#2-根据元素创建栅格列表
for(i in 1:(length(ord_dates))){
#指向每个单独的.tif文件
tif_file< - ord_dates [i]#问题:仅访问ord_dates的第一项
#使光栅出来
r< - 光栅(tif_file)#我们不能在这里使用列表作为输入。给出错误:
#.local(x,...)中的错误:list没有x
#给它一个标准名称(r1,r2,r3等)
name < - paste(r,1:length(ord_dates),sep =)
#将光栅写入文件
writeRaster(r,filename = name,format =GTiff,覆盖= T)
}
我也尝试过使用lapply(),但没有取得太大的成功。
r = lapply(ord_dates,raster)
你可以给我一个关于遵循什么概念的建议吗?我猜我应该使用矩阵,但我不明白哪里是他们的优点,或者他们需要什么步骤。
任何帮助真的很感谢!
在此先感谢
假设 ord_dates
的文件名(包含完整路径或位于 getwd()
)中,您可以使用 lapply
。
convertAllToRaster< - function(tif_file){
r< - raster (tif_file)
#给它一个标准名称(r1,r2,r3等)
名称< - paste(r,1:length(ord_dates),sep =)
#将光栅写入文件
writeRaster(r,filename = name,format =GTiff,overwrite = T)
消息(Eeee,也许它写成功了。)
}
lapply(ord_dates,FUN = convertAllToRaster)
I started recently to work with R so this question has probably a simple solution. I have some .tif satellite images from different scenes. I can create a test raster brick with it but the process needs to be automatised because of the huge amount of files. Therefore I have been trying to create a function to read the list of .tif files and to output a list of rasters. You can find here below the code I have been using:
# 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 )
}
I have also tried to use lapply() without much success.
r = lapply(ord_dates, raster)
Can you give me an advice on what concept to follow? I am guessing I should be using matrices but I don't really understand which are their advantages here or in what step they are required.
Any help is really appreciated! Thanks in advance
Assuming ord_dates
is a list of file names (that have full path or are in your getwd()
), you can apply a (any) function to this list using lapply
. I haven't tested this, unfortunately.
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)
这篇关于R将栅格函数应用于字符列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!