Thursday, December 3, 2026 | London 11°C · Drizzle
DailyGlimpse

AI

Building Spatial Graph Neural Networks for Urban Function Inference with city2graph, OSMnx, and PyTorch Geometric

Build a spatial graph learning pipeline using city2graph with OpenStreetMap POI and street data.

MarkTechPost 2 min read

Building Spatial Graph Neural Networks for Urban Function Inference with city2graph, OSMnx, and PyTorch Geometric

In this tutorial, we build an end-to-end spatial graph learning pipeline using city2graph. We start by collecting real urban POI data and street network information from OpenStreetMap, with a synthetic fallback to ensure the workflow remains reliable. We then engineer spatial features, construct multiple proximity graph families, and compare how different graph-building strategies represent the same urban environment. After that, we create both heterogeneous and homogeneous graph structures, convert them into PyTorch Geometric format, and train a GraphSAGE model to predict POI categories from spatial structure. Through this process, we integrate geospatial data processing, graph construction, and GNN-based urban function inference into a single practical workflow.

Installing city2graph and Importing Geospatial and Graph Learning Libraries

We begin by installing the required libraries and importing the geospatial, graph learning, and machine learning tools used throughout the tutorial. We verify that city2graph and PyTorch Geometric are available so the rest of the workflow can run properly. We also set a fixed random seed to make the graph construction, training split, and model results more reproducible.

Collecting OpenStreetMap POI Data with a Synthetic Fallback

We collect real POI data from OpenStreetMap around Shibuya, Tokyo, and group the locations into broad urban function categories such as food, retail, education, and health. We also download the walkable street network so that the POIs can later be connected with urban-form features. If the OSM request fails, we generate a synthetic clustered dataset, which keeps the tutorial runnable even when online data access is unavailable.

Engineering Spatial Features and Building Proximity Graph Families

We compute spatial features including local density (number of nearby POIs within 150 meters) and distance to the nearest street. Then we use city2graph to build several proximity graphs: K-nearest neighbors (KNN), Delaunay triangulation, Gabriel graph, relative neighborhood graph (RNG), Euclidean minimum spanning tree (EMST), and Waxman random graph. Each graph captures different aspects of spatial adjacency. We compare the number of edges and average degree across these graph families, noting how density and connectivity vary.

React