2009-04-16 19:22:38 +02:00
#!/usr/bin/env python
from nose . tools import *
2010-03-11 19:12:22 +01:00
import os , mapnik2
2010-09-27 01:41:16 +02:00
from utilities import Todo
2009-04-16 19:22:38 +02:00
def test_simplest_render ( ) :
2010-03-11 19:12:22 +01:00
m = mapnik2 . Map ( 256 , 256 )
i = mapnik2 . Image ( m . width , m . height )
2009-04-16 19:22:38 +02:00
2010-03-11 19:12:22 +01:00
mapnik2 . render ( m , i )
2009-04-16 19:22:38 +02:00
s = i . tostring ( )
2010-09-27 01:41:16 +02:00
eq_ ( s , 256 * 256 * ' \x00 \x00 \x00 \x00 ' )
2009-04-16 19:22:38 +02:00
def test_render_image_to_string ( ) :
2010-03-11 19:12:22 +01:00
i = mapnik2 . Image ( 256 , 256 )
2009-04-16 19:22:38 +02:00
2010-03-11 19:12:22 +01:00
i . background = mapnik2 . Color ( ' black ' )
2009-04-16 19:22:38 +02:00
s = i . tostring ( )
2010-09-27 01:41:16 +02:00
eq_ ( s , 256 * 256 * ' \x00 \x00 \x00 \xff ' )
2009-04-16 19:22:38 +02:00
s = i . tostring ( ' png ' )
2010-09-24 18:12:23 +02:00
def test_setting_alpha ( ) :
w , h = 256 , 256
im1 = mapnik2 . Image ( w , h )
# white, half transparent
im1 . background = mapnik2 . Color ( ' rgba(255,255,255,.5) ' )
# pure white
im2 = mapnik2 . Image ( w , h )
im2 . background = mapnik2 . Color ( ' rgba(255,255,255,1) ' )
im2 . set_alpha ( .5 )
eq_ ( len ( im1 . tostring ( ) ) , len ( im2 . tostring ( ) ) )
2009-04-16 19:22:38 +02:00
def test_render_image_to_file ( ) :
2010-03-11 19:12:22 +01:00
i = mapnik2 . Image ( 256 , 256 )
2009-04-16 19:22:38 +02:00
2010-03-11 19:12:22 +01:00
i . background = mapnik2 . Color ( ' black ' )
2009-04-16 19:22:38 +02:00
2010-06-03 21:50:27 +02:00
if mapnik2 . has_jpeg ( ) :
i . save ( ' test.jpg ' )
2009-04-16 19:22:38 +02:00
i . save ( ' test.png ' , ' png ' )
if os . path . exists ( ' test.jpg ' ) :
os . remove ( ' test.jpg ' )
else :
return False
if os . path . exists ( ' test.png ' ) :
os . remove ( ' test.png ' )
else :
return False
2009-07-24 08:10:42 +02:00
def get_paired_images ( w , h , mapfile ) :
tmp_map = ' tmp_map.xml '
2010-03-11 19:12:22 +01:00
m = mapnik2 . Map ( w , h )
mapnik2 . load_map ( m , mapfile )
i = mapnik2 . Image ( w , h )
2009-07-24 08:10:42 +02:00
m . zoom_all ( )
2010-03-11 19:12:22 +01:00
mapnik2 . render ( m , i )
mapnik2 . save_map ( m , tmp_map )
m2 = mapnik2 . Map ( w , h )
mapnik2 . load_map ( m2 , tmp_map )
i2 = mapnik2 . Image ( w , h )
2009-07-24 08:10:42 +02:00
m2 . zoom_all ( )
2010-03-11 19:12:22 +01:00
mapnik2 . render ( m2 , i2 )
2009-07-24 08:10:42 +02:00
os . remove ( tmp_map )
return i , i2
def test_render_from_serialization ( ) :
i , i2 = get_paired_images ( 100 , 100 , ' ../data/good_maps/building_symbolizer.xml ' )
eq_ ( i . tostring ( ) , i2 . tostring ( ) )
i , i2 = get_paired_images ( 100 , 100 , ' ../data/good_maps/polygon_symbolizer.xml ' )
eq_ ( i . tostring ( ) , i2 . tostring ( ) )
2009-10-21 23:24:44 +02:00
2011-04-29 21:25:00 +02:00
grid_correct = { " keys " : [ " " , " North West " , " North East " , " South West " , " South East " ] , " data " : { " South East " : { " Name " : " South East " } , " North East " : { " Name " : " North East " } , " North West " : { " Name " : " North West " } , " South West " : { " Name " : " South West " } } , " grid " : [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " !!! ### " , " !!!!! ##### " , " !!!!! ##### " , " !!! ### " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " $$$$ %% %% " , " $$$$$ %% %% % " , " $$$$$ %% %% % " , " $$$ %% % " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , "
def test_render_grid ( ) :
places_ds = mapnik2 . PointDatasource ( )
places_ds . add_point ( 143.10 , - 38.60 , ' Name ' , ' South East ' )
places_ds . add_point ( 142.48 , - 38.60 , ' Name ' , ' South West ' )
places_ds . add_point ( 142.48 , - 38.38 , ' Name ' , ' North West ' )
places_ds . add_point ( 143.10 , - 38.38 , ' Name ' , ' North East ' )
s = mapnik2 . Style ( )
r = mapnik2 . Rule ( )
#symb = mapnik2.PointSymbolizer()
symb = mapnik2 . MarkersSymbolizer ( )
symb . allow_overlap = True
r . symbols . append ( symb )
label = mapnik2 . TextSymbolizer ( mapnik2 . Expression ( ' [Name] ' ) ,
' DejaVu Sans Book ' ,
10 ,
mapnik2 . Color ( ' black ' )
)
label . allow_overlap = True
label . displacement = ( 0 , - 10 )
#r.symbols.append(label)
s . rules . append ( r )
lyr = mapnik2 . Layer ( ' Places ' )
lyr . datasource = places_ds
lyr . styles . append ( ' places_labels ' )
m = mapnik2 . Map ( 256 , 256 )
m . append_style ( ' places_labels ' , s )
m . layers . append ( lyr )
ul_lonlat = mapnik2 . Coord ( 142.30 , - 38.20 )
lr_lonlat = mapnik2 . Coord ( 143.40 , - 38.80 )
m . zoom_to_box ( mapnik2 . Box2d ( ul_lonlat , lr_lonlat ) )
grid = mapnik2 . render_grid ( m , 0 , ' Name ' , 4 , fields = [ ' Name ' , ' __id__ ' ] )
eq_ ( grid , grid_correct )
2009-10-21 23:24:44 +02:00
def test_render_points ( ) :
2011-04-14 23:02:31 +02:00
if not mapnik2 . has_cairo ( ) : return
# create and populate point datasource (WGS84 lat-lon coordinates)
places_ds = mapnik2 . PointDatasource ( )
places_ds . add_point ( 142.48 , - 38.38 , ' Name ' , ' Westernmost Point ' ) # westernmost
places_ds . add_point ( 143.10 , - 38.60 , ' Name ' , ' Southernmost Point ' ) # southernmost
# create layer/rule/style
s = mapnik2 . Style ( )
r = mapnik2 . Rule ( )
symb = mapnik2 . PointSymbolizer ( )
symb . allow_overlap = True
r . symbols . append ( symb )
s . rules . append ( r )
lyr = mapnik2 . Layer ( ' Places ' , ' +proj=latlon +datum=WGS84 ' )
lyr . datasource = places_ds
lyr . styles . append ( ' places_labels ' )
# latlon bounding box corners
ul_lonlat = mapnik2 . Coord ( 142.30 , - 38.20 )
lr_lonlat = mapnik2 . Coord ( 143.40 , - 38.80 )
# render for different projections
projs = {
' latlon ' : ' +proj=latlon +datum=WGS84 ' ,
' merc ' : ' +proj=merc +datum=WGS84 +k=1.0 +units=m +over +no_defs ' ,
' google ' : ' +proj=merc +ellps=sphere +R=6378137 +a=6378137 +units=m ' ,
' utm ' : ' +proj=utm +zone=54 +datum=WGS84 '
}
for projdescr in projs . iterkeys ( ) :
m = mapnik2 . Map ( 1000 , 500 , projs [ projdescr ] )
m . append_style ( ' places_labels ' , s )
m . layers . append ( lyr )
p = mapnik2 . Projection ( projs [ projdescr ] )
m . zoom_to_box ( p . forward ( mapnik2 . Box2d ( ul_lonlat , lr_lonlat ) ) )
# Render to SVG so that it can be checked how many points are there with string comparison
svg_file = ' /tmp/ %s .svg '
mapnik2 . render_to_file ( m , svg_file )
num_points_present = len ( places_ds . all_features ( ) )
svg = open ( svg_file , ' r ' ) . read ( )
num_points_rendered = svg . count ( ' <image ' )
eq_ ( num_points_present , num_points_rendered , " Not all points were rendered ( %d instead of %d ) at projection %s " % ( num_points_rendered , num_points_present , projdescr ) )
2009-10-21 23:24:44 +02:00
2011-04-29 21:25:00 +02:00