我有两个不同的栅格图层,具有不同的遮罩区域。它们重叠了几个像素,这是所期望的。所以我想将两个图层与重叠值“平滑”过渡合并。然而,对于 terra 包,我能得到的最好的就是 fun =“mean”。我构建了一个示例,它显示了我所做的事情:
library(terra)
r <- rast(ncols = 36, nrows = 18)
r[450] <- 1
r <- 1 * buffer(r, width = 5000000)
r[r == 0] <- NA
plot(r)
b <- rast(ncols = 36, nrows = 18)
b[345] <- 1
b <- 1 * buffer(b, width = 5000000)
b[b == 1] <- 5
b[b == 0] <- NA
plot(b)
m <- mosaic(r, b, fun = "mean")
plot(m)
现在的问题是,我希望 r 和 b 之间的过渡是平滑的,而不仅仅是平均的。所以应该有一个过渡,例如2、3、4 从 r 到 b 中的重叠像素。有人能帮我一下吗?我在网上没有发现任何类似的问题,说实话,我什至不知道在这种情况下该搜索什么。
谢谢您的问候。
library(cgwtools)
library(terra)
rb_intersect <- intersect(r, b)
rb_intersect
class : SpatRaster
dimensions : 18, 36, 1 (nrow, ncol, nlyr)
resolution : 10, 10 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
source(s) : memory
name : layer
min value : FALSE
max value : TRUE
which(rb_intersect[r] == TRUE)
[1] 2 3 4 5 8 9 10 11 12 16 17 18 19 20 21 26 27 28 29 30 31 32 38 39 40
[26] 41 42 43 50 51 52 53 54
这些系列看起来“行式”,两个类似 rle 的视图,cgwtools::seqle 给出了序列的 rle
rle(which(rb_intersect[r] == TRUE))
Run Length Encoding
lengths: int [1:33] 1 1 1 1 1 1 1 1 1 1 ...
values : int [1:33] 2 3 4 5 8 9 10 11 12 16 ...
cgwtools::seqle(which(rb_intersect[r] == TRUE))
Run Length Encoding
lengths: int [1:6] 4 5 6 7 6 5
values : int [1:6] 2 8 16 26 38 50
长度很有趣,因为它们建议从 2、3、4 过渡到 5,但逻辑是应该向上还是跨? 鉴于 50:55 既是最低值又最接近 1 值,也许这些就是您的 2(s)。 2、3、4、5 是如何分配的,向上还是横向,2 多于 3,可能会由您的数据指示。
考虑到这可能是一种完全愚蠢的方法,尽管我自己可能会采用这种方法,并且其他人可能会提供更好的指导。欢迎来到 Stackoverflow。