简体   繁体   English

从光栅块中提取特定的单元格值

[英]Extract Specific Cell Values out of a Raster Brick

I have calculated the Wind Speed and Wind Direction from two Raster Bricks.我已经从两个光栅砖计算了风速和风向。

Now I want to extract the Maximum Wind Speed for each cell.现在我想提取每个单元格的最大风速。 I did that.我就是这么做的。 Now the problem is that I also need the Wind Direction for that specific cell.现在的问题是我还需要该特定单元格的风向。

my code looks like this:我的代码是这样的:

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

Anyone has an idea how to get the specific Wind direction cell for the Max Wind Speed cell in the "maxWindspeed" layer?任何人都知道如何为“maxWindspeed”图层中的最大风速单元格获取特定的风向单元格?

Best Regards Max最好的问候马克斯

I don't know if I have understood correctly what you want to do but maybe you can try this:我不知道我是否正确理解了你想要做什么,但也许你可以试试这个:

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)

With raster you can use which.max and stackSelect .对于raster您可以使用which.maxstackSelect Using Elia's (slightly modified) example data使用 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)

With terra you can use selectRange使用terra您可以使用selectRange

library(terra)
spd <- rast(speed)
dir <- rast(direction)

mxspd <- which.max(spd)
sd <- selectRange(dir, mxspd)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM