Putting Things Together – Saving the Google Directions API output to a File

Today I made significant progress in my script. I was able to open up a csv file with the list of To and From directions, then loop through each pair and send a request to the Google Directions API and get a GeoJSON object in return, and finally I was able to save each GeoJSON object as a unique file on my computer. I set up the script to add a counter to each file, so that each time it encountered a new TO and FROM pair in the spreadsheet it saved the output with a unique number.

Let’s dive into how we did it!

First, I import all my libraries, and open up the .csv file containing my TO and FROM locations. Then I create a new variable called counter that starts at 0.


import urllib.request
import csv

f = open('testtable.csv', 'r')
csvf = csv.reader(f)
counter = 0

Next I start looping through each row of my csv file row by row. I print each value within the row just so that I can see what TO and FROM areas it is reading. Also at this point within the loop I update the value of my counter to add 1 to what the previous value was.


for row in csvf:
        print(row[0])
        print(row[1])
        counter += 1

Now I create the new file name that the results of the first row will go into. Notice I add the counter number to the end of the file name so that each row creates a uniquely numbered file.


        filename = "newfile{}.xml".format(counter)
        print(filename)
        text_file = open(filename, 'w')

Its time to contact google and get the directions! I format the portion of the http request to input the to and from directions first. After that I tack it onto the rest of the url. I then contact the API and save the response from the API as a variable called googlejsonoutput.


        direction1="origin={}&destination={}".format(row[0], row[1])
        url="https://maps.googleapis.com/maps/api/directions/json?%s" % direction1
        print(url)
        response = urllib.request.urlopen(url)
        googlejsonoutput = response.read()

Finally, all I do is write the google output from the variable into my new file. I was having trouble writing it directly to the file, so the %s direction below simply tells Python to write my output as a string. I found that this does work. After the output has been written I close the text file so that there are no problems.


text_file.write("%s" % googlejsonoutput)

And so that things don't get messy, at the very bottom of my script outside of my loop, I close the csv file that we were reading from.


f.close()

I can double check my success, as my spreadsheet was 2 rows long, I have two new output files in my working folder called newfile1.geojson and newfile2.geojson. Each of the files have geojson objects populated in them.

However, there is a big problem! QGIS won't actually open the data. I think I will have to go through each file and do a bit of formatting to get it in GIS format. Thats the topic of the next blog post.

Thanks for reading! The entire script is pasted below if you don't wish to view it all cut up like above. I add lots of print() commands because I like to track the progress of my script in the Python shell while they execute. These commands are not necessary.


import urllib.request
import csv


f = open('testtable.csv', 'r')
csvf = csv.reader(f)
counter = 0

for row in csvf:
        print(row[0])
        print(row[1])
        print("end of row")
        
        counter += 1
        print(counter)
        
        filename = "newfile{}.geojson".format(counter)
        print(filename)
        
        text_file = open(filename, 'w')
        direction1="origin={}&destination={}".format(row[0], row[1])
        url="https://maps.googleapis.com/maps/api/directions/json?%s" % direction1
        print(url)
        
        response = urllib.request.urlopen(url)
        googlejsonoutput = response.read()

        text_file.write("%s" % googlejsonoutput)
        text_file.close()

##close files
text_file.close()
f.close()
 
        

Reading CSV files in Python

I know the easiest way to get my large table of From and To results passed to Python is to create a comma separated value (.csv) table. Its very easy to edit this, as I can simply open a text editor and start my data entry. Below is an example of how a very simple TO and FROM table would look like in csv:

From,To
Edmonton,Hinton
Vancouver,Burnaby
Edmonton,Calgary

To load and parse through these values in Python I need open the file and then return all the rows. The script do do this is below. Please note that it is very important to store the .csv file in your current working directory instead of some random folder on your hard drive!

import csv
f = open('testtable.csv')
csvf = csv.reader(f)
for row in csvf:
    print(row)

And from running that with the csv above we get the following output, with each row as a separate list of strings:

['From', 'To']
['Edmonton', 'Hinton']
['Vancouver', 'Burnaby']
['Edmonton', 'Calgary']

The next step is to pass each value in this list of strings to the Google Directions API and store the returns in a database!