【问题标题】:Extract Specific Cell Values out of a Raster Brick从光栅砖中提取特定的单元格值
【发布时间】:2020-10-28 11:28:46
【问题描述】:

我已经从两个光栅砖计算了风速和风向。

现在我想提取每个单元格的最大风速。我这样做了。现在的问题是我还需要那个特定单元格的风向。

我的代码如下所示:

speed <- brick(speed)
direction <- brick(dir)
maxWindspeed <- max(speed, na.rm = TRUE) # this gives me a raster with the Max Wind Speed Values

有人知道如何获取“maxWindspeed”层中最大风速单元格的特定风向单元格吗?

最好的问候 最大

【问题讨论】:

  • 你不能用maxWindspeed[maxWindspeed$...,]来选择风向吗?您可以通过在 r 上调用栅格并将其布局粘贴到此处来举一个栅格示例吗?

标签: r extract raster


【解决方案1】:

我不知道我是否正确理解了你想要做什么,但也许你可以试试这个:

library(raster)
#simulate data
r <- s <- t <- raster()
r[] <- sample(1:10,ncell(s),replace = T)
s[] <- sample(2:11,ncell(s),replace = T)
t[] <- sample(3:13,ncell(s),replace = T)

speed <- brick(r,s,t)
direction <- brick(r*s,s/t,t-r)
#max values across layers in speed
maxWindspeed <- max(speed, na.rm = TRUE)
plot(maxWindspeed)
plot(direction)
#boolean raster of where maxWindspeed is max
masker <- maxWindspeed==max(maxWindspeed[])
#brick of values in direction where maxWindspeed is max
plot(maxdirection <- mask(direction,masker,maskvalue=0))
#max values across layers in maxdirection
maxdirectioncell <- max(maxdirection,na.rm = T)
plot(maxdirectioncell)

【讨论】:

    【解决方案2】:

    对于raster,您可以使用which.maxstackSelect。使用 Elia 的(稍作修改)示例数据

    library(raster)
    r <- s <- t <- raster(ncol=10, nrow=10, crs="+proj=utm +zone=1 +datum=WGS84")
    r[] <- sample(1:10,ncell(s),replace = T)
    s[] <- sample(2:11,ncell(s),replace = T)
    t[] <- sample(3:13,ncell(s),replace = T)
    speed <- stack(r,s,t)
    direction <- stack(r*s,s/t,t-r)
    
    mxspeed <- which.max(speed)
    sdir <- stackSelect(direction, mxspeed)
    

    使用terra,您可以使用selectRange

    library(terra)
    spd <- rast(speed)
    dir <- rast(direction)
    
    mxspd <- which.max(spd)
    sd <- selectRange(dir, mxspd)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2017-02-06
      相关资源
      最近更新 更多