-
Notifications
You must be signed in to change notification settings - Fork 0
/
greatcircle.py
52 lines (42 loc) · 1.64 KB
/
greatcircle.py
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
import sys
import math
import json
import os.path
from operator import itemgetter
class GreatCircleCalculator:
def __init__(self, lat, long):
self.origin = { "latitude": lat, "longitude": long }
#using the Haversine formula
def getDistanceFromOrigin(self, lat2, long2):
lat1 = self.origin['latitude']
long1 = self.origin['longitude']
R = 6371;
phi1 = math.radians(lat2);
phi2 = math.radians(lat1);
phi = math.radians(lat2-lat1);
lmda = math.radians(long2-long1);
a = math.sin(phi/2) * math.sin(phi/2) + math.cos(phi1) * math.cos(phi2) * math.sin(lmda/2) * math.sin(lmda/2);
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a));
return R * c;
# return only the customers within (limit)km, sorted by ascending user_id
def getInvitees(limit, customers):
invitees = []
for customer in sorted(customers, key=itemgetter('user_id')):
if customer['distance'] < limit:
print str(customer['user_id']) + ' ' + customer['name']
invitees.append(customer)
return invitees
def main():
gcCalc = GreatCircleCalculator(53.339428, -6.257664)
customers = []
with open('customers.json', 'r+') as f:
print f
for line in iter(f):
customer = json.loads(line)
lat = float(customer['latitude'])
long = float(customer['longitude'])
customer['distance'] = gcCalc.getDistanceFromOrigin(lat, long)
customers.append(customer)
invitees = getInvitees(100, customers)
if __name__ == '__main__':
main()