-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtravelingSuitcase.py
More file actions
98 lines (75 loc) · 2.93 KB
/
travelingSuitcase.py
File metadata and controls
98 lines (75 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#Get modules/libraries
import webbrowser
import urllib
from xml.etree.ElementTree import parse
import math
import time
import datetime
def trackBuses():
#Open static map page in Chrome
def openMap(lat, lon):
baseStr = 'https://maps.googleapis.com/maps/api/staticmap?center='
locStr = '' + str(lat) + ',' + str(lon)
zoomStr = '&zoom=15'
sizeStr = '&size=800x800'
markers = '&markers=color:blue|' + locStr
queryStr = baseStr + locStr + zoomStr + sizeStr + markers
webbrowser.open(queryStr)
def writeOutput(busInfo, dist):
time = datetime.datetime.now()
with open("busResults.txt", "a") as myfile:
myfile.write(busInfo['route'] + ' traveling: ' + busInfo['direction'] + ' at time: ' + str(time) + ' distance: ' + str(dist) + ' miles')
myfile.write('\n')
#Calculate distance between office and bus coords
def unitSphereDistance(lat1, long1, lat2, long2):
# Convert latitude and longitude to
# spherical coordinates in radians.
degToRadians = math.pi/180.0
# phi = 90 - latitude
phi1 = (90.0 - lat1)*degToRadians
phi2 = (90.0 - lat2)*degToRadians
# theta = longitude
theta1 = long1*degToRadians
theta2 = long2*degToRadians
# Compute spherical distance from spherical coordinates.
# For two locations in spherical coordinates
# (1, theta, phi) and (1, theta, phi)
# cosine( arc length ) =
# sin phi sin phi' cos(theta-theta') + cos phi cos phi'
# distance = rho * arc length
cos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) +
math.cos(phi1)*math.cos(phi2))
arc = math.acos( cos )
earthRadiusInMiles = 3963.1676
# Remember to multiply arc by the radius of the earth
# in your favorite set of units to get length.
return arc * earthRadiusInMiles
#Startup Data
officeCoords = {'latitude': '41.98062', 'longitude': '-87.668458'}
# Get XML of all buses in Chicago
u = urllib.urlopen('http://ctabustracker.com/bustime/map/getBusesForRoute.jsp?route=22')
data = u.read()
print(data)
f = open('rt22.xml', 'wb')
f.write(data)
f.close()
#Turn xml into a document tree
doc = parse('rt22.xml')
busNodes = doc.findall('bus')
busList = {}
#Get information for buses on route
i = 0
for bus in busNodes:
i = bus.findtext('id')
busList[i] = {'direction': bus.findtext('d'), 'latitude': bus.findtext('lat'), 'longitude': bus.findtext('lon'), 'route': bus.findtext('rt')}
dist = unitSphereDistance(float(officeCoords['latitude']), float(officeCoords['longitude']), float(busList[i]['latitude']), float(busList[i]['longitude']))
print(dist)
if dist < 1:
writeOutput(busList[i], dist)
openMap(busList[i]['latitude'], busList[i]['longitude'])
def executeSomething():
#code here
trackBuses()
time.sleep(60)
while True:
executeSomething()