如何从具有索引的图层中访问SpatRaster中的特定图层?

huangapple go评论62阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年4月7日 02:40:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定