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

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

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.

  1. library(terra)
  2. a <- rast(ncol = 2, nrow = 2)
  3. values(a) <- c(1,2,3,4)
  4. names(a) <- "layer_one"
  5. b <- rast(ncol = 2, nrow = 2)
  6. values(b) <- c(5,6,7,8)
  7. names(b) <- "layer_two"
  8. c <- rast(ncol = 2, nrow = 2)
  9. values(c) <- c(9,10,11,12)
  10. names(c) <- "layer_three"
  11. brick <- c(a,b,c)
  12. layer_indices <- rast(ncol = 2, nrow = 2)
  13. values(layer_indices) <- c(1,3,2,3)
  14. names(layer_indices) <- "layer_indices"
  15. # desired output
  16. output <- rast(ncol = 2, nrow = 2)
  17. values(output) <- c(1,10,7,12)

答案1

得分: 1

  1. out <- selectRange(brick, layer_indices)

翻译为:

  1. out <- selectRange(brick, layer_indices)
英文:

You can do

  1. 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:

确定