Script to grab stop codes from allstops.xml and sort
[ottawa-travel-planner.git] / LatLongTools.py
blobe7df6158f7a55fd0bf1c784d84f0f52d766b25fe
2 # vi: set softtabstop=4 shiftwidth=4 tabstop=8 expandtab:
4 import math
6 def latdist(lat1, lat2):
7 return (lat1 - lat2) * 111.325
9 def longdist(lat, long1, long2):
10 return (long1 - long2) * math.cos(lat / 180.0 * math.pi) * 111.325
12 def locdist(loc1, loc2):
13 """terrible approximation"""
14 xdist = longdist(loc1.latitude, loc1.longitude, loc2.longitude)
15 ydist = latdist(loc1.latitude, loc2.latitude)
16 return math.sqrt(xdist * xdist + ydist * ydist)
18 def findCorners(center, entries):
19 """Takes a list of two-element tuples: (location, tag).
20 Returns a four-element list, one for each corner (northwest,
21 northeast, southwest, southeast). Each element is in turn a list of
22 tags, sorted by descending distance from the center."""
24 # Split by nearest corner
25 splitEntries = [[], [], [], []]
26 for entry in entries:
27 if entry[0].latitude >= center.latitude:
28 if entry[0].longitude <= center.longitude:
29 splitEntries[NORTHWEST].append(entry)
30 else:
31 splitEntries[NORTHEAST].append(entry)
32 else:
33 if entry[0].longitude <= center.longitude:
34 splitEntries[SOUTHWEST].append(entry)
35 else:
36 splitEntries[SOUTHEAST].append(entry)
38 for corner in splitEntries:
39 # Descending sort by distance from center.
40 corner.sort(None, lambda entry: locdist(entry[0], center), True)
42 # Return the tags.
43 return [[entry[1] for entry in corner] for corner in splitEntries]
45 NORTHWEST = 0
46 NORTHEAST = 1
47 SOUTHWEST = 2
48 SOUTHEAST = 3