我有一个terra::rast
图层n
。我想只使用包提取某些层的值terra
(我更喜欢这个包的答案而不是raster
)。看来我不明白如何layer
在函数中使用参数terra::extract
。这是一个最小(几乎)工作示例:
rs1 = terra::rast(nrows = 3, ncols = 4)
terra::values(rs1) = 4:15
rs2 = terra::rast(nrows = 3, ncols = 4)
terra::values(rs2) = 25:36
rs3 = terra::rast(nrows = 3, ncols = 4)
terra::values(rs3) = 97:108
rs4 = terra::rast(nrows = 3, ncols = 4)
terra::values(rs4) = 51:62
rs = c(rs1, rs2, rs3, rs4)
names(rs) = paste0("layer", 1:nlyr(rs))
coords = matrix(data = c(0, 5, 10, -54, 0, 12), byrow = FALSE, ncol = 2)
colnames(coords) = c("x", "y")
terra::extract(x = rs, y = coords)[, c("layer1", "layer4")] # Works but not elegant in terms of memory
terra::extract(x = rs, y = coords, layer = c("layer1", "layer4")) # Does not work
terra::extract(x = rs, y = coords, layer = c(1, 4)) # Does not work
此代码提供以下错误:
Error in .local(x, y, ...) : length(layer) == nrow(y) is not TRUE
我不明白为什么层数应该等于y
. 我想它应该是最大的层数x
。如何专门提取第 1 层和第 4 层的给定坐标处的值(或任何其他层,这只是一个示例)?