Fix totally broken longitude calculation.
[ottawa-travel-planner.git] / analyzeStops.py
blob88243c7619141da22c36e31e952fad688c81b8c0
1 #!/usr/bin/python
2 # vi: set softtabstop=4 shiftwidth=4 tabstop=8 expandtab:
4 """Thinky thinky"""
6 import BusStopMashup
7 import pickle
8 import math
10 def latdist(lat1, lat2):
11 return (lat1 - lat2) * 111.325
13 def longdist(lat, long1, long2):
14 return (long1 - long2) * math.cos(lat / 180.0 * math.pi) * 111.325
16 def locdist(loc1, loc2):
17 """terrible approximation"""
18 xdist = longdist(loc1.latitude, loc1.longitude, loc2.longitude)
19 ydist = latdist(loc1.latitude, loc2.latitude)
20 return math.sqrt(xdist * xdist + ydist * ydist)
22 def analyze(stoplist):
23 home = [stop for stop in stoplist
24 if isinstance(stop, BusStopMashup.HomeLocation)][0]
26 westiest = min(stop.location.longitude for stop in stoplist)
27 eastiest = max(stop.location.longitude for stop in stoplist)
28 northiest = min(stop.location.latitude for stop in stoplist)
29 southiest = max(stop.location.latitude for stop in stoplist)
31 print "\twest-east distance: %f" % (eastiest - westiest)
32 print "\twest-east distance: %f km" % longdist(home.location.latitude,
33 eastiest, westiest)
34 print "\tnorth-south distance: %f" % (southiest - northiest)
35 print "\tnorth-south distance: %f km" % latdist(southiest, northiest)
37 maxdist = 0.0
38 for stop in stoplist:
39 dist = locdist(stop.location, home.location)
40 if dist > maxdist:
41 maxdist = dist
43 print "\tMax dist from home: %f km" % maxdist
45 print "\tDegree dist from home: %f N, %f S, %f W, %f E" % (
46 home.location.latitude - northiest,
47 southiest - home.location.latitude,
48 home.location.longitude - westiest,
49 eastiest - home.location.longitude)
51 if __name__ == '__main__':
52 filename = "grabs/stoplocations.pickle"
53 try:
54 datapoints = pickle.load(open(filename, "r"))
55 except IOError, e:
56 c = BusStopMashup.Client()
57 datapoints = {}
58 for k in ('41 antares', 'heron & data centre', 'kent & albert',
59 'carling & preston', 'bank & florence'):
60 datapoints[k] = list(c.findStops(k))
62 f = open(filename, "w")
63 pickle.dump(datapoints, f)
64 f.close()
65 for k in datapoints:
66 print k
67 analyze(datapoints[k])