Author: Geoff Boeing
This notebook demonstrates how to save networks to disk as GeoPackages, GraphML files, and .osm formatted XML, and how to load an OSMnx-created network from a GraphML file.
import osmnx as ox
ox.__version__
# get a network
place = "Piedmont, California, USA"
G = ox.graph_from_place(place, network_type="drive")
# 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.features_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.features_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.settings.all_oneway = True
ox.settings.log_console = True
G = ox.graph_from_place("Piedmont, California, USA", network_type="drive", simplify=False)
ox.save_graph_xml(G, filepath="./data/piedmont.osm")