Using OSRM, OSMnx & Networkx, And Google Maps Directions API With Python For Spatial Routes

Rajesvari Parasa
2 min readFeb 13, 2021

[Link to code repo]

A map showing routes generated using OSRM, OSMnx & Networkx, and Google Directions API

Finding the right spatial routing tool for your need can be a difficult task. The decision largely depends on whether you’re trying to find shortest or fastest or traffic based routes. And also on the number and frequency of requests you’ll be making.

In this notebook, I explore three tools for finding street network based routes between a set of origin-destination (O-D) points. These tools are:

Free and open source —

  • Open Source Routing Machine for calculating traffic-independent fastest routes between the O-D points. (Requests are sent to the OSRM server. Hence, no OSRM specific package installations required unless setting up a local instance)
  • OSMnx and Networkx for calculating shortest routes. (Will require you to install both the python libraries)

Others —

  • Google Maps Directions API for calculating traffic-based most-efficient routes. While the API does allow a decent free tier for making routing requests, you still might have to set up a billing account to be able to access the traffic information. More details in the notebook.

The notebook also shows how you can finally export the routes calculated into a spatial format, such as shapefile, along with route attributes — travel distances and/or travel times.

For the relevant code snippets and more details on using each tool head straight to the notebook here.

If you found this notebook useful for any of your work, would love to know! If you are stuck somewhere or need more help on this, do leave a comment here or write to me at prkrajesvari3[at]gmail[dot]com.

--

--

Rajesvari Parasa

Dabbling around with Spatial Data & Urban Science. Senior Analyst, IDFC Institute | Ex- Product Manager, Lakeer Foundation | Ex- Urban Fellow, IIHS Bangalore