-
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 segmented network
- Loading branch information
Showing
8 changed files
with
198 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#' Create a graph from nodes and links | ||
#' | ||
#' This function creates a graph object from a set of nodes and links. | ||
#' | ||
#' @param nodes A `sf` object representing nodes. | ||
#' @param links A `sf` object representing links between nodes. | ||
#' @param directed A logical indicating whether the graph is directed. | ||
#' @return A `igraph` object. | ||
#' @export | ||
#' @examples | ||
#' # Create nodes and links | ||
#' nodes <- extract_road_network_nodes(demo_roads) | ||
#' links <- extract_road_network_links(demo_roads, nodes) | ||
#' | ||
#' # Create the graph | ||
#' graph <- create_graph(nodes, links) | ||
#' | ||
#' # Plot the graph | ||
#' plot(graph) | ||
create_graph <- function(nodes, links, directed = FALSE) { | ||
# Create graph from nodes and links | ||
edges <- st_drop_geometry(links)[, c("from", "to")] | ||
node_coordinates <- st_coordinates(nodes) | ||
vertices <- data.frame( | ||
id = nodes$id, | ||
x = node_coordinates[, 1], | ||
y = node_coordinates[, 2] | ||
) | ||
graph <- graph_from_data_frame( | ||
edges, | ||
directed = directed, | ||
vertices = vertices | ||
) | ||
|
||
# Set edge weights based on link length | ||
E(graph)$weight <- st_length(links) | ||
|
||
return(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
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,82 @@ | ||
#' Create a segmented road network | ||
#' | ||
#' This function creates a segmented road network by dividing each link of the | ||
#' input road network into segments of a specified length. | ||
#' | ||
#' @param road_network A `road_network` object representing the input road | ||
#' network. | ||
#' @param segment_length A numeric value specifying the length of each segment. | ||
#' @return A `segmented_network` object. | ||
#' @export | ||
#' @examples | ||
#' # Create a road network | ||
#' road_network <- create_road_network(demo_roads) | ||
#' | ||
#' # Create a segmented road network | ||
#' segmented_network <- create_segmented_network(road_network) | ||
#' segmented_network | ||
#' | ||
#' # Plot the segmented road network | ||
#' plot(segmented_network) | ||
create_segmented_network <- function(road_network, segment_length = 1) { | ||
# Extract nodes and links of segmented road network | ||
nodes <- extract_segmented_network_nodes(road_network, segment_length) | ||
links <- extract_segmented_network_links(road_network, nodes) | ||
|
||
# Create graph from nodes and links | ||
graph <- create_graph( | ||
nodes, | ||
links, | ||
directed = is_directed(road_network$graph) | ||
) | ||
|
||
# Construct the road network object | ||
segmented_network <- structure(list( | ||
graph = graph, | ||
nodes = nodes, | ||
links = links, | ||
origin_network = road_network, | ||
segment_length = segment_length | ||
), class = c("segmented_network")) | ||
|
||
return(segmented_network) | ||
} | ||
|
||
#' @export | ||
print.segmented_network <- function(x, ...) { | ||
cat("Segmented network\n") | ||
cat("Segment length: ", x$segment_length, "\n") | ||
cat("Nodes:\n") | ||
if (nrow(x$nodes) <= 5) { | ||
print(as.data.frame(x$nodes), ...) | ||
} else { | ||
print(as.data.frame(x$nodes)[1:5, ], ...) | ||
cat("...", nrow(x$nodes) - 5, "more nodes\n") | ||
} | ||
cat("\n") | ||
cat("Links:\n") | ||
if (nrow(x$links) <= 5) { | ||
print(as.data.frame(x$links), ...) | ||
} else { | ||
print(as.data.frame(x$links)[1:5, ], ...) | ||
cat("...", nrow(x$links) - 5, "more links\n") | ||
} | ||
} | ||
|
||
#' @export | ||
plot.segmented_network <- function(x, ...) { | ||
plot(x$links$geometry, lwd = 1, ...) | ||
plot(x$nodes$geometry, cex = 1, pch = 16, add = TRUE, ...) | ||
} | ||
|
||
#' @export | ||
summary.segmented_network <- function(object, ...) { | ||
cat("Segmented network summary\n") | ||
cat("Segment length:\n") | ||
cat(" Desired: ", object$segment_length, "\n") | ||
cat(" Max. : ", max(st_length(object$links)), "\n") | ||
cat(" Mean : ", mean(st_length(object$links)), "\n") | ||
cat(" Min. : ", min(st_length(object$links)), "\n") | ||
cat("Number of nodes: ", nrow(object$nodes), "\n") | ||
cat("Number of links: ", nrow(object$links), "\n") | ||
} |
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.