/
object_test.py
566 lines (460 loc) · 16.4 KB
/
object_test.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from nose.tools import *
from utilities import execution_path, run_all
import tempfile
import mapnik
def setup():
# All of the paths used are relative, if we run the tests
# from another directory we need to chdir()
os.chdir(execution_path('.'))
def test_debug_symbolizer():
s = mapnik.DebugSymbolizer()
eq_(s.mode,mapnik.debug_symbolizer_mode.collision)
def test_raster_symbolizer():
s = mapnik.RasterSymbolizer()
eq_(s.comp_op,mapnik.CompositeOp.src_over) # note: mode is deprecated
eq_(s.scaling,mapnik.scaling_method.NEAR)
eq_(s.opacity,1.0)
eq_(s.colorizer,None)
eq_(s.filter_factor,-1)
eq_(s.mesh_size,16)
eq_(s.premultiplied,None)
s.premultiplied = True
eq_(s.premultiplied,True)
def test_line_pattern():
s = mapnik.LinePatternSymbolizer(mapnik.PathExpression('../data/images/dummy.png'))
eq_(s.filename, '../data/images/dummy.png')
eq_(s.smooth,0.0)
eq_(s.transform,'')
eq_(s.comp_op,mapnik.CompositeOp.src_over)
eq_(s.clip,True)
def test_line_symbolizer():
s = mapnik.LineSymbolizer()
eq_(s.rasterizer, mapnik.line_rasterizer.FULL)
eq_(s.smooth,0.0)
eq_(s.comp_op,mapnik.CompositeOp.src_over)
eq_(s.clip,True)
eq_(s.stroke.width, 1)
eq_(s.stroke.opacity, 1)
eq_(s.stroke.color, mapnik.Color('black'))
eq_(s.stroke.line_cap, mapnik.line_cap.BUTT_CAP)
eq_(s.stroke.line_join, mapnik.line_join.MITER_JOIN)
l = mapnik.LineSymbolizer(mapnik.Color('blue'), 5.0)
eq_(l.stroke.width, 5)
eq_(l.stroke.opacity, 1)
eq_(l.stroke.color, mapnik.Color('blue'))
eq_(l.stroke.line_cap, mapnik.line_cap.BUTT_CAP)
eq_(l.stroke.line_join, mapnik.line_join.MITER_JOIN)
s = mapnik.Stroke(mapnik.Color('blue'), 5.0)
l = mapnik.LineSymbolizer(s)
eq_(l.stroke.width, 5)
eq_(l.stroke.opacity, 1)
eq_(l.stroke.color, mapnik.Color('blue'))
eq_(l.stroke.line_cap, mapnik.line_cap.BUTT_CAP)
eq_(l.stroke.line_join, mapnik.line_join.MITER_JOIN)
def test_line_symbolizer_stroke_reference():
l = mapnik.LineSymbolizer(mapnik.Color('green'),0.1)
l.stroke.add_dash(.1,.1)
l.stroke.add_dash(4,4)
eq_(l.stroke.get_dashes(), [(.1,.1),(4,4)])
eq_(l.stroke.color,mapnik.Color('green'))
eq_(l.stroke.opacity,1.0)
assert_almost_equal(l.stroke.width,0.1)
# https://github.com/mapnik/mapnik/issues/1427
def test_stroke_dash_api():
stroke = mapnik.Stroke()
dashes = [(1.0,1.0)]
stroke.dasharray = dashes
eq_(stroke.dasharray, dashes)
stroke.add_dash(.1,.1)
dashes.append((.1,.1))
eq_(stroke.dasharray, dashes)
def test_text_symbolizer():
s = mapnik.TextSymbolizer()
eq_(s.orientation,None)
s.orientation = mapnik.Expression('45');
eq_(str(s.orientation),str(mapnik.Expression('45')))
eq_(s.comp_op,mapnik.CompositeOp.src_over)
eq_(s.clip,True)
eq_(s.halo_rasterizer,mapnik.halo_rasterizer.FULL)
# https://github.com/mapnik/mapnik/issues/1420
eq_(s.text_transform, mapnik.text_transform.NONE)
# https://github.com/mapnik/mapnik/issues/1427
eq_(s.wrap_char,ord(' '))
eq_(s.wrap_character,ord(' '))
s.wrap_char = ord('\n')
eq_(s.wrap_char,ord('\n'))
eq_(s.wrap_character,ord('\n'))
eq_(s.format.wrap_character,ord('\n'))
s.wrap_character = ord('\r')
eq_(s.wrap_char,ord('\r'))
eq_(s.wrap_character,ord('\r'))
eq_(s.format.wrap_character,ord('\r'))
s.format.wrap_character = ord(' ')
eq_(s.wrap_char,ord(' '))
eq_(s.wrap_character,ord(' '))
eq_(s.format.wrap_character,ord(' '))
# old args required method
ts = mapnik.TextSymbolizer(mapnik.Expression('[Field_Name]'), 'Font Name', 8, mapnik.Color('black'))
# eq_(str(ts.name), str(mapnik2.Expression('[Field_Name]'))) name field is no longer supported
eq_(ts.format.face_name, 'Font Name')
eq_(ts.format.text_size, 8)
eq_(ts.format.fill, mapnik.Color('black'))
eq_(ts.properties.label_placement, mapnik.label_placement.POINT_PLACEMENT)
eq_(ts.properties.horizontal_alignment, mapnik.horizontal_alignment.AUTO)
def test_shield_symbolizer_init():
s = mapnik.ShieldSymbolizer(mapnik.Expression('[Field Name]'), 'DejaVu Sans Bold', 6, mapnik.Color('#000000'), mapnik.PathExpression('../data/images/dummy.png'))
eq_(s.comp_op,mapnik.CompositeOp.src_over)
eq_(s.clip,True)
eq_(s.displacement, (0.0,0.0))
eq_(s.allow_overlap, False)
eq_(s.avoid_edges, False)
eq_(s.character_spacing,0)
#eq_(str(s.name), str(mapnik2.Expression('[Field Name]'))) name field is no longer supported
eq_(s.face_name, 'DejaVu Sans Bold')
eq_(s.allow_overlap, False)
eq_(s.fill, mapnik.Color('#000000'))
eq_(s.force_odd_labels, False)
eq_(s.halo_fill, mapnik.Color('rgb(255,255,255)'))
eq_(s.halo_radius, 0)
eq_(s.label_placement, mapnik.label_placement.POINT_PLACEMENT)
eq_(s.minimum_distance, 0.0)
eq_(s.text_ratio, 0)
eq_(s.text_size, 6)
eq_(s.wrap_width, 0)
eq_(s.vertical_alignment, mapnik.vertical_alignment.AUTO)
eq_(s.label_spacing, 0)
eq_(s.label_position_tolerance, 0)
# 22.5 * M_PI/180.0 initialized by default
assert_almost_equal(s.max_char_angle_delta, 0.39269908169872414)
eq_(s.wrap_character, ' ')
eq_(s.text_transform, mapnik.text_transform.NONE)
eq_(s.line_spacing, 0)
eq_(s.character_spacing, 0)
# r1341
eq_(s.wrap_before, False)
eq_(s.horizontal_alignment, mapnik.horizontal_alignment.AUTO)
eq_(s.justify_alignment, mapnik.justify_alignment.AUTO)
eq_(s.opacity, 1.0)
# r2300
eq_(s.minimum_padding, 0.0)
# was mixed with s.opacity
eq_(s.text_opacity, 1.0)
eq_(s.shield_displacement, (0.0,0.0))
# TODO - the pattern in bindings seems to be to get/set
# strings for PathExpressions... should we pass objects?
eq_(s.filename, '../data/images/dummy.png')
# 11c34b1: default transform list is empty, not identity matrix
eq_(s.transform, '')
eq_(s.fontset, None)
# ShieldSymbolizer missing image file
# images paths are now PathExpressions are evaluated at runtime
# so it does not make sense to throw...
#@raises(RuntimeError)
#def test_shieldsymbolizer_missing_image():
# s = mapnik.ShieldSymbolizer(mapnik.Expression('[Field Name]'), 'DejaVu Sans Bold', 6, mapnik.Color('#000000'), mapnik.PathExpression('../#data/images/broken.png'))
def test_shield_symbolizer_modify():
s = mapnik.ShieldSymbolizer(mapnik.Expression('[Field Name]'), 'DejaVu Sans Bold', 6, mapnik.Color('#000000'), mapnik.PathExpression('../data/images/dummy.png'))
# transform expression
def check_transform(expr, expect_str=None):
s.transform = expr
eq_(s.transform, expr if expect_str is None else expect_str)
check_transform("matrix(1 2 3 4 5 6)", "matrix(1, 2, 3, 4, 5, 6)")
check_transform("matrix(1, 2, 3, 4, 5, 6 +7)", "matrix(1, 2, 3, 4, 5, (6+7))")
check_transform("rotate([a])")
check_transform("rotate([a] -2)", "rotate(([a]-2))")
check_transform("rotate([a] -2 -3)", "rotate([a], -2, -3)")
check_transform("rotate([a] -2 -3 -4)", "rotate(((([a]-2)-3)-4))")
check_transform("rotate([a] -2, 3, 4)", "rotate(([a]-2), 3, 4)")
check_transform("translate([tx]) rotate([a])")
check_transform("scale([sx], [sy]/2)")
# TODO check expected failures
def test_point_symbolizer():
p = mapnik.PointSymbolizer()
eq_(p.filename,'')
eq_(p.transform,'')
eq_(p.opacity,1.0)
eq_(p.allow_overlap,False)
eq_(p.ignore_placement,False)
eq_(p.comp_op,mapnik.CompositeOp.src_over)
eq_(p.placement, mapnik.point_placement.CENTROID)
p = mapnik.PointSymbolizer(mapnik.PathExpression("../data/images/dummy.png"))
p.allow_overlap = True
p.opacity = 0.5
p.ignore_placement = True
p.placement = mapnik.point_placement.INTERIOR
eq_(p.allow_overlap, True)
eq_(p.opacity, 0.5)
eq_(p.filename,'../data/images/dummy.png')
eq_(p.ignore_placement,True)
eq_(p.placement, mapnik.point_placement.INTERIOR)
def test_markers_symbolizer():
p = mapnik.MarkersSymbolizer()
eq_(p.allow_overlap, False)
eq_(p.opacity,1.0)
eq_(p.fill_opacity,None)
eq_(p.filename,'shape://ellipse')
eq_(p.placement,mapnik.marker_placement.POINT_PLACEMENT)
eq_(p.multi_policy,mapnik.marker_multi_policy.EACH)
eq_(p.fill,None)
eq_(p.ignore_placement,False)
eq_(p.spacing,100)
eq_(p.max_error,0.2)
eq_(p.width,None)
eq_(p.height,None)
eq_(p.transform,'')
eq_(p.clip,True)
eq_(p.comp_op,mapnik.CompositeOp.src_over)
p.width = mapnik.Expression('12')
p.height = mapnik.Expression('12')
eq_(str(p.width),'12')
eq_(str(p.height),'12')
p.width = mapnik.Expression('[field] + 2')
p.height = mapnik.Expression('[field] + 2')
eq_(str(p.width),'([field]+2)')
eq_(str(p.height),'([field]+2)')
stroke = mapnik.Stroke()
stroke.color = mapnik.Color('black')
stroke.width = 1.0
p.stroke = stroke
p.fill = mapnik.Color('white')
p.allow_overlap = True
p.opacity = 0.5
p.fill_opacity = 0.5
p.placement = mapnik.marker_placement.LINE_PLACEMENT
p.multi_policy = mapnik.marker_multi_policy.WHOLE
eq_(p.allow_overlap, True)
eq_(p.opacity, 0.5)
eq_(p.fill_opacity, 0.5)
eq_(p.multi_policy,mapnik.marker_multi_policy.WHOLE)
eq_(p.placement,mapnik.marker_placement.LINE_PLACEMENT)
#https://github.com/mapnik/mapnik/issues/1285
#https://github.com/mapnik/mapnik/issues/1427
p.marker_type = 'arrow'
eq_(p.marker_type,'shape://arrow')
eq_(p.filename,'shape://arrow')
# PointSymbolizer missing image file
# images paths are now PathExpressions are evaluated at runtime
# so it does not make sense to throw...
#@raises(RuntimeError)
#def test_pointsymbolizer_missing_image():
# p = mapnik.PointSymbolizer(mapnik.PathExpression("../data/images/broken.png"))
def test_polygon_symbolizer():
p = mapnik.PolygonSymbolizer()
eq_(p.smooth,0.0)
eq_(p.comp_op,mapnik.CompositeOp.src_over)
eq_(p.clip,True)
eq_(p.fill, mapnik.Color('gray'))
eq_(p.fill_opacity, 1)
p = mapnik.PolygonSymbolizer(mapnik.Color('blue'))
eq_(p.fill, mapnik.Color('blue'))
eq_(p.fill_opacity, 1)
def test_building_symbolizer_init():
p = mapnik.BuildingSymbolizer()
eq_(p.fill, mapnik.Color('gray'))
eq_(p.fill_opacity, 1)
eq_(p.height,None)
def test_stroke_init():
s = mapnik.Stroke()
eq_(s.width, 1)
eq_(s.opacity, 1)
eq_(s.color, mapnik.Color('black'))
eq_(s.line_cap, mapnik.line_cap.BUTT_CAP)
eq_(s.line_join, mapnik.line_join.MITER_JOIN)
eq_(s.gamma,1.0)
s = mapnik.Stroke(mapnik.Color('blue'), 5.0)
s.gamma = .5
eq_(s.width, 5)
eq_(s.opacity, 1)
eq_(s.color, mapnik.Color('blue'))
eq_(s.gamma, .5)
eq_(s.line_cap, mapnik.line_cap.BUTT_CAP)
eq_(s.line_join, mapnik.line_join.MITER_JOIN)
def test_stroke_dash_arrays():
s = mapnik.Stroke()
s.add_dash(1,2)
s.add_dash(3,4)
s.add_dash(5,6)
eq_(s.get_dashes(), [(1,2),(3,4),(5,6)])
def test_map_init():
m = mapnik.Map(256, 256)
eq_(m.width, 256)
eq_(m.height, 256)
eq_(m.srs, '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
eq_(m.base, '')
eq_(m.maximum_extent, None)
m = mapnik.Map(256, 256, '+proj=latlong')
eq_(m.srs, '+proj=latlong')
def test_map_style_access():
m = mapnik.Map(256, 256)
sty = mapnik.Style()
m.append_style("style",sty)
styles = list(m.styles)
eq_(len(styles),1)
eq_(styles[0][0],'style')
# returns a copy so let's just check it is the right instance
eq_(isinstance(styles[0][1],mapnik.Style),True)
def test_map_maximum_extent_modification():
m = mapnik.Map(256, 256)
eq_(m.maximum_extent, None)
m.maximum_extent = mapnik.Box2d()
eq_(m.maximum_extent, mapnik.Box2d())
m.maximum_extent = None
eq_(m.maximum_extent, None)
# Map initialization from string
def test_map_init_from_string():
map_string = '''<Map background-color="steelblue" base="./" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
<Style name="My Style">
<Rule>
<PolygonSymbolizer fill="#f2eff9"/>
<LineSymbolizer stroke="rgb(50%,50%,50%)" stroke-width="0.1"/>
</Rule>
</Style>
<Layer name="boundaries">
<StyleName>My Style</StyleName>
<Datasource>
<Parameter name="type">shape</Parameter>
<Parameter name="file">../../demo/data/boundaries</Parameter>
</Datasource>
</Layer>
</Map>'''
if 'shape' in mapnik.DatasourceCache.plugin_names():
m = mapnik.Map(600, 300)
eq_(m.base, '')
mapnik.load_map_from_string(m, map_string)
eq_(m.base, './')
mapnik.load_map_from_string(m, map_string, False, "") # this "" will have no effect
eq_(m.base, './')
tmp_dir = tempfile.gettempdir()
try:
mapnik.load_map_from_string(m, map_string, False, tmp_dir)
except RuntimeError:
pass # runtime error expected because shapefile path should be wrong and datasource will throw
eq_(m.base, tmp_dir) # tmp_dir will be set despite the exception because load_map mostly worked
m.base = 'foo'
mapnik.load_map_from_string(m, map_string, True, ".")
eq_(m.base, '.')
# Color initialization
@raises(Exception) # Boost.Python.ArgumentError
def test_color_init_errors():
c = mapnik.Color()
@raises(RuntimeError)
def test_color_init_errors():
c = mapnik.Color('foo') # mapnik config
def test_color_init():
c = mapnik.Color('blue')
eq_(c.a, 255)
eq_(c.r, 0)
eq_(c.g, 0)
eq_(c.b, 255)
eq_(c.to_hex_string(), '#0000ff')
c = mapnik.Color('#f2eff9')
eq_(c.a, 255)
eq_(c.r, 242)
eq_(c.g, 239)
eq_(c.b, 249)
eq_(c.to_hex_string(), '#f2eff9')
c = mapnik.Color('rgb(50%,50%,50%)')
eq_(c.a, 255)
eq_(c.r, 128)
eq_(c.g, 128)
eq_(c.b, 128)
eq_(c.to_hex_string(), '#808080')
c = mapnik.Color(0, 64, 128)
eq_(c.a, 255)
eq_(c.r, 0)
eq_(c.g, 64)
eq_(c.b, 128)
eq_(c.to_hex_string(), '#004080')
c = mapnik.Color(0, 64, 128, 192)
eq_(c.a, 192)
eq_(c.r, 0)
eq_(c.g, 64)
eq_(c.b, 128)
eq_(c.to_hex_string(), '#004080c0')
def test_color_equality():
c1 = mapnik.Color('blue')
c2 = mapnik.Color(0,0,255)
c3 = mapnik.Color('black')
c3.r = 0
c3.g = 0
c3.b = 255
c3.a = 255
eq_(c1, c2)
eq_(c1, c3)
c1 = mapnik.Color(0, 64, 128)
c2 = mapnik.Color(0, 64, 128)
c3 = mapnik.Color(0, 0, 0)
c3.r = 0
c3.g = 64
c3.b = 128
eq_(c1, c2)
eq_(c1, c3)
c1 = mapnik.Color(0, 64, 128, 192)
c2 = mapnik.Color(0, 64, 128, 192)
c3 = mapnik.Color(0, 0, 0, 255)
c3.r = 0
c3.g = 64
c3.b = 128
c3.a = 192
eq_(c1, c2)
eq_(c1, c3)
c1 = mapnik.Color('rgb(50%,50%,50%)')
c2 = mapnik.Color(128, 128, 128, 255)
c3 = mapnik.Color('#808080')
c4 = mapnik.Color('gray')
eq_(c1, c2)
eq_(c1, c3)
eq_(c1, c4)
c1 = mapnik.Color('hsl(0, 100%, 50%)') # red
c2 = mapnik.Color('hsl(120, 100%, 50%)') # lime
c3 = mapnik.Color('hsla(240, 100%, 50%, 0.5)') # semi-transparent solid blue
eq_(c1, mapnik.Color('red'))
eq_(c2, mapnik.Color('lime'))
eq_(c3, mapnik.Color(0,0,255,128))
def test_rule_init():
min_scale = 5
max_scale = 10
r = mapnik.Rule()
eq_(r.name, '')
eq_(r.min_scale, 0)
eq_(r.max_scale, float('inf'))
eq_(r.has_else(), False)
eq_(r.has_also(), False)
r = mapnik.Rule()
r.set_else(True)
eq_(r.has_else(), True)
eq_(r.has_also(), False)
r = mapnik.Rule()
r.set_also(True)
eq_(r.has_else(), False)
eq_(r.has_also(), True)
r = mapnik.Rule("Name")
eq_(r.name, 'Name')
eq_(r.min_scale, 0)
eq_(r.max_scale, float('inf'))
eq_(r.has_else(), False)
eq_(r.has_also(), False)
r = mapnik.Rule("Name")
eq_(r.name, 'Name')
eq_(r.min_scale, 0)
eq_(r.max_scale, float('inf'))
eq_(r.has_else(), False)
eq_(r.has_also(), False)
r = mapnik.Rule("Name", min_scale)
eq_(r.name, 'Name')
eq_(r.min_scale, min_scale)
eq_(r.max_scale, float('inf'))
eq_(r.has_else(), False)
eq_(r.has_also(), False)
r = mapnik.Rule("Name", min_scale, max_scale)
eq_(r.name, 'Name')
eq_(r.min_scale, min_scale)
eq_(r.max_scale, max_scale)
eq_(r.has_else(), False)
eq_(r.has_also(), False)
if __name__ == "__main__":
setup()
run_all(eval(x) for x in dir() if x.startswith("test_"))