Display journey times with Google Directions and Python
I thought it would be interesting to calculate and display journey times using Google Directions API and Python. After the journey times for each transport modes have been calculated they can be overlayed on a colour coded map, in this case I am using Singapore as an example. To do this I will query Google Directions API and for each destination calculate the journey time for driving, public transport (MRT or Bus) and cycling. Each destination is colour coded with the transport mode that is the quickest.
- Define a fixed location where all journeys start from.
- Define a bounding box of locations to find directions to.
- Run script to query journey times for each point in the bounding box for each transport mode.
The Google Directions API and the Google Geocoding API limit you to 2500 queries per 24 hour period and 10 requests per second. You will need to turn on these service before making any API calls. Go to the API Console and turn on the geocoding and directions services.
This script will collect the journey times for sampled locations in the specified bounding box for three transport modes: driving, public transport and walking. I don’t display walking on the map, it is too slow, instead I convert it to cycling by dividing the journey time by 4.
python Script to calculate journey times
Python file: geocode.py Running the script from the command line:
$ python geocode.py --max-lat 1.332 --min-lat 1.306 --min-lng 103.7550 --max-lng 103.765 ... Lots of output ... $ head timings.csv 1.300000 103.825000 597 780 605 3207 1.298750 103.825000 477 780 855 3305 1.297500 103.825000 494 780 932 3209 1.296250 103.825000 477 480 1033 3045 1.295000 103.825000 502 480 1000 3012 1.293750 103.825000 473 240 1054 2776 1.292500 103.825000 506 240 991 2716 1.291250 103.825000 451 240 934 2659 1.290000 103.825000 421 240 788 2570 1.300000 103.826250 527 780 788 3336
The output is a tab delimited file containing the latitude, longitude, driving time, public transport time, public transport walking time (to and from the station/bus) and walking time.
html file: transport_modes.html
The yellow star marks the start location of the journey.
- Red = Driving
- Green = Public Transport (MRT or Bus)
- Blue = Cycling
Journey starting from the The Pinnacle @ Duxton.
Journey starting from Pasir Ris.
Transport Modes Map: transport_modes.html
When the origin and destination are far apart driving is the quickest method of transport. For mixed residential and commercial areas cycling can be the best method. Public transport is rarely the best method, this is because of the amount of time required to walk to/from the MRT Station/Bus to the destination.
This can be seen by removing the walking component from the public transport times so that only the time spent on public transport is used. Here is that map after we apply this change:
( Red=Driving, Green=Public Transport, Blue=Cycling )
Notes about journey times:
I add 25% to the travel time for driving to account for traffic, diversions, congestion etc. For cycling I use the walking route duration divided by four. Some locations map to the sea or lakes, Google will find the nearest point on land when planning the route.
If you liked this article please share! Thanks.