Skip to main content

My Project: Travel time bot with Google Maps API and Python - Part 1 - Finding the Data [Programming]

My son will be starting university in just under a week at a school where my wife is also a professor. This means they are going to be carpooling on many days. Since the school is about 60 miles from our house, it is important to keep tabs on the driving time as here in Los Angeles, these times can change dramatically over the course of the day. This need to track driving time gave me an excuse to dive back into programming — which I was never that fond of anyway — despite my 30+ years working in computer support and consulting.

Here is a step by step story of the project and where it stands today.

Get the Data

The first step, of course, is to figure out where I can get the real-time routing and drive time data. I immediately thought about Google Maps and found the Google Maps API pages.

Google maps api

It is free to sign up for a developer account, which gives you an API key for each request you make to the Google Maps API servers. For me, my usage of the API is well below the number of free API requests per day, so there is no cost to run my program and gather the data — even if I were to request it about once per minute throughout the day. If you re going to develop high-usage commercial application, you’d have to sign up for one of Google API payment plants, based on the number of requests you make.

The specific Google Maps API I am using is the Directions API, which allows you to calculate directions between locations and returns a JSON-encoded file with a wide variety of information. They provide a basic example URL to get you started and then there are a host of options you can fine tune for your specific needs.

https://maps.googleapis.com/maps/api/directions/json?origin=Disneyland&destination=Universal+Studios+Hollywood4&key=YOUR_API_KEY

After a bit of trial and error, I came up with an appropriate URL that I could request manually and see the results returned.

https://maps.googleapis.com/maps/api/directions/json?origin=34.178026,-118.4528837&destination=34.057835,-117.8217123&departure_time=now&key=YOUR_API_KEY

This URL returns this JSON file...

{
   "geocoded_waypoints" : [
      {
         "geocoder_status" : "OK",
         "place_id" : "EjE1OTE0LTU5MTggQ2Vkcm9zIEF2ZSwgU2hlcm1hbiBPYWtzLCBDQSA5MTQxMSwgVVNB",
         "types" : [ "street_address" ]
      },
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJLVP1K64uw4ARc3vu78f00B0",
         "types" : [ "street_address" ]
      }
   ],
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 34.1794199,
               "lng" : -117.8012388
            },
            "southwest" : {
               "lat" : 34.0452654,
               "lng" : -118.453076
            }
         },
         "copyrights" : "Map data ©2016 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "46.2 mi",
                  "value" : 74333
               },
               "duration" : {
                  "text" : "54 mins",
                  "value" : 3251
               },
               "duration_in_traffic" : {
                  "text" : "58 mins",
                  "value" : 3455
               },
               "end_address" : "9 Olive Lane Walk, Pomona, CA 91768, USA",
               "end_location" : {
                  "lat" : 34.0577836,
                  "lng" : -117.8221829
               },
               "start_address" : "5914-5918 Cedros Ave, Sherman Oaks, CA 91411, USA",
               "start_location" : {
                  "lat" : 34.1780256,
                  "lng" : -118.4530714
               },

[… more information snipped…]

So far, so good. 

No I needed to figure out how to get the data out of this raw JSON file and into a form I could use it. The fact is, the API returns a lot more data than I really need, so I am only pulling out 2 pieces of information that are important to me. 

Navigating JSON Data Structures

Of course, now that I have the raw JSON data — which is a series of dicts, keys and values — I had to figure out the structure of the data and which pieces I needed. This was one of the more difficult learning parts for me. I understood that the JSOn file was a nested database of data, but figuring out how to get an individual pieces of data took me some time. Once I figured it out, though, it opened the door to any API that returns data in JSON format — a huge leap in my programming knowledge.

Visualizing JSON Data

Looking at a JSON file, especially a large one, and trying to discern the structure of the data can be a bit difficult. Levels of indentation aren’t always lear and nested curly braces always give me a headache. (LAUGH) Thankfully I searched out a web page that would allow me to view the data in a more database-like fashion, clearly showing how each piece of data was nested in the file.

