Merging HERE Road Speeds with OSMnx Urban Networks

OSMnx is a powerful Python package for road networks but does not incorporate current traffic conditions — let’s use the HERE traffic API to impute real-time traffic information in OSMnx

Merging HERE Road Speeds with OSMnx Urban Networks

OSMnx is a powerful Python package for road networks but does not incorporate current traffic conditions — let’s use the HERE traffic API to impute real-time traffic information in OSMnx

OSMnx obtains static road network information from OpenStreetMap- a free editable geographic database of the world. Here is a brief code snippet showing how OSMnx can be used to obtain the road network of Washington DC:

place_name="Washington DC, USA"
G = ox.graph_from_place(place_name,network_type='drive', simplify=True)
fig, ax = ox.plot_graph(G,node_size=0)
Washington DC Road Network From OSMnx | Skanda Vivek

However, OSMnx lacks dynamic information. In particular, OSMnx currently calculates shortest time paths from individual road traversal times based on their speed limits. However, this might not be representative especially during rush hours, where some commonly used roads could be completely backed up due to congestion or accidents. It would be great if there was a way to figure out shortest-time paths based on the current traffic conditions. So how do we obtain real-time traffic information for OSMnx edges?

The HERE API makes it quite easy to obtain speeds from road segments within a certain bounding box. I've written an article on how to obtain road speeds using the HERE API:

Visualizing real-time traffic patterns using HERE traffic api
While Google Maps shows live traffic, there’s no way to access the underlying traffic data. HERE technologies offers a variety of location…

It is easy to get setup for a HERE API.

Obtaining HERE REST API Keys | Skanda Vivek

Given an urban road network G from OSMnx, you can input that road network to a custom function that figures out the appropriate bounding box to pull speeds from. The function speeds within the get_speeds.py module does just that:

from get_speeds import *
speeds(G,API_KEY)

Since the HERE API gives information about both traffic speeds and speed limits, I plotted a graph to visualize relative speeds on roads.

Speeds from HERE API | Skanda Vivek

However, there is an issue. HERE provides speeds in a bounding box. But notice that OSMnx provides road networks only from the Washington DC area, which corresponds to the right half of the bounding box. We can fix this by combining the speeds obtained from the HERE road segment data with OSMnx edges. For this, we need to assign each edge in OSMnx the speed of the closest road segment from the HERE road segments data. It takes a few seconds to compute the nearest OSMnx edge and corresponding distance from a point. Since there are 40,000+ road segments and speeds from the HERE data, expect this to take a while!

API_KEY='BLAH'
get_edge_speeds(G,API_KEY)

I utilized all 16 CPU cores using joblib and this took more than 10 hours to complete! Filtering all road segments within a threshold distance from an OSMnx edge gives the following image:

Filtered traffic speeds by closeness to OSMnx edges

As you can see, it looks much better- and more comparable to the original OSMnx graph!

df_es=pd.read_csv('df_es.csv')
edges1=add_edge_speeds(G,df_es,0.01) #change cutoff as necessary

Finally, based on the cutoff (0.01 here) , you can get the network edges and speeds!

And that's it - now you can perform various network algorithms such as shortest time paths, taking into account traffic patterns! One caveat though is that the HERE API does not provide information about speeds on certain smaller roads, whereas OSMnx provides more granular information. This would cause incorrect estimates of speed on smaller roads. Depending on your use case, this could either be a huge issue or not a big deal at all.

The code can be found in this github repo:

GitHub - skandavivek/osmnx-edge-speeds: Obtaining road speeds corresponding to OSMnx urban road networks from the HERE traffic API
Obtaining road speeds corresponding to OSMnx urban road networks from the HERE traffic API - GitHub - skandavivek/osmnx-edge-speeds: Obtaining road speeds corresponding to OSMnx urban road networks...



Thanks for reading! For Data Science and Machine Learning mentoring, please contact us! We develop custom learning pathways for individual clients and enable cutting edge AI based research. We also provide access to high-end computational servers based on needs.