-
Notifications
You must be signed in to change notification settings - Fork 1
/
mvg_example.py
48 lines (35 loc) · 1.38 KB
/
mvg_example.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
import json
from datetime import datetime
from math import log2
from heatmap import HeatMap, DefaultRenderer
from heatmap.extractors import MVGExtractor
home = (48.128446, 11.650027)
mvg_key = 'YOUR MVG KEY' # Replace with your own key
mvg = MVGExtractor(mvg_key)
def color(value: float) -> str:
""" A blue-to-red color scale """
pct = 1 - max(0, min(1, value))
hue = 210.0 * pct
return 'hsl(%.2f, 75%%, 50%%)' % hue
def label(unit):
""" Show travel time on the label """
time = int(unit.get("time"))
return f'<div style="font-size: 20px; width: 120px;text-align: center;">{time} min</div>'
with open('./geo/muenchen.json', 'r') as fp:
geo_json = json.load(fp)
def calc_time(pt1, pt2):
""" Calculate the time to get somewhere by UBahn """
t_ubahn = mvg.average_time_between(pt1, pt2, datetime.now())
if not t_ubahn:
return None
value = int(t_ubahn / 60.0)
# Since the value will be normalized, we save it also on a separate key to show it on the label
return {'value': value, 'time': value}
renderer = DefaultRenderer(center=home, zoom=12, color_scale=color, label=label)
h_map = HeatMap(home, geo_json, square_size=250, filename='mvg', load_intermediate_results=True)
h_map.generate(calc_time)
# Normalize on a log2 scale
h_map.normalize(lambda v, _1, _2: log2(v))
# Then again on a 0 to 1 range
h_map.normalize()
h_map.render(renderer)