首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从光栅列表中计算平均值,并将其保存在不同的名称中。

从光栅列表中计算平均值,并将其保存在不同的名称中。
EN

Stack Overflow用户
提问于 2019-07-05 01:55:13
回答 1查看 877关注 0票数 0

我有一个栅格(.tif格式)的列表数年。这是一个来自landsat的16天NDVI,我想做一个每月NDVI (连续两个栅格的平均值),并将它保存在同一个或不同的目录中,作为一个月平均。

我列出了光栅光栅,并对其进行叠加,后来我使用stackApply计算平均值,但它将产生空光栅。我有23张一年的图片,我想把它平均下来,做12个月。这就是我的光栅文件的样子

代码语言:javascript
复制
 "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个空光栅,我还想保存栅格砖为单一子集每月光栅。

代码语言:javascript
复制
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

区块报价

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-05 08:34:30

不确定stackapply是如何工作的,但是类似这样的东西应该可以完成所需的工作。

代码语言:javascript
复制
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 stack
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56895662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档