英文:
How to reach to specific layers in a SpatRaster from a layer with indices?
问题
我有一个包含多个值的光栅砖,我需要访问其中特定的图层。这些位置由单独的“索引光栅图层”给出。
希望这足够清楚,我考虑使用extract或values来实现,但不确定如何在这里实现。
英文:
I have a raster brick with multiple values, and I need to reach to specific layers within it. The positions are given by a single "index raster layer".
Hope this is clear enough, I have thought of using extract or values somehow, but am not sure how to implement that here.
library(terra)
a <- rast(ncol = 2, nrow = 2)
values(a) <- c(1,2,3,4)
names(a) <- "layer_one"
b <- rast(ncol = 2, nrow = 2)
values(b) <- c(5,6,7,8)
names(b) <- "layer_two"
c <- rast(ncol = 2, nrow = 2)
values(c) <- c(9,10,11,12)
names(c) <- "layer_three"
brick <- c(a,b,c)
layer_indices <- rast(ncol = 2, nrow = 2)
values(layer_indices) <- c(1,3,2,3)
names(layer_indices) <- "layer_indices"
# desired output
output <- rast(ncol = 2, nrow = 2)
values(output) <- c(1,10,7,12)
答案1
得分: 1
out <- selectRange(brick, layer_indices)
翻译为:
out <- selectRange(brick, layer_indices)
英文:
You can do
out <- selectRange(brick, layer_indices)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论