-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
299 lines (223 loc) · 9.11 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
from flask import Flask, request
from math import sin, cos, atan, sqrt, pi, log
import numpy as np
from flask_pymongo import PyMongo
import datetime
from flask.json import jsonify
from bson.objectid import ObjectId
from numba import jit
app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb://localhost:27017/Soopa"
mongo = PyMongo(app)
earthRadius = 6371
max_lat = 51.69031564760695
min_lat = 51.29150416286872
max_lng = 0.3226031423929305
min_lng = -0.49931701722075333
lat_grid_steps = 92
lng_grid_steps = 45
lat_grid_size = (max_lat - min_lat) / lat_grid_steps
lng_grid_size = (max_lng - min_lng) / lng_grid_steps
severity_map = {'Violence and sexual offences': 10, 'Anti-social behaviour': 3, 'Burglary': 5,
'Criminal damage and arson': -1, 'Other theft': -1, 'Possession of weapons': 7,
'Robbery': 8, 'Theft from the person': 8, 'Vehicle crime': 5, 'Other crime': -1,
'Public order': 9, 'Shoplifting': 2, 'Drugs': 1, 'Bicycle theft': 10}
# Get Great Circle Distance between 2 coordinates
@jit(nopython=True, parallel=True, fastmath=True)
def lat_lng_distance(lat1, lng1, lat2, lng2):
lat1 = lat1 * pi / 180
lat2 = lat2 * pi / 180
lng1 = lng1 * pi / 180
lng2 = lng2 * pi / 180
radian_distance = atan(sqrt(((cos(lat2) * sin(lng1 - lng2)) ** 2 + (
cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(lng1 - lng2)) ** 2)) / (
sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lng1 - lng2)))
if radian_distance < 0:
radian_distance += pi
return earthRadius * radian_distance
building_heights_data = []
heightData = np.genfromtxt("building_heights.csv", delimiter=",")
for building in heightData:
building_heights_data.append((float(building[0]), float(building[1]), float(building[2])))
# Building Height from Lat Long and radius
@app.route("/building_height/")
def building_heights():
current_lat = float(request.args.get("lat"))
current_lng = float(request.args.get("lng"))
radius = float(request.args.get("radius"))
buildings = get_tallest_buildings(current_lat, current_lng, radius, 3)
# Convert to JSON
json_building_list = []
for (lat, lng, height) in buildings:
json_building_list.append({
"lat": lat,
"lng": lng,
"height": height
})
return jsonify(json_building_list)
def get_tallest_buildings(current_lat, current_lng, radius, count):
buildings_in_radius = [(lat, lng, height) for (lat, lng, height) in building_heights_data if
(lat_lng_distance(lat, lng, current_lat, current_lng) <= radius)]
# Filter for top 3
top_buildings_in_radius = sorted(buildings_in_radius, key=lambda x: x[2], reverse=True)[:count]
return top_buildings_in_radius
# Active Crimes (Last 5 minutes)
@app.route("/active_crimes")
def get_active_crimes():
current_lat = float(request.args.get("lat"))
current_lng = float(request.args.get("lng"))
radius = float(request.args.get("radius"))
superhero = request.args.get("superhero")
time_limit = datetime.datetime.now() - datetime.timedelta(minutes=5)
active_crimes = mongo.db.crimes.find({"datetime": {"$gt": time_limit}})
# Filter based on radius
active_crimes_in_radius = [active_crime for active_crime in active_crimes if (
lat_lng_distance(active_crime["lat"], active_crime["lng"], current_lat, current_lng) <= radius)]
for crime in active_crimes_in_radius:
crime["_id"] = str(crime["_id"])
tallest_buildings = get_tallest_buildings(crime["lat"], crime["lng"], 0.2, 3)
json_building_list = []
for (lat, lng, height) in tallest_buildings:
json_building_list.append({
"lat": lat,
"lng": lng,
"height": height
})
crime["vantage_points"] = json_building_list
crime["crime_duration"] = get_crime_duration_estimate(superhero, crime["type"])
return jsonify(active_crimes_in_radius)
def get_crime_duration_estimate(superhero, crime_type):
crimes = list(mongo.db.crimes.find({
"solved_by": superhero,
"type": crime_type
}))
times = [(crime["solved_at"] - crime["datetime"]).total_seconds() for crime in crimes]
estimate = np.nanmean(times) / 60
return estimate
@app.route("/new_crime")
def new_crime():
crime = {}
crime['datetime'] = datetime.datetime.now()
crime['lng'] = np.random.uniform(-0.49931701722075333, 0.3226031423929305)
crime['lat'] = np.random.uniform(51.29150416286872, 51.69031564760695)
crime['type'] = np.random.choice(list(severity_map.keys()))
lat_param = request.args.get("lat")
if lat_param is not None:
crime["lat"] = float(lat_param)
lng_param = request.args.get("lat")
if lng_param is not None:
crime["lat"] = float(lat_param)
sev = severity_map[crime["type"]]
if sev != -1:
crime['severity'] = sev
else:
crime['severity'] = np.random.randint(3, 8)
crimes = mongo.db.crimes.find({
"type": crime.get('type')
})
times = [(crime["solved_at"] - crime["datetime"]).total_seconds() for crime in crimes]
solve_time = np.nanmean(times)
crime['solved_at'] = crime['datetime'] + datetime.timedelta(seconds=solve_time)
crime['solved_by'] = np.random.choice(['Spiderman', 'Wonder Woman', 'Ironman', 'Batman', 'Aquaman'])
return str(mongo.db.crimes.insert_one(crime).inserted_id)
@app.route("/new_crime_unsolved")
def new_crime_unsolved():
crime = {}
crime['datetime'] = datetime.datetime.now()
crime['lng'] = np.random.uniform(-0.49931701722075333, 0.3226031423929305)
crime['lat'] = np.random.uniform(51.29150416286872, 51.69031564760695)
crime['type'] = np.random.choice(list(severity_map.keys()))
lat_param = request.args.get("lat")
if lat_param is not None:
crime["lat"] = float(lat_param)
lng_param = request.args.get("lat")
if lng_param is not None:
crime["lat"] = float(lat_param)
sev = severity_map[crime["type"]]
if sev != -1:
crime['severity'] = sev
else:
crime['severity'] = np.random.randint(3, 8)
crime['solved_at'] = ""
crime['solved_by'] = datetime.datetime.min
return str(mongo.db.crimes.insert_one(crime).inserted_id)
# Heatmap data
@app.route("/heatmap")
def heatmap_data():
# Calculate Heatmap data based on discounted severity
discount_factor_scale = 0.5
discount_factor_seconds = 1800
minimum_severity_threshold = 0.005
discount_factor = discount_factor_scale ** (1 / discount_factor_seconds)
maximum_age = (discount_factor_seconds * (log(minimum_severity_threshold) - log(10))) / log(discount_factor_scale)
# Get all crimes more recent than minimum age
crimes = list(
mongo.db.crimes.find({"datetime": {"$gt": datetime.datetime.now() - datetime.timedelta(seconds=maximum_age)}}))
# Discount severity based on age
output = []
for crime in crimes:
severity = crime["severity"] * (
discount_factor ** (datetime.datetime.now() - crime["datetime"]).total_seconds())
output.append([crime["lat"], crime["lng"], severity])
return jsonify(output)
@app.route("/demo/crime1")
def demo_crime_1():
crime = {
"datetime": datetime.datetime.now(),
"lat": 51.524992,
"lng": -0.133230,
"type": "Anti-social behaviour",
"severity": 3,
"solved_by": "",
"solved_at": datetime.datetime.min
}
return str(mongo.db.crimes.insert_one(crime).inserted_id)
@app.route("/demo/crime1")
def demo_crime_1():
crime = {
"datetime": datetime.datetime.now(),
"lat": 51.524992,
"lng": -0.133230,
"type": "Anti-social behaviour",
"severity": 3,
"solved_by": "",
"solved_at": datetime.datetime.min
}
return str(mongo.db.crimes.insert_one(crime).inserted_id)
@app.route("/demo/crime2")
def demo_crime_2():
crime = {
"datetime": datetime.datetime.now(),
"lat": 51.522728,
"lng": -0.140155,
"type": "Drugs",
"severity": 1,
"solved_by": "",
"solved_at": datetime.datetime.min
}
return str(mongo.db.crimes.insert_one(crime).inserted_id)
@app.route("/demo/crime3")
def demo_crime_3():
crime = {
"datetime": datetime.datetime.now(),
"lat": 51.523825,
"lng": -0.128404,
"type": "Bicycle theft",
"severity": 10,
"solved_by": "",
"solved_at": datetime.datetime.min
}
return str(mongo.db.crimes.insert_one(crime).inserted_id)
# User Login
@app.route("/user/<username>")
def user_login(username):
users = mongo.db.heroes.find_one({"name": username})
return jsonify(users)
@app.route("/solved")
def solved_crime():
id_param = ObjectId(request.args.get("id"))
superhero = request.args.get("superhero")
mongo.db.crimes.find_one_and_update({'_id': id_param}, {"$set": {"solved_by": superhero}}, upsert=False)
mongo.db.crimes.find_one_and_update({'_id': id_param}, {"$set": {"solved_at": datetime.datetime.now()}},
upsert=False)
app.run()