具有多个栅格堆栈的 ClusterR

ClusterR with multiple raster stacks

这是栅格库提供的使用 clusterR 和叠加函数的示例:

library(raster)
beginCluster()
r <- raster()
r[] <- 1:ncell(r)
s <- stack(r, r*2, r*3)
f2 <- function(d,e,f) (d + e) / (f * param)
param <- 122
ov <- clusterR(s, overlay, args=list(fun=f2), export='param')

如果我有多个光栅堆栈,我想知道如何运行该功能:

s <- stack(r, r*2, r*3)
s2 <- stack(r*2, r*3, r*4)
s3 <- stack(r*3, r*4, r*5)

我想要这样的东西(d,e,f 函数 f2s, s2s3 中的每一层:

ov <- clusterR(s,s2,s3, overlay, args=list(fun=f2), export='param')

首先,我会在您的堆栈中创建一个虚拟栅格图层,其中包含 param 值。因此,操作可以向量化:

p <- 122
rp <- r
rp[] <- p
s <- stack(s, rp)
s2 <- stack(s2, rp)
s3 <- stack(s3, rp)

然后你像这样改变你的函数:

f2 <- function(x) (x[[1]] + x[[2]]) / (x[[3]] * x[[4]])

因此,正确引用了单个堆栈的层 x。第4层是param值(这里是p

然后创建层堆栈列表:

stackList <- list(s, s2, s3)

然后你 lapply clusterR 函数。

ov <- lapply(stackList, function(x){clusterR(x, fun = f2, progress = "text")})

ov 将是您的叠加图层列表。