r/MachineLearning • u/Tough_Ad_6598 • Aug 13 '26
Research City2Graph: A Python library for Heterogeneous Graph Neural Networks and spatial analysis in urban systems [R]
City2Graph is a Python library I built that turns geospatial data into analysis-ready graphs (for spatial analysis, network analysis, and Graph Neural Networks as GeoAI), and the paper describing it has just been published, so I wanted to share it here.
Repository: https://github.com/c2g-dev/city2graph
import city2graph as c2g
# buildings + street segments -> heterogeneous morphological graph
nodes, edges = c2g.morphological_graph(buildings, segments)
# straight into PyTorch Geometric
data = c2g.gdf_to_pyg(nodes, edges)
What it covers:
- Morphology: graphs of buildings, streets, and tessellated urban fabric from OpenStreetMap and Overture Maps
- Transportation: GTFS and GBFS feeds loaded through DuckDB, with GTFS aggregated into stop-to-stop transit graphs
- Mobility: OD matrices and flow data (migration, bike-sharing, pedestrian counts) as weighted spatial graphs
- Proximity and contiguity: KNN, Delaunay, Gilbert, Waxman, plus queen/rook contiguity, under Euclidean, Manhattan, or network distances
- Heterogeneous graphs and metapaths: several node and edge types in one graph, with metapath-derived edges composing relations across them
- Conversion: round trips between GeoDataFrames, NetworkX, rustworkx, and PyTorch Geometric
Data/HeteroData, with geometries and attributes kept intact
It sets out why urban data is better treated as heterogeneous graphs than as flat feature tables, how the morphological, transport, mobility, and proximity constructions relate to each other, and how the library keeps geometry and graph structure consistent across conversions. If you use the library in research, that is the citation.
Paper
Sato, Y., Pietrostefani, E., Mahabir, R., & Arribas-Bel, D. (2026). City2Graph: A Python library for Heterogeneous Graph Neural Networks and spatial analysis in urban systems. Computers, Environment and Urban Systems, 130, 102492.
Happy to answer questions about the design, and issues or PRs are very welcome. I am especially keen to hear which data sources people want supported next.



3
u/rog-uk Aug 16 '26
Nice work! May I suggest that you share this in r/OperationsResearch ? It could help quite a bit with some of the stuff they care about.
10
u/aozorahime Aug 13 '26
Awesome!!
6
u/Stillane Aug 13 '26
can anyone explain like I'm 5 ?
4
u/Old-Composer8069 Aug 13 '26
Interesting, let save this to library's
3
u/ConnectKale Aug 13 '26
Whoa. This is cool.
3
u/Puzzleheaded_Meat522 Aug 13 '26
This looks very cool. Shared it with a collaborator. Will definitely give this a try soon
3
u/marr75 Aug 13 '26
Dani Arribas-Bel is back, baby!
3
u/Felix-ML Aug 13 '26
Best library I've seen in this year.
3
u/commenterzero Aug 14 '26
Great for real estate analysis
3
u/Tiny_Arugula_5648 Aug 16 '26
OMG you're timing couldn't have been any better.. you just saved me a massive amount of work. This is exactly what I need for hyperlocal dataset that I'm creating for understanding population and their environment.
3
u/AdmissibilityScience Aug 13 '26
Great work at first I thought this was a biological cell from afar.
2
u/Midsummer858 Aug 14 '26
This is really cool, definitely going to look into this!
2
u/BetFar352 Aug 19 '26
Someone else said it - amidst all the AI slop, this is the best GitHub repo I have seen all this year. Thank you! Amazing work, lot of applications!
2
u/Willeny_Arch Aug 21 '26
Good to see more stuff around spatial GNNs!
2
u/tamkite Aug 22 '26
On a weekly basis I often think about how something like this could/should be used as part of real-time control of traffic lights to stop the phantom bottlenecks and pointless stopping at red. Would love to know if that already exists. Cool stuff!