Learn R Programming

flownet (version 0.1.2)

nodes_from_graph: Extract Nodes from Graph

Description

Extract unique nodes with their coordinates from a graph data frame.

Usage

nodes_from_graph(graph_df, sf = FALSE, crs = 4326)

Value

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.

Arguments

graph_df

A data frame representing a graph with columns: from, to, FX, FY, TX, TY.

sf

Logical. If TRUE, returns result as an sf POINT object. Default: FALSE.

crs

Coordinate reference system for sf output; default is 4326.

Details

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.

Examples

Run this code
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)

Run the code above in your browser using DataLab