Skip to content

Conversation

@RicoFio
Copy link

@RicoFio RicoFio commented Oct 1, 2025

Description

This PR implements the feature enhancement suggested in #47. It enables City2Graph to download data from the TransportationNetworks repository and convert it into a networkX graph. I haven't added it yet to the documentation since I first wanted to check whether this should go into a separate file.

Usage

from city2graph.data import load_transportation_networks_data
from city2graph import gdf_to_nx
import geopandas as gpd
import networkx as nx
from shapely.geometry import LineString
import pandas as pd
from matplotlib import pyplot as plt

data = load_transportation_networks_data('SiouxFalls')

nodes = gpd.GeoDataFrame(data['nodes'], geometry=gpd.points_from_xy(data['nodes']['x'], data['nodes']['y']), crs="EPSG:4326")
edges = gpd.GeoDataFrame(data['network'], geometry=None)
# Create geometry for edges using from_node and to_node coordinates
edge_geometries = []
edge_indices = []

for _, edge in edges.iterrows():
    from_node = nodes[nodes['node'] == edge['init_node']].iloc[0]
    to_node = nodes[nodes['node'] == edge['term_node']].iloc[0]

    line = LineString([from_node.geometry.coords[0], to_node.geometry.coords[0]])
    edge_geometries.append(line)
    edge_indices.append((edge['init_node'], edge['term_node']))

edges = gpd.GeoDataFrame(
    edges,
    geometry=edge_geometries,
    index=pd.MultiIndex.from_tuples(edge_indices, names=['u', 'v']),
    crs="EPSG:4326"
)
graph: nx.DiGraph = gdf_to_nx(nodes, edges, directed=True)
pos = {row['node']-1: (row['x'], row['y']) for _, row in nodes.iterrows()}
nx.draw(graph, pos, with_labels=True, node_size=80, font_size=8, arrows=True)
plt.show()

Checklist

  • I have read the Contributing Guide.
  • I have updated the documentation, if necessary.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • Pre-commit checks passed locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant