October 20, 2016
Activity 1: Login and Record Attendance
We will be taking roll in lab each week! Please come to your assigned lab to be counted present!
If you are in an Olsson lab, click "Lab Attendance" on the left-hand menu in Collab to register your attendance and keep up with your lab grade.
YOU MUST CLICK THE LARGE GREEN OR YELLOW BUTTON FOR YOUR ATTENDANCE TO COUNT!!
You must do this from a machine in Olsson 001 and not your laptop. If you have trouble, talk to your lab TA. Students in Lab 109 will do attendance via direction from the TA.
Activity 2: Take Quiz 4
While you are waiting for lab to start, click on Tests & Quizzes in Collab from either the desktop or laptop and take Quiz 4 - a short review quiz on the material we have covered thus far. If you do not complete it today, you have until Sunday to do so.
Activity 4: Starting Out
Today the TAs are going to review reading files. They'll also introduce how to import the
webbrowser library so you can open your computer's default web browser to a particular URL. Find a partner to work with and get ready to go!
Activity 4: Finding Your Location
Our goal for this lab is to write a program that will find the closest Wendy's location to a given set of GPS coordinates (like you were using a GPS unit in your car). Then we want to open Google Maps to show where that Wendy's is.
Create a new python file called
wendys.py. Also download https://cs1110.cs.virginia.edu/code/wendys.csv and put it into your project directory. (If you wish, you can load the file from the Internet using the
urllib.request.urlopen() command as well.)
You are going to need some locations to test with. So go put some addresses you know in the tool here: http://www.gps-coordinates.net/
To start you out, the GPS coordinates for Rice Hall are: (38.0317274,-78.5110432).
Write down your test coordinates. You don't need EVERY decimal place of precision...
Activity 5: Reading the File
Using the example code that we have done in class the past few days, write code that will loop through the
wendys.csv file and compare the coordinates the user entered to the coordinates from the .csv file. Note that the 0 position in the .csv is the latitude and the 1 position is the longitude. First, prompt the user for their current latitude and longitude.
Since we want to find the closest Wendy's, we need a way to figure out the distance between two sets of GPS coordinates. Copy and paste the following method in your
import math def distance_between(lat_1, lon_1, lat_2, lon_2): theta = lon_1 - lon_2 dist = math.sin(lat_1 * math.pi / 180.0) * math.sin(lat_2 * math.pi / 180.0) + math.cos(lat_1 * math.pi / 180.0) * math.cos(lat_2 * math.pi / 180.0) * math.cos(theta * math.pi / 180.0) dist = math.acos(dist) dist = dist * 180.0 / math.pi dist = dist * 60 * 1.1515 return dist }
You can execute this code by calling:
distance = distance_between(user_lat, user_lon, wendys_lat, wendys_lon)
user_lon are what the user typed in and you saved from the pop up windows and
wendys_lon are the 0 and 1 columns from the .csv (remember you can get these after you split the line... look at the examples from class!).
distance here is the distance in miles.
Loop through the entire file, keeping up with which Wendy's gave you the shortest distance from the user's coordinates to that Wendy's coordinates. Save that information in some variables that you setup!
Activity 6: Google Maps
Now let's put it on a map!
We can take the address (which is the combination of columns 4, 5, and 6 from the .csv) and create a web address object (URI) that Java can use to open your computer's default browser.
# You'll need this at the top of your file import webbrowser
Once you have imported the
webbrowser library, you'll need to create the proper URL for Google Maps.
The URL should look like:
http://maps.google.com/maps?q=" + the address (created by concatenating columns 4, 5, and 6 from the .csv)
You'll also need to replace all the spaces in the address with
+ signs, since spaces aren't allowed in URLs.
url = url.replace(' ', '+') webbrowser.open(url)
Give it a shot and see what Wendy's you can find!
Submission: Each partner should submit one .py file named
wendys.py to https://archimedes.cs.virginia.edu/cs1110/. Please put both partners' names and id's in two comments at the top of the file.
Submit before you leave! Even if you don't finish, submit what you have.
# Mark Sherriff (mss2x) import math import webbrowser google_maps_url = "https://firstname.lastname@example.org,-81.954957,15z?hl=en" def distance_between(lat_1, lon_1, lat_2, lon_2): theta = lon_1 - lon_2 dist = math.sin(lat_1 * math.pi / 180.0) * math.sin(lat_2 * math.pi / 180.0) + math.cos(lat_1 * math.pi / 180.0) * math.cos(lat_2 * math.pi / 180.0) * math.cos(theta * math.pi / 180.0) dist = math.acos(dist) dist = dist * 180.0 / math.pi dist = dist * 60 * 1.1515 return dist #lat = float(input("Current latitude: ")) #lon = float(input("Current longitude: ")) lat = 38.0322727 lon = -78.50997339999999 datafile = open("wendys.csv", "r") closest_dist = 200 closest_wendys = "" for line in datafile: entry = line.split(";") dist_to_wendys = distance_between(lat, lon, float(entry), float(entry)) if dist_to_wendys < closest_dist: google_maps_url = "https://www.google.com/maps?q=" + str(entry) + "+" + str(entry) + "+" + str(entry) closest_dist = dist_to_wendys closest_wendys = entry datafile.close() print("The closest Wendy's (", closest_wendys, ") is", closest_dist, "miles away.") google_maps_url = google_maps_url.replace(' ', '+') webbrowser.open(google_maps_url)