OSM - cityseer

Last update

November 30, 2024

See the accompanying discussion in the guide.

Install and update cityseer if necessary.

# !pip install --upgrade cityseer
# !pip install osmnx

Setup

If you’re prepared to get your hands dirty, it is possible to directly query the OSM API and to then use this data to create a cityseer graph. This can be preferred in situations where you want detailed control over the structure of the OSM API query. However, if you prefer, OSM data can also be retrieved with OSMnx and then converted into a cityseer compatible networkX graph.

The following example makes use of data for London Soho.

import osmnx as ox
from shapely import geometry
import utm

from cityseer.tools import graphs, plot, io

# centrepoint
lng, lat = -0.13396079424572427, 51.51371088849723

# select extents for plotting
easting, northing = utm.from_latlon(lat, lng)[:2]
buffer_dist = 1250
buffer_poly = geometry.Point(easting, northing).buffer(1000)
min_x, min_y, max_x, max_y = buffer_poly.bounds


# reusable plot function
def simple_plot(_G):
    # plot using the selected extents
    plot.plot_nx(
        _G,
        labels=False,
        plot_geoms=True,
        node_size=4,
        edge_width=1,
        x_lim=(min_x, max_x),
        y_lim=(min_y, max_y),
        figsize=(6, 6),
        dpi=150,
    )


# Let's use OSMnx to fetch an OSM graph
# We'll use the same raw network for both workflows (hence simplify=False)
multi_di_graph_raw = ox.graph_from_point((lat, lng), dist=buffer_dist, simplify=False)

Workflow 1

One option is to both download and simplify the data in OSMnx and to then convert the graph from OSMnx to cityseer (per the io.nx_from_osm_nx method).

# Workflow 1: Using OSMnx to prepare the graph
# ============================================
# explicit simplification and consolidation via OSMnx
multi_di_graph_utm = ox.project_graph(multi_di_graph_raw)
multi_di_graph_simpl = ox.simplify_graph(multi_di_graph_utm)
multi_di_graph_cons = ox.consolidate_intersections(
    multi_di_graph_simpl, tolerance=10, dead_ends=True
)
# let's use the same plotting function for both scenarios to aid visual comparisons
multi_graph_cons = io.nx_from_osm_nx(multi_di_graph_cons, tolerance=50)
simple_plot(multi_graph_cons)
INFO:cityseer.tools.io:Converting OSMnx MultiDiGraph to cityseer MultiGraph.
100%|██████████| 7663/7663 [00:01<00:00, 5540.06it/s]
INFO:cityseer.tools.plot:Preparing graph nodes
INFO:cityseer.tools.plot:Preparing graph edges
100%|██████████| 4390/4390 [00:00<00:00, 20269.83it/s]

Workflow 2

Another option is to simply download the OSM graph directly from within cityseer. The following uses a default OSM query, however, the OSM Overpass API can be used to roll-your-own. See documentation for more information.

# WORKFLOW 2: Using cityseer to download and automatically simplify the graph
# ===========================================================================
poly_wgs, _ = io.buffered_point_poly(lng, lat, buffer_dist)
G_utm = io.osm_graph_from_poly(poly_wgs, simplify=True)
simple_plot(G_utm)
WARNING:cityseer.tools.io:Unable to parse level info: -`;-4
WARNING:cityseer.tools.io:Unable to parse level info: -`;-4
WARNING:cityseer.tools.io:Unable to parse level info: -`;-4
INFO:cityseer.tools.io:Converting networkX graph from EPSG code 4326 to EPSG code 32630.
INFO:cityseer.tools.io:Processing node x, y coordinates.
100%|██████████| 14807/14807 [00:00<00:00, 439324.16it/s]
INFO:cityseer.tools.io:Processing edge geom coordinates, if present.
100%|██████████| 16107/16107 [00:00<00:00, 740244.29it/s]
INFO:cityseer.tools.graphs:Generating interpolated edge geometries.
100%|██████████| 16107/16107 [00:00<00:00, 57870.53it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 14807/14807 [00:02<00:00, 6451.58it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 7848/7848 [00:00<00:00, 472855.61it/s]
100%|██████████| 7848/7848 [00:02<00:00, 3795.43it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 6548/6548 [00:00<00:00, 23363.44it/s]
100%|██████████| 5079/5079 [00:00<00:00, 33004.53it/s]
INFO:cityseer.tools.graphs:Removing dangling nodes.
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 5677/5677 [00:00<00:00, 283931.50it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 4592/4592 [00:00<00:00, 581634.47it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 3537/3537 [00:00<00:00, 110482.46it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 4592/4592 [00:00<00:00, 55651.29it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 4564/4564 [00:00<00:00, 438000.31it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 3537/3537 [00:00<00:00, 12090.62it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 4617/4617 [00:00<00:00, 117831.29it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 4607/4607 [00:00<00:00, 453222.90it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 3537/3537 [00:00<00:00, 56660.73it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 4608/4608 [00:00<00:00, 142304.37it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 4608/4608 [00:00<00:00, 386810.09it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 3537/3537 [00:00<00:00, 35616.46it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 4609/4609 [00:00<00:00, 169779.01it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 3537/3537 [00:00<00:00, 70694.90it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 3537/3537 [00:00<00:00, 128787.18it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 4609/4609 [00:00<00:00, 174239.71it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 3537/3537 [00:00<00:00, 145533.55it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 3502/3502 [00:00<00:00, 60738.25it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 3502/3502 [00:00<00:00, 11631.62it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 4166/4166 [00:00<00:00, 27836.81it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 3180/3180 [00:00<00:00, 176263.87it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 3147/3147 [00:00<00:00, 63096.51it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 3147/3147 [00:00<00:00, 26721.53it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 3986/3986 [00:00<00:00, 90436.73it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 3113/3113 [00:00<00:00, 340368.30it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 3105/3105 [00:00<00:00, 57833.07it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 3105/3105 [00:00<00:00, 19035.98it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 3883/3883 [00:00<00:00, 102713.65it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 3036/3036 [00:00<00:00, 319281.57it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 3030/3030 [00:00<00:00, 5977.97it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 3870/3870 [00:00<00:00, 583778.33it/s]
INFO:cityseer.tools.graphs:Snapping gapped endings.
100%|██████████| 3030/3030 [00:00<00:00, 8043.87it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 3985/3985 [00:00<00:00, 543218.87it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 3030/3030 [00:00<00:00, 8091.44it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 4457/4457 [00:00<00:00, 159507.95it/s]
INFO:cityseer.tools.graphs:Removing dangling nodes.
100%|██████████| 3293/3293 [00:00<00:00, 225271.45it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 2803/2803 [00:00<00:00, 11731.19it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 3289/3289 [00:00<00:00, 394403.92it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 2126/2126 [00:00<00:00, 5028.93it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 3307/3307 [00:00<00:00, 59999.50it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 2126/2126 [00:00<00:00, 59239.93it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 2126/2126 [00:00<00:00, 3416.91it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 2745/2745 [00:00<00:00, 26092.20it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 2640/2640 [00:00<00:00, 715677.52it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 1742/1742 [00:00<00:00, 6260.83it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 2643/2643 [00:00<00:00, 102307.65it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 1742/1742 [00:00<00:00, 61838.60it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 1742/1742 [00:00<00:00, 3953.75it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 2329/2329 [00:00<00:00, 29974.21it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 1481/1481 [00:00<00:00, 51119.74it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 50.
100%|██████████| 2190/2190 [00:00<00:00, 20668.71it/s]
INFO:cityseer.tools.graphs:Ironing edges.
100%|██████████| 2174/2174 [00:00<00:00, 14169.42it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 1.
100%|██████████| 2162/2162 [00:00<00:00, 6590.96it/s]
INFO:cityseer.tools.graphs:Removing dangling nodes.
100%|██████████| 1396/1396 [00:00<00:00, 384480.16it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 1386/1386 [00:00<00:00, 120645.54it/s]
INFO:cityseer.tools.plot:Preparing graph nodes
INFO:cityseer.tools.plot:Preparing graph edges
100%|██████████| 2122/2122 [00:00<00:00, 29194.85it/s]