diff --git a/NAMESPACE b/NAMESPACE index 7d239638..c37154f9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -139,6 +139,7 @@ export(quadrant) export(read_table_builder) export(reproject) export(rnet_add_node) +export(rnet_boundary_df) export(rnet_boundary_points) export(rnet_boundary_points_lwgeom) export(rnet_boundary_unique) diff --git a/R/rnet_boundary_points.R b/R/rnet_boundary_points.R index bd8c0908..9cd06cb2 100644 --- a/R/rnet_boundary_points.R +++ b/R/rnet_boundary_points.R @@ -7,25 +7,33 @@ #' if(has_sfheaders) { #' rnet <- rnet_roundabout #' bp1 <- rnet_boundary_points(rnet) -#' bp2 <- rnet_boundary_points_lwgeom(rnet) # slower version with lwgeom -#' bp3 <- line2points(rnet) # slower version with lwgeom +#' bp2 <- line2points(rnet) # slower version with lwgeom +#' bp3 <- rnet_boundary_points_lwgeom(rnet) # slower version with lwgeom +#' bp4 <- rnet_boundary_unique(rnet) +#' nrow(bp1) +#' nrow(bp3) #' identical(sort(sf::st_coordinates(bp1)), sort(sf::st_coordinates(bp2))) +#' identical(sort(sf::st_coordinates(bp3)), sort(sf::st_coordinates(bp4))) #' plot(rnet$geometry) -#' plot(bp1, add = TRUE) +#' plot(bp3, add = TRUE) #' } rnet_boundary_points <- function(rnet) { + pairs <- rnet_boundary_df(rnet) + pairs_xyz <- pairs[names(pairs) %in% c("x", "y", "z")] + boundary_points <- sfheaders::sf_point(pairs_xyz) + sf::st_crs(boundary_points) <- sf::st_crs(rnet) + boundary_points +} +#' @rdname rnet_boundary_points +#' @export +rnet_boundary_df <- function(rnet) { stopifnot(requireNamespace("sfheaders", quietly = TRUE)) coordinates <- sfheaders::sf_to_df(rnet) - # names(coordinates) # "sfg_id" "linestring_id" "x" "y" - # head(coordinates) - coordinates <- coordinates[-1] - first_pair <- !duplicated(coordinates[, 1]) - last_pair <- !duplicated(coordinates[, 1], fromLast = TRUE) + first_pair <- !duplicated(coordinates[["sfg_id"]]) + last_pair <- !duplicated(coordinates[["sfg_id"]], fromLast = TRUE) idxs <- first_pair | last_pair pairs <- coordinates[idxs, ] - boundary_points <- sfheaders::sf_point(pairs) - sf::st_crs(boundary_points) <- sf::st_crs(rnet) - boundary_points + pairs } #' @rdname rnet_boundary_points #' @export diff --git a/man/rnet_boundary_points.Rd b/man/rnet_boundary_points.Rd index 086a673f..4d7739fb 100644 --- a/man/rnet_boundary_points.Rd +++ b/man/rnet_boundary_points.Rd @@ -2,6 +2,7 @@ % Please edit documentation in R/rnet_boundary_points.R \name{rnet_boundary_points} \alias{rnet_boundary_points} +\alias{rnet_boundary_df} \alias{rnet_boundary_unique} \alias{rnet_boundary_points_lwgeom} \alias{rnet_duplicated_vertices} @@ -9,6 +10,8 @@ \usage{ rnet_boundary_points(rnet) +rnet_boundary_df(rnet) + rnet_boundary_unique(rnet) rnet_boundary_points_lwgeom(rnet) @@ -29,10 +32,14 @@ has_sfheaders <- requireNamespace("sfheaders", quietly = TRUE) if(has_sfheaders) { rnet <- rnet_roundabout bp1 <- rnet_boundary_points(rnet) -bp2 <- rnet_boundary_points_lwgeom(rnet) # slower version with lwgeom -bp3 <- line2points(rnet) # slower version with lwgeom +bp2 <- line2points(rnet) # slower version with lwgeom +bp3 <- rnet_boundary_points_lwgeom(rnet) # slower version with lwgeom +bp4 <- rnet_boundary_unique(rnet) +nrow(bp1) +nrow(bp3) identical(sort(sf::st_coordinates(bp1)), sort(sf::st_coordinates(bp2))) +identical(sort(sf::st_coordinates(bp3)), sort(sf::st_coordinates(bp4))) plot(rnet$geometry) -plot(bp1, add = TRUE) +plot(bp3, add = TRUE) } }