JSONViewer.stack.hu allows you to either load a JSON file by URL or copy and paste a JSON file into the viewer. You can then click the Viewer Tab to see the data in its hierarchical format — expanding and collapsing nodes as needed.

Jsonviewer

Finding the Data I Needed

After a short while poking around in the Google Maps API JSON file, I found the 2 pieces of information I was most interested in — the summary or route of travel and the duration_in_traffic field.

Gmaps json expanded

This information would allow me to tell my wife and son which route was suggested by Google Maps (which can change based on traffic, road work and incidents) and the travel time in both text format and seconds. I did some basic checking with the interactive version of Google Maps to make sure the travel times were similar and saw that the duration field was a bit optimistic in its estimates. The duration_in_traffic field seemed to present a more realistic estimate, especially based on our heavy traffic here in Los Angeles.

Ok, so now I had the data I needed, how was I going to manipulate and use this data to make something useful?

In Part 2 of this series, I’ll dive into my Python program and discuss how I learned to process JSON data in Python, how I learned enough Python to do this and more and show off my working, if extremely basic, program. 

Sample Screen Output from Python program
[DEW-Mini:~/Downloads/py] dewelch% python cpp2.py

Via CA-134 E and I-210 E
Duration: 58 Minutes
0 Hours and 58 Minutes

Congratulations! You've fired the cpp event
[DEW-Mini:~/Downloads/py] dewelch% 

In future parts, I’ll discuss how I presented the data via screen, text, email and voice and how I got the code running on an Arduino Yun in preparation for creating a passive information device that will visually show the current travel time via graphical icons, colored lights and more!

Comments

Popular posts from this blog

Microsoft release Outlook.com email services to replace Hotmail

Today Microsoft released its new email service Outlook.com to replace its Hotmail brand. This new streamlined Metro interface design looks good and functions well so far. You can use your existing Microsoft account to log in and then choose an email alias (i.e. douglaswelch@outlook.com) for your new email address. Here are several articles that discuss Outlook.com... Goodbye, Hotmail; Hello, Outlook.com [REVIEW]  Outlook Is a Completely New, Feature-Filled Webmail Service from Microsoft Go Get Your @Outlook Email Address Quick Before Someone Else Does I will post links to more articles and reviews as they appear.

Shared calendars are one part of an organized family

by Douglas E. Welch , techiq@welchwrite.com 206-338-5832 Reader/Listener Line As a parent with a school-age child, I often hear other parents bemoaning their disorganized existence. Along with the busy schedules of two working parents you might have art classes, karate classes, Little League, soccer and more. Add in more than one kid and organizing your life can quickly become a nightmare. This is exactly why one of my most important organizing devices is a shared calendar that reflects all the activities and events for everyone in the household...and I do mean everything. If someone -- is required to be somewhere -- at sometime, it goes into the calendar. If we are given a calendar that reflects all the events for a particular activity (say, Little League), all these events immediately go into the calendar, along with notations on whether we are providing the team snack, working in the snack bar, etc. Even events that occur anytime during the day, like family birthdays, and other rem...

Google Docs adds templates

I have started to move away from using Microsoft Office (Word, Excel, Powerpoint, etc) in favor of online tools like Google Docs . Even as a computer consultant, I find that I use probably less than 10% of the features in any of these projects. One feature I really like in Google Docs is the easy, online file sharing that also allows you to edit simultaneously with someone else. Today, Google Docs added some templates including business cards, brochures and more. At the moment, it looks like they are rolling out this feature over time, so you might not yet see the templates on your account. When available, you will find the templates under the New... menu item. From Open Loops ... Today, Google quietly rolled out a new feature for their Google Docs Applications: Templates! It's so new that it isn't even listed on the new features page at this time. When one opens their Google Docs account and chooses to start a new document, a new "From Template..." command is pre...