有条件地计算max(raster)与栅格堆栈的每个栅格图层之间的差 [英] Conditionally calculating the difference between max(raster) and each raster layer of raster stack

查看:97
本文介绍了有条件地计算max(raster)与栅格堆栈的每个栅格图层之间的差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为r.lst的栅格堆栈列表.我需要计算max(r.lst[[i]])(堆叠的栅格)与该栅格堆栈的每个栅格图层之间的差.但是,我想通过考虑which.max(r.lst[[i]])对每个像素有条件地执行此操作.这样,每个单元格都会从其上一个最大值中扣除,而不是下一个.请参见以下示例:

I have a list of raster stacks named r.lst. I need to calculate the difference between max(r.lst[[i]]) (stacked rasters) and every raster layer of that raster stack. However, I want to do this conditionally for each pixel by considering which.max(r.lst[[i]]). So that each cell in deducted from its previous max not the next. Please see below example:

# example data-------------------------------------------
set.seed(123)
#our list of rasters
r.lst <- as.list(1:3)
# setting up list pf raster stacks
r1 <- raster(nrows = 1, ncols = 1, res = 0.5, xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5, vals = runif(36, 1, 5))
r.lst[[1]] <- stack(lapply(1:8, function(i) setValues(r1,runif(ncell(r1)))))
r.lst[[2]] <- stack(lapply(1:6, function(i) setValues(r1,runif(ncell(r1)))))
r.lst[[3]] <- stack(lapply(1:7, function(i) setValues(r1,runif(ncell(r1)))))

我可以很容易地计算出r.lst[[i]]的每个三个栅格堆栈中每个图层的差异,如下所示:

I can easily calculate the difference for every single layer within each three raster stack of r.lst[[i]] as below:

# compute the difference between max and min
x1 <- list()
x2 <- list()
xx <- list()

for (i in 1:length(r.lst)) {
  x1[[i]] <- max(r.lst[[i]])
  x2[[i]] <- which.max(r.lst[[i]])
  x <- list()
  for (j in 1:nlayers(r.lst[[i]])){
    x[[j]] <- x1[[i]]-r.lst[[i]][[j]]
  }
  xx[[i]] <- stack(x)
  print(i)
}

par(mfrow=c(1,4), oma=c(0,0,0,1), mai=c(0.7,0.4,0.7,0.4))
plot(r.lst[[1]][[1]], main="Input.Raster")  #Input from stack
plot(x1[[1]], main="Max")                   #Max of stack
plot(x2[[1]], main="Max.Index")             #Index of Max value in raster stack
plot(xx[[1]][[1]], main="Results")          #OUTPUT (not what I wanted)

但这不是我想要的.我需要在每轮计算中考虑which.max(r.lst[[i]]),以便如果正在考虑的图层的索引大于which.max(r.lst[[i]])的索引,我们将计算差异,如果不是,我需要考虑较早的最大图层来计算差异.只需比较每个栅格图层的计算结果就可以了,与其之前的最大值相比,它是无效的.

But this is not what I want. I need to consider which.max(r.lst[[i]]) in each round of calculation so that if the index of a layer under consideration is bigger than the index of which.max(r.lst[[i]]) we calculate the difference, if not I need to calculate the difference considering earlier max layer. Simply the calculation for every raster layer is only valid compared to its earlier max not the next.

推荐答案

我相信您正在寻找类似以下内容的东西.

I believe you are looking for something like the below.

示例数据

library(raster)
set.seed(123)
r <- raster(res = 0.5, xmn = -1.5, xmx = 1.5, ymn = -1.5, ymx = 1.5)
r.lst <- list()
r.lst[[1]] <- stack(lapply(1:8, function(i) setValues(r, runif(ncell(r)))))
r.lst[[2]] <- stack(lapply(1:6, function(i) setValues(r, runif(ncell(r)))))
r.lst[[3]] <- stack(lapply(1:7, function(i) setValues(r, runif(ncell(r)))))

解决方案

xx <- list()
for (i in 1:length(r.lst)) {
    xx[[i]] <- cummax( r.lst[[i]] ) -  r.lst[[i]]
}

也就是说,您可以使用cummax来获取先前值的最大值(包括当前值)

That is, you can use cummax to get the max value of the prior values (including the current value)

x <- c(1:3, 3:1, 4, 3)
cummax(x)
#[1] 1 2 3 3 3 3 4 4
x - cummax(x)
#[1]  0  0  0  0 -1 -2  0 -1

这篇关于有条件地计算max(raster)与栅格堆栈的每个栅格图层之间的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