-
Notifications
You must be signed in to change notification settings - Fork 2
/
traffic_manager.py
162 lines (130 loc) · 5.23 KB
/
traffic_manager.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
import os
import math
import aiohttp
import aiofiles
from PIL import Image
import time
from geopy import distance
import pandas as pd
import seaborn as sns
import numpy as np
import json
import logging
logging.basicConfig()
logging.root.setLevel(logging.NOTSET)
env = json.loads(open("variables.env").read())
for k, v in env.items():
os.environ[k] = v
async def get_tiles(format_="png", increase_zoom=4):
substriction_key = os.getenv("SUBSCRIPTION_KEY")
style = "absolute"
zoom = 10 + increase_zoom
# left-upper bound
x1 = 593 * 2**increase_zoom
y1 = 382 * 2**increase_zoom
# right-bottom bound
x2 = ((593+2) * 2**increase_zoom) + increase_zoom
y2 = ((382+2) * 2**increase_zoom) + increase_zoom
files = {}
async with aiohttp.ClientSession(trust_env=True) as session:
# for i, tile in enumerate(tiles):
curr_x = x1
while curr_x < x2:
curr_y = y1
while curr_y < y2:
async with session.get(
f"https://atlas.microsoft.com/traffic/flow/tile/{format_}?subscription-key={substriction_key}&api-version=1.0&style={style}&zoom={zoom}&x={curr_x}&y={curr_y}",
ssl=False) as resp:
if resp.status == 200:
f = await aiofiles.open(f'utils/map{curr_x}-{curr_y}.{format_}', mode='wb')
await f.write(await resp.read())
await f.close()
files[f"{curr_x-x1},{curr_y-y1}"] = f'utils/map{curr_x}-{curr_y}.{format_}'
else:
logging.warning(resp)
curr_y+=1
curr_x+=1
return files, (x1, y1), (x2, y2)
def preprocess_png_tiles(tiles, x1, x2):
size = x2-x1
new_tile = Image.new("RGB", (size*256, size*256), "white")
for tile, path in tiles.items():
img = Image.open(path)
x, y = tile.split(',')
new_tile.paste(img, (int(x)*256, int(y)*256))
os.remove(path)
new_tile.save("utils/concat.png")
return "utils/concat.png"
def get_coordinates_of_pixel(pixelX, pixelY, tileSize):
# https://docs.microsoft.com/en-us/azure/azure-maps/zoom-levels-and-tile-grid?tabs=csharp
tmp = math.e ** ((0.5 - pixelY / (tileSize * math.pow(2, 14))) * 4 * math.pi)
sinLatitude = (tmp - 1) / (tmp + 1)
latitude = math.asin(sinLatitude) * 180 / math.pi
longitude = 360 * pixelX / (math.pow(2, 14) * tileSize) - 180
return longitude, latitude
def get_dense_coordinates(path, coordinate1, coordinate2):
img = Image.open(path).load()
tileSize = 256 * (coordinate2[0]-coordinate1[0])
x = 0
vector_tile = np.zeros((tileSize, tileSize))
while x < tileSize:
y=0
while y < tileSize:
curr_heat = img[x, y][0] - img[x, y][1]
if x != 0:
pix = img[x-1, y]
if pix[0] - pix[1] < curr_heat:
curr_heat += 50
if x != tileSize-1:
pix = img[x + 1, y]
if pix[0] - pix[1] < curr_heat:
curr_heat += 50
if y != 0:
pix = img[x, y-1]
if pix[0] - pix[1] < curr_heat:
curr_heat += 50
if y != tileSize-1:
pix = img[x - 1, y]
if pix[0] - pix[1] < curr_heat:
curr_heat += 50
vector_tile[y][x] = curr_heat
y+=1
x+=1
# sns.heatmap(vector_tile).get_figure().savefig("utils/heatmap.png")
pixelStartX = 256 * coordinate1[0]
pixelStartY = 256 * coordinate1[1]
indices = np.nonzero(vector_tile > 350)
coordinates = []
for i in range(len(indices[1])):
x = indices[1][i]
y = indices[0][i]
long, lat = get_coordinates_of_pixel(pixelStartX+x, pixelStartY+y, tileSize)
coordinates.append((long, lat))
return coordinates
async def traffic_flow_tiles(data_frames):
while True:
tiles, coordinate1, coordinate2 = await get_tiles(format_="png")
# Create concatanated png
time_stamp = time.time()
concat_path = preprocess_png_tiles(tiles, coordinate1[0], coordinate2[0])
# extract density date
coordinates = get_dense_coordinates(concat_path, (593 * 2**4, 382 * 2**4), (((593+2) * 2**4) + 4, ((382+2) * 2**4) + 4))
railway_stations = data_frames['railway_stations']
unique_station_names = railway_stations['STATION_NAME'].drop_duplicates()
stations = railway_stations.iloc[unique_station_names.index.to_list()]
traffic_index = np.zeros(len(stations))
for i in coordinates:
for j, row in stations.iterrows():
s_x = row['LONGITUDE']
s_y = row['LATITUDE']
if pd.notna(s_x) and pd.notna(s_y):
dist = distance.geodesic((i[1], i[0], ), (s_y, s_x)).km
if dist < 3:
traffic_index[j] += 1
stations['traffic_index'] = traffic_index
stations.to_csv(f'utils/traffic_index/{time_stamp}.csv')
# wait 5m to get next traffic index
current_time = time.time()
run_time = current_time-time_stamp
if run_time > 0:
time.sleep(500-(run_time))