forked from jonblack/cmpgpx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfx.py
48 lines (34 loc) · 1.23 KB
/
gfx.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 logging
import math
_log = logging.getLogger(__name__)
import cairocffi as cairo
import geotiler
def draw_track(track, bounds):
""" Draws the given tracks with the given bounds onto a cairo surface. """
_log.info("Drawing track")
mm = geotiler.Map(extent=bounds, zoom=14)
width, height = mm.size
image = geotiler.render_map(mm)
# create cairo surface
buff = bytearray(image.convert('RGBA').tobytes('raw', 'BGRA'))
surface = cairo.ImageSurface.create_for_data(
buff, cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context(surface)
p_radius = 2
for p in track:
cr.set_source_rgba(0.0, 0.0, 1.0, 1.0)
a1_x, a1_y = mm.rev_geocode((p.longitude, p.latitude))
cr.arc(a1_x, a1_y, p_radius, 0, 2 * math.pi)
cr.fill()
return surface
def add_padding(bbox, padding_pct):
""" Add the given percentage padding to the given bounding box. """
min_lat = bbox[1]
max_lat = bbox[3]
min_lon = bbox[0]
max_lon = bbox[2]
lat_pad = ((max_lat - min_lat) / 100) * padding_pct
lon_pad = ((max_lon - min_lon) / 100) * padding_pct
bbox = (min_lon - lon_pad, min_lat - lat_pad,
max_lon + lon_pad, max_lat + lat_pad)
return bbox