Nishadh KA

Finding route between two points from OSM road network input

2019-01-03


Finding navigable route such as for driving between two points has several application. Open Street Map(OSM) with its free downloadable map, routing algorithms can be applied to find best route between two location. OSMNX, a python library import osm (a default file format provided by OSM) into graphml format which can be used with network analysis to get route network. The OSM downloaded from overpass using the polygon bouundary for an urban area, the overpass query https://overpass-api.de/api/map?bbox=77.3,12.7,77.9,13.3 was then created by

import geopandas as gp
ex=gp.read_file('urban_polygon.shp')
ex.bounds
print('https://overpass-api.de/api/map?bbox=',ex.bounds['minx'][0],',',ex.bounds['miny'][0],',',ex.bounds['maxx'][0],',',ex.bounds['maxy'][0])

The osm file is then converted into graphml file format by

import osmnx as ox
import networkx as nx
B1=ox.graph_from_file('bng.osm', network_type='all', simplify=False, retain_all=False, name='b1')
ox.save_graphml(B1, filename='bng.graphml')

Route network was then found by using Network analysis libraries [Networkx] [1] and [graph-tool] [2]. Conversion of OSM into graphml makes it Graph notations to read by network analysis libraries. Graphml represents vertices in OSM road network file into node/vertex and line connecting the vertices as edge.
The locations to be queried to get route network has to be made into nodes as well.

Networkx

The graphml is read by networkx as follows

import osmnx as ox
import networkx as nx
G2 = ox.load_graphml('bng.graphml')

The nodes of the querry location points are derived as follows. A shape file with point fetures containing origin and destination deatils are read and node deatials are derived by querying nearest nodes

import numpy as np
import itertools
import osmnx as ox
db=gp.read_file('bangalore_wards.json')
#list=np.arange(0,len(db.index))
db['centroid']=db['geometry'].centroid
db["x"] = db.centroid.map(lambda p: p.x)
db["y"] = db.centroid.map(lambda p: p.y)
gmc=[]
for idx, row in db.iterrows():
    od_node = ox.get_nearest_node(G2, (row['y'],row['x']), method='euclidean')
    gmc.append(od_node)
    od_node=[]
db['gmcnode']=gmc

graph-tool

To be continued….