Skip to content

Commit

Permalink
fixed error that was happening when POINT geometries were produced fr…
Browse files Browse the repository at this point in the history
…om st_intersection in trim_transects_to_polygons() function
  • Loading branch information
anguswg-ucsb committed Dec 2, 2024
1 parent b524dca commit 323c01a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: hydrofabric3D
Title: hydrofabric3D
Version: 0.1.85
Version: 0.1.86
Authors@R: c(person("Mike", "Johnson", role = c("aut"), email = "[email protected]"),
person("Angus", "Watters", role = c("aut", "cre"), email = "[email protected]"),
person("Arash", "Modaresi", role = "ctb"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ importFrom(sf,st_difference)
importFrom(sf,st_distance)
importFrom(sf,st_drop_geometry)
importFrom(sf,st_geometry)
importFrom(sf,st_geometry_type)
importFrom(sf,st_intersection)
importFrom(sf,st_intersects)
importFrom(sf,st_is_empty)
Expand Down
7 changes: 6 additions & 1 deletion R/transect_to_polygon_extender.R
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ get_line_node_pts <- function(
#' @param dissolve logical, whether to dissolve polygon internal boundaries or not. Default is FALSE.
#' @importFrom dplyr filter select any_of mutate bind_rows n slice_max group_by ungroup across distinct
#' @importFrom rmapshaper ms_explode
#' @importFrom sf st_intersection
#' @importFrom sf st_intersection st_geometry_type
#' @importFrom hydroloom rename_geometry
#' @return sf dataframe
#' @export
Expand Down Expand Up @@ -1353,6 +1353,7 @@ trim_transects_to_polygons <- function(transect_lines,
dplyr::select(-dplyr::any_of(POLYGON_ID)) %>%
sf::st_intersection(polygons) %>%
dplyr::select(!dplyr::any_of(c(POLYGON_ID, paste0(POLYGON_ID, ".1")))) %>%
dplyr::filter(sf::st_geometry_type(geometry) %in% c("LINESTRING", "MULTILINESTRING")) %>%
rmapshaper::ms_explode() %>%
add_intersects_ids(
flowlines %>%
Expand Down Expand Up @@ -1415,6 +1416,10 @@ trim_transects_to_polygons <- function(transect_lines,
merged_transects %>%
cs_arrange(crosswalk_id = crosswalk_id, order_by = "cs_id")

# recalculate lengths
merged_transects <-
merged_transects %>%
add_length_col("cs_lengthm")

return(merged_transects)
}
Expand Down
11 changes: 7 additions & 4 deletions tests/testthat/test-trim-transects-to-polygons.R
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,8 @@ testthat::test_that("trim_transects_to_polygons() a single set of transects with
) %>%
dplyr::mutate(
is_extended = left_distance > 0 | right_distance > 0
)
) %>%
hydrofabric3D::add_length_col("old_length")
# %>%
# dplyr::select(
# dplyr::any_of(CROSSWALK_ID),
Expand Down Expand Up @@ -627,7 +628,7 @@ testthat::test_that("trim_transects_to_polygons() a single set of transects with
was_not_trimmed$new_length
)

actually_trimmed_transects_are_not_equal_length <- was_trimmed$cs_lengthm != was_trimmed$new_length
actually_trimmed_transects_are_not_equal_length <- was_trimmed$old_length != was_trimmed$new_length
testthat::expect_true(
actually_trimmed_transects_are_not_equal_length
)
Expand Down Expand Up @@ -703,7 +704,8 @@ testthat::test_that("trim_transects_to_polygons() a single set of transects with
keep_lengths = FALSE,
reindex_cs_ids = FALSE,
verbose = TRUE
)
) %>%
hydrofabric3D::add_length_col("old_length")

# plot(flowlines$geom, col = "blue", add = F)
# plot(big_polygons$geom, col = scales::alpha("pink", 0.3), add = T)
Expand Down Expand Up @@ -783,13 +785,14 @@ testthat::test_that("trim_transects_to_polygons() a single set of transects with
dplyr::filter(id == "wb-1003265", cs_id %in% c(9, 10)) %>%
hydrofabric3D::add_length_col("new_length")

actually_trimmed_transects_are_not_equal_length <- all(was_trimmed$cs_lengthm != was_trimmed$new_length)
actually_trimmed_transects_are_not_equal_length <- all(was_trimmed$old_length != was_trimmed$new_length)

testthat::expect_true(
actually_trimmed_transects_are_not_equal_length
)

})

testthat::test_that("trim_transects_to_polygons() complex junction flowlines with all flowlines having a polygon to trim against", {

flowlines <- sf::read_sf(testthat::test_path("testdata", "junction_flowlines.gpkg"))
Expand Down

0 comments on commit 323c01a

Please sign in to comment.