At the conclusion of this programming assignment, participants should be able to:
Before starting this programming assignment, participants should be able to:
Content used in this assignment is based upon information in the following sources:
Write a program (road_trip.py) that computes simple statistics about a road trip! To do this, we are going to use the Google Maps API. API stands for application programming interface. An API provides functions for programmers to use, without needing to know the details of the implementation. For example, Google Maps is a complex, well-tested program. We can use Google Maps via its API without knowing Google Map's details. This is also one of the beauties of functions. As long as we know:
Then we can use Google Maps!
Write a program that reads in US locations from an input file. The locations include a road trip origin (starting location), destination (ending location), and several road trip "waypoints" (stops in between the origin and destination).
The road trip file is a text file formatted such that it starts with the origin location and ends with the destination location. In between the origin and destination are waypoint/day pairs: one waypoint per line, immediately followed by the number of days spent at a waypoint on the next line. Waypoint/days spent pairs are separated from other waypoint/days spent pairs by a blank line. Here is an example with an origin (Seattle, WA), two waypoints (1 day spent in Portland, OR and 3 days spent in San Francisco, CA), and destination (San Diego, CA):
Seattle, WA
Portland, OR
1
San Francisco, CA
3
San Diego, CA
Here is an example input file florida_roadtrip.txt that represents a roadtrip I took in 2012. The origin of this roadtrip is Spokane, WA and the destination is Gainesville, FL. There are 10 waypoints between Spokane and Gainesville.
Note: The road trip input file will always have an origin, at least 1 waypoint, and a destination in it. Your code should handle any number of waypoints between [1, 23] inclusive in the road trip file; you are guaranteed there will be at least one waypoint in the file.
The output of your program has two parts:
<input file base name>_stats.txt
. For example, for the provided input file, "florida_roadtrip.txt", the corresponding output file is "florida_roadtrip_stats.txt". The stats to be written to the output file include:All distances reported should be in miles and with two decimal places.
Note: The above stats are similar to the stats you computed for Lab 7 Task 2. The solution to this task is posted in Piazza.
I am providing code for you that queries the Google Maps API and extracts the required information from the response. To use this code, copy and paste it into your program. Do not modify this code.
import urllib.request
def format_city_string(city_str):
'''
111 students: no need to call this function
To prepare the city string for the query:
1. remove comma
2. replace spaces with +
'''
city_str = city_str.replace(",", "")
city_str = city_str.replace(" ", "+")
return city_str
def build_query(origin, dest):
'''
111 students: no need to call this function
Builds the query string for the Google Distance Matrix API according to this website:
https://developers.google.com/maps/documentation/distance-matrix/start
'''
query_base = "http://maps.googleapis.com/maps/api/distancematrix/json?origins="
query = query_base + origin
query += "&destinations="
query += dest
query += "&mode=driving&sensor=false"
return query
def extract_distance(results_str):
'''
111 students: no need to call this function
Extracts the distance in meters from the JSON response.
'''
index = results_str.find("distance")
results_str = results_str[index:]
index = results_str.find("value")
results_str = results_str[index:]
index = results_str.find(":")
results_str = results_str[index + 2:]
index = results_str.find(r"\n")
results_str = results_str[:index]
dist = int(results_str)
return dist
def get_distance(city1, city2):
'''
111 STUDENTS: THIS IS THE FUNCTION YOU WILL CALL
Accepts 2 strings representing cities in the U.S.
Returns the integer distance in meters between city1 and city2
'''
city1 = format_city_string(city1)
city2 = format_city_string(city2)
query = build_query(city1, city2)
web_obj = urllib.request.urlopen(query)
# web_obj.read() returns an array of bytes, need to convert to a string
results_str = str(web_obj.read())
web_obj.close()
dist = extract_distance(results_str)
return dist
The only function you need to interact with is get_distance(<city1 string>, <city2 string>)
. Here is an example of how to call get_distance()
:
dist = get_distance("Seattle, WA", "Pullman, WA")
print("The distance between Seattle, WA and Pullman, WA is %d meters" %(dist))
The distance between Seattle, WA and Pullman, WA is 457989 meters
Note: Even though I am providing this code for you to copy and paste, read through it anyways for fun. In CptS 111, you have already learned much of what this code is doing to interface with the Google Maps API, how cool!? You can always ask the instructor or your TA any questions you may have about this code if you are interested :)
You are free to decide which functions to define to solve this problem. Break the problem into tasks and build your solution accordingly. You will be graded on your approach. Ask the instructor or your TA for help with this!
Here is example output to the user (via the console) for the sample input file florida_roadtrip.txt (including the Bonus task):
Welcome to the road trip analyzer program, utilizing Google Maps!
Roadtrip origin, destination, waypoints, and number of days spent at each waypoint will be read in from a file.
The program write to an output file the distance in miles between each waypoint on the trip
and statistics about the trip. Enjoy!
Reading in road trip stop information from florida_roadtrip.txt...
Roadtrip origin: Spokane, WA
Processing waypoint Yellowstone National Park, WY (2 days)...
Processing waypoint Grand Teton National Park, WY (1 days)...
Processing waypoint Salt Lake City, UT (1 days)...
Processing waypoint Bryce Canyon National Park, UT (1 days)...
Processing waypoint Grand Canyon, AZ (1 days)...
Processing waypoint Surprise, AZ (2 days)...
Processing waypoint Carlsbad Caverns National Park, NM (1 days)...
Processing waypoint San Antonio, TX (3 days)...
Processing waypoint New Orleans, LA (1 days)...
Processing waypoint Panama City, FL (1 days)...
Roadtrip destination: Gainesville, FL
Writing stop stats to florida_roadtrip_stats.txt...
(Bonus) Launching Google Maps for this roadtrip in the browser...
Closing files...
And the associated output file florida_roadtrip_stats.txt.
Using the Google Maps Embed API, create a webpage containing an embedded Google Map for the road trip. For example, the webpage we are going to build for florida_roadtrip.txt looks like the following:
To do the Bonus, you will need a free Google account and a free Google Maps API key:
api_key
to store your copied API key.api_key
to an empty string before you submit your code to Blackboard. The grader will use their own key to run your code.api_key
in your code is bad practice!! However, in CptS 111 it is "okay" because only you will have access to your key. If you are going to release your code to others (after you finish CptS 111 because remember, you are not allowed to share your programming assignment code with others) follow Google's "Best practices for securing API keys".Next, read the Google Maps Embed API documentation, specifically read:
waypoints
)Now, we are ready to write code! There are 3 coding tasks for the bonus:
waypoints
parameter according to the Google Maps Embed API documentation. While iterating through each waypoint in the input file, use the provided format_city_string()
function from the starter code and the string concatenation operator to build a string for the waypoints parameter. For the florida_roadtrip.txt files, this string should look like the following (no spaces or newlines):Yellowstone+National+Park+WY|Grand+Teton+National+Park+WY|Salt+Lake+City+UT|Bryce+Canyon+National+Park+UT|
Grand+Canyon+AZ|Surprise+AZ|Carlsbad+Caverns+National+Park+NM|San+Antonio+TX|New+Orleans+LA|Panama+City+FL
<iframe>
according to the Google Maps Embed API documentation. This function should accept the following arguments and return the <iframe>
string:format_city_string()
format_city_string()
(from the starter code)waypoints
parameter (built in the previous step)<iframe>
string to a file called <input file base name>.html
. For example, for the provided input file, "florida_roadtrip.txt", the corresponding output file is "florida_roadtrip.html". Now, programmatically open a web browser to the html file you just created. Python makes this really simple to do! To open a webpage, we can import the webbrowser
module and pass in a string argument representing the webpage:import webbrowser
webbrowser.open("florida_roadtrip.html")
True
<your last name>_pa5.zip
by the due date and time.Note: By submitting your code to be graded, you are stating that your submission does not violate the CptS 111 Academic Integrity Policy outlined in the Syllabus.
This assignment is worth 100 points + 10 points bonus. Your assignment will be evaluated based on a successful compilation and adherence to the program requirements. We will grade according to the following criteria: