【发布时间】:2021-02-22 18:30:20
【问题描述】:
当上一层和下一层之间的差异都是NA 时,我想创建光栅堆栈的子集并将它们写为新堆栈。即,从第 1 层开始,我想创建光栅堆栈的子集,直到前一层和下一层之间没有重叠像素(即,两层之间的差异都是NA)所以我想要的是;从第 1 层开始,保留前一层和下一层之间至少有 1 个公共像素的所有层,将它们写为 1 堆栈,然后移动到下一层。以下是示例数据和不成功的 for 循环。在这个例子中,我想保留 1:8 层,命名并编写它们,然后从第 9 层重新开始,依此类推。
r <- raster(ncol=5, nrow=5)
set.seed(0)
#create raster layers with some values
s <- stack(lapply(1:8, function(i) setValues(r, runif(ncell(r)))))
s1<-extend(s,c(-500,100,-400,100))
#to recreate the condition I am looking for, create 2 layers with `NA` vlaues
s2 <- stack(lapply(1:2, function(i) setValues(r, runif(ncell(r)))))
s1e<-extend(s2,c(-500,100,-400,100))
s1e[]<-NA
#Stack the layers
r_stk<-stack(s1,s1e)
plot(r_stk)
#here is the sample code showing what i am expecting here but could not get
required_rst_lst<-list() # sample list of raster layers with overlapping pixels I am hoping to create
for ( i in 1: nlayers(r_stk))
# i<-1
lr1<-subset(r_stk,i)
lr1
lr2<-subset(r_stk,i+1)
lr2
diff_lr<-lr1-lr2
plot(diff_lr)
if ((sum(!is.na(getValues(diff_lr)))) ==0)) #??
required_rst_lst[[i]] #?? I want layers 1: 8 in this list
#because the difference in these layers in not NA
【问题讨论】: