2 # vi: set softtabstop=4 shiftwidth=4 tabstop=8 expandtab:
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
= [[], [], [], []]
27 if entry
[0].latitude
>= center
.latitude
:
28 if entry
[0].longitude
<= center
.longitude
:
29 splitEntries
[NORTHWEST
].append(entry
)
31 splitEntries
[NORTHEAST
].append(entry
)
33 if entry
[0].longitude
<= center
.longitude
:
34 splitEntries
[SOUTHWEST
].append(entry
)
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)
43 return [[entry
[1] for entry
in corner
] for corner
in splitEntries
]