2010-03-18 20:05:08 +00:00
|
|
|
#encoding: utf8
|
2010-03-18 20:04:35 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from nose.tools import *
|
2011-08-30 22:51:42 +00:00
|
|
|
|
2010-09-26 23:41:16 +00:00
|
|
|
from utilities import execution_path, save_data, contains_word
|
2010-03-18 20:04:35 +00:00
|
|
|
|
|
|
|
import os, mapnik2
|
|
|
|
|
2010-03-19 09:33:57 +00:00
|
|
|
def test_renders_with_agg():
|
2010-03-18 20:05:08 +00:00
|
|
|
sym = mapnik2.GlyphSymbolizer("DejaVu Sans Condensed",
|
|
|
|
mapnik2.Expression("'í'"))
|
|
|
|
sym.allow_overlap = True
|
|
|
|
sym.angle = mapnik2.Expression("[azimuth]+90") #+90 so the top of the glyph points upwards
|
|
|
|
sym.size = mapnik2.Expression("[value]")
|
|
|
|
sym.color = mapnik2.Expression("'#ff0000'")
|
|
|
|
|
|
|
|
_map = create_map_and_append_symbolyzer(sym)
|
|
|
|
im = mapnik2.Image(_map.width,_map.height)
|
|
|
|
mapnik2.render(_map, im)
|
2010-03-19 09:33:57 +00:00
|
|
|
save_data('agg_glyph_symbolizer.png', im.tostring('png'))
|
2010-09-26 23:41:16 +00:00
|
|
|
assert contains_word('\xff\x00\x00\xff', im.tostring())
|
2010-03-19 09:33:57 +00:00
|
|
|
|
|
|
|
def test_renders_with_cairo():
|
|
|
|
if not mapnik2.has_pycairo():
|
|
|
|
return
|
|
|
|
sym = mapnik2.GlyphSymbolizer("DejaVu Sans Condensed",
|
|
|
|
mapnik2.Expression("'í'"))
|
|
|
|
sym.allow_overlap = True
|
|
|
|
sym.angle = mapnik2.Expression("[azimuth]+90") #+90 so the top of the glyph points upwards
|
|
|
|
sym.size = mapnik2.Expression("[value]")
|
|
|
|
sym.color = mapnik2.Expression("'#ff0000'")
|
|
|
|
_map = create_map_and_append_symbolyzer(sym)
|
|
|
|
|
|
|
|
from cStringIO import StringIO
|
|
|
|
import cairo
|
|
|
|
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 256, 256)
|
|
|
|
mapnik2.render(_map, surface)
|
|
|
|
im = mapnik2.Image.from_cairo(surface)
|
2010-03-19 09:38:08 +00:00
|
|
|
save_data('cairo_glyph_symbolizer.png', im.tostring('png'))
|
2010-09-26 23:41:16 +00:00
|
|
|
assert contains_word('\xff\x00\x00\xff', im.tostring())
|
2010-03-18 20:05:08 +00:00
|
|
|
|
2010-03-19 11:19:43 +00:00
|
|
|
def test_load_save_load_map():
|
2010-03-18 20:05:08 +00:00
|
|
|
map = mapnik2.Map(256,256)
|
|
|
|
in_map = "../data/good_maps/glyph_symbolizer.xml"
|
|
|
|
mapnik2.load_map(map, in_map)
|
2010-03-22 11:21:17 +00:00
|
|
|
style = map.find_style('arrows')
|
|
|
|
sym = style.rules[0].symbols[0]
|
|
|
|
assert isinstance(sym, mapnik2.GlyphSymbolizer)
|
|
|
|
assert sym.angle_mode == mapnik2.angle_mode.AZIMUTH
|
2010-03-18 20:05:08 +00:00
|
|
|
|
2010-09-26 23:41:16 +00:00
|
|
|
out_map = mapnik2.save_map_to_string(map).decode('utf8')
|
2010-03-19 11:19:43 +00:00
|
|
|
map = mapnik2.Map(256,256)
|
|
|
|
mapnik2.load_map_from_string(map, out_map.encode('utf8'))
|
2010-03-18 20:05:08 +00:00
|
|
|
assert 'GlyphSymbolizer' in out_map
|
2010-03-19 13:42:25 +00:00
|
|
|
# make sure non-ascii characters are well supported since most interesting
|
|
|
|
# glyphs for symbology are usually in that range
|
2010-03-19 11:19:43 +00:00
|
|
|
assert u'í' in out_map, out_map
|
2010-03-18 20:05:08 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Utilities and setup code
|
|
|
|
#
|
|
|
|
|
2010-03-18 20:04:35 +00:00
|
|
|
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('.'))
|
|
|
|
|
2010-03-18 20:05:08 +00:00
|
|
|
def create_map_and_append_symbolyzer(sym):
|
2010-03-18 20:04:35 +00:00
|
|
|
srs = '+init=epsg:32630'
|
|
|
|
lyr = mapnik2.Layer('arrows')
|
|
|
|
lyr.datasource = mapnik2.Shapefile(
|
|
|
|
file = '../data/shp/arrows.shp',
|
|
|
|
)
|
|
|
|
lyr.srs = srs
|
|
|
|
_map = mapnik2.Map(256,256, srs)
|
|
|
|
style = mapnik2.Style()
|
|
|
|
rule = mapnik2.Rule()
|
|
|
|
rule.symbols.append(sym)
|
2010-03-18 20:05:08 +00:00
|
|
|
|
2010-03-18 20:32:57 +00:00
|
|
|
# put a test symbolizer to see what is the azimuth being read
|
2010-03-18 20:05:08 +00:00
|
|
|
ts = mapnik2.TextSymbolizer(mapnik2.Expression('[azimuth]'),
|
|
|
|
"DejaVu Sans Book",
|
|
|
|
10,
|
|
|
|
mapnik2.Color("black"))
|
|
|
|
ts.allow_overlap = True
|
|
|
|
rule.symbols.append(ts)
|
|
|
|
|
2010-03-18 20:04:35 +00:00
|
|
|
style.rules.append(rule)
|
|
|
|
_map.append_style('foo', style)
|
|
|
|
lyr.styles.append('foo')
|
|
|
|
_map.layers.append(lyr)
|
|
|
|
_map.zoom_to_box(mapnik2.Box2d(0,0,8,8))
|
2010-03-18 20:05:08 +00:00
|
|
|
return _map
|
2010-03-18 20:04:35 +00:00
|
|
|
|
2011-08-30 22:51:42 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
setup()
|
|
|
|
[eval(run)() for run in dir() if 'test_' in run]
|
|
|
|
|