-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add function to create midpoint network
- Loading branch information
Showing
8 changed files
with
180 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#' Create a data frame of nodes for a graph | ||
#' | ||
#' This function creates a data frame of nodes for a graph from a set of node | ||
#' IDs and geometries. | ||
#' | ||
#' @param node_ids A vector of node IDs. | ||
#' @param node_geometries A `sfc_POINT` object containing node geometries. | ||
#' @return A data frame of nodes with columns `id`, `x`, and `y`. | ||
#' @export | ||
#' @examples | ||
#' library(sf) | ||
#' | ||
#' # Create node IDs and geometries | ||
#' node_ids <- c("jn_000001", "jn_000002", "jn_000003") | ||
#' | ||
#' # Create some node geometries | ||
#' node_geometries <- st_sfc( | ||
#' st_point(c(0, 1)), | ||
#' st_point(c(1, 0)), | ||
#' st_point(c(2, 1)) | ||
#' ) | ||
#' | ||
#' # Create a data frame of nodes | ||
#' create_graph_nodes(node_ids, node_geometries) | ||
create_graph_nodes <- function(node_ids, node_geometries) { | ||
# Extract node coordinates | ||
coordinates <- st_coordinates(node_geometries) | ||
|
||
# Create a data frame of nodes | ||
graph_nodes <- data.frame( | ||
id = node_ids, | ||
x = coordinates[, 1], | ||
y = coordinates[, 2] | ||
) | ||
|
||
return(graph_nodes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#' Create a midpoint graph from a network | ||
#' | ||
#' This function creates a midpoint graph where nodes represent the midpoints | ||
#' of links in the input network. | ||
#' | ||
#' @param network A `road_network` or `segmented_network` object. | ||
#' @return A `igraph` object representing the midpoint graph. | ||
#' @export | ||
#' @examples | ||
#' # Create a road network | ||
#' network <- create_road_network(demo_roads) | ||
#' | ||
#' # Plot the road network | ||
#' plot(network$graph) | ||
#' | ||
#' # Create a midpoint graph from a road network | ||
#' graph <- create_midpoint_graph(network) | ||
#' | ||
#' # Plot the midpoint graph | ||
#' plot(graph) | ||
create_midpoint_graph <- function(network) { | ||
# Get adjacent links for each link | ||
adjacent_links <- get_adjacent_links( | ||
network, | ||
network$links$id, | ||
reachable_only = TRUE | ||
) | ||
|
||
# Create a data frame of links for midpoint graph | ||
midpoint_graph_links <- create_graph_links( | ||
rep(network$links$id, lengths(adjacent_links)), | ||
unlist(adjacent_links), | ||
directed = is_directed(network$graph), | ||
unique_links = TRUE | ||
) | ||
|
||
# Create a data frame of nodes for midpoint graph | ||
midpoint_graph_nodes <- create_graph_nodes( | ||
network$links$id, | ||
st_centroid(network$links$geometry) | ||
) | ||
|
||
# Create a midpoint graph from the links and nodes | ||
midpoint_graph <- graph_from_data_frame( | ||
midpoint_graph_links, | ||
directed = is_directed(network$graph), | ||
vertices = midpoint_graph_nodes | ||
) | ||
|
||
# Calculate weights for each edge | ||
link_indices <- match(as.matrix(midpoint_graph_links), network$links$id) | ||
link_length <- st_length(network$links$geometry[link_indices]) | ||
E(midpoint_graph)$weight <- rowMeans(matrix(link_length, ncol = 2)) | ||
|
||
return(midpoint_graph) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.