diff --git a/create_poster.py b/create_poster.py index 80dd324..6038c31 100755 --- a/create_poster.py +++ b/create_poster.py @@ -54,6 +54,10 @@ def main(): help='Statistics: minimal distance') args_parser.add_argument('--stat-max', dest='stat_max', metavar='KM', type=float, default=0.0, help='Statistics: maximal distance') + args_parser.add_argument('--map-url', dest='map_url', metavar='URL', type=str, default='', + help='URL to fetch a static map (e.g. http://domain.tld/staticmap.php') + args_parser.add_argument('--map-provider', dest='map_provider', metavar='NAME', type=str, default='', + help='Name of map provider (must be defined within staticmap service)') args_parser.add_argument('--background-color', dest='background_color', metavar='COLOR', type=str, default='#222222', help='Background color of poster (default: "#222222").') args_parser.add_argument('--track-color', dest='track_color', metavar='COLOR', type=str, default='#4DD2FF', @@ -92,6 +96,8 @@ def main(): 'total': args.stat_total, 'min': args.stat_min, 'max': args.stat_max} + p.map_url = args.map_url + p.map_provider = args.map_provider p.tracks = tracks p.draw(args.output) diff --git a/src/heatmap_drawer.py b/src/heatmap_drawer.py index e1e6b52..f94f3eb 100644 --- a/src/heatmap_drawer.py +++ b/src/heatmap_drawer.py @@ -4,7 +4,7 @@ # license that can be found in the LICENSE file. from . import utils - +import urllib.request class TracksDrawer: def __init__(self): @@ -45,8 +45,31 @@ def draw(self, poster, d, w, h, offset_x, offset_y): d_x = max_x - min_x d_y = max_y - min_y + #print ("{:f}\n".format(max_x(max_y - min_y), (h / (d_y * scale) - 1) / 2)) + # compute scale - scale = w/d_x if w/h <= d_x/d_y else h/d_y + if self.poster.map_url and self.poster.map_provider: + if w/h <= d_x/d_y: + scale = w/d_x + max_y_ = max_y + (max_y - min_y) * (h / (d_y * scale) - 1) / 2 + min_y_ = min_y - (max_y - min_y) * (h / (d_y * scale) - 1) / 2 + max_x_ = max_x + min_x_ = min_x + else: + scale = h/d_y + max_x_ = max_x + (max_x - min_x) * (w / (d_x * scale) - 1) / 2 + min_x_ = min_x - (max_x - min_x) * (w / (d_x * scale) - 1) / 2 + max_y_ = max_y + min_y_ = min_y + + (min_lat, min_lng) = utils.xy2latlng(min_x_, min_y_) + (max_lat, max_lng) = utils.xy2latlng(max_x_, max_y_) + + backgroundImage = "{}?bounds={:f},{:f},{:f},{:f}&size=2048x2048&maptype={}"; + urllib.request.urlretrieve(backgroundImage.format(self.poster.map_url, min_lat, min_lng, max_lat, max_lng, self.poster.map_provider), "img/map.png") + d.add(d.image("img/map.png", insert=(offset_x, offset_y), size=(w, h))) + else: + scale = w/d_x if w/h <= d_x/d_y else h/d_y # compute offsets such that projected track is centered in its rect offset_x += 0.5 * w - 0.5 * scale * d_x diff --git a/src/poster.py b/src/poster.py index bae35f1..8165a83 100644 --- a/src/poster.py +++ b/src/poster.py @@ -14,6 +14,8 @@ def __init__(self, drawer): self.tracks = [] self.colors = {"background": "#222222", "text": "#FFFFFF", "special": "#FFFF00", "track": "#4DD2FF"} self.statistics = {"label": "Runs", "num": 0, "total": 0.0, "min": 0.0, "max": 0.0} + self.map_url = "" + self.map_provider = "" self.width = 200 self.height = 300 self.tracks_drawer = drawer diff --git a/src/utils.py b/src/utils.py index 6ed6f3d..a7c7cd0 100644 --- a/src/utils.py +++ b/src/utils.py @@ -11,6 +11,10 @@ def latlng2xy(lat, lng): return lng/180+1, 0.5-math.log(math.tan(math.pi/4*(1+lat/90)))/math.pi +def xy2latlng(x, y): + return (math.atan(math.exp((0.5-y)*math.pi))*4/math.pi-1)*90, (x-1)*180 + + def compute_bounds_xy(polylines): min_x = None max_x = None