Build Your Free GPS Routing API with Python Flask

Samir Saci
Mar 14 · 5 min read

Create a free, easy-to-implement and customizable (and a bit slow :D) Distance Matrix API using Flask with a Selenium Bot deployed on Heroku

Photo by Samantha Gollnick on Unsplash

If you are interested in articles related to Data Science for Supply Chain feel free to have a look at my portfolio: https://samirsaci.com

My first project using GPS routing was 4 years ago. I wanted to optimize a transport plan for 1,200 trucks 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; Google Maps API was free with a limit of 10,000 requests per day.

What about now?

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?
  • you don’t care if it takes a long time?
  • you 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.

How does it work?

This will be simple, quick and dirty, with no intention to be a scalable solution. The performance will be way lower than if you directly query the official Google API — but here it’s free :)

Full Process to get the distance between two cities in your Google Sheet — (Image by Author)

Build your API

  1. Build a Selenium Bot that will query the distance from City A to City B in Google Maps Website
  2. Set up your Flask API that will receive the request and return a distance
  3. Deploy your code on Heroku

Set up your Selenium Bot

  • Input Environment Variables that will be created in your Heroku instance
Setup of your selenium bot

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

Prepare files for deployment on Heroku

(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

Buildpacks menu in your Heroku App Settings — (Image by Author)

Enter Two Links

Set up Environment Variables

Config Vars menu in your Heroku App Settings — (Image by Author)
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

Request link
http://xxx-xxx.herokuapp.com/distance/Paris,France/Marseille,France
(replace xxx-xxx by your Heroku app name)

Response
{“distance”:”775 km”}

Your API response for the query of distance from Paris to Marseille — (Image by Author)

What can we get in Google Maps?

Distance from Paris (France) to Marseille (France): 775 km — (Image by Author)

It’s matching :)

Conclusion and next steps

Google Sheet using my newly created geolocation API to get the distance between cities — (Image by Author)

Next Step 1: Find a way to ensure that your sheets send queries once at a time

If you not, you can quickly exceed your memory quota

Heroku logs — (Image by Author)

Step 2: Errors management and extract all distances

Distance and route time from Paris (France) to Bordeaux (France) — (Image by Author)

You can see in the example above, the first result showed is 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.

References

[1] Michael Browne, Running ChromeDriver with Python Selenium on Heroku (2019), Link

[2] Moses Gitau, Deploying a Flask application on Heroku (2018), Link

Please feel free to contact me, I am willing to share and exchange on topics related to Data Science and Supply Chain.
My Portfolio:
https://samirsaci.com
Linkedin:
www.linkedin.com/in/samir-saci

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Samir Saci

Written by

Senior Supply Chain Engineer — http://samirsaci.com | Data Science for Warehousing📦, Transportation 🚚 and Demand Forecasting 📈

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store