-
Notifications
You must be signed in to change notification settings - Fork 2
/
chartrenderer.py
149 lines (108 loc) · 4.52 KB
/
chartrenderer.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
import re
import math
import sys
import cairo
import io
import json
from tilecoords import *
import tile_provider
import framerenderer
import scalerenderer
from units import *
import testutils
import maplayout
import tile_provider
import chartgeometry
class ChartRenderer:
def __init__ (self, chart_geometry):
assert chart_geometry is not None
self.geometry = chart_geometry
self.map_layout = chart_geometry.map_layout
# Assumes that the current transformation matrix is set up for millimeters
def render_to_cairo (self, cr):
self.geometry.compute_extents_of_downloaded_tiles ()
if self.map_layout.draw_map:
self.render_map_data (cr)
self.render_map_frame (cr)
if self.map_layout.draw_scale:
self.render_scale (cr)
def render_map_frame (self, cr):
cr.save ()
frame_renderer = framerenderer.FrameRenderer (self.geometry)
if self.map_layout.draw_map_frame:
frame_renderer.render_frame (cr)
if self.map_layout.draw_ticks:
frame_renderer.render_ticks (cr)
cr.restore ()
def clip_to_map (self, cr):
cr.rectangle (self.map_layout.map_to_left_margin_mm, self.map_layout.map_to_top_margin_mm,
self.map_layout.map_width_mm, self.map_layout.map_height_mm)
cr.clip ()
# Downloads tiles and paints them on the master surface
def make_map_surface (self, cr):
geometry = self.geometry
provider = geometry.tile_provider
width_tiles = geometry.east_tile_idx - geometry.west_tile_idx + 1
height_tiles = geometry.south_tile_idx - geometry.north_tile_idx + 1
assert width_tiles >= 1
assert height_tiles >= 1
tile_size = provider.get_tile_size ()
tiles_downloaded = 0
print ("Downloading {0} tiles...".format (width_tiles * height_tiles))
for y in range (0, height_tiles):
for x in range (0, width_tiles):
tile_x = x + geometry.west_tile_idx
tile_y = y + geometry.north_tile_idx
tiles_downloaded += 1
print ("Downloading tile {0}".format(tiles_downloaded), end='\r', flush=True)
png_data = provider.get_tile_png (self.map_layout.zoom, tile_x, tile_y)
tile_surf = cairo.ImageSurface.create_from_png (io.BytesIO (png_data))
tile_xpos = x * tile_size
tile_ypos = y * tile_size
cr.set_source_surface (tile_surf, tile_xpos, tile_ypos)
cr.paint ()
del tile_surf
print ("")
def render_map_data (self, cr):
cr.save ()
self.clip_to_map (cr)
matrix = self.geometry.compute_matrix_from_page_mm_to_map_surface_coordinates ()
matrix.invert ()
cr.transform (matrix)
self.make_map_surface (cr)
cr.restore ()
def render_scale (self, cr):
scale_renderer = scalerenderer.ScaleRenderer (self.map_layout)
scale_renderer.render (cr, self.map_layout.scale_xpos_mm, self.map_layout.scale_ypos_mm)
#################### tests ####################
class TestChartRenderer (testutils.TestCaseHelper):
def make_test_map_layout (self):
layout = maplayout.MapLayout ()
layout.load_from_json (json.loads ("""
{
"paper-width" : "11 in",
"paper-height" : "8.5 in",
"zoom" : 15,
"center-lat" : 19.4621106,
"center-lon" : -96.9040473,
"map-scale" : 50000,
"map-width" : "10 in",
"map-height" : "7.375 in",
"map-to-left-margin" : "0.5 in",
"map-to-top-margin" : "0.375 in"
}
"""))
return layout
def test_downloads_the_correct_range_of_tiles (self):
map_layout = self.make_test_map_layout ()
provider = tile_provider.NullTileProvider ()
geometry = chartgeometry.ChartGeometry (map_layout, provider)
chart_renderer = ChartRenderer (geometry)
geometry.compute_extents_of_downloaded_tiles ()
surface = cairo.ImageSurface (cairo.FORMAT_RGB24, 256, 256)
cr = cairo.Context (surface)
chart_renderer.make_map_surface (cr)
self.assertEqual (provider.west_tile_requested_limit, 7558)
self.assertEqual (provider.north_tile_requested_limit, 14573)
self.assertEqual (provider.east_tile_requested_limit, 7569)
self.assertEqual (provider.south_tile_requested_limit, 14581)