2012-02-05 01:48:25 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import mapnik
|
|
|
|
import sys
|
2012-03-20 21:28:21 +01:00
|
|
|
import os.path
|
2012-03-20 21:34:16 +01:00
|
|
|
from compare import compare, summary
|
2012-02-05 01:48:25 +01:00
|
|
|
|
2012-03-23 20:42:11 +01:00
|
|
|
dirname = os.path.dirname(__file__)
|
|
|
|
|
2012-02-17 21:00:40 +01:00
|
|
|
class MyText(mapnik.FormattingNode):
|
2012-02-05 01:48:25 +01:00
|
|
|
def __init__(self):
|
2012-02-17 21:00:40 +01:00
|
|
|
mapnik.FormattingNode.__init__(self)
|
2012-02-05 01:48:25 +01:00
|
|
|
self.expr = mapnik.Expression("[name]")
|
2012-02-05 16:29:27 +01:00
|
|
|
self.expr_nr = mapnik.Expression("[nr]")
|
2012-02-05 01:48:25 +01:00
|
|
|
|
|
|
|
def apply(self, properties, feature, output):
|
2012-02-05 21:32:03 +01:00
|
|
|
colors = [mapnik.Color('red'),
|
|
|
|
mapnik.Color('green'),
|
2012-02-05 01:48:25 +01:00
|
|
|
mapnik.Color('blue')]
|
2012-02-05 04:09:09 +01:00
|
|
|
text = self.expr.evaluate(feature)
|
2012-02-05 16:29:27 +01:00
|
|
|
if int(feature['nr']) > 5:
|
|
|
|
i = 0
|
|
|
|
my_properties = mapnik.CharProperties(properties)
|
|
|
|
for char in text:
|
|
|
|
my_properties.fill = colors[i % len(colors)]
|
|
|
|
output.append(my_properties, char)
|
|
|
|
i += 1
|
|
|
|
else:
|
|
|
|
output.append(properties, text)
|
2012-02-05 21:32:03 +01:00
|
|
|
|
2012-02-05 04:09:09 +01:00
|
|
|
def add_expressions(self, output):
|
|
|
|
output.insert(self.expr)
|
2012-02-05 16:29:27 +01:00
|
|
|
output.insert(self.expr_nr)
|
2012-02-05 01:48:25 +01:00
|
|
|
|
2012-02-05 21:32:03 +01:00
|
|
|
|
2012-02-17 21:00:40 +01:00
|
|
|
class IfElse(mapnik.FormattingNode):
|
2012-02-05 21:32:03 +01:00
|
|
|
def __init__(self, condition, if_node, else_node):
|
2012-02-17 21:00:40 +01:00
|
|
|
mapnik.FormattingNode.__init__(self)
|
2012-02-05 21:32:03 +01:00
|
|
|
self.condition = mapnik.Expression(condition)
|
|
|
|
self.if_node = if_node
|
|
|
|
self.else_node = else_node
|
|
|
|
|
|
|
|
def apply(self, properties, feature, output):
|
|
|
|
c = self.condition.evaluate(feature)
|
|
|
|
if c:
|
|
|
|
self.if_node.apply(properties, feature, output)
|
|
|
|
else:
|
|
|
|
self.else_node.apply(properties, feature, output)
|
|
|
|
|
|
|
|
def add_expressions(self, output):
|
|
|
|
output.insert(self.condition)
|
|
|
|
self.if_node.add_expressions(output)
|
|
|
|
self.else_node.add_expressions(output)
|
|
|
|
|
2012-02-05 16:29:27 +01:00
|
|
|
m = mapnik.Map(600, 100)
|
2012-02-05 01:48:25 +01:00
|
|
|
m.background = mapnik.Color('white')
|
|
|
|
|
|
|
|
text = mapnik.TextSymbolizer()
|
2012-02-05 16:29:27 +01:00
|
|
|
text.placements.defaults.displacement = (0, 5)
|
2012-02-17 21:00:40 +01:00
|
|
|
text.placements.defaults.format.face_name = 'DejaVu Sans Book'
|
2012-02-05 01:48:25 +01:00
|
|
|
|
|
|
|
point = mapnik.PointSymbolizer()
|
|
|
|
|
2012-02-05 21:32:03 +01:00
|
|
|
rule = mapnik.Rule()
|
2012-02-05 01:48:25 +01:00
|
|
|
rule.symbols.append(text)
|
|
|
|
rule.symbols.append(point)
|
|
|
|
|
2012-02-05 21:32:03 +01:00
|
|
|
style = mapnik.Style()
|
2012-02-05 01:48:25 +01:00
|
|
|
style.rules.append(rule)
|
|
|
|
|
|
|
|
m.append_style('Style', style)
|
|
|
|
|
|
|
|
|
|
|
|
layer = mapnik.Layer('Layer')
|
2012-03-23 20:42:11 +01:00
|
|
|
layer.datasource = mapnik.Shapefile(file=os.path.join(dirname,"data/points.shp"))
|
2012-02-05 01:48:25 +01:00
|
|
|
layer.styles.append('Style')
|
|
|
|
m.layers.append(layer)
|
|
|
|
|
2012-02-05 16:29:27 +01:00
|
|
|
bbox = mapnik.Box2d(-0.05, -0.01, 0.95, 0.01)
|
|
|
|
m.zoom_to_box(bbox)
|
2012-02-05 01:48:25 +01:00
|
|
|
|
2012-02-19 02:03:25 +01:00
|
|
|
formatnode = mapnik.FormattingFormat()
|
|
|
|
formatnode.child = mapnik.FormattingText("[name]")
|
2012-02-11 11:21:33 +01:00
|
|
|
formatnode.fill = mapnik.Color("green")
|
2012-02-05 01:48:25 +01:00
|
|
|
|
|
|
|
format_trees = [
|
2012-02-19 02:03:25 +01:00
|
|
|
('TextNode', mapnik.FormattingText("[name]")),
|
2012-02-05 21:32:03 +01:00
|
|
|
('MyText', MyText()),
|
|
|
|
('IfElse', IfElse("[nr] != '5'",
|
2012-02-19 02:03:25 +01:00
|
|
|
mapnik.FormattingText("[name]"),
|
|
|
|
mapnik.FormattingText("'SPECIAL!'"))),
|
2012-02-11 11:21:33 +01:00
|
|
|
('Format', formatnode),
|
2012-02-19 02:03:25 +01:00
|
|
|
('List', mapnik.FormattingList([
|
|
|
|
mapnik.FormattingText("[name]+'\n'"),
|
2012-02-11 11:21:33 +01:00
|
|
|
MyText()
|
2012-02-19 02:03:25 +01:00
|
|
|
])
|
|
|
|
)
|
2012-02-05 01:48:25 +01:00
|
|
|
]
|
|
|
|
|
2012-02-19 02:03:25 +01:00
|
|
|
|
2012-02-05 01:48:25 +01:00
|
|
|
for format_tree in format_trees:
|
|
|
|
text.placements.defaults.format_tree = format_tree[1]
|
2012-03-23 20:42:11 +01:00
|
|
|
mapnik.render_to_file(m, os.path.join(dirname,"images", 'python-%s.png' % format_tree[0]), 'png')
|
|
|
|
compare(os.path.join(dirname,"images", 'python-%s.png' % format_tree[0]),
|
|
|
|
os.path.join(dirname,"images", 'python-%s-reference.png' % format_tree[0])
|
2012-03-20 21:34:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
summary()
|