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
|
|
|
|
2011-11-23 11:33:58 +00:00
|
|
|
import os, mapnik
|
2010-03-18 20:04:35 +00:00
|
|
|
|
2010-03-19 09:33:57 +00:00
|
|
|
def test_renders_with_agg():
|
2011-11-23 11:33:58 +00:00
|
|
|
sym = mapnik.GlyphSymbolizer("DejaVu Sans Condensed",
|
|
|
|
mapnik.Expression("'í'"))
|
2010-03-18 20:05:08 +00:00
|
|
|
sym.allow_overlap = True
|
2011-11-23 11:33:58 +00:00
|
|
|
sym.angle = mapnik.Expression("[azimuth]+90") #+90 so the top of the glyph points upwards
|
|
|
|
sym.size = mapnik.Expression("[value]")
|
|
|
|
sym.color = mapnik.Expression("'#ff0000'")
|
2010-03-18 20:05:08 +00:00
|
|
|
|
|
|
|
_map = create_map_and_append_symbolyzer(sym)
|
2011-10-29 00:06:23 +00:00
|
|
|
if _map:
|
2011-11-23 11:33:58 +00:00
|
|
|
im = mapnik.Image(_map.width,_map.height)
|
|
|
|
mapnik.render(_map, im)
|
2011-10-29 00:06:23 +00:00
|
|
|
save_data('agg_glyph_symbolizer.png', im.tostring('png'))
|
|
|
|
assert contains_word('\xff\x00\x00\xff', im.tostring())
|
2010-03-19 09:33:57 +00:00
|
|
|
|
|
|
|
def test_renders_with_cairo():
|
2011-11-23 11:33:58 +00:00
|
|
|
if not mapnik.has_pycairo():
|
2010-03-19 09:33:57 +00:00
|
|
|
return
|
2011-11-23 11:33:58 +00:00
|
|
|
sym = mapnik.GlyphSymbolizer("DejaVu Sans Condensed",
|
|
|
|
mapnik.Expression("'í'"))
|
2010-03-19 09:33:57 +00:00
|
|
|
sym.allow_overlap = True
|
2011-11-23 11:33:58 +00:00
|
|
|
sym.angle = mapnik.Expression("[azimuth]+90") #+90 so the top of the glyph points upwards
|
|
|
|
sym.size = mapnik.Expression("[value]")
|
|
|
|
sym.color = mapnik.Expression("'#ff0000'")
|
2010-03-19 09:33:57 +00:00
|
|
|
_map = create_map_and_append_symbolyzer(sym)
|
2011-10-29 00:06:23 +00:00
|
|
|
if _map:
|
|
|
|
from cStringIO import StringIO
|
|
|
|
import cairo
|
|
|
|
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 256, 256)
|
2011-11-23 11:33:58 +00:00
|
|
|
mapnik.render(_map, surface)
|
|
|
|
im = mapnik.Image.from_cairo(surface)
|
2011-10-29 00:06:23 +00:00
|
|
|
save_data('cairo_glyph_symbolizer.png', im.tostring('png'))
|
|
|
|
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():
|
2011-11-23 11:33:58 +00:00
|
|
|
map = mapnik.Map(256,256)
|
2010-03-18 20:05:08 +00:00
|
|
|
in_map = "../data/good_maps/glyph_symbolizer.xml"
|
2011-10-29 00:06:23 +00:00
|
|
|
try:
|
2011-11-23 11:33:58 +00:00
|
|
|
mapnik.load_map(map, in_map)
|
2011-10-29 00:06:23 +00:00
|
|
|
style = map.find_style('arrows')
|
|
|
|
sym = style.rules[0].symbols[0]
|
2011-11-23 11:33:58 +00:00
|
|
|
assert isinstance(sym, mapnik.GlyphSymbolizer)
|
|
|
|
assert sym.angle_mode == mapnik.angle_mode.AZIMUTH
|
2011-10-29 00:06:23 +00:00
|
|
|
|
2011-11-23 11:33:58 +00:00
|
|
|
out_map = mapnik.save_map_to_string(map).decode('utf8')
|
|
|
|
map = mapnik.Map(256,256)
|
|
|
|
mapnik.load_map_from_string(map, out_map.encode('utf8'))
|
2011-10-29 00:06:23 +00:00
|
|
|
assert 'GlyphSymbolizer' in out_map
|
|
|
|
# make sure non-ascii characters are well supported since most interesting
|
|
|
|
# glyphs for symbology are usually in that range
|
|
|
|
assert u'í' in out_map, out_map
|
|
|
|
except RuntimeError, e:
|
|
|
|
# only test datasources that we have installed
|
|
|
|
if not 'Could not create datasource' in str(e):
|
|
|
|
raise RuntimeError(e)
|
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'
|
2011-11-23 11:33:58 +00:00
|
|
|
lyr = mapnik.Layer('arrows')
|
2011-10-29 00:06:23 +00:00
|
|
|
try:
|
2011-11-23 11:33:58 +00:00
|
|
|
lyr.datasource = mapnik.Shapefile(
|
2011-10-29 00:06:23 +00:00
|
|
|
file = '../data/shp/arrows.shp',
|
|
|
|
)
|
|
|
|
lyr.srs = srs
|
2011-11-23 11:33:58 +00:00
|
|
|
_map = mapnik.Map(256,256, srs)
|
|
|
|
style = mapnik.Style()
|
|
|
|
rule = mapnik.Rule()
|
2011-10-29 00:06:23 +00:00
|
|
|
rule.symbols.append(sym)
|
|
|
|
|
|
|
|
# put a test symbolizer to see what is the azimuth being read
|
2011-11-23 11:33:58 +00:00
|
|
|
ts = mapnik.TextSymbolizer(mapnik.Expression('[azimuth]'),
|
2011-10-29 00:06:23 +00:00
|
|
|
"DejaVu Sans Book",
|
|
|
|
10,
|
2011-11-23 11:33:58 +00:00
|
|
|
mapnik.Color("black"))
|
2011-10-29 00:06:23 +00:00
|
|
|
ts.allow_overlap = True
|
|
|
|
rule.symbols.append(ts)
|
|
|
|
|
|
|
|
style.rules.append(rule)
|
|
|
|
_map.append_style('foo', style)
|
|
|
|
lyr.styles.append('foo')
|
|
|
|
_map.layers.append(lyr)
|
2011-11-23 11:33:58 +00:00
|
|
|
_map.zoom_to_box(mapnik.Box2d(0,0,8,8))
|
2011-10-29 00:06:23 +00:00
|
|
|
return _map
|
|
|
|
except RuntimeError, e:
|
|
|
|
# only test datasources that we have installed
|
|
|
|
if not 'Could not create datasource' in str(e):
|
|
|
|
raise RuntimeError(e)
|
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]
|
|
|
|
|