I have saved a road network of a town using osmnx. I am currently using networkx to then manipulate the transport network by adding / removing elements such as nodes or edges (roads) to that network.
I then want to save this adjusted networkx graph to a .osm.pbf file. The aim is to use the .osm.pbf to run large origin-destination queries. So, the final adjusted network needs to be in a .osm.pbf format.
Is it possible to save the adjusted netwokrx graph to a .osm.pbf file?* Do I need to convert the networkx graph or graphml file to a secondary file format before then saving it as a .osm.pbf file (e.g., saving as a .shp file first then using ogr2pbf)?
Here is a simplified example of my code.
import osmnx as ox
import networkx as nx
from shapely.geometry import Polygon, LineString, Point
ox.config(log_console=True, use_cache=True)
# find shortest route based on the mode of travel
mode = 'drive' # 'drive', 'bike', 'walk'
# find shortest path based on distance or time
optimizer = 'time' # 'length','time'
# location where you want to find your route
place = 'Auckland, New Zealand'
# create graph from OSM within the boundaries of some
# geocodable place(s)
graph = ox.graph_from_place(place, network_type = mode)
# Save network as graphml
ox.save_graphml(graph, 'data/auckland.graphml', gephi=False)
# Load saved graphml file as the network
graph = ox.load_graphml("data/auckland.graphml")
def create_new_transport_link(graph):
"""
Creates a new road network based off osm transport network
Note this should also work for adding/removing any node and edge in the osm network
"""
# Start and end location of the road
location_1 = (-36.84, 174.73)
location_2 = (-36.82, 174.73)
# Finding the nearest nodes in the network to start and end of the road
nearest_node_1 = ox.get_nearest_node(graph, location_1)
nearest_node_2 = ox.get_nearest_node(graph, location_2)
# Creating an edge/new road between each of the nodes
graph.add_edge(nearest_node_1, nearest_node_2, name='added_road', highway='motorway', oneway='True', length=1000, maxspeed='100')
return graph
new_networkx_graph = create_new_transport_link(graph)
# Then I would like to save the new networkx graph ('new_networkx_graph') to .osm.pbf file