-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess_data.py
More file actions
107 lines (91 loc) · 3.2 KB
/
preprocess_data.py
File metadata and controls
107 lines (91 loc) · 3.2 KB
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
import csv
import requests
class Coordinate:
def __init__(
self,
latitude: float = None,
longitude: float = None,
crime_severity: dict = None,
):
self.latitude = latitude
self.longitude = longitude
self.weightage = 0
self.crime_severity = crime_severity
def __str__(self):
return f"Coordinate({self.latitude}, {self.longitude}, {self.crime_severity})"
def get_latlong(self):
return [self.latitude, self.longitude]
def get_longlat(self):
return [self.longitude, self.latitude]
def get_crime_severity(self):
return self.crime_severity
def get_weight(self):
crime_weights = {
"outrage_of_modesty": 3,
"theft_in_dwelling": 2,
"voyeurism": 3,
"shop_theft": 1,
"rape": 5,
"murder": 5,
"littering": 1,
"jaywalking": 1,
"traffic_accident": 2,
"pickpocketing": 2,
}
weighted_sum = sum(
int(self.crime_severity[crime]) * crime_weights[crime]
for crime in crime_weights
)
self.weightage = weighted_sum
return weighted_sum
class CoordinateGroup:
def __init__(self, group=""):
self.group = group
self.data = self.read_data()
def read_data(self):
"""
Returns a list of Coordinates object
"""
result = []
with open(f"./data/{self.group}.csv", "r") as f:
data = csv.DictReader(f, skipinitialspace=True)
for entry in data:
coordinate = Coordinate(
latitude=float(entry["latitude"]),
longitude=float(entry["longitude"]),
)
del entry["latitude"]
del entry["longitude"]
# this is anti-pattern
coordinate.crime_severity = entry
result.append(coordinate)
self.data = result
return result
def get_coordinates(self):
return [coords.get_longlat() for coords in self.data]
def get_normalized_weights(self):
result = []
# i should actually normalize across the whole map, but i cant be bothered
weighted_sums = [coords.get_weight() for coords in self.data]
min_sum = min(weighted_sums)
max_sum = max(weighted_sums)
normalized_values = [
(value - min_sum) / (max_sum - min_sum) for value in weighted_sums
]
for coords, normalized_value in zip(self.data, normalized_values):
# anti pattern again ahhh
result.append((coords.latitude, coords.longitude, normalized_value))
return result
def get_route(self):
"""
coordinates (list): an array of lat, long values
"""
coordinates = ";".join(
",".join(map(str, coordinate_pair))
for coordinate_pair in self.get_coordinates()
)
print(coordinates)
URL = f"http://router.project-osrm.org/trip/v1/driving/{coordinates}"
PARAMS = {"roundtrip": "true", "geometries": "geojson", "steps": "true"}
r = requests.get(url=URL, params=PARAMS)
return r.json()