Author: Geoff Boeing
This notebook demonstrates how to save networks to disk as shapefiles, geopackages, graphml, and xml, and how to load an OSMnx-created network from a graphml file.
import osmnx as ox
%matplotlib inline
ox.config(log_console=True)
ox.__version__
# get a network
place = 'Piedmont, California, USA'
G = ox.graph_from_place(place, network_type='drive')
# save graph as a shapefile
ox.save_graph_shapefile(G, filepath='./data/piedmont')
# save graph as a geopackage
ox.save_graph_geopackage(G, filepath='./data/piedmont.gpkg')
# save/load graph as a graphml file: this is the best way to save your model
# for subsequent work later
filepath = './data/piedmont.graphml'
ox.save_graphml(G, filepath)
G = ox.load_graphml(filepath)
# if you want to work with your model in gephi, use gephi compatibility mode
ox.save_graphml(G, filepath=filepath, gephi=True)
# save street network as SVG
fig, ax = ox.plot_graph(G, show=False, save=True, close=True, filepath='./images/piedmont.svg')
# get all "amenities" and save as a geopackage via geopandas
gdf = ox.geometries_from_place(place, tags={'amenity':True})
gdf = gdf.apply(lambda c: c.astype(str) if c.name != 'geometry' else c, axis=0)
gdf.to_file('./data/pois.gpkg', driver='GPKG')
# get all building footprints and save as a geopackage via geopandas
gdf = ox.geometries_from_place(place, tags={'building':True})
gdf = gdf.apply(lambda c: c.astype(str) if c.name != 'geometry' else c, axis=0)
gdf.to_file('./data/building_footprints.gpkg', driver='GPKG')
To save your graph to disk as a .osm formatted XML file, ensure that you created the graph with ox.settings.all_oneway=True
for save_graph_xml
to work properly. See docstring for details.
To save/load full-featured OSMnx graphs to/from disk for later use, use the save_graphml
and load_graphml
functions instead.
# save graph to disk as .osm xml file
ox.config(all_oneway=True, log_console=True, use_cache=True)
G = ox.graph_from_place('Piedmont, California, USA', network_type='drive')
ox.save_graph_xml(G, filepath='./data/piedmont.osm')