根据另一个栅格的像素值从堆栈中的某个图层中提取值
Extract values from a certain layer in a stack based on pixel value of another raster
假设我有一个栅格图层 r0
,其值范围为 1: nlayers s
。我需要从波段 s
中提取与图层 r0
中的像素值相匹配的值。
例如,如果 r0
中的像素值是 2
我想要堆叠层 band 2
中完全相同的像素值 s
等等。因此,最终输出将只有一层表示基于层 r0
的层 s
的值。希望我能把问题解释清楚。
library(raster)
r <- raster(nrow=5, ncol=5)
r0 <- setValues(r, round(runif(25,min = 1, max = 10)), 0)
r1 <- setValues(r, runif(ncell(r)))
r2 <- setValues(r, runif(ncell(r)))
r3 <- setValues(r, runif(ncell(r)))
r4 <- setValues(r, runif(ncell(r)))
r5 <- setValues(r, runif(ncell(r)))
r6 <- setValues(r, runif(ncell(r)))
r7 <- setValues(r, runif(ncell(r)))
r8 <- setValues(r, runif(ncell(r)))
r9 <- setValues(r, runif(ncell(r)))
r10 <- setValues(r, runif(ncell(r)))
s <- stack(r1, r2, r3, r4,r5, r6,r7,r8,r9,r10)
见terra::selectRange
示例数据
library(terra)
r <- rast(ncols=10, nrows=10)
values(r) <- 1
s <- c(r, r+2, r+5)
s <- c(s, s)
set.seed(1)
values(r) <- sample(3, ncell(r), replace=TRUE)
解决方案
x <- selectRange(s, r)
假设我有一个栅格图层 r0
,其值范围为 1: nlayers s
。我需要从波段 s
中提取与图层 r0
中的像素值相匹配的值。
例如,如果 r0
中的像素值是 2
我想要堆叠层 band 2
中完全相同的像素值 s
等等。因此,最终输出将只有一层表示基于层 r0
的层 s
的值。希望我能把问题解释清楚。
library(raster)
r <- raster(nrow=5, ncol=5)
r0 <- setValues(r, round(runif(25,min = 1, max = 10)), 0)
r1 <- setValues(r, runif(ncell(r)))
r2 <- setValues(r, runif(ncell(r)))
r3 <- setValues(r, runif(ncell(r)))
r4 <- setValues(r, runif(ncell(r)))
r5 <- setValues(r, runif(ncell(r)))
r6 <- setValues(r, runif(ncell(r)))
r7 <- setValues(r, runif(ncell(r)))
r8 <- setValues(r, runif(ncell(r)))
r9 <- setValues(r, runif(ncell(r)))
r10 <- setValues(r, runif(ncell(r)))
s <- stack(r1, r2, r3, r4,r5, r6,r7,r8,r9,r10)
见terra::selectRange
示例数据
library(terra)
r <- rast(ncols=10, nrows=10)
values(r) <- 1
s <- c(r, r+2, r+5)
s <- c(s, s)
set.seed(1)
values(r) <- sample(3, ncell(r), replace=TRUE)
解决方案
x <- selectRange(s, r)