Author: Geoff Boeing
import osmnx as ox
%matplotlib inline
ox.config(log_console=True, use_cache=True)
ox.__version__
# get the walking network for piedmont
G = ox.graph_from_place('Piedmont, California, USA', network_type='walk')
fig, ax = ox.plot_graph(G)
# project the network to an appropriate UTM (automatically determined)
G_projected = ox.project_graph(G)
# you can also plot/save figures as SVGs to work with in Illustrator later
fig, ax = ox.plot_graph(G_projected, save=True, file_format='svg')
# you can also create a network with a buffer distance (meters) around the place
G = ox.graph_from_place('Piedmont, California, USA', network_type='walk', buffer_dist=200)
fig, ax = ox.plot_graph(ox.project_graph(G))
If you have a very large network (ie, large city or metropolitan-scale), it will be much faster to project the graph first, then simplify its topology.
# create a network from multiple places
places = ['Piedmont, California, USA',
'Berkeley, California, USA',
'Emeryville, California, USA']
# use retain_all if you want to keep all disconnected subgraphs (e.g. when your places aren't adjacent)
G = ox.graph_from_place(places, network_type='drive', retain_all=True)
fig, ax = ox.plot_graph(G, save=True, filename='places')
# save network to disk as GraphML file
ox.save_graphml(G, filename='berkeley-emeryville-piedmont.graphml')
# or save graph to disk as ESRI shapefile
ox.save_graph_shapefile(G, filename='berkeley-emeryville-piedmont')
# you can also create a network with a buffer distance (meters) around the list of places
G = ox.graph_from_place(places, network_type='drive', retain_all=True, buffer_dist=500)
fig, ax = ox.plot_graph(G)
# or create a network from structured place queries
places = [{'city':'Daly City', 'state':'California'},
{'city':'South San Francisco', 'state':'California'}]
G = ox.graph_from_place(places, network_type='drive', buffer_dist=250)
fig, ax = ox.plot_graph(G)
# get the network for manhattan
G = ox.graph_from_place('Manhattan, New York, New York, USA', network_type='drive')
fig, ax = ox.plot_graph(G, fig_height=6, node_size=2, node_alpha=0.5,
edge_linewidth=0.3, save=True, dpi=100, filename='manhattan')
# get the network for all of LA - takes a couple minutes to do all the downloading and processing
place = 'Los Angeles, Los Angeles County, California, USA'
G = ox.graph_from_place(place, network_type='drive_service', simplify=False, retain_all=True)
fig, ax = ox.plot_graph(G, node_size=0, edge_linewidth=0.1, save=True, filename='la')
Note that if you want a projected network, it's usually faster for large cities to say simplify=False, then project the graph, then simplify it. Projecting a simplified graph with spatial geometries embedded as edge attributes can take a long time in large networks.
# create a network constrained to the shape of hong kong island
G = ox.graph_from_place('Hong Kong Island', network_type='drive')
# project the network to UTM (zone calculated automatically) then plot it
G_projected = ox.project_graph(G)
fig, ax = ox.plot_graph(G_projected)