我有一个栅格(.tif格式)的列表数年。这是一个来自landsat的16天NDVI,我想做一个每月NDVI (连续两个栅格的平均值),并将它保存在同一个或不同的目录中,作为一个月平均。
我列出了光栅光栅,并对其进行叠加,后来我使用stackApply计算平均值,但它将产生空光栅。我有23张一年的图片,我想把它平均下来,做12个月。这就是我的光栅文件的样子
"landsatNDVISC05SLC2000001.tif" "landsatNDVISC05SLC2000017.tif"
"landsatNDVISC05SLC2000033.tif" "landsatNDVISC05SLC2000049.tif"
"landsatNDVISC05SLC2000065.tif" "landsatNDVISC05SLC2000081.tif"
"landsatNDVISC05SLC2000097.tif" "landsatNDVISC05SLC2000113.tif"
"landsatNDVISC05SLC2000129.tif" "landsatNDVISC05SLC2000145.tif"
"landsatNDVISC05SLC2000161.tif" "landsatNDVISC05SLC2000177.tif"
"landsatNDVISC05SLC2000193.tif" "landsatNDVISC05SLC2000209.tif"
"landsatNDVISC05SLC2000225.tif" "landsatNDVISC05SLC2000241.tif"
"landsatNDVISC05SLC2000257.tif" "landsatNDVISC05SLC2000273.tif"
"landsatNDVISC05SLC2000289.tif" "landsatNDVISC05SLC2000305.tif"
"landsatNDVISC05SLC2000321.tif" "landsatNDVISC05SLC2000337.tif"
"landsatNDVISC05SLC2000353.tif此代码工作,但将产生超过12个空光栅,我还想保存栅格砖为单一子集每月光栅。
library(raster)
lrast<-list.files("G:/LANDSAT-NDVI/testAverage")
layers<-paste("landsatNDVISC05SLC2000", seq(from=001, to=353,by=16))
stak<-stack(lrast)
raster<-stackApply(stak, layers, fun = mean)我想把landsatNDVISC05SLC2000001.tif和landsatNDVISC05SLC2000017.tif作为landsatNDVISC05SLC2000M1.tif的月平均值。同样,33,49并且由于我只有23个光栅,我想保留landsatNDVISC05SLC2000353.tif作为landsatNDVISC05SLC2000M12.tif
区块报价
发布于 2019-07-05 08:34:30
不确定stackapply是如何工作的,但是类似这样的东西应该可以完成所需的工作。
library(raster)
files <- list.files(path = "...", full.names = T, pattern = ".tif")
stk <- stack()
for (i in files){
print(i)
as <- raster(files[i])
stk <- addLayer(stk, as)
}
jday <-c("landsatNDVISC05SLC2000017.tif","landsatNDVISC05SLC2000033.tif",
"landsatNDVISC05SLC2000049.tif","landsatNDVISC05SLC2000065.tif","landsatNDVISC05SLC2000081.tif",
"landsatNDVISC05SLC2000097.tif","landsatNDVISC05SLC2000113.tif","landsatNDVISC05SLC2000129.tif",
"landsatNDVISC05SLC2000145.tif","landsatNDVISC05SLC2000161.tif","landsatNDVISC05SLC2000177.tif",
"landsatNDVISC05SLC2000193.tif","landsatNDVISC05SLC2000209.tif","landsatNDVISC05SLC2000225.tif",
"landsatNDVISC05SLC2000241.tif","landsatNDVISC05SLC2000257.tif","landsatNDVISC05SLC2000273.tif",
"landsatNDVISC05SLC2000289.tif","landsatNDVISC05SLC2000305.tif","landsatNDVISC05SLC2000321.tif",
"landsatNDVISC05SLC2000337.tif","landsatNDVISC05SLC2000353.tif")
jday <- as.numeric(substr(jday, 24, 25)) #substract the julien days (which I think these number represent before .tif; or you can substract the names from the 'files' vector)
dates <- as.Date(jday, origin=as.Date("2000-01-01")) # create a Date vector
stk <- setZ(stk, dates) # assign the date vector to the raster stack
raster <- zApply(stk, by = format(dates,"%Y-%m"), fun = mean, na.rm = T) # create the monthly stackhttps://stackoverflow.com/questions/56895662
复制相似问题