Skip to content

Commit

Permalink
Update get_boundary_points
Browse files Browse the repository at this point in the history
The definition of the function is taken from #59
  • Loading branch information
agila5 committed Jun 20, 2020
1 parent 76b380e commit 7dd71d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ importFrom(sf,NA_agr_)
importFrom(sf,st_agr)
importFrom(sf,st_as_sf)
importFrom(sf,st_bbox)
importFrom(sf,st_boundary)
importFrom(sf,st_cast)
importFrom(sf,st_collection_extract)
importFrom(sf,st_coordinates)
Expand Down
29 changes: 27 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,35 @@ get_boundary_node_indices = function(x, out = "both") {
#' @return An object of class \code{\link[sf]{sfc}} with \code{POINT}
#' geometries.
#'
#' @importFrom sf st_boundary st_cast st_geometry
#' See #59 for a discussion on this function.
#'
#' @importFrom sf st_coordinates st_cast st_sfc
#' @noRd
get_boundary_points = function(x) {
sf::st_cast(sf::st_boundary(sf::st_geometry(x)), "POINT")
# 1a. extract coordinates
x_coordinates <- sf::st_coordinates(x)

# 1b. Find index of L1 column
L1_index <- ncol(x_coordinates)

# 1c. Remove colnames
x_coordinates <- unname(x_coordinates)

# 2. Find idxs of first and last coordinate (i.e. the boundary points)
first_pair <- !duplicated(x_coordinates[, L1_index])
last_pair <- !duplicated(x_coordinates[, L1_index], fromLast = TRUE)
idxs <- first_pair | last_pair

# 3. Extract idxs and rebuild sfc
x_pairs <- x_coordinates[idxs, ]
x_nodes <- sf::st_cast(
sf::st_sfc(
sf::st_multipoint(x_pairs[, -L1_index]),
crs = sf::st_crs(x)
),
"POINT"
)
x_nodes
}

#' Directly extract the edges from an sfnetwork
Expand Down

0 comments on commit 7dd71d0

Please sign in to comment.