-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
158 lines (130 loc) · 5.58 KB
/
function.py
File metadata and controls
158 lines (130 loc) · 5.58 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
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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pickle
import sys
paths_to_data = {"crime": 'data/MVD_News/news_with_location.csv',
"problems": "data/GorodGovSpb/GorodGovSpb.csv",
"art": 'data/OpenStreetMap/objects.csv',
"amenities": "data/OpenStreetMap/amenities.csv"}
#"buildings": 'data/OpenStreetMap/buildings_data.csv',}
#"wifi": 'wifi_data.csv'}
object_rate = {'fountain': 10,
'bench': 9,
'place_of_worship': 8,
'toilets': 7,
'bus_station': 6,
'theatre': 7,
'bicycle_parking': 6,
'payment_terminal': 6,
'atm': 5,
'wifi': 0
}
amenities_rate = {'restaurant': 6,
'waste_disposal': 5,
'bicycle_parking': 6,
'ice_cream': 7,
'vehicle_ramp': 5,
'drinking_water': 6,
'bicycle_rental': 6}
category_rate = {'Фасад': 4,
'Благоустройство': 3,
'Повреждения или неисправность элементов уличной инфраструктуры': 3,
'Незаконная информационная и (или) рекламная конструкция': 4,
'Санитарное состояние': 2}
def crime_type(name):
if "краж" in name.lower():
return 4
elif "граб" in name.lower():
return 2
elif "убийство" in name.lower():
return 0.01
elif "простит" in name.lower():
return 3
elif "уголовный" in name.lower():
return 1
elif "уголовно-процессуальный" in name.lower():
return 3
elif "Кодекс Об Административных Правонарушениях".lower() in name.lower():
return 5
else:
return 0
def problem_type(type_):
try:
return amenities_rate[type_]
except KeyError:
return 0
def art_type(type_):
return object_rate[type_]
def amenities_type(type_):
try:
return amenities_rate[type_]
except KeyError:
return 0
def wifi_type():
return 0
def count_rate(data_, radius_, lat_, lng_):
tr = data_[(data_.lat <= lat_ + radius_) & (lat_ - radius_ <= data_.lat) & (data_.lng <= lng_ + radius_) & (
lng_ - radius_ <= data_.lng)]
if tr.rate.count() == 0:
return 0
else:
x = tr.lat.values - np.ones(tr.lat.count())*lat_
y = tr.lng.values - np.ones(tr.lng.count())*lng_
return np.dot(tr.rate.values, np.exp(x**2 + y**2))
def calculate_tables(left_top_corner_lat=59.90, right_bottom_corner_lat=59.63, left_top_corner_lng=30.42,
right_bottom_corner_lng=30.72):
y_step = (left_top_corner_lat - right_bottom_corner_lat)/100
x_step = -(left_top_corner_lng - right_bottom_corner_lng)/100
radius = 5 * (y_step + x_step)/2
heat_map_image = {type_: np.ndarray(shape=(100, 100), dtype=float) for type_ in paths_to_data.keys()}
data = {type_: pd.read_csv(paths_to_data[type_]) for type_ in paths_to_data.keys()}
data["art"]['rate'] = data["art"].type.apply(art_type)
data["crime"]['rate'] = data["crime"].decreepart.apply(crime_type)
data["amenities"]['rate'] = data["amenities"].amenity.apply(amenities_type)
data["problems"]['rate'] = data["problems"].category.apply(problem_type)
heat_map_image["json"] = {
"type": "FeatureCollection",
"features": []
}
x_line = np.linspace(right_bottom_corner_lat, left_top_corner_lat, 101)
y_line = np.linspace(left_top_corner_lng, right_bottom_corner_lng, 101)
x, y = 0, 0
for i in range(10000):
pol_coord = [[x_line[x], y_line[y]], [x_line[x + 1], y_line[y]],
[x_line[x + 1], y_line[y + 1]], [x_line[x], y_line[y + 1]], [x_line[x], y_line[y]]]
for retr in pol_coord:
retr.reverse()
heat_map_image["json"]["features"].append({"id": i,
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [pol_coord]
},
"properties": {'highlight': {}, 'name': i, 'style': {}}
})
if x < 99:
x += 1
else:
x = 0
y += 1
for i in range(100):
for j in range(100):
lat = right_bottom_corner_lat + y_step*i + y_step/2
lng = left_top_corner_lng + x_step*j + x_step/2
for type_ in heat_map_image.keys():
if type_ != "json":
heat_map_image[type_][i][j] = count_rate(data[type_], radius, lat, lng)
prefix = "[({},{})-({},{})]".format(int(left_top_corner_lat*1000), int(left_top_corner_lng*1000),
int(right_bottom_corner_lat*1000), int(right_bottom_corner_lng*1000))
pickle.dump(heat_map_image, open("dump/heat_map_image{}".format(prefix), "wb"))
for type_ in heat_map_image.keys():
if type_ != 'json':
plt.imshow(heat_map_image[type_])
plt.savefig("images/{}{}".format(type_, prefix))
if __name__ == "__main__":
try:
calculate_tables(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
except:
calculate_tables(left_top_corner_lat=59.90, right_bottom_corner_lat=59.83, left_top_corner_lng=30.12,
right_bottom_corner_lng=30.22)