问题
I'm trying to figure out how to calculate the water filled area of a river cross section for different water levels.
For the cross-section I have the depth at every 25 cm over the 5 m wide river and the area can be calculated based on a nicely answered previous question 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)
But this cross section is only partially filled with water, and it is the water filled cross section that I now try to calculate.
The water starts filling the cross section from the deepest part and the depth then vary, for example like this:
water_level<-c(40, 38, 25, 33, 40, 42, 50, 39)
Does anyone have any ideas on how this could be done in r? Thanks in advance.
回答1:
This function computes the intersection of the profile with a line at the specified depth from the bottom of the profile. It is slightly redundant in that it also needs the x and y profile values which could in theory be extracted from 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)
}
So in use:
> 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")
Note the first green region is slightly covered by the less deeper regions. Note also how the blue regions is in two sections. You can get the cross-section with st_area
, and at depth zero the area is zero:
> 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
Not sure what happens if you go above the top of your profile...
来源:https://stackoverflow.com/questions/46650208/calculate-area-of-cross-section-as-function-of-height