我有一个文件夹,里面装有geotiffs,我想加载栅格并从中提取值。 .tif是01APR2010.tif,02APR2010.tif,01MAR2010.tif等。我想按时间顺序(按天)阅读文件,这样我就可以保持组织的价值观。我有2000年至2011年的01MAR到30AUG。
我有一个名为lsa,usa和alp的SpatialPointsDataFrame,其坐标是我想要的相应值。 我已经阅读了这些文件(感谢SO上的其他问题/答案)
library(raster)
library(plyr)
year=2010
site=list(lsa,usa,alp)
rcn.extract.swe<-function(r,site){
projection(r)=CRS('+proj=longlat +datum=NAD83')
return(mean(extract(r,coordinates(site)[,1:2])))
}
temp_geotiffs=getwd()#location of geotiffs
filenames <- list.files(temp_geotiffs, pattern="*.tif", full.names=TRUE)
ldf <- llply(filenames, raster)
res <- outer(ldf,site,Vectorize(rcn.extract.swe)) #this gives a matrix
也许有更好的方法?我也尝试mapply
,但outer
执行所有组合
这里有一些要使用的变量:
lsa=structure(c(-105.593648807562, -105.593721469436, -105.593667442221,
-105.593755471559, -105.593796239505, -105.59393253876, -105.593864787348,
-105.594020998966, -105.593758049064, 40.0538437274748, 40.0538836912761,
40.0540185900867, 40.0540081923506, 40.0541962757639, 40.0542507971756,
40.0544078670555, 40.054427734347, 40.0540699483592), .Dim = c(9L,
2L), .Dimnames = list(NULL, c("coords.x1", "coords.x2")))
usa=structure(c(-105.580580448185, -105.580661572731, -105.58071127033,
-105.580955908821, -105.580938632289, 40.0514730866613, 40.0513509958902,
40.0512184221927, 40.0513010575266, 40.0514244681361, 3437.769,
3434.92, 3431.876, 3434.075, 3436.987), .Dim = c(5L, 3L), .Dimnames = list(
NULL, c("coords.x1", "coords.x2", "coords.x3")))
alp=structure(c(-105.593648807562, -105.593721469436, -105.593667442221,
-105.593755471559, -105.593796239505, -105.59393253876, -105.593864787348,
-105.594020998966, -105.593758049064, 40.0538437274748, 40.0538836912761,
40.0540185900867, 40.0540081923506, 40.0541962757639, 40.0542507971756,
40.0544078670555, 40.054427734347, 40.0540699483592, 3538.319,
3539.285, 3541.733, 3541.962, 3545.753, 3547.899, 3550.574, 3552.051,
0), .Dim = c(9L, 3L), .Dimnames = list(NULL, c("coords.x1", "coords.x2",
"coords.x3")))
r=raster(matrix(runif(100,0,3),nrow=20),xmn=-112.25,xmx=-104.125,ymn=33,ymx=43.75)
writeRaster(r,'01APR2010.tif')
writeRaster(r,'02APR2010.tif')
writeRaster(r,'03APR2010.tif')
writeRaster(r,'04APR2010.tif')
writeRaster(r,'01MAR2010.tif')
writeRaster(r,'02MAR2010.tif')
writeRaster(r,'03MAR2010.tif')
感谢您的帮助!
答案 0 :(得分:0)
目前尚不清楚为什么要订购数据,因为外部会执行所有组合。 您可以通过从中提取日期部分并将其转换为正确的日期来订购文件名。例如:
full.filenames <- list.files(pattern="*.tif", full.names=TRUE)
filenames <- list.files(pattern="*.tif")
full.filenames[order(as.Date(tolower(gsub('[.].*','',filenames)),
'%d%b%Y'))]
"./01MAR2010.tif" "./02MAR2010.tif" "./03MAR2010.tif" "./01APR2010.tif"
[5] "./02APR2010.tif" "./03APR2010.tif" "./04APR2010.tif"