The simple user interfaces I explained in my previous blog post work fine for typing in origins and destinations for a trip and exporting a GPX or KML file with the polyline information. Obviously if I want to create a database with hundreds or thousands of routes, I need to find out a way to do this more efficiently. To my satisfaction I found two API’s that will allow me to do this.
How Google Directions APIs work
The OSM YOURS and Google Directions APIs are actually quite simple in concept. Instead of typing in your route origin and destination into a website interface, you include your routing request in the text of a url, hit enter on your browser, and the API will return you some text or a file containing the routing directions. You can customize the response you get from the server by adding in flags to the url itself that are explained in the API documentation. For the purpose of brevity I’ll be focusing on the Google Directions API in this blog, but the OSM YOURS API offers similar capabilities with a gpx file output option instead of a geoJSON file.
Google Directions API and GeoJSON
The Google Directions API has options to return your routing results in XML or geoJSON format. For those unfamiliar with these file types, they are simply files that contain the route information coordinates in different formats. For this project, it is important that I can easily open the data in QGIS (an open source mapping software) to visualize how things look on a map and possibly edit my data. I chose geoJSON because it is easy to open in QGIS.
So how do you send an request to the Google Directions API and get back a geoJSON file? Its actually quite simple. Note that copying and pasting the link below into your browser gives you a file back with the route from Vancouver to Seattle. Note the
destination=Seattle text. Simple!
Now for a look at the geoJSON text file that we get in return. Thanks to Chris Whong’s NYC Taxi blog for explaining this in detail. All the information below is a brief summary of his excellent blog post.
A quick look at the geoJSON file we get back looks like this in our browser:
See that complicated block of characters after “points”? That is actually an encoded string of points that makes up the entire route. I won’t be needing to decode this since I can load the geoJSON file directly into my database and mapping software, but Google does offer an interface to decode this. Simply copy and paste the code into the encoded poly lines window at this page, and you will get a preview of the route along with a list of the lat long coordinates that are strung together to create the line.
Great! Now I’ve cracked the basics of how to send an API request to Google and get my precious route linework in return. Next in the process I will have to figure out how to send these requests from Python on my desktop and get a response. After that I will need to automate the url generation so I can make large list of the url routing codes based on a spreadsheet of all my trip destinations. I think my next step after that will be to create some sort of database to store all my data for web hosting, or for utilizing in QGIS to make some nice looking static maps!