| nodes_from_graph | R Documentation |
Extract unique nodes with their coordinates from a graph data frame.
nodes_from_graph(graph_df, sf = FALSE, crs = 4326)
graph_df |
A data frame representing a graph with columns:
|
sf |
Logical. If TRUE, returns result as an |
crs |
Coordinate reference system for sf output; default is 4326. |
This function extracts all unique nodes from both the from and to
columns of the graph, along with their corresponding coordinates. Duplicate nodes
are removed, keeping only unique node IDs with their coordinates.
A data frame (or sf object if sf = TRUE) with unique nodes and coordinates:
node - Node ID
X - Node X-coordinate (typically longitude)
Y - Node Y-coordinate (typically latitude)
Result is sorted by node ID.
library(flownet)
library(sf)
# Load existing network edges and convert to graph
africa_net <- africa_network[!africa_network$add, ]
graph <- linestrings_to_graph(africa_net)
# Extract nodes from graph
nodes <- nodes_from_graph(graph)
head(nodes)
# Get nodes as sf POINT object for spatial operations
nodes_sf <- nodes_from_graph(graph, sf = TRUE)
class(nodes_sf)
# Find nearest network nodes to cities/ports
nearest_nodes <- nodes_sf$node[st_nearest_feature(africa_cities_ports, nodes_sf)]
head(nearest_nodes)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.