我试图弄清楚如何计算不同水位的河流横截面积水面积。
对于横截面,我在5米宽的河流上每25厘米有一个深度,该区域可以根据一个很好回答的上一个问题来计算 Calculate area of cross section for varying height
x_profile <- seq(0, 500, 25)
y_profile <- c(50, 73, 64, 59, 60, 64, 82, 78, 79, 76, 72,
68, 63, 65, 62, 61, 56, 50, 44, 39, 25)
library(sf)
#Create matrix with coordinates
m <- matrix(c(0, x_profile, 500, 0, 0, -y_profile, 0, 0),
byrow = FALSE, ncol = 2)
#Create a polygon
poly <- st_polygon(list(m))
# Calcualte the area
st_area(poly)
但是这个横截面只是部分地充满了水,现在我试图计算出充满水的横截面。
水开始从最深部分填充横截面,然后深度变化,例如:
water_level<-c(40, 38, 25, 33, 40, 42, 50, 39)
有没有人对如何在r中做到这一点有任何想法?提前谢谢。
答案 0 :(得分:5)
此函数计算轮廓与轮廓底部指定深度处的直线的交点。它有点多余,因为它还需要x和y轮廓值,理论上可以从profile
中提取:
filler <- function(depth, profile, xprof, yprof, xdelta=100, ydelta=100){
d = -(max(yprof))+depth
xr = range(xprof)
yr = range(-yprof)
xdelta = 100
xc = xr[c(1,2,2,1,1)] + c(-xdelta, xdelta, xdelta, -xdelta, -xdelta)
yc = c(d, d, min(yr)-ydelta, min(yr)-ydelta, d)
water = st_polygon(list(cbind(xc,yc)))
st_intersection(profile, water)
}
所以在使用中:
> plot(poly)
> plot(filler(40, poly, x_profile, y_profile), add=TRUE, col="green")
> plot(filler(30, poly, x_profile, y_profile), add=TRUE, col="red")
> plot(filler(15, poly, x_profile, y_profile), add=TRUE, col="blue")
请注意,较深的区域会略微覆盖第一个绿色区域。另请注意蓝色区域如何分为两部分。您可以使用st_area
得到横截面,并且在深度为零时,该区域为零:
> st_area(filler(20, poly, x_profile, y_profile))
[1] 2450.761
> st_area(filler(2, poly, x_profile, y_profile))
[1] 15.27778
> st_area(filler(0, poly, x_profile, y_profile))
[1] 0
如果你超越个人资料的顶部,不确定会发生什么......