Converting two locations into a turn by turn mapping
I have done a number of professional projects where I had the same fairly well defined problem in different domains. The problem boiled down to one of starting with two destinations and programmatically mapping them as part of a larger trip.
This broke down to the following steps :
1. Geocode the 2 locations. This was the act of taking an address and determining latitude and longitude
2. Determine the the directions between the two using popular mapping software (I’ll illustrate with Mapquest here)
3. Map the route within my web app.
The first step above is increasingly easier to do via any number of online services such as Google Maps and Mapquest. Usually I’m presented with a number of locations to geocode, and this is fairly easy to do programatically using a service’s API.
So now we have a database with our locations and their associated latitudes and longitudes, so now you’ll find the world is our oyster. We could certainly create a GEOJSON file to show all our locations on a map as shown here. But we’ve got a slightly bigger fish to fry; we eventually want to show our locations on a map and show directions between them. So we are going to need to figure out how to determine the directions between two geodcoded points and then show them on the map. We will show how to do this using Mapquest, though there are other APIs available to do so.
The Polyline
When you call the Mapquest API for directions between two points, one of the results returned is the “polyline”. This is a term used in graphics, but is essentially an encoded series of lines indicating the directions between the two points.
http://www.webopedia.com/TERM/P/polyline.html
These can get quite complicated as the distance and number of turns between the two points et’s larger, but let’s look at a fairly simple example.
I live in the Bucktown area of Chicago. Now suppose I wanted to go from my local library to legendary beer bar The Map Room (better that order than the bar first). Here’s the simple directions from Google maps. A simple 2 turn direction, mostly on one street
Google Maps direction from library to Map Room
Here is the call to the Mapquest API, with my key replaced with the XXs. Note that I have requested walking directions to make it consistent with the Google call above, and I have geocoded the two locations as the from and to locations
http://www.mapquestapi.com/directions/v1/route?key=XXXXXXXXX&from=41.912482,%20-87.680308&to=41.917671,%20-87.679793&outShapeFormat=cmp&generalize=100&routeType=pedestrian&outFormat=json
A portion of the returned JSON from the Mapquest API is the polyline, denoted as
“shapePoints”:”{~x~FpadvOTa@??Ky@??w`@X”
Well that’s not very helpful to a non-savant like myself, so we need to translate that to something more readable for our purposes.There are algorithms for decoding such things, and here is a Python implementation that I found very helpful
http://seewah.blogspot.com/2009/11/gpolyline-decoding-in-python.html
Using the code and the returned polyline above returns the following list of latitudes and longitudes (you can see how detailed it is already for a simple direction)
41.9123 -87.68041 41.91219 -87.68024 41.91219 -87.68024 41.91225 -87.67995 41.91225 -87.67995 41.91765 -87.68008
NEXT: Creating a GEOJSON file of linestrings from the translated polyline