# !pip install --upgrade cityseer
# !pip install osmnx
OSM - cityseer
See the accompanying discussion in the guide.
Install and update cityseer
if necessary.
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
= -0.13396079424572427, 51.51371088849723
lng, lat
# select extents for plotting
= utm.from_latlon(lat, lng)[:2]
easting, northing = 1250
buffer_dist = geometry.Point(easting, northing).buffer(1000)
buffer_poly = buffer_poly.bounds
min_x, min_y, max_x, max_y
# reusable plot function
def simple_plot(_G):
# plot using the selected extents
plot.plot_nx(
_G,=False,
labels=True,
plot_geoms=4,
node_size=1,
edge_width=(min_x, max_x),
x_lim=(min_y, max_y),
y_lim=(6, 6),
figsize=150,
dpi
)
# Let's use OSMnx to fetch an OSM graph
# We'll use the same raw network for both workflows (hence simplify=False)
= ox.graph_from_point((lat, lng), dist=buffer_dist, simplify=False) multi_di_graph_raw
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
= ox.project_graph(multi_di_graph_raw)
multi_di_graph_utm = ox.simplify_graph(multi_di_graph_utm)
multi_di_graph_simpl = ox.consolidate_intersections(
multi_di_graph_cons =10, dead_ends=True
multi_di_graph_simpl, tolerance
)# let's use the same plotting function for both scenarios to aid visual comparisons
= io.nx_from_osm_nx(multi_di_graph_cons, tolerance=50)
multi_graph_cons simple_plot(multi_graph_cons)
INFO:cityseer.tools.io:Converting OSMnx MultiDiGraph to cityseer MultiGraph.
100%|██████████| 6036/6036 [00:01<00:00, 4437.64it/s]
INFO:cityseer.tools.plot:Preparing graph nodes
INFO:cityseer.tools.plot:Preparing graph edges
100%|██████████| 3595/3595 [00:00<00:00, 20294.44it/s]
Workflow 2
Another option is to immediately convert the osmnx
graph to cityseer
before downstream processing. This is preferable because it is then easier for cityseer
to retain the integrity of the roadway geometries while enforcing node coordinate validation for intersections.
# WORKFLOW 2: Using cityseer to manually clean an OSMnx graph
# ===========================================================
= io.nx_from_osm_nx(multi_di_graph_raw) G_raw
INFO:cityseer.tools.io:Converting OSMnx MultiDiGraph to cityseer MultiGraph.
100%|██████████| 30898/30898 [00:07<00:00, 4326.17it/s]
= io.nx_wgs_to_utm(G_raw)
G = graphs.nx_simple_geoms(G)
G = graphs.nx_remove_filler_nodes(G)
G = graphs.nx_remove_dangling_nodes(G) G
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%|██████████| 15843/15843 [00:00<00:00, 312282.86it/s]
INFO:cityseer.tools.io:Processing edge geom coordinates, if present.
100%|██████████| 18245/18245 [00:02<00:00, 6530.14it/s]
INFO:cityseer.tools.graphs:Generating interpolated edge geometries.
100%|██████████| 18245/18245 [00:00<00:00, 39269.89it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 15843/15843 [00:02<00:00, 6851.57it/s]
INFO:cityseer.tools.graphs:Removing dangling nodes.
100%|██████████| 5475/5475 [00:00<00:00, 246880.26it/s]
= graphs.nx_consolidate_nodes(G, buffer_dist=12, crawl=True)
G1 = graphs.nx_split_opposing_geoms(G1, buffer_dist=15)
G2 = graphs.nx_consolidate_nodes(G2, buffer_dist=15, neighbour_policy="indirect")
G3 = graphs.nx_remove_filler_nodes(G3)
G4 = graphs.nx_iron_edges(G4)
G5 simple_plot(G5)
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 4989/4989 [00:00<00:00, 67623.61it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 4989/4989 [00:02<00:00, 2288.62it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 4656/4656 [00:00<00:00, 9415.34it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 4101/4101 [00:00<00:00, 637493.17it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 2685/2685 [00:00<00:00, 2979.42it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 4361/4361 [00:00<00:00, 12188.54it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 2685/2685 [00:00<00:00, 82508.78it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 2685/2685 [00:00<00:00, 7964.17it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 3890/3890 [00:00<00:00, 114301.42it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 2648/2648 [00:00<00:00, 28091.59it/s]
INFO:cityseer.tools.graphs:Ironing edges.
100%|██████████| 3568/3568 [00:00<00:00, 4297.52it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 1.
100%|██████████| 3568/3568 [00:00<00:00, 34758.11it/s]
INFO:cityseer.tools.plot:Preparing graph nodes
INFO:cityseer.tools.plot:Preparing graph edges
100%|██████████| 3560/3560 [00:00<00:00, 25998.14it/s]
Workflow 3
A further 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 3: Using cityseer to download and automatically simplify the graph
# ===========================================================================
= io.buffered_point_poly(lng, lat, buffer_dist)
poly_wgs, _ = io.osm_graph_from_poly(poly_wgs, simplify=True)
G_utm simple_plot(G_utm)
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%|██████████| 12429/12429 [00:00<00:00, 411966.02it/s]
INFO:cityseer.tools.io:Processing edge geom coordinates, if present.
100%|██████████| 13773/13773 [00:00<00:00, 792593.11it/s]
INFO:cityseer.tools.graphs:Generating interpolated edge geometries.
100%|██████████| 13773/13773 [00:00<00:00, 63453.10it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 12429/12429 [00:01<00:00, 6430.53it/s]
INFO:cityseer.tools.graphs:Removing dangling nodes.
100%|██████████| 4170/4170 [00:00<00:00, 162044.26it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 4781/4781 [00:00<00:00, 550754.39it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 3358/3358 [00:00<00:00, 90179.30it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 60.
100%|██████████| 4781/4781 [00:00<00:00, 128016.19it/s]
WARNING:cityseer.tools.graphs:Be cautious with large buffer distances when using crawl!
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 3358/3358 [00:00<00:00, 66223.78it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 3358/3358 [00:00<00:00, 92212.08it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 60.
100%|██████████| 4781/4781 [00:00<00:00, 129266.40it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 3358/3358 [00:00<00:00, 20936.70it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 4428/4428 [00:00<00:00, 167363.96it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 3005/3005 [00:00<00:00, 75603.19it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 40.
100%|██████████| 4428/4428 [00:00<00:00, 122649.05it/s]
WARNING:cityseer.tools.graphs:Be cautious with large buffer distances when using crawl!
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 3005/3005 [00:00<00:00, 64465.38it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 3005/3005 [00:00<00:00, 77660.81it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 40.
100%|██████████| 4428/4428 [00:00<00:00, 88344.40it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 3005/3005 [00:00<00:00, 626279.93it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 4428/4428 [00:00<00:00, 15310.85it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 3005/3005 [00:01<00:00, 2707.85it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 40.
100%|██████████| 4595/4595 [00:00<00:00, 26443.73it/s]
WARNING:cityseer.tools.graphs:Be cautious with large buffer distances when using crawl!
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 3005/3005 [00:00<00:00, 56759.42it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 3005/3005 [00:00<00:00, 11112.55it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 40.
100%|██████████| 3917/3917 [00:00<00:00, 51039.13it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 2604/2604 [00:00<00:00, 284618.95it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 3873/3873 [00:00<00:00, 552967.95it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 2591/2591 [00:00<00:00, 32198.68it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 30.
100%|██████████| 3880/3880 [00:00<00:00, 74537.74it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 2591/2591 [00:00<00:00, 77438.18it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 2591/2591 [00:00<00:00, 52761.23it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 30.
100%|██████████| 3843/3843 [00:00<00:00, 95931.00it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 2568/2568 [00:00<00:00, 564427.64it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 3843/3843 [00:00<00:00, 621432.27it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 2568/2568 [00:00<00:00, 12985.37it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 30.
100%|██████████| 3863/3863 [00:00<00:00, 82458.06it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 2568/2568 [00:00<00:00, 70683.56it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 2568/2568 [00:00<00:00, 25106.81it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 30.
100%|██████████| 3732/3732 [00:00<00:00, 92279.78it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 2477/2477 [00:00<00:00, 443683.42it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 3727/3727 [00:00<00:00, 700837.08it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 2473/2473 [00:00<00:00, 11369.32it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 3736/3736 [00:00<00:00, 35870.42it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 2473/2473 [00:00<00:00, 77869.97it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 2473/2473 [00:00<00:00, 13033.01it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 3594/3594 [00:00<00:00, 92849.09it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 2369/2369 [00:00<00:00, 376932.07it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 3584/3584 [00:00<00:00, 684223.28it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 2361/2361 [00:00<00:00, 105637.24it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 30.
100%|██████████| 3584/3584 [00:00<00:00, 96317.61it/s]
WARNING:cityseer.tools.graphs:Be cautious with large buffer distances when using crawl!
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 2361/2361 [00:00<00:00, 75265.46it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 2361/2361 [00:00<00:00, 118595.83it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 30.
100%|██████████| 3584/3584 [00:00<00:00, 93952.41it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 2361/2361 [00:00<00:00, 521060.34it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 3584/3584 [00:00<00:00, 657728.53it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 2361/2361 [00:00<00:00, 25945.64it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 3588/3588 [00:00<00:00, 84907.43it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 2361/2361 [00:00<00:00, 74800.41it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 2361/2361 [00:00<00:00, 35500.10it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 3564/3564 [00:00<00:00, 47975.98it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 2351/2351 [00:00<00:00, 133322.63it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 3563/3563 [00:00<00:00, 521416.04it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 2351/2351 [00:00<00:00, 34965.30it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 3563/3563 [00:00<00:00, 89462.21it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 2351/2351 [00:00<00:00, 80679.49it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 2351/2351 [00:00<00:00, 44581.12it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 3556/3556 [00:00<00:00, 103059.99it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 2349/2349 [00:00<00:00, 524818.63it/s]
INFO:cityseer.tools.util:Creating edges STR tree.
100%|██████████| 3554/3554 [00:00<00:00, 651652.74it/s]
INFO:cityseer.tools.graphs:Splitting opposing edges.
100%|██████████| 2347/2347 [00:01<00:00, 2267.01it/s]
INFO:cityseer.tools.graphs:Squashing opposing nodes
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 3867/3867 [00:00<00:00, 14490.59it/s]
INFO:cityseer.tools.util:Creating nodes STR tree
100%|██████████| 2347/2347 [00:00<00:00, 71002.73it/s]
INFO:cityseer.tools.graphs:Consolidating nodes.
100%|██████████| 2347/2347 [00:00<00:00, 3063.43it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 25.
100%|██████████| 2912/2912 [00:00<00:00, 19564.89it/s]
INFO:cityseer.tools.graphs:Removing filler nodes.
100%|██████████| 1925/1925 [00:00<00:00, 57653.56it/s]
INFO:cityseer.tools.graphs:Ironing edges.
100%|██████████| 2754/2754 [00:00<00:00, 3669.98it/s]
INFO:cityseer.tools.graphs:Merging parallel edges within buffer of 1.
100%|██████████| 2754/2754 [00:00<00:00, 164271.99it/s]
INFO:cityseer.tools.plot:Preparing graph nodes
INFO:cityseer.tools.plot:Preparing graph edges
100%|██████████| 2754/2754 [00:00<00:00, 25726.77it/s]