|
| 1 | +#' Calculate upstream cross section |
| 2 | +#' |
| 3 | +#' @description Calculates the upstream cross section coordinates, updates |
| 4 | +#' cross section end points, reverses incorrectly digitized cross sections. |
| 5 | +#' |
| 6 | +#' @param cross_section sf line object, The cross section feature. |
| 7 | +#' |
| 8 | +#' @return sf line object |
| 9 | +#' @export |
| 10 | +#' |
| 11 | +#' @importFrom dplyr %>% arrange select one_of left_join mutate lead last |
| 12 | +#' filter |
| 13 | +#' @importFrom sf st_drop_geometry |
| 14 | +#' |
| 15 | +xs_upstream <- function(cross_section) { |
| 16 | + # Calculate xs start/end points |
| 17 | + xs_start <- sf_line_end_point(cross_section, end = "start") |
| 18 | + xs_end <- sf_line_end_point(cross_section, end = "end") |
| 19 | + |
| 20 | + xs_update <- cross_section %>% |
| 21 | + arrange(Seq) %>% |
| 22 | + select(-one_of("x_start", "y_start", "x_end", "y_end", |
| 23 | + "from_measure", "to_measure", "ReachName")) %>% |
| 24 | + left_join(y = st_drop_geometry(xs_start), by = "Seq") %>% |
| 25 | + select(-one_of("ReachName")) %>% |
| 26 | + left_join(y = st_drop_geometry(xs_end), by = "Seq") %>% |
| 27 | + |
| 28 | + # get next upstream flowline point |
| 29 | + mutate(upstream_x = lead(x = POINT_X, n = 1, |
| 30 | + default = last(.$POINT_X))) %>% |
| 31 | + mutate(upstream_y = lead(x = POINT_Y, n = 1, |
| 32 | + default = last(.$POINT_Y))) %>% |
| 33 | + |
| 34 | + # determine if the xs start point is on the left descending bank |
| 35 | + mutate(start_left = start_left(upstream_x, upstream_y, |
| 36 | + POINT_X, POINT_Y, |
| 37 | + x_start, y_start)) |
| 38 | + # Assure XS's digitized beginning on the left descending bank |
| 39 | + # XS's that begin on the left descending bank |
| 40 | + xs_left <- xs_update %>% |
| 41 | + filter(start_left == TRUE) %>% |
| 42 | + mutate(fixed_start = start_left(upstream_x, upstream_y, |
| 43 | + POINT_X, POINT_Y, |
| 44 | + x_start, y_start)) |
| 45 | + # XS's that do not begin on the left descending bank; need to be flipped |
| 46 | + xs_right <- xs_update %>% |
| 47 | + filter(start_left == FALSE) %>% |
| 48 | + sf_line_reverse() |
| 49 | + |
| 50 | + # update XS start and end points of the backward XS's |
| 51 | + xs_start <- sf_line_end_point(xs_right, end = "start") |
| 52 | + xs_end <- sf_line_end_point(xs_right, end = "end") |
| 53 | + |
| 54 | + # check that the reverse works |
| 55 | + xs_right_rev <- xs_right %>% |
| 56 | + select(-one_of("x_start", "y_start", "x_end", "y_end", "ReachName")) %>% |
| 57 | + left_join(y = st_drop_geometry(xs_start), by = "Seq") %>% |
| 58 | + select(-one_of("ReachName")) %>% |
| 59 | + left_join(y = st_drop_geometry(xs_end), by = "Seq") %>% |
| 60 | + mutate(fixed_start = start_left(upstream_x, upstream_y, |
| 61 | + POINT_X, POINT_Y, |
| 62 | + x_start, y_start)) |
| 63 | + # combine |
| 64 | + xs_fixed <- bind_rows(xs_left, xs_right_rev) %>% |
| 65 | + arrange(Seq) |
| 66 | + |
| 67 | + return(xs_fixed) |
| 68 | +} |
0 commit comments