Build a GPS Routing API with Python Flask
Create a free Distance Matrix API using Flask with a Selenium Bot deployed on Heroku.
Do you need to calculate distances for your transportation planning but don’t want to spend much on API costs?
This article explains how to build a free and easy-to-setup GPS routing API using Python Flask and a Selenium bot.
With detailed instructions and code examples, you can create your API and optimize your transportation planning.
Introduction
My first project using GPS routing was four years ago.
I wanted to optimize a transport plan for 1,200 truck deliveries/month covering 50 stores from a Cross-Docking platform.
I have built a Transportation Route Optimization tool using Excel-VBA — mainly for transport plan design using distance collected from Google Maps API.
This was my first experience using an API. The Google Maps API was free, but I could only make 10,000 daily requests.
What about now?
A few years later, Google changed its billing policy, so you have to pay from the first request.
If you have never subscribed to the Google Cloud Platform (GCP) service, you can have 200$ free credits after setting up a credit card.
But what if you
- Need to get several thousand distances?
- Don’t care if it takes a long time?
- Don’t feel confident about using your personal card (or cannot get a company credit card) for non-personal projects?
This article will show you a solution built with a Flask API using a selenium bot connected to Google Map WebPage.
You can find the full code uploaded in this repository: Github
How does it work?
Before reading this part, please forget everything you know about putting fast, efficient, and stable code into production that ensures quick response with limited resources.
This will be simple, quick, and dirty, with no intention of being a scalable solution. The performance will be way lower than if you directly query the official Google API — but here, it’s free :)
🏫 Discover 70+ case studies using Python and VBA to automate manual task 🤖 and support supply chain optimization 🚚 in this Cheat Sheet
Build your API
Let us do it in three steps
- Build a Selenium Bot that will query the distance from City A to City B on Google Maps Website.
- Set up your Flask API that will receive the request and return a distance
- Deploy your code on Heroku
You can find the full code in this Github repository: Link.
Set up your Selenium Bot
- Set ChromeDriver options to ensure the highest speed of execution
- Input Environment Variables that will be created in your Heroku instance
Write your distance scrapper
Google Maps link to get distance from "Paris,France" to "Marseille, France"https://www.google.fr/maps/dir/Paris,France/Marseille, France/data=!4m2!4m1!3e0"/data=!4m2!4m1!3e0" is added to ensure that you take the road transportation distance
Set up your Flask API
Your API link to get distance from "Paris,France" to "Marseille, France"http://xxx-xxx.herokuapp.com/distance/Paris,France/Marseille,France
(replace xxx-xxx by your heroku app name)<fr> = Paris,France
<to> = Marseille, France
Deploy your API
I will skip details on creating and deploying an app on Heroku.
At the end of the article, you can find links to Medium articles explaining detailed steps to create your Heroku instance.
Prepare files for deployment on Heroku
Prepare requirements.txt file with a listing of libraries needed with pip freeze
(env) C:\Users\yourprojectfolder> pip freeze > requirements.txt
Create ProcFile to launch your web app
(env) C:\Users\yourprojectfolder> echo web: gunicorn -t 120 -b :$PORT app:app > ProcfileP.S: Please make sure that your app name is "app" and your python script is named "app.py"
Download Buildpacks on Heroku to use Selenium + ChromeDriver
Go to settings > Add Buildpack
Enter Two Links
- https://github.com/heroku/heroku-buildpack-google-chrome
- https://github.com/heroku/heroku-buildpack-chromedriver
Set up Environment Variables
CHROMEDRIVER_PATH: /app/.chromedriver/bin/chromedriver
GOOGLE_CHROME_BIN: /app/.apt/usr/bin/google-chrome
Test your API
Test your API to calculate the distance
From: Paris, France
To: Marseille, France
Request link
http://xxx-xxx.herokuapp.com/distance/Paris,France/Marseille,France
(replace xxx-xxx by your Heroku app name)
Response
{“distance”:”775 km”}
What can we get in Google Maps?
It’s matching :)
Conclusion and next steps
Follow me on Medium for more insights related to Data Science for Supply Chain.
I deployed this solution on a free Heroku instance and tested it using a Google Sheet, querying my API to get 40 road distances.
Next Step 1: Find a way to ensure that your sheets send queries once at a time
If you do not, you can quickly exceed your memory quota
Step 2: Errors management and extract all distances
You can see in the example above that the first result shows the shortest travel time and may not be the shortest distance.
Route time can change if you query at a different time of the day, so you’d better take the three distances.
About Me
Let’s connect on Linkedin and Twitter, I am a Supply Chain Engineer who uses data analytics to improve logistics operations and reduce costs.
If you need consulting or advice on analytics and sustainable supply chain transformation, feel free to contact me through Logigreen Consulting.
If you are interested in Data Analytics and Supply Chain, look at my website.
References
This article skips many details to make it concise and easy to read. You can find detailed instructions in the excellent articles listed below.
[1] Michael Browne, Running ChromeDriver with Python Selenium on Heroku (2019), Link
[2] Moses Gitau, Deploying a Flask application on Heroku (2018), Link