-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
138 lines (112 loc) · 3.49 KB
/
main.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import json
import requests
from bottle import route, run, abort, static_file, request, post
from math import radians, cos, sin, asin, sqrt
USERS = None
class User:
def __init__(self, ju):
self.name = ju["name"]
self.address = ju["address"]
self.coords = geocode(self.address)
def get_distance_dict(self, loc):
result = self.__dict__
result["distance"] = haversine(self.coords, loc)
return result
def geocode(address):
# print self.name
params = dict(address=address, sensor="false")
r = requests.get("http://maps.googleapis.com/maps/api/geocode/json", params=params)
return r.json()["results"][0]["geometry"]["location"]
def haversine(loc1, loc2):
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(
radians, [loc1["lng"], loc1["lat"], loc2["lng"], loc2["lat"]]
)
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
c = 2 * asin(sqrt(a))
km = 6367 * c
return km
def build_users(filename):
fh = open(filename)
input_users = json.load(fh)
fh.close()
users = []
for u in input_users:
users.append(User(u))
return users
def find_distances_from(address):
address_loc = geocode(address)
result = []
for u in USERS:
result.append(u.get_distance_dict(address_loc))
return (address_loc, result)
USERS = build_users("input.json")
@route("/static/<path:path>")
def callback(path):
return static_file(path, root="static")
@post("/find_users")
def find_users():
address = request.POST["address"]
(address_loc, result) = find_distances_from(address)
return build_html(address, address_loc, result)
def build_html(address, address_loc, users):
result = []
result.append(
"""
<html>
<head>
<title>
Distances
</title>
</head>
<body>
"""
)
users = sorted(users, key=lambda x: x["distance"])
result.append("<h2>Results for location " + address + "</h2>")
url = "http://maps.googleapis.com/maps/api/staticmap"
center = "%s,%s" % (address_loc["lat"], address_loc["lng"])
markers = []
for (i, u) in enumerate(users):
label = chr(ord("A") + i)
markers.append(
"markers=color:red|label:%s|%s,%s"
% (label, u["coords"]["lat"], u["coords"]["lng"])
)
markers.append(
"markers=color:blue|label:.|%s,%s" % (address_loc["lat"], address_loc["lng"])
)
image_src = "%s?center=%s&size=600x300&maptype=roadmap&%s&zoom=9&sensor=false" % (
url,
center,
"&".join(markers),
)
result.append('<img src="%s">' % (image_src))
result.append("<br/><br/>")
result.append("<h2>List of programmers by distance</h2>")
result.append('<table border="1">')
result.append("<tr>")
result.append("<th>Label</th>")
result.append("<th>Name</th>")
result.append("<th>Address</th>")
result.append("<th>Distance</th>")
result.append("</tr>")
for (i, u) in enumerate(users):
result.append("<tr>")
result.append("<td>%s</td>" % (chr(ord("A") + i)))
result.append("<td>%s</td>" % (u["name"]))
result.append("<td>%s</td>" % (u["address"]))
result.append("<td>%s</td>" % (u["distance"]))
result.append("</tr>")
result.append("</table>")
result.append(
"""
</body>
</html>
"""
)
return "\n".join(result)
run(host="localhost", port=8080, reloader=True)