基于栅格层网格单元数差异的子集R栅格堆栈
我想创建光栅堆栈的子集,当上一层和下一层之间的差异都是基于栅格层网格单元数差异的子集R栅格堆栈,r,raster,r-raster,R,Raster,R Raster,我想创建光栅堆栈的子集,当上一层和下一层之间的差异都是NA时,将它们作为新堆栈写入。也就是说,从第1层开始,我想创建光栅堆栈的子集,直到上一层和下一层之间没有重叠像素(即,两层之间的差异都是NA),所以我想是;从第1层开始,保留上一层和下一层之间至少有1个公共像素的所有层,将它们作为1堆栈写入,然后移动到下一层。下面是循环的示例数据和未成功的示例。在本例中,我希望保留层1:8,命名并写入它们,然后从层9重新开始,依此类推 r <- raster(ncol=5, nrow=5) set.se
NA
时,将它们作为新堆栈写入。也就是说,从第1层开始,我想创建光栅堆栈的子集,直到上一层和下一层之间没有重叠像素(即,两层之间的差异都是NA
),所以我想是;从第1层开始,保留上一层和下一层之间至少有1个公共像素的所有层,将它们作为1堆栈写入,然后移动到下一层。下面是循环的示例数据和未成功的示例。在本例中,我希望保留层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
r像这样的东西可能适合你
您的示例数据
library(raster)
r <- raster(ncol=5, nrow=5)
set.seed(0)
s <- stack(lapply(1:8, function(i) setValues(r, runif(ncell(r)))))
s1 <- extend(s,c(-500,100,-400,100))
s2 <- stack(lapply(1:2, function(i) setValues(r, runif(ncell(r)))))
s1e <- extend(s2,c(-500,100,-400,100))
values(s1e) <- NA
r_stk <- stack(s1,s1e)
库(光栅)
r感谢Robert的解决方案,它几乎像预期的那样工作,只是在每个rasterstack集合的末尾给了我一个额外的层。在上面的示例中,我只希望第一个堆栈集中的层为1:8,但解决方案中有9层。为什么第9层设置了第一个堆栈,而我希望它只有8层,因为第9层具有所有NA
值。我包括了导致所有单元格都NA
的层。我更新了答案,让它做什么,你是后真棒的作品像一个魅力。谢谢。这是一个不同的问题谢谢你,我发布了一个新的问题,其中包括我想要达到的目标:
out <- lst <- list()
nc <- ncell(r_stk)
for (i in 1:nlayers(r_stk)) {
if (i==1) {
j <- 1
s <- r_stk[[i]]
} else {
s <- s + r_stk[[i]]
}
if (freq(s, value=NA) == nc) {
ii <- max(j, i-1)
out <- c(out, r_stk[[j:ii]])
s <- r_stk[[i]]
j <- i
}
}
out <- c(out, r_stk[[j:i]])
out
#[[1]]
#class : RasterStack
#dimensions : 14, 9, 126, 8 (nrow, ncol, ncell, nlayers)
#resolution : 72, 36 (x, y)
#extent : -468, 180, -414, 90 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs
#names : layer.1.1, layer.2.1, layer.3, layer.4, layer.5, layer.6, layer.7, layer.8
#min values : 0.06178627, 0.01339033, 0.07067905, 0.05893438, 0.01307758, 0.03554058, 0.06380848, 0.10087313
#max values : 0.9919061, 0.8696908, 0.9128759, 0.9606180, 0.9926841, 0.9850952, 0.8950941, 0.9437248
#
#[[2]]
#class : RasterLayer
#dimensions : 14, 9, 126 (nrow, ncol, ncell)
#resolution : 72, 36 (x, y)
#extent : -468, 180, -414, 90 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs
#source : memory
#names : layer.1.2
#values : NA, NA (min, max)
#
#[[3]]
#class : RasterLayer
#dimensions : 14, 9, 126 (nrow, ncol, ncell)
#resolution : 72, 36 (x, y)
#extent : -468, 180, -414, 90 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs
#source : memory
#names : layer.2.2
#values : NA, NA (min, max)