library(flownet)
library(sf)
# Convert segments to undirected graph
graph <- africa_segments |>
linestrings_from_graph() |>
linestrings_to_graph() |>
create_undirected_graph(FUN = "fsum")
# Get city/port nodes to preserve
nodes_df <- nodes_from_graph(graph, sf = TRUE)
nearest_nodes <- nodes_df$node[st_nearest_feature(africa_cities_ports, nodes_df)]
# Initial consolidation
graph <- consolidate_graph(graph, keep = nearest_nodes, w = ~ passes)
# Method 1: Shortest-paths simplification (keeps only traversed edges)
graph_simple <- simplify_network(graph, nearest_nodes,
method = "shortest-paths",
cost.column = ".length")
nrow(graph_simple) # Reduced number of edges
# \donttest{
# Method 2: Cluster-based simplification (contracts graph spatially)
# Compute node weights for clustering
node_weights <- collapse::rowbind(
collapse::fselect(graph, node = from, gravity_rd),
collapse::fselect(graph, to, gravity_rd),
use.names = FALSE) |>
collapse::collap(~ node, "fsum")
graph_cluster <- simplify_network(graph, nearest_nodes,
method = "cluster",
cost.column = node_weights$gravity_rd,
radius_km = list(nodes = 30, cluster = 27),
w = ~ passes)
nrow(graph_cluster)
# }
Run the code above in your browser using DataLab