Skip to content

Commit

Permalink
first working solution with background image
Browse files Browse the repository at this point in the history
e.g. with `… —map-url http://localhost/staticmaplite/staticmap.php
--map-provider carto_nolabels_dark`
  • Loading branch information
laufhannes committed Feb 1, 2017
1 parent 301b151 commit ebb0823
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
6 changes: 6 additions & 0 deletions create_poster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)

Expand Down
27 changes: 25 additions & 2 deletions src/heatmap_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# license that can be found in the LICENSE file.

from . import utils

import urllib.request

class TracksDrawer:
def __init__(self):
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/poster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ebb0823

Please sign in to comment.