-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathheatmap_drawer.py
212 lines (194 loc) · 9.29 KB
/
heatmap_drawer.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
"""Draw a heatmap poster."""
# Copyright 2016-2023 Florian Pigorsch & Contributors. All rights reserved.
#
# Use of this source code is governed by a MIT-style
# license that can be found in the LICENSE file.
import argparse
import logging
import math
from typing import Dict, List, Optional, Tuple
import s2sphere # type: ignore
import svgwrite # type: ignore
from geopy.distance import distance # type: ignore
from gpxtrackposter import utils
from gpxtrackposter.exceptions import ParameterError
from gpxtrackposter.poster import Poster
from gpxtrackposter.tracks_drawer import TracksDrawer
from gpxtrackposter.xy import XY
log = logging.getLogger(__name__)
class HeatmapDrawer(TracksDrawer):
"""Draw a heatmap Poster based on the tracks.
Attributes:
_center: Center of the heatmap.
_radius: Scale the heatmap so that a circle with radius (in KM) is visible.
Methods:
create_args: Create arguments for heatmap.
fetch_args: Get arguments passed.
draw: Draw the heatmap based on the Poster's tracks.
"""
def __init__(self, the_poster: Poster):
super().__init__(the_poster)
self._center = None
self._radius = None
self._heatmap_line_width_low: float = 10.0
self._heatmap_line_width_upp: float = 1000.0
self._heatmap_line_width_lower: List[Tuple[float, float]] = [(0.10, 5.0), (0.20, 2.0), (1.0, 0.30)]
self._heatmap_line_width_upper: List[Tuple[float, float]] = [(0.02, 0.5), (0.05, 0.2), (1.0, 0.05)]
self._heatmap_line_width: Optional[List[Tuple[float, float]]] = self._heatmap_line_width_lower
def create_args(self, args_parser: argparse.ArgumentParser) -> None:
group = args_parser.add_argument_group("Heatmap Type Options")
group.add_argument(
"--heatmap-center",
dest="heatmap_center",
metavar="LAT,LNG",
type=str,
help="Center of the heatmap (default: automatic).",
)
group.add_argument(
"--heatmap-radius",
dest="heatmap_radius",
metavar="RADIUS_KM",
type=float,
help="Scale the heatmap such that at least a circle with radius=RADIUS_KM is visible "
"(default: automatic).",
)
group.add_argument(
"--heatmap-line-transparency-width",
dest="heatmap_line_width",
metavar="TRANSP_1,WIDTH_1, TRANSP_2,WIDTH_2, TRANSP_3,WIDTH_3",
type=str,
help="Define three transparency and width tuples for the heatmap lines or set it to "
"`automatic` for automatic calculation (default: 0.1,5.0, 0.2,2.0, 1.0,0.3).",
)
# pylint: disable=too-many-branches
def fetch_args(self, args: argparse.Namespace) -> None:
"""Get arguments that were passed, and also perform basic validation on them.
For example, make sure the center is an actual lat, lng , and make sure the radius is a
positive number. Also, if radius is passed, then center must also be passed.
Raises:
ParameterError: Center was not a valid lat, lng coordinate, or radius was not positive.
ParameterError: Line transparency and width values are not valid
"""
self._center = None
if args.heatmap_center:
latlng_str = args.heatmap_center.split(",")
if len(latlng_str) != 2:
raise ParameterError(f"Not a valid LAT,LNG pair: {args.heatmap_center}")
try:
lat = float(latlng_str[0].strip())
lng = float(latlng_str[1].strip())
except ValueError as e:
raise ParameterError(f"Not a valid LAT,LNG pair: {args.heatmap_center}") from e
if not -90 <= lat <= 90 or not -180 <= lng <= 180:
raise ParameterError(f"Not a valid LAT,LNG pair: {args.heatmap_center}")
self._center = s2sphere.LatLng.from_degrees(lat, lng)
if args.heatmap_radius:
if args.heatmap_radius <= 0:
raise ParameterError(f"Not a valid radius: {args.heatmap_radius} (must be > 0)")
if not args.heatmap_center:
raise ParameterError("--heatmap-radius needs --heatmap-center")
self._radius = args.heatmap_radius
if args.heatmap_line_width:
if args.heatmap_line_width.lower() == "automatic":
self._heatmap_line_width = None
else:
trans_width_str = args.heatmap_line_width.split(",")
if len(trans_width_str) != 6:
raise ParameterError(f"Not three valid TRANSPARENCY,WIDTH pairs: {args.heatmap_line_width}")
try:
self._heatmap_line_width = []
for value in range(0, 5, 2):
transparency = float(trans_width_str[value].strip())
width = float(trans_width_str[value + 1].strip())
if transparency < 0 or transparency > 1:
raise ParameterError(
f"Not a valid TRANSPARENCY value (0 < value < 1): {transparency} in "
f"{args.heatmap_line_width}"
)
self._heatmap_line_width.append((transparency, width))
except ValueError as e:
raise ParameterError(f"Not three valid TRANSPARENCY,WIDTH pairs: {args.heatmap_line_width}") from e
def _get_line_transparencies_and_widths(self, bbox: s2sphere.sphere.LatLngRect) -> List[Tuple[float, float]]:
if self._heatmap_line_width:
return self._heatmap_line_width
# automatic calculation of line transparencies and widths
low = self._heatmap_line_width_low
upp = self._heatmap_line_width_upp
lower = self._heatmap_line_width_lower
upper = self._heatmap_line_width_upper
d = distance(
(bbox.lo().lat().degrees, bbox.lo().lng().degrees), (bbox.hi().lat().degrees, bbox.hi().lng().degrees)
).km
log.info("Length of diagonal of boundary box %s", str(d))
if d > upp:
return upper
if d < low:
return lower
return [
(
lower[0][0] + d / (upp - low) * (upper[0][0] - lower[0][0]),
(lower[0][1] + d / (upp - low) * (upper[0][1] - lower[0][1])),
),
(
lower[1][0] + d / (upp - low) * (upper[1][0] - lower[1][0]),
(lower[1][1] + d / (upp - low) * (upper[1][1] - lower[1][1])),
),
(
lower[2][0] + d / (upp - low) * (upper[2][0] - lower[2][0]),
(lower[2][1] + d / (upp - low) * (upper[2][1] - lower[2][1])),
),
]
def _determine_bbox(self) -> s2sphere.LatLngRect:
if self._center:
log.info("Forcing heatmap center to %s", str(self._center))
dlat, dlng = 0, 0
if self._radius:
er = 6378.1
quarter = er * math.pi / 2
dlat = 90 * self._radius / quarter
scale = 1 / math.cos(self._center.lat().radians)
dlng = scale * 90 * self._radius / quarter
else:
for tr in self.poster.tracks:
for line in tr.polylines:
for latlng in line:
d = abs(self._center.lat().degrees - latlng.lat().degrees)
dlat = max(dlat, d)
d = abs(self._center.lng().degrees - latlng.lng().degrees)
while d > 360:
d -= 360
if d > 180:
d = 360 - d
dlng = max(dlng, d)
return s2sphere.LatLngRect.from_center_size(self._center, s2sphere.LatLng.from_degrees(2 * dlat, 2 * dlng))
tracks_bbox = s2sphere.LatLngRect()
for tr in self.poster.tracks:
tracks_bbox = tracks_bbox.union(tr.bbox())
return tracks_bbox
def draw(self, dr: svgwrite.Drawing, g: svgwrite.container.Group, size: XY, offset: XY) -> None:
"""Draw the heatmap based on tracks."""
bbox = self._determine_bbox()
line_transparencies_and_widths = self._get_line_transparencies_and_widths(bbox)
year_groups: Dict[int, svgwrite.container.Group] = {}
for tr in self.poster.tracks:
year = tr.start_time().year
if year not in year_groups:
g_year = dr.g(id=f"year{year}")
g.add(g_year)
year_groups[year] = g_year
else:
g_year = year_groups[year]
color = self.color(self.poster.length_range, tr.length(), tr.special)
for line in utils.project(bbox, size, offset, tr.polylines):
for opacity, width in line_transparencies_and_widths:
g_year.add(
dr.polyline(
points=line,
stroke=color,
stroke_opacity=opacity,
fill="none",
stroke_width=width,
stroke_linejoin="round",
stroke_linecap="round",
)
)