{"id":97157,"date":"2026-06-13T17:57:52","date_gmt":"2026-06-13T17:57:52","guid":{"rendered":"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/"},"modified":"2026-06-13T17:57:52","modified_gmt":"2026-06-13T17:57:52","slug":"a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric","status":"publish","type":"post","link":"https:\/\/youzum.net\/fr\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/","title":{"rendered":"A Coding Implementation on Spatial Graph Neural Networks for Urban Function Inference Using city2graph, OSMnx, and PyTorch Geometric"},"content":{"rendered":"<p class=\"wp-block-paragraph\">In this tutorial, we build an end-to-end spatial graph learning pipeline using<a href=\"https:\/\/github.com\/c2g-dev\/city2graph\"> <strong>city2graph<\/strong><\/a>. 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.<\/p>\n<h3 class=\"wp-block-heading\"><strong>Installing city2graph and Importing Geospatial and Graph Learning Libraries<\/strong><\/h3>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\"no-line-numbers\"><code class=\"no-wrap language-php\">!pip -q install \"city2graph[cpu]\" osmnx contextily scikit-learn 2&gt;\/dev\/null\nimport warnings, numpy as np, pandas as pd, geopandas as gpd\nwarnings.filterwarnings(\"ignore\")\nfrom shapely.geometry import Point\nimport matplotlib.pyplot as plt\nimport city2graph as c2g\nprint(\"city2graph version:\", getattr(c2g, \"__version__\", \"unknown\"))\nprint(\"PyTorch \/ PyG available:\", c2g.is_torch_available())\nimport torch\nimport torch.nn.functional as F\nfrom torch_geometric.nn import SAGEConv, to_hetero\nfrom torch_geometric.utils import to_undirected\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.neighbors import NearestNeighbors\nfrom sklearn.metrics import accuracy_score, f1_score\nfrom sklearn.decomposition import PCA\nSEED = 42\nnp.random.seed(SEED); torch.manual_seed(SEED)\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p class=\"wp-block-paragraph\">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.<\/p>\n<h3 class=\"wp-block-heading\"><strong>Collecting OpenStreetMap POI Data with a Synthetic Fallback<\/strong><\/h3>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\"no-line-numbers\"><code class=\"no-wrap language-php\">CENTER = (35.6595, 139.7005)\nDIST_M = 1100\nTAG_QUERIES = {\n   \"food\":      {\"amenity\": [\"restaurant\", \"cafe\", \"fast_food\", \"bar\", \"pub\"]},\n   \"retail\":    {\"shop\": True},\n   \"education\": {\"amenity\": [\"school\", \"university\", \"college\", \"kindergarten\", \"library\"]},\n   \"health\":    {\"amenity\": [\"hospital\", \"clinic\", \"pharmacy\", \"doctors\", \"dentist\"]},\n}\ndef to_points(gdf):\n   g = gdf.copy()\n   g[\"geometry\"] = g.geometry.representative_point()\n   return g\npoi_gdf, segments_gdf = None, None\ntry:\n   import osmnx as ox\n   ox.settings.use_cache = True\n   ox.settings.log_console = False\n   frames = []\n   for label, tags in TAG_QUERIES.items():\n       try:\n           f = ox.features_from_point(CENTER, tags=tags, dist=DIST_M)\n           f = f[f.geometry.notna()]\n           if len(f):\n               f = to_points(f)[[\"geometry\"]].copy()\n               f[\"category\"] = label\n               frames.append(f)\n       except Exception as e:\n           print(f\"  (skip {label}: {e})\")\n   if not frames:\n       raise RuntimeError(\"No POIs returned from Overpass.\")\n   poi_gdf = gpd.GeoDataFrame(pd.concat(frames, ignore_index=True), crs=\"EPSG:4326\")\n   G = ox.graph_from_point(CENTER, dist=DIST_M, network_type=\"walk\")\n   segments_gdf = ox.graph_to_gdfs(G, nodes=False, edges=True).reset_index(drop=True)[[\"geometry\"]]\n   print(f\"OSM acquisition OK -&gt; {len(poi_gdf)} POIs, {len(segments_gdf)} street segments\")\nexcept Exception as e:\n   print(f\"OSM unavailable ({e}) -&gt; generating synthetic clustered POIs.\")\n   rng = np.random.default_rng(SEED)\n   cats = list(TAG_QUERIES.keys())\n   centers = rng.uniform(-0.01, 0.01, size=(8, 2)) + np.array(CENTER[::-1])\n   rows = []\n   for ci, c in enumerate(centers):\n       dom = cats[ci % len(cats)]\n       n = rng.integers(40, 90)\n       pts = c + rng.normal(0, 0.0016, size=(n, 2))\n       for (lon, lat) in pts:\n           cat = dom if rng.random() &lt; 0.75 else rng.choice(cats)\n           rows.append({\"geometry\": Point(lon, lat), \"category\": cat})\n   poi_gdf = gpd.GeoDataFrame(rows, crs=\"EPSG:4326\")\n   segments_gdf = None\n   print(f\"Synthetic dataset -&gt; {len(poi_gdf)} POIs\")\nif len(poi_gdf) &gt; 700:\n   poi_gdf = poi_gdf.sample(700, random_state=SEED).reset_index(drop=True)\nmetric_crs = poi_gdf.estimate_utm_crs()\npoi_gdf = poi_gdf.to_crs(metric_crs).reset_index(drop=True)\nif segments_gdf is not None:\n   segments_gdf = segments_gdf.to_crs(metric_crs)\nprint(\"Class balance:n\", poi_gdf[\"category\"].value_counts())\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p class=\"wp-block-paragraph\">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.<\/p>\n<h3 class=\"wp-block-heading\"><strong>Engineering Spatial Features and Building Proximity Graph Families<\/strong><\/h3>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\"no-line-numbers\"><code class=\"no-wrap language-php\">poi_gdf[\"cx\"] = poi_gdf.geometry.x\npoi_gdf[\"cy\"] = poi_gdf.geometry.y\ncoords = poi_gdf[[\"cx\", \"cy\"]].to_numpy()\nnn = NearestNeighbors(radius=150.0).fit(coords)\npoi_gdf[\"local_density\"] = [len(idx) - 1 for idx in nn.radius_neighbors(coords, return_distance=False)]\nif segments_gdf is not None and len(segments_gdf):\n   try:\n       joined = gpd.sjoin_nearest(poi_gdf[[\"geometry\"]], segments_gdf[[\"geometry\"]],\n                                  distance_col=\"dist_street\")\n       poi_gdf[\"dist_street\"] = joined.groupby(level=0)[\"dist_street\"].min().reindex(poi_gdf.index).fillna(0.0)\n   except Exception:\n       poi_gdf[\"dist_street\"] = 0.0\nelse:\n   poi_gdf[\"dist_street\"] = 0.0\npoi_gdf[\"category\"] = poi_gdf[\"category\"].astype(\"category\")\npoi_gdf[\"label\"] = poi_gdf[\"category\"].cat.codes.astype(int)\nCLASS_NAMES = list(poi_gdf[\"category\"].cat.categories)\nprint(\"Classes:\", CLASS_NAMES)\ndef graph_stats(name, builder):\n   try:\n       nodes, edges = builder()\n       deg = pd.Series(np.r_[edges.index.get_level_values(0),\n                             edges.index.get_level_values(1)]).value_counts()\n       return name, len(edges), round(deg.mean(), 2), (nodes, edges)\n   except Exception as e:\n       return name, f\"ERR: {e}\", None, None\nbuilders = {\n   \"KNN (k=8)\":  lambda: c2g.knn_graph(poi_gdf, distance_metric=\"euclidean\", k=8, as_nx=False),\n   \"Delaunay\":   lambda: c2g.delaunay_graph(poi_gdf, as_nx=False),\n   \"Gabriel\":    lambda: c2g.gabriel_graph(poi_gdf, as_nx=False),\n   \"RNG\":        lambda: c2g.relative_neighborhood_graph(poi_gdf, as_nx=False),\n   \"EMST\":       lambda: c2g.euclidean_minimum_spanning_tree(poi_gdf, as_nx=False),\n   \"Waxman\":     lambda: c2g.waxman_graph(poi_gdf, distance_metric=\"euclidean\", r0=150, beta=0.6),\n}\nprint(\"n--- Proximity graph comparison ---\")\nprint(f\"{'graph':&lt;14}{'#edges':&gt;10}{'avg_degree':&gt;12}\")\nbuilt = {}\nfor nm, b in builders.items():\n   name, ne, avgdeg, payload = graph_stats(nm, b)\n   print(f\"{name:&lt;14}{str(ne):&gt;10}{str(avgdeg):&gt;12}\")\n   if payload: built[nm] = payload\nfig, axes = plt.subplots(1, 3, figsize=(16, 5))\nfor ax, key in zip(axes, [\"KNN (k=8)\", \"Delaunay\", \"EMST\"]):\n   if key in built:\n       n_, e_ = built[key]\n       e_.plot(ax=ax, linewidth=0.4, color=\"#3b7dd8\", alpha=0.6)\n       poi_gdf.plot(ax=ax, markersize=4, color=\"#d83b5c\")\n       ax.set_title(key); ax.set_axis_off()\nplt.suptitle(\"Spatial graph topologies on the same POI set\", y=1.02)\nplt.tight_layout(); plt.show()\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p class=\"wp-block-paragraph\">We engineer spatial features for each POI by extracting its projected coordinates, calculating local density, and estimating distance to the nearest street segment. We then assign category labels and build several families of proximity graphs, including KNN, Delaunay, Gabriel, RNG, EMST, and Waxman. We compare their edge counts and average degrees, then visualize selected graph topologies to see how differently they connect the same set of POIs.<\/p>\n<h3 class=\"wp-block-heading\"><strong>Constructing Heterogeneous and Homogeneous Graphs in PyTorch Geometric<\/strong><\/h3>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\"no-line-numbers\"><code class=\"no-wrap language-php\">nodes_dict = {}\nfor cat in CLASS_NAMES:\n   sub = poi_gdf[poi_gdf[\"category\"] == cat].copy().reset_index(drop=True)\n   nodes_dict[cat] = sub[[\"geometry\", \"cx\", \"cy\", \"local_density\"]]\ntry:\n   _, bridge_edges = c2g.bridge_nodes(nodes_dict, proximity_method=\"knn\", k=3,\n                                      distance_metric=\"euclidean\")\n   hetero = c2g.gdf_to_pyg(\n       nodes_dict, bridge_edges,\n       node_feature_cols={cat: [\"cx\", \"cy\", \"local_density\"] for cat in CLASS_NAMES},\n   )\n   print(\"nHeteroData node types:\", hetero.node_types)\n   print(\"HeteroData edge types:\")\n   for et in hetero.edge_types:\n       print(f\"   {et}: {hetero[et].edge_index.shape[1]} edges\")\nexcept Exception as e:\n   hetero = None\n   print(\"Heterogeneous build skipped:\", e)\nnodes, edges = c2g.knn_graph(poi_gdf, distance_metric=\"euclidean\", k=8, as_nx=False)\ndeg = pd.Series(np.r_[edges.index.get_level_values(0),\n                     edges.index.get_level_values(1)]).value_counts()\nnodes[\"degree\"] = deg.reindex(nodes.index).fillna(0).astype(float)\nfor col in [\"cx\", \"cy\", \"local_density\", \"dist_street\", \"label\"]:\n   if col not in nodes.columns:\n       nodes[col] = poi_gdf.loc[nodes.index, col].values\nFEATS = [\"cx\", \"cy\", \"local_density\", \"dist_street\", \"degree\"]\nnodes[FEATS] = StandardScaler().fit_transform(nodes[FEATS].astype(float))\ndata = c2g.gdf_to_pyg(nodes, edges, node_feature_cols=FEATS, node_label_cols=[\"label\"])\ndata.edge_index = to_undirected(data.edge_index)\ndata.x = data.x.float()\ny = data.y.long().view(-1)\nN, num_classes = data.num_nodes, int(y.max()) + 1\nprint(f\"nHomogeneous Data: {N} nodes, {data.edge_index.shape[1]} directed-edges, \"\n     f\"{data.x.shape[1]} features, {num_classes} classes\")\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p class=\"wp-block-paragraph\">We construct a heterogeneous multi-layer graph by separating POIs into node types based on their urban function categories. We then use bridge edges to connect nearby nodes across different layers and convert the result into PyTorch Geometric HeteroData format. After that, we build a homogeneous KNN graph, attach degree and engineered features, standardize them, and prepare the final PyG Data object for GraphSAGE training.<\/p>\n<h3 class=\"wp-block-heading\"><strong>Defining and Training a GraphSAGE Model for POI Classification<\/strong><\/h3>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\"no-line-numbers\"><code class=\"no-wrap language-php\">perm = torch.randperm(N, generator=torch.Generator().manual_seed(SEED))\nn_tr, n_va = int(0.6 * N), int(0.2 * N)\ntrain_mask = torch.zeros(N, dtype=torch.bool); train_mask[perm[:n_tr]] = True\nval_mask   = torch.zeros(N, dtype=torch.bool); val_mask[perm[n_tr:n_tr + n_va]] = True\ntest_mask  = torch.zeros(N, dtype=torch.bool); test_mask[perm[n_tr + n_va:]] = True\nclass GraphSAGE(torch.nn.Module):\n   def __init__(self, in_dim, hidden, out_dim, p=0.3):\n       super().__init__()\n       self.c1 = SAGEConv(in_dim, hidden)\n       self.c2 = SAGEConv(hidden, hidden)\n       self.lin = torch.nn.Linear(hidden, out_dim)\n       self.p = p\n   def forward(self, x, ei, return_emb=False):\n       h = F.relu(self.c1(x, ei))\n       h = F.dropout(h, p=self.p, training=self.training)\n       h = F.relu(self.c2(h, ei))\n       out = self.lin(h)\n       return (out, h) if return_emb else out\nmodel = GraphSAGE(data.x.shape[1], 64, num_classes)\nopt = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)\ndef evaluate(mask):\n   model.eval()\n   with torch.no_grad():\n       pred = model(data.x, data.edge_index).argmax(1)\n   yt, yp = y[mask].numpy(), pred[mask].numpy()\n   return accuracy_score(yt, yp), f1_score(yt, yp, average=\"macro\")\nprint(\"n--- Training GraphSAGE ---\")\nbest_val, best_state = 0.0, None\nfor epoch in range(1, 201):\n   model.train(); opt.zero_grad()\n   out = model(data.x, data.edge_index)\n   loss = F.cross_entropy(out[train_mask], y[train_mask])\n   loss.backward(); opt.step()\n   if epoch % 20 == 0:\n       va_acc, va_f1 = evaluate(val_mask)\n       if va_acc &gt; best_val:\n           best_val, best_state = va_acc, {k: v.clone() for k, v in model.state_dict().items()}\n       print(f\"epoch {epoch:3d} | loss {loss.item():.3f} | val_acc {va_acc:.3f} | val_f1 {va_f1:.3f}\")\nif best_state: model.load_state_dict(best_state)\nte_acc, te_f1 = evaluate(test_mask)\nprint(f\"nTEST  accuracy={te_acc:.3f}  macro-F1={te_f1:.3f}\")\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p class=\"wp-block-paragraph\">We split the graph nodes into training, validation, and test masks so the model can learn and be evaluated properly. We define a two-layer GraphSAGE model that learns node representations from both node features and graph structure. We train the model for 200 epochs, monitor validation accuracy and macro-F1, save the best model state, and finally report test performance.<\/p>\n<h3 class=\"wp-block-heading\"><strong>Visualizing Embeddings and Running a Heterogeneous GNN Forward Pass<\/strong><\/h3>\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\">\n<div class=\"control-language\">\n<div class=\"dm-buttons\">\n<div class=\"dm-buttons-left\">\n<div class=\"dm-button-snippet red-button\"><\/div>\n<div class=\"dm-button-snippet orange-button\"><\/div>\n<div class=\"dm-button-snippet green-button\"><\/div>\n<\/div>\n<div class=\"dm-buttons-right\"><a><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\">Copied<\/span><span class=\"dm-error-message\">Use a different Browser<\/span><\/a><\/div>\n<\/div>\n<pre class=\"no-line-numbers\"><code class=\"no-wrap language-php\">model.eval()\nwith torch.no_grad():\n   logits, emb = model(data.x, data.edge_index, return_emb=True)\n   pred = logits.argmax(1).numpy()\nemb2d = PCA(n_components=2).fit_transform(emb.numpy())\nfig, axes = plt.subplots(1, 2, figsize=(15, 6))\nfor cls in range(num_classes):\n   m = y.numpy() == cls\n   axes[0].scatter(emb2d[m, 0], emb2d[m, 1], s=10, label=CLASS_NAMES[cls], alpha=0.7)\naxes[0].set_title(\"GraphSAGE node embeddings (PCA), coloured by TRUE class\")\naxes[0].legend(fontsize=8); axes[0].set_xticks([]); axes[0].set_yticks([])\nplot_gdf = nodes.copy(); plot_gdf[\"pred\"] = pred\nplot_gdf[\"pred_name\"] = [CLASS_NAMES[p] for p in pred]\nplot_gdf.plot(ax=axes[1], column=\"pred_name\", legend=True, markersize=12, cmap=\"tab10\")\naxes[1].set_title(\"Predicted urban function (mapped back to geography)\")\naxes[1].set_axis_off()\ntry:\n   import contextily as ctx\n   ctx.add_basemap(axes[1], crs=plot_gdf.crs, source=ctx.providers.CartoDB.Positron)\nexcept Exception:\n   pass\nplt.tight_layout(); plt.show()\nif hetero is not None:\n   try:\n       for nt in hetero.node_types:\n           hetero[nt].x = hetero[nt].x.float()\n       class HGNN(torch.nn.Module):\n           def __init__(self, hid, out):\n               super().__init__()\n               self.c1 = SAGEConv((-1, -1), hid)\n               self.c2 = SAGEConv((-1, -1), out)\n           def forward(self, x, ei):\n               x = {k: F.relu(v) for k, v in self.c1(x, ei).items()}\n               return self.c2(x, ei)\n       hmodel = to_hetero(HGNN(32, 16), hetero.metadata(), aggr=\"sum\")\n       out_dict = hmodel(hetero.x_dict, hetero.edge_index_dict)\n       print(\"nHeterogeneous GNN output embedding shapes:\")\n       for nt, t in out_dict.items():\n           print(f\"   {nt}: {tuple(t.shape)}\")\n   except Exception as e:\n       print(\"Hetero GNN forward skipped:\", e)\nprint(\"n<img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\" alt=\"\u2705\" class=\"wp-smiley\" \/> Done \u2014 proximity comparison, hetero construction, and a trained spatial GNN.\")\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p class=\"wp-block-paragraph\">We use the trained GraphSAGE model to extract node embeddings and predictions from the homogeneous graph. We reduce the learned embeddings with PCA and visualize them alongside a geographic prediction map to understand how the model separates urban functions. We also run a heterogeneous GNN forward pass with to_hetero, showing that the tutorial supports both homogeneous training and heterogeneous graph experimentation.<\/p>\n<h3 class=\"wp-block-heading\"><strong>Key Takeaways<\/strong><\/h3>\n<ul class=\"wp-block-list\">\n<li>city2graph turns raw OpenStreetMap POI and street data into spatial graphs.<\/li>\n<li>Six proximity graph families (KNN, Delaunay, Gabriel, RNG, EMST, Waxman) connect the same POIs differently.<\/li>\n<li>A synthetic clustered fallback keeps the workflow runnable without OSM access.<\/li>\n<li>A two-layer GraphSAGE model predicts urban function categories from spatial structure.<\/li>\n<li>The pipeline supports both homogeneous training and heterogeneous graph experimentation via to_hetero.<\/li>\n<\/ul>\n<h3 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">In conclusion, we completed a full spatial GNN pipeline that transforms raw city data into graph-based learning and visualization. We compared several proximity graph methods, built a heterogeneous multi-layer graph, trained a homogeneous GraphSAGE classifier, and inspected the learned embeddings and geographic predictions. It gives us a practical understanding of how spatial relationships among POIs can be represented as graph structures and used to predict urban functions. It also shows how city2graph, GeoPandas, OSMnx, and PyTorch Geometric work together to support advanced geospatial machine learning experiments in a Colab-friendly setup.<\/p>\n<p class=\"wp-block-paragraph\">\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<\/p><p class=\"wp-block-paragraph\">Check out\u00a0the\u00a0<strong><a href=\"https:\/\/github.com\/MARKTECHPOST-AI-MEDIA-INC\/AI-Agents-Projects-Tutorials\/blob\/main\/Data%20Analysis\/city2graph_spatial_gnn_urban_function_inference_Marktechpost.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">Full Codes with Notebook here<\/a>.\u00a0<\/strong>Also,\u00a0feel free to follow us on\u00a0<strong><a href=\"https:\/\/x.com\/intent\/follow?screen_name=marktechpost\" target=\"_blank\" rel=\"noreferrer noopener\"><mark>Twitter<\/mark><\/a><\/strong>\u00a0and don\u2019t forget to join our\u00a0<strong><a href=\"https:\/\/www.reddit.com\/r\/machinelearningnews\/\" target=\"_blank\" rel=\"noreferrer noopener\">150k+ML SubReddit<\/a><\/strong>\u00a0and Subscribe to\u00a0<strong><a href=\"https:\/\/www.aidevsignals.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">our Newsletter<\/a><\/strong>. Wait! are you on telegram?\u00a0<strong><a href=\"https:\/\/t.me\/machinelearningresearchnews\" target=\"_blank\" rel=\"noreferrer noopener\">now you can join us on telegram as well.<\/a><\/strong><\/p>\n<p class=\"wp-block-paragraph\">Need to partner with us for promoting your GitHub Repo OR Hugging Face Page OR Product Release OR Webinar etc.?\u00a0<strong><a href=\"https:\/\/forms.gle\/wbash1wF6efRj8G58\" target=\"_blank\" rel=\"noreferrer noopener\"><mark>Connect with us<\/mark><\/a><\/strong><\/p>\n<p>The post <a href=\"https:\/\/www.marktechpost.com\/2026\/06\/12\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/\">A Coding Implementation on Spatial Graph Neural Networks for Urban Function Inference Using city2graph, OSMnx, and PyTorch Geometric<\/a> appeared first on <a href=\"https:\/\/www.marktechpost.com\/\">MarkTechPost<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>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 Copy CodeCopiedUse a different Browser !pip -q install &#8220;city2graph[cpu]&#8221; osmnx contextily scikit-learn 2&gt;\/dev\/null import warnings, numpy as np, pandas as pd, geopandas as gpd warnings.filterwarnings(&#8220;ignore&#8221;) from shapely.geometry import Point import matplotlib.pyplot as plt import city2graph as c2g print(&#8220;city2graph version:&#8221;, getattr(c2g, &#8220;__version__&#8221;, &#8220;unknown&#8221;)) print(&#8220;PyTorch \/ PyG available:&#8221;, c2g.is_torch_available()) import torch import torch.nn.functional as F from torch_geometric.nn import SAGEConv, to_hetero from torch_geometric.utils import to_undirected from sklearn.preprocessing import StandardScaler from sklearn.neighbors import NearestNeighbors from sklearn.metrics import accuracy_score, f1_score from sklearn.decomposition import PCA SEED = 42 np.random.seed(SEED); torch.manual_seed(SEED) 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 Copy CodeCopiedUse a different Browser CENTER = (35.6595, 139.7005) DIST_M = 1100 TAG_QUERIES = { &#8220;food&#8221;: {&#8220;amenity&#8221;: [&#8220;restaurant&#8221;, &#8220;cafe&#8221;, &#8220;fast_food&#8221;, &#8220;bar&#8221;, &#8220;pub&#8221;]}, &#8220;retail&#8221;: {&#8220;shop&#8221;: True}, &#8220;education&#8221;: {&#8220;amenity&#8221;: [&#8220;school&#8221;, &#8220;university&#8221;, &#8220;college&#8221;, &#8220;kindergarten&#8221;, &#8220;library&#8221;]}, &#8220;health&#8221;: {&#8220;amenity&#8221;: [&#8220;hospital&#8221;, &#8220;clinic&#8221;, &#8220;pharmacy&#8221;, &#8220;doctors&#8221;, &#8220;dentist&#8221;]}, } def to_points(gdf): g = gdf.copy() g[&#8220;geometry&#8221;] = g.geometry.representative_point() return g poi_gdf, segments_gdf = None, None try: import osmnx as ox ox.settings.use_cache = True ox.settings.log_console = False frames = [] for label, tags in TAG_QUERIES.items(): try: f = ox.features_from_point(CENTER, tags=tags, dist=DIST_M) f = f[f.geometry.notna()] if len(f): f = to_points(f)[[&#8220;geometry&#8221;]].copy() f[&#8220;category&#8221;] = label frames.append(f) except Exception as e: print(f&#8221; (skip {label}: {e})&#8221;) if not frames: raise RuntimeError(&#8220;No POIs returned from Overpass.&#8221;) poi_gdf = gpd.GeoDataFrame(pd.concat(frames, ignore_index=True), crs=&#8221;EPSG:4326&#8243;) G = ox.graph_from_point(CENTER, dist=DIST_M, network_type=&#8221;walk&#8221;) segments_gdf = ox.graph_to_gdfs(G, nodes=False, edges=True).reset_index(drop=True)[[&#8220;geometry&#8221;]] print(f&#8221;OSM acquisition OK -&gt; {len(poi_gdf)} POIs, {len(segments_gdf)} street segments&#8221;) except Exception as e: print(f&#8221;OSM unavailable ({e}) -&gt; generating synthetic clustered POIs.&#8221;) rng = np.random.default_rng(SEED) cats = list(TAG_QUERIES.keys()) centers = rng.uniform(-0.01, 0.01, size=(8, 2)) + np.array(CENTER[::-1]) rows = [] for ci, c in enumerate(centers): dom = cats[ci % len(cats)] n = rng.integers(40, 90) pts = c + rng.normal(0, 0.0016, size=(n, 2)) for (lon, lat) in pts: cat = dom if rng.random() &lt; 0.75 else rng.choice(cats) rows.append({&#8220;geometry&#8221;: Point(lon, lat), &#8220;category&#8221;: cat}) poi_gdf = gpd.GeoDataFrame(rows, crs=&#8221;EPSG:4326&#8243;) segments_gdf = None print(f&#8221;Synthetic dataset -&gt; {len(poi_gdf)} POIs&#8221;) if len(poi_gdf) &gt; 700: poi_gdf = poi_gdf.sample(700, random_state=SEED).reset_index(drop=True) metric_crs = poi_gdf.estimate_utm_crs() poi_gdf = poi_gdf.to_crs(metric_crs).reset_index(drop=True) if segments_gdf is not None: segments_gdf = segments_gdf.to_crs(metric_crs) print(&#8220;Class balance:n&#8221;, poi_gdf[&#8220;category&#8221;].value_counts()) 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 Copy CodeCopiedUse a different Browser poi_gdf[&#8220;cx&#8221;] = poi_gdf.geometry.x poi_gdf[&#8220;cy&#8221;] = poi_gdf.geometry.y coords = poi_gdf[[&#8220;cx&#8221;, &#8220;cy&#8221;]].to_numpy() nn = NearestNeighbors(radius=150.0).fit(coords) poi_gdf[&#8220;local_density&#8221;] = [len(idx) &#8211; 1 for idx in nn.radius_neighbors(coords, return_distance=False)] if segments_gdf is not None and len(segments_gdf): try: joined = gpd.sjoin_nearest(poi_gdf[[&#8220;geometry&#8221;]], segments_gdf[[&#8220;geometry&#8221;]], distance_col=&#8221;dist_street&#8221;) poi_gdf[&#8220;dist_street&#8221;] = joined.groupby(level=0)[&#8220;dist_street&#8221;].min().reindex(poi_gdf.index).fillna(0.0) except Exception: poi_gdf[&#8220;dist_street&#8221;] = 0.0 else: poi_gdf[&#8220;dist_street&#8221;] = 0.0 poi_gdf[&#8220;category&#8221;] = poi_gdf[&#8220;category&#8221;].astype(&#8220;category&#8221;) poi_gdf[&#8220;label&#8221;] = poi_gdf[&#8220;category&#8221;].cat.codes.astype(int) CLASS_NAMES = list(poi_gdf[&#8220;category&#8221;].cat.categories) print(&#8220;Classes:&#8221;, CLASS_NAMES) def graph_stats(name, builder): try: nodes, edges = builder() deg = pd.Series(np.r_[edges.index.get_level_values(0), edges.index.get_level_values(1)]).value_counts() return name, len(edges), round(deg.mean(), 2), (nodes, edges) except Exception as e: return name, f&#8221;ERR: {e}&#8221;, None, None builders = { &#8220;KNN (k=8)&#8221;: lambda: c2g.knn_graph(poi_gdf, distance_metric=&#8221;euclidean&#8221;, k=8, as_nx=False), &#8220;Delaunay&#8221;: lambda: c2g.delaunay_graph(poi_gdf, as_nx=False), &#8220;Gabriel&#8221;: lambda: c2g.gabriel_graph(poi_gdf, as_nx=False), &#8220;RNG&#8221;: lambda: c2g.relative_neighborhood_graph(poi_gdf, as_nx=False), &#8220;EMST&#8221;: lambda: c2g.euclidean_minimum_spanning_tree(poi_gdf, as_nx=False), &#8220;Waxman&#8221;: lambda: c2g.waxman_graph(poi_gdf, distance_metric=&#8221;euclidean&#8221;, r0=150, beta=0.6), } print(&#8220;n&#8212; Proximity graph comparison &#8212;&#8220;) print(f&#8221;{&#8216;graph&#8217;:&lt;14}{&#8216;#edges&#8217;:&gt;10}{&#8216;avg_degree&#8217;:&gt;12}&#8221;) built = {} for nm, b in builders.items(): name, ne, avgdeg, payload = graph_stats(nm, b) print(f&#8221;{name:&lt;14}{str(ne):&gt;10}{str(avgdeg):&gt;12}&#8221;) if payload: built[nm] = payload fig, axes = plt.subplots(1, 3, figsize=(16, 5)) for ax, key in zip(axes, [&#8220;KNN (k=8)&#8221;, &#8220;Delaunay&#8221;, &#8220;EMST&#8221;]): if key in built: n_, e_ = built[key] e_.plot(ax=ax, linewidth=0.4, color=&#8221;#3b7dd8&#8243;, alpha=0.6) poi_gdf.plot(ax=ax, markersize=4, color=&#8221;#d83b5c&#8221;) ax.set_title(key); ax.set_axis_off() plt.suptitle(&#8220;Spatial graph topologies on the same POI set&#8221;, y=1.02) plt.tight_layout(); plt.show() We engineer spatial features for each POI by extracting its projected coordinates, calculating local density, and estimating distance to the nearest street segment. We then assign category labels and build several families of proximity graphs, including KNN, Delaunay, Gabriel, RNG, EMST, and Waxman. We compare their edge counts and average degrees, then visualize selected graph topologies to see how differently they connect the same set of POIs. Constructing Heterogeneous and Homogeneous Graphs in PyTorch Geometric Copy CodeCopiedUse a different Browser nodes_dict = {} for cat in CLASS_NAMES: sub = poi_gdf[poi_gdf[&#8220;category&#8221;] == cat].copy().reset_index(drop=True) nodes_dict[cat] = sub[[&#8220;geometry&#8221;, &#8220;cx&#8221;, &#8220;cy&#8221;, &#8220;local_density&#8221;]] try: _, bridge_edges = c2g.bridge_nodes(nodes_dict, proximity_method=&#8221;knn&#8221;, k=3, distance_metric=&#8221;euclidean&#8221;) hetero = c2g.gdf_to_pyg( nodes_dict, bridge_edges, node_feature_cols={cat: [&#8220;cx&#8221;, &#8220;cy&#8221;, &#8220;local_density&#8221;] for cat in CLASS_NAMES}, ) print(&#8220;nHeteroData node types:&#8221;, hetero.node_types) print(&#8220;HeteroData edge types:&#8221;) for et in hetero.edge_types: print(f&#8221; {et}: {hetero[et].edge_index.shape[1]} edges&#8221;) except Exception as e: hetero = None print(&#8220;Heterogeneous build skipped:&#8221;, e) nodes, edges = c2g.knn_graph(poi_gdf, distance_metric=&#8221;euclidean&#8221;, k=8, as_nx=False) deg = pd.Series(np.r_[edges.index.get_level_values(0), edges.index.get_level_values(1)]).value_counts() nodes[&#8220;degree&#8221;] = deg.reindex(nodes.index).fillna(0).astype(float) for col in [&#8220;cx&#8221;, &#8220;cy&#8221;, &#8220;local_density&#8221;, &#8220;dist_street&#8221;, &#8220;label&#8221;]: if col not in nodes.columns: nodes[col] = poi_gdf.loc[nodes.index, col].values FEATS = [&#8220;cx&#8221;, &#8220;cy&#8221;, &#8220;local_density&#8221;, &#8220;dist_street&#8221;, &#8220;degree&#8221;] nodes[FEATS] = StandardScaler().fit_transform(nodes[FEATS].astype(float)) data = c2g.gdf_to_pyg(nodes, edges, node_feature_cols=FEATS, node_label_cols=[&#8220;label&#8221;]) data.edge_index = to_undirected(data.edge_index) data.x = data.x.float() y = data.y.long().view(-1) N, num_classes = data.num_nodes, int(y.max()) + 1 print(f&#8221;nHomogeneous Data: {N} nodes, {data.edge_index.shape[1]} directed-edges, &#8221; f&#8221;{data.x.shape[1]} features, {num_classes} classes&#8221;) We construct<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"pmpro_default_level":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_pvb_checkbox_block_on_post":false,"footnotes":""},"categories":[52,5,7,1],"tags":[],"class_list":["post-97157","post","type-post","status-publish","format-standard","hentry","category-ai-club","category-committee","category-news","category-uncategorized","pmpro-has-access"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A Coding Implementation on Spatial Graph Neural Networks for Urban Function Inference Using city2graph, OSMnx, and PyTorch Geometric - YouZum<\/title>\n<meta name=\"description\" content=\"\u0e01\u0e34\u0e08\u0e01\u0e23\u0e23\u0e21\u0e40\u0e01\u0e35\u0e48\u0e22\u0e27\u0e01\u0e31\u0e1a\u0e42\u0e14\u0e23\u0e19\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/youzum.net\/fr\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Coding Implementation on Spatial Graph Neural Networks for Urban Function Inference Using city2graph, OSMnx, and PyTorch Geometric - YouZum\" \/>\n<meta property=\"og:description\" content=\"\u0e01\u0e34\u0e08\u0e01\u0e23\u0e23\u0e21\u0e40\u0e01\u0e35\u0e48\u0e22\u0e27\u0e01\u0e31\u0e1a\u0e42\u0e14\u0e23\u0e19\" \/>\n<meta property=\"og:url\" content=\"https:\/\/youzum.net\/fr\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/\" \/>\n<meta property=\"og:site_name\" content=\"YouZum\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DroneAssociationTH\/\" \/>\n<meta property=\"article:published_time\" content=\"2026-06-13T17:57:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\" \/>\n<meta name=\"author\" content=\"admin NU\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u00c9crit par\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin NU\" \/>\n\t<meta name=\"twitter:label2\" content=\"Dur\u00e9e de lecture estim\u00e9e\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/\"},\"author\":{\"name\":\"admin NU\",\"@id\":\"https:\/\/yousum.gpucore.co\/#\/schema\/person\/97fa48242daf3908e4d9a5f26f4a059c\"},\"headline\":\"A Coding Implementation on Spatial Graph Neural Networks for Urban Function Inference Using city2graph, OSMnx, and PyTorch Geometric\",\"datePublished\":\"2026-06-13T17:57:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/\"},\"wordCount\":846,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/yousum.gpucore.co\/#organization\"},\"image\":{\"@id\":\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\",\"articleSection\":[\"AI\",\"Committee\",\"News\",\"Uncategorized\"],\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/\",\"url\":\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/\",\"name\":\"A Coding Implementation on Spatial Graph Neural Networks for Urban Function Inference Using city2graph, OSMnx, and PyTorch Geometric - YouZum\",\"isPartOf\":{\"@id\":\"https:\/\/yousum.gpucore.co\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\",\"datePublished\":\"2026-06-13T17:57:52+00:00\",\"description\":\"\u0e01\u0e34\u0e08\u0e01\u0e23\u0e23\u0e21\u0e40\u0e01\u0e35\u0e48\u0e22\u0e27\u0e01\u0e31\u0e1a\u0e42\u0e14\u0e23\u0e19\",\"breadcrumb\":{\"@id\":\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#primaryimage\",\"url\":\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\",\"contentUrl\":\"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/youzum.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A Coding Implementation on Spatial Graph Neural Networks for Urban Function Inference Using city2graph, OSMnx, and PyTorch Geometric\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/yousum.gpucore.co\/#website\",\"url\":\"https:\/\/yousum.gpucore.co\/\",\"name\":\"YouSum\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/yousum.gpucore.co\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/yousum.gpucore.co\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/yousum.gpucore.co\/#organization\",\"name\":\"Drone Association Thailand\",\"url\":\"https:\/\/yousum.gpucore.co\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/yousum.gpucore.co\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/youzum.net\/wp-content\/uploads\/2024\/11\/tranparent-logo.png\",\"contentUrl\":\"https:\/\/youzum.net\/wp-content\/uploads\/2024\/11\/tranparent-logo.png\",\"width\":300,\"height\":300,\"caption\":\"Drone Association Thailand\"},\"image\":{\"@id\":\"https:\/\/yousum.gpucore.co\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/DroneAssociationTH\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/yousum.gpucore.co\/#\/schema\/person\/97fa48242daf3908e4d9a5f26f4a059c\",\"name\":\"admin NU\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/yousum.gpucore.co\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/youzum.net\/wp-content\/uploads\/avatars\/2\/1746849356-bpfull.png\",\"contentUrl\":\"https:\/\/youzum.net\/wp-content\/uploads\/avatars\/2\/1746849356-bpfull.png\",\"caption\":\"admin NU\"},\"url\":\"https:\/\/youzum.net\/fr\/members\/adminnu\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A Coding Implementation on Spatial Graph Neural Networks for Urban Function Inference Using city2graph, OSMnx, and PyTorch Geometric - YouZum","description":"\u0e01\u0e34\u0e08\u0e01\u0e23\u0e23\u0e21\u0e40\u0e01\u0e35\u0e48\u0e22\u0e27\u0e01\u0e31\u0e1a\u0e42\u0e14\u0e23\u0e19","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/youzum.net\/fr\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/","og_locale":"fr_FR","og_type":"article","og_title":"A Coding Implementation on Spatial Graph Neural Networks for Urban Function Inference Using city2graph, OSMnx, and PyTorch Geometric - YouZum","og_description":"\u0e01\u0e34\u0e08\u0e01\u0e23\u0e23\u0e21\u0e40\u0e01\u0e35\u0e48\u0e22\u0e27\u0e01\u0e31\u0e1a\u0e42\u0e14\u0e23\u0e19","og_url":"https:\/\/youzum.net\/fr\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/","og_site_name":"YouZum","article_publisher":"https:\/\/www.facebook.com\/DroneAssociationTH\/","article_published_time":"2026-06-13T17:57:52+00:00","og_image":[{"url":"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png","type":"","width":"","height":""}],"author":"admin NU","twitter_card":"summary_large_image","twitter_misc":{"\u00c9crit par":"admin NU","Dur\u00e9e de lecture estim\u00e9e":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#article","isPartOf":{"@id":"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/"},"author":{"name":"admin NU","@id":"https:\/\/yousum.gpucore.co\/#\/schema\/person\/97fa48242daf3908e4d9a5f26f4a059c"},"headline":"A Coding Implementation on Spatial Graph Neural Networks for Urban Function Inference Using city2graph, OSMnx, and PyTorch Geometric","datePublished":"2026-06-13T17:57:52+00:00","mainEntityOfPage":{"@id":"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/"},"wordCount":846,"commentCount":0,"publisher":{"@id":"https:\/\/yousum.gpucore.co\/#organization"},"image":{"@id":"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#primaryimage"},"thumbnailUrl":"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png","articleSection":["AI","Committee","News","Uncategorized"],"inLanguage":"fr-FR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/","url":"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/","name":"A Coding Implementation on Spatial Graph Neural Networks for Urban Function Inference Using city2graph, OSMnx, and PyTorch Geometric - YouZum","isPartOf":{"@id":"https:\/\/yousum.gpucore.co\/#website"},"primaryImageOfPage":{"@id":"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#primaryimage"},"image":{"@id":"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#primaryimage"},"thumbnailUrl":"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png","datePublished":"2026-06-13T17:57:52+00:00","description":"\u0e01\u0e34\u0e08\u0e01\u0e23\u0e23\u0e21\u0e40\u0e01\u0e35\u0e48\u0e22\u0e27\u0e01\u0e31\u0e1a\u0e42\u0e14\u0e23\u0e19","breadcrumb":{"@id":"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/"]}]},{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#primaryimage","url":"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png","contentUrl":"https:\/\/s.w.org\/images\/core\/emoji\/17.0.2\/72x72\/2705.png"},{"@type":"BreadcrumbList","@id":"https:\/\/youzum.net\/a-coding-implementation-on-spatial-graph-neural-networks-for-urban-function-inference-using-city2graph-osmnx-and-pytorch-geometric\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/youzum.net\/"},{"@type":"ListItem","position":2,"name":"A Coding Implementation on Spatial Graph Neural Networks for Urban Function Inference Using city2graph, OSMnx, and PyTorch Geometric"}]},{"@type":"WebSite","@id":"https:\/\/yousum.gpucore.co\/#website","url":"https:\/\/yousum.gpucore.co\/","name":"YouSum","description":"","publisher":{"@id":"https:\/\/yousum.gpucore.co\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/yousum.gpucore.co\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"},{"@type":"Organization","@id":"https:\/\/yousum.gpucore.co\/#organization","name":"Drone Association Thailand","url":"https:\/\/yousum.gpucore.co\/","logo":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/yousum.gpucore.co\/#\/schema\/logo\/image\/","url":"https:\/\/youzum.net\/wp-content\/uploads\/2024\/11\/tranparent-logo.png","contentUrl":"https:\/\/youzum.net\/wp-content\/uploads\/2024\/11\/tranparent-logo.png","width":300,"height":300,"caption":"Drone Association Thailand"},"image":{"@id":"https:\/\/yousum.gpucore.co\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DroneAssociationTH\/"]},{"@type":"Person","@id":"https:\/\/yousum.gpucore.co\/#\/schema\/person\/97fa48242daf3908e4d9a5f26f4a059c","name":"admin NU","image":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/yousum.gpucore.co\/#\/schema\/person\/image\/","url":"https:\/\/youzum.net\/wp-content\/uploads\/avatars\/2\/1746849356-bpfull.png","contentUrl":"https:\/\/youzum.net\/wp-content\/uploads\/avatars\/2\/1746849356-bpfull.png","caption":"admin NU"},"url":"https:\/\/youzum.net\/fr\/members\/adminnu\/"}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"admin NU","author_link":"https:\/\/youzum.net\/fr\/members\/adminnu\/"},"rttpg_comment":0,"rttpg_category":"<a href=\"https:\/\/youzum.net\/fr\/category\/ai-club\/\" rel=\"category tag\">AI<\/a> <a href=\"https:\/\/youzum.net\/fr\/category\/committee\/\" rel=\"category tag\">Committee<\/a> <a href=\"https:\/\/youzum.net\/fr\/category\/news\/\" rel=\"category tag\">News<\/a> <a href=\"https:\/\/youzum.net\/fr\/category\/uncategorized\/\" rel=\"category tag\">Uncategorized<\/a>","rttpg_excerpt":"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\u2026","_links":{"self":[{"href":"https:\/\/youzum.net\/fr\/wp-json\/wp\/v2\/posts\/97157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/youzum.net\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/youzum.net\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/youzum.net\/fr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/youzum.net\/fr\/wp-json\/wp\/v2\/comments?post=97157"}],"version-history":[{"count":0,"href":"https:\/\/youzum.net\/fr\/wp-json\/wp\/v2\/posts\/97157\/revisions"}],"wp:attachment":[{"href":"https:\/\/youzum.net\/fr\/wp-json\/wp\/v2\/media?parent=97157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/youzum.net\/fr\/wp-json\/wp\/v2\/categories?post=97157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/youzum.net\/fr\/wp-json\/wp\/v2\/tags?post=97157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}