-
Notifications
You must be signed in to change notification settings - Fork 2
/
maplayout.py
344 lines (262 loc) · 12.7 KB
/
maplayout.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import json
from parsedegrees import *
from units import *
import testutils
# The following values are declared here instead of
# MapLayout.__init__() so that we can use the same values in the unit
# tests.
#
# You can also change these values if you want different defaults
# when not using a configuration file. May I suggest that you
# make a known-good configuration file instead and base the
# rest of your work on that.
#
default_draw_map_frame = True
default_draw_ticks = True
default_draw_map = True
default_draw_scale = True
default_paper_width_mm = inch_to_mm (11)
default_paper_height_mm = inch_to_mm (8.5)
default_zoom = 15
default_center_lat = 19.4621106
default_center_lon = -96.9040473
default_map_scale_denom = 50000
default_map_width_mm = inch_to_mm (10)
default_map_height_mm = inch_to_mm (7.375)
default_map_to_left_margin_mm = inch_to_mm (0.5)
default_map_to_top_margin_mm = inch_to_mm (0.375)
default_scale_xpos_mm = inch_to_mm (5.5)
default_scale_ypos_mm = inch_to_mm (8.125)
class MapLayout:
def __init__ (self):
# Sane defaults for if a config file is not specified
self.draw_map_frame = default_draw_map_frame
self.draw_ticks = default_draw_ticks
self.draw_map = default_draw_map
self.draw_scale = default_draw_scale
self.paper_width_mm = default_paper_width_mm
self.paper_height_mm = default_paper_height_mm
self.zoom = default_zoom
self.center_lat = default_center_lat
self.center_lon = default_center_lon
self.map_scale_denom = default_map_scale_denom
self.map_width_mm = default_map_width_mm
self.map_height_mm = default_map_height_mm
self.map_to_left_margin_mm = default_map_to_left_margin_mm
self.map_to_top_margin_mm = default_map_to_top_margin_mm
self.scale_xpos_mm = default_scale_xpos_mm
self.scale_ypos_mm = default_scale_ypos_mm
self.scale_large_divisions_interval_m = 1000
self.scale_num_large_divisions = 4
self.scale_small_divisions_interval_m = 100
self.scale_num_small_divisions = 10
self.scale_large_ticks_m = [ 0, "0",
1000, "1",
2000, "2",
3000, "3",
4000, "4 Km" ]
self.scale_small_ticks_m = [ 0, "0 m",
500, "500",
1000, "1000" ]
def validate (self):
if not (type (self.zoom) == int and self.zoom >= 0 and self.zoom <= 19):
raise ValueError ("Zoom must be an integer in the range [0, 19]")
def load_from_json (self, json_obj):
if "draw-map-frame" in json_obj:
self.draw_map_frame = json_obj["draw-map-frame"]
if "draw-ticks" in json_obj:
self.draw_ticks = json_obj["draw-ticks"]
if "draw-map" in json_obj:
self.draw_map = json_obj["draw-map"]
if "draw-scale" in json_obj:
self.draw_scale = json_obj["draw-scale"]
if "paper-width" in json_obj:
self.paper_width_mm = parse_units_value (json_obj["paper-width"])
if "paper-height" in json_obj:
self.paper_height_mm = parse_units_value (json_obj["paper-height"])
if "zoom" in json_obj:
self.zoom = json_obj["zoom"]
if "center-lon" in json_obj:
self.center_lon = parse_degrees_value (json_obj["center-lon"])
if "center-lat" in json_obj:
self.center_lat = parse_degrees_value (json_obj["center-lat"])
if "map-scale" in json_obj:
self.map_scale_denom = json_obj["map-scale"]
if "map-width" in json_obj:
self.map_width_mm = parse_units_value (json_obj["map-width"])
if "map-height" in json_obj:
self.map_height_mm = parse_units_value (json_obj["map-height"])
if "map-to-left-margin" in json_obj:
self.map_to_left_margin_mm = parse_units_value (json_obj["map-to-left-margin"])
if "map-to-top-margin" in json_obj:
self.map_to_top_margin_mm = parse_units_value (json_obj["map-to-top-margin"])
if "scale-xpos" in json_obj:
self.scale_xpos_mm = parse_units_value (json_obj["scale-xpos"])
if "scale-ypos" in json_obj:
self.scale_ypos_mm = parse_units_value (json_obj["scale-ypos"])
if "scale-large-divisions-interval-m" in json_obj:
self.scale_large_divisions_interval_m = json_obj["scale-large-divisions-interval-m"]
if "scale-num-large-divisions" in json_obj:
self.scale_num_large_divisions = json_obj["scale-num-large-divisions"]
if "scale-small-divisions-interval-m" in json_obj:
self.scale_small_divisions_interval_m = json_obj["scale-small-divisions-interval-m"]
if "scale-num-small-divisions" in json_obj:
self.scale_num_small_divisions = json_obj["scale-num-small-divisions"]
if "scale-large-ticks-m" in json_obj:
self.scale_large_ticks_m = json_obj["scale-large-ticks-m"]
if "scale-small-ticks-m" in json_obj:
self.scale_small_ticks_m = json_obj["scale-small-ticks-m"]
#################### tests ####################
class TestMapLayout (testutils.TestCaseHelper):
def test_map_layout_has_defaults_for_what_to_render (self):
layout = MapLayout ()
self.assertEqual (layout.draw_map_frame, default_draw_map_frame)
self.assertEqual (layout.draw_ticks, default_draw_ticks)
self.assertEqual (layout.draw_map, default_draw_map)
self.assertEqual (layout.draw_scale, default_draw_scale)
def test_map_layout_parses_what_to_render (self):
layout = MapLayout ()
layout.load_from_json (json.loads ("""
{ "draw-map-frame" : false,
"draw-ticks" : false,
"draw-map" : false,
"draw-scale" : false }
"""))
self.assertEqual (layout.draw_map_frame, False)
self.assertEqual (layout.draw_ticks, False)
self.assertEqual (layout.draw_map, False)
self.assertEqual (layout.draw_scale, False)
def test_map_layout_has_us_letter_default_paper_size (self):
layout = MapLayout ()
self.assertFloatEquals (layout.paper_width_mm, inch_to_mm (11.0))
self.assertFloatEquals (layout.paper_height_mm, inch_to_mm (8.5))
def test_map_layout_parses_numeric_paper_size (self):
paper_size_numeric = """
{
"paper-width" : 50.8,
"paper-height" : 25.4
}
"""
layout = MapLayout ()
layout.load_from_json (json.loads (paper_size_numeric))
self.assertFloatEquals (layout.paper_width_mm, 50.8)
self.assertFloatEquals (layout.paper_height_mm, 25.4)
def test_map_layout_parses_inches_paper_size (self):
paper_size_numeric = """
{
"paper-width" : "11 in",
"paper-height" : "8.5 in"
}
"""
layout = MapLayout ()
layout.load_from_json (json.loads (paper_size_numeric))
self.assertFloatEquals (layout.paper_width_mm, inch_to_mm (11))
self.assertFloatEquals (layout.paper_height_mm, inch_to_mm (8.5))
def test_map_layout_has_default_paper_size (self):
layout = MapLayout ()
self.assertFloatEquals (layout.paper_width_mm, default_paper_width_mm)
self.assertFloatEquals (layout.paper_height_mm, default_paper_height_mm)
def test_map_layout_parses_zoom (self):
layout = MapLayout ()
layout.load_from_json (json.loads ("""
{ "zoom" : 15 }
"""))
self.assertEqual (layout.zoom, 15)
def test_map_layout_parses_center_lon_and_lat (self):
layout = MapLayout ()
layout.load_from_json (json.loads ("""
{ "center-lat" : "19d27m43s",
"center-lon" : -96.9040473 }
"""))
self.assertFloatEquals (layout.center_lat, parse_degrees ("19d27m43s"))
self.assertFloatEquals (layout.center_lon, -96.9040473)
def test_map_layout_parses_map_scale (self):
layout = MapLayout ()
layout.load_from_json (json.loads ("""
{ "map-scale" : 50000 }
"""))
self.assertFloatEquals (layout.map_scale_denom, 50000)
def test_map_layout_has_default_center_and_scale (self):
layout = MapLayout ()
self.assertFloatEquals (layout.center_lat, default_center_lat)
self.assertFloatEquals (layout.center_lon, default_center_lon)
self.assertFloatEquals (layout.map_scale_denom, 50000)
def test_map_layout_has_default_map_size (self):
layout = MapLayout ()
self.assertFloatEquals (layout.map_width_mm, default_map_width_mm)
self.assertFloatEquals (layout.map_height_mm, default_map_height_mm)
def test_map_layout_parses_map_width_and_height (self):
layout = MapLayout ()
layout.load_from_json (json.loads ("""
{ "map-width" : "100 mm",
"map-height" : "200 mm" }
"""))
self.assertFloatEquals (layout.map_width_mm, 100)
self.assertFloatEquals (layout.map_height_mm, 200)
def test_map_layout_has_default_map_to_top_left_margin (self):
layout = MapLayout ()
self.assertFloatEquals (layout.map_to_left_margin_mm, default_map_to_left_margin_mm)
self.assertFloatEquals (layout.map_to_top_margin_mm, default_map_to_top_margin_mm)
def test_map_layout_parses_map_to_top_left_margin (self):
layout = MapLayout ()
layout.load_from_json (json.loads ("""
{ "map-to-left-margin" : "100 mm",
"map-to-top-margin" : "200 mm" }
"""))
self.assertFloatEquals (layout.map_to_left_margin_mm, 100)
self.assertFloatEquals (layout.map_to_top_margin_mm, 200)
def test_map_layout_has_default_scale_position (self):
layout = MapLayout ()
self.assertFloatEquals (layout.scale_xpos_mm, default_scale_xpos_mm)
self.assertFloatEquals (layout.scale_ypos_mm, default_scale_ypos_mm)
def test_map_layout_parses_scale_position (self):
layout = MapLayout ()
layout.load_from_json (json.loads ("""
{ "scale-xpos" : "100 mm",
"scale-ypos" : "200 mm" }
"""))
self.assertFloatEquals (layout.scale_xpos_mm, 100)
self.assertFloatEquals (layout.scale_ypos_mm, 200)
def test_map_layout_has_defaults_for_scale_parameters (self):
layout = MapLayout ()
self.assertEqual (layout.scale_large_divisions_interval_m, 1000)
self.assertEqual (layout.scale_num_large_divisions, 4)
self.assertEqual (layout.scale_small_divisions_interval_m, 100)
self.assertEqual (layout.scale_num_small_divisions, 10)
self.assertEqual (layout.scale_large_ticks_m, [ 0, "0",
1000, "1",
2000, "2",
3000, "3",
4000, "4 Km" ])
self.assertEqual (layout.scale_small_ticks_m, [ 0, "0 m",
500, "500",
1000, "1000" ])
def test_map_layout_parses_scale_parameters (self):
layout = MapLayout ()
layout.load_from_json (json.loads ("""
{ "scale-large-divisions-interval-m" : 1000,
"scale-num-large-divisions" : 4,
"scale-small-divisions-interval-m" : 100,
"scale-num-small-divisions" : 10,
"scale-large-ticks-m" : [ 0, 0,
1000, 1,
2000, 2,
3000, 3,
4000, 4 ],
"scale-small-ticks-m" : [ 0, 0,
500, 500,
1000, 1000 ]
}
"""))
self.assertEqual (layout.scale_large_divisions_interval_m, 1000)
self.assertEqual (layout.scale_num_large_divisions, 4)
self.assertEqual (layout.scale_small_divisions_interval_m, 100)
self.assertEqual (layout.scale_num_small_divisions, 10)
self.assertEqual (layout.scale_large_ticks_m, [ 0, 0,
1000, 1,
2000, 2,
3000, 3,
4000, 4 ])
self.assertEqual (layout.scale_small_ticks_m, [ 0, 0,
500, 500,
1000, 1000 ])