2009-04-16 19:22:38 +02:00
#!/usr/bin/env python
2011-05-21 02:20:49 +02:00
# -*- coding: utf-8 -*-
2009-04-16 19:22:38 +02:00
from nose . tools import *
2011-11-30 03:15:25 +01:00
import tempfile
2011-11-23 12:33:58 +01:00
import os , mapnik
2011-08-31 00:51:42 +02:00
from nose . tools import *
from utilities import execution_path
2009-04-16 19:22:38 +02:00
2011-08-31 00:51:42 +02: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 ( ' . ' ) )
2009-04-16 19:22:38 +02:00
def test_simplest_render ( ) :
2011-11-23 12:33:58 +01:00
m = mapnik . Map ( 256 , 256 )
i = mapnik . Image ( m . width , m . height )
2009-04-16 19:22:38 +02:00
2011-11-23 12:33:58 +01:00
mapnik . 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 ( ) :
2011-11-23 12:33:58 +01:00
i = mapnik . Image ( 256 , 256 )
2012-02-24 22:13:56 +01:00
2011-11-23 12:33:58 +01:00
i . background = mapnik . Color ( ' black ' )
2012-02-24 22:13:56 +01:00
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
2011-11-23 12:33:58 +01:00
im1 = mapnik . Image ( w , h )
2010-09-24 18:12:23 +02:00
# white, half transparent
2011-11-23 12:33:58 +01:00
im1 . background = mapnik . Color ( ' rgba(255,255,255,.5) ' )
2012-02-24 22:13:56 +01:00
2010-09-24 18:12:23 +02:00
# pure white
2011-11-23 12:33:58 +01:00
im2 = mapnik . Image ( w , h )
im2 . background = mapnik . Color ( ' rgba(255,255,255,1) ' )
2010-09-24 18:12:23 +02:00
im2 . set_alpha ( .5 )
2012-02-24 22:13:56 +01:00
2010-09-24 18:12:23 +02:00
eq_ ( len ( im1 . tostring ( ) ) , len ( im2 . tostring ( ) ) )
2009-04-16 19:22:38 +02:00
def test_render_image_to_file ( ) :
2011-11-23 12:33:58 +01:00
i = mapnik . Image ( 256 , 256 )
2012-02-24 22:13:56 +01:00
2011-11-23 12:33:58 +01:00
i . background = mapnik . Color ( ' black ' )
2009-04-16 19:22:38 +02:00
2011-11-23 12:33:58 +01:00
if mapnik . has_jpeg ( ) :
2010-06-03 21:50:27 +02:00
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
2012-02-24 22:13:56 +01:00
2009-04-16 19:22:38 +02:00
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 '
2011-11-23 12:33:58 +01:00
m = mapnik . Map ( w , h )
mapnik . load_map ( m , mapfile )
i = mapnik . Image ( w , h )
2009-07-24 08:10:42 +02:00
m . zoom_all ( )
2011-11-23 12:33:58 +01:00
mapnik . render ( m , i )
mapnik . save_map ( m , tmp_map )
m2 = mapnik . Map ( w , h )
mapnik . load_map ( m2 , tmp_map )
i2 = mapnik . Image ( w , h )
2009-07-24 08:10:42 +02:00
m2 . zoom_all ( )
2011-11-23 12:33:58 +01:00
mapnik . render ( m2 , i2 )
2009-07-24 08:10:42 +02:00
os . remove ( tmp_map )
return i , i2
def test_render_from_serialization ( ) :
2011-10-29 02:06:23 +02:00
try :
i , i2 = get_paired_images ( 100 , 100 , ' ../data/good_maps/building_symbolizer.xml ' )
eq_ ( i . tostring ( ) , i2 . tostring ( ) )
2012-02-24 22:13:56 +01:00
2011-10-29 02:06:23 +02:00
i , i2 = get_paired_images ( 100 , 100 , ' ../data/good_maps/polygon_symbolizer.xml ' )
eq_ ( i . tostring ( ) , i2 . tostring ( ) )
except RuntimeError , e :
# only test datasources that we have installed
if not ' Could not create datasource ' in str ( e ) :
raise RuntimeError ( e )
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 " : [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " !!! ### " , " !!!!! ##### " , " !!!!! ##### " , " !!! ### " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " $$$$ %% %% " , " $$$$$ %% %% % " , " $$$$$ %% %% % " , " $$$ %% % " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , "
2011-05-21 02:20:49 +02:00
def resolve ( grid , x , y ) :
""" Resolve the attributes for a given pixel in a grid.
2012-02-24 22:13:56 +01:00
2011-05-21 02:20:49 +02:00
js version :
https : / / github . com / mapbox / mbtiles - spec / blob / master / 1.1 / utfgrid . md
spec :
https : / / github . com / mapbox / wax / blob / master / control / lib / gridutil . js
2012-02-24 22:13:56 +01:00
2011-05-21 02:20:49 +02:00
"""
utf_val = grid [ ' grid ' ] [ x ] [ y ]
#http://docs.python.org/library/functions.html#ord
codepoint = ord ( utf_val )
if ( codepoint > = 93 ) :
codepoint - = 1
if ( codepoint > = 35 ) :
codepoint - = 1
codepoint - = 32
key = grid [ ' keys ' ] [ codepoint ]
2011-06-03 02:15:17 +02:00
return grid [ ' data ' ] . get ( key )
2011-05-21 02:20:49 +02:00
2011-04-29 21:25:00 +02:00
def test_render_grid ( ) :
2012-01-24 09:27:44 +01:00
ds = mapnik . MemoryDatasource ( )
context = mapnik . Context ( )
context . push ( ' Name ' )
f = mapnik . Feature ( context , 1 )
f [ ' Name ' ] = ' South East '
f . add_geometries_from_wkt ( ' POINT (143.10 -38.60) ' )
ds . add_feature ( f )
f = mapnik . Feature ( context , 2 )
f [ ' Name ' ] = ' South West '
f . add_geometries_from_wkt ( ' POINT (142.48 -38.60) ' )
ds . add_feature ( f )
f = mapnik . Feature ( context , 3 )
f [ ' Name ' ] = ' North West '
f . add_geometries_from_wkt ( ' POINT (142.48 -38.38) ' )
ds . add_feature ( f )
f = mapnik . Feature ( context , 4 )
f [ ' Name ' ] = ' North East '
f . add_geometries_from_wkt ( ' POINT (143.10 -38.38) ' )
ds . add_feature ( f )
2011-11-23 12:33:58 +01:00
s = mapnik . Style ( )
r = mapnik . Rule ( )
symb = mapnik . MarkersSymbolizer ( )
2011-04-29 21:25:00 +02:00
symb . allow_overlap = True
r . symbols . append ( symb )
s . rules . append ( r )
2011-11-23 12:33:58 +01:00
lyr = mapnik . Layer ( ' Places ' )
2012-01-24 09:27:44 +01:00
lyr . datasource = ds
2011-04-29 21:25:00 +02:00
lyr . styles . append ( ' places_labels ' )
2011-11-23 12:33:58 +01:00
m = mapnik . Map ( 256 , 256 )
2011-04-29 21:25:00 +02:00
m . append_style ( ' places_labels ' , s )
m . layers . append ( lyr )
2011-11-23 12:33:58 +01:00
ul_lonlat = mapnik . Coord ( 142.30 , - 38.20 )
lr_lonlat = mapnik . Coord ( 143.40 , - 38.80 )
m . zoom_to_box ( mapnik . Box2d ( ul_lonlat , lr_lonlat ) )
grid = mapnik . render_grid ( m , 0 , key = ' Name ' , resolution = 4 , fields = [ ' Name ' ] )
2011-05-21 02:20:49 +02:00
eq_ ( grid , grid_correct )
eq_ ( resolve ( grid , 0 , 0 ) , None )
2012-02-24 22:13:56 +01:00
2011-05-21 02:20:49 +02:00
# check every pixel of the nw symbol
expected = { " Name " : " North West " }
2012-02-24 22:13:56 +01:00
2011-05-21 02:20:49 +02:00
# top row
eq_ ( resolve ( grid , 23 , 9 ) , expected )
eq_ ( resolve ( grid , 23 , 10 ) , expected )
eq_ ( resolve ( grid , 23 , 11 ) , expected )
# core
eq_ ( resolve ( grid , 24 , 8 ) , expected )
eq_ ( resolve ( grid , 24 , 9 ) , expected )
eq_ ( resolve ( grid , 24 , 10 ) , expected )
eq_ ( resolve ( grid , 24 , 11 ) , expected )
eq_ ( resolve ( grid , 24 , 12 ) , expected )
eq_ ( resolve ( grid , 25 , 8 ) , expected )
eq_ ( resolve ( grid , 25 , 9 ) , expected )
eq_ ( resolve ( grid , 25 , 10 ) , expected )
eq_ ( resolve ( grid , 25 , 11 ) , expected )
eq_ ( resolve ( grid , 25 , 12 ) , expected )
2012-02-24 22:13:56 +01:00
2011-05-21 02:20:49 +02:00
# bottom row
eq_ ( resolve ( grid , 26 , 9 ) , expected )
eq_ ( resolve ( grid , 26 , 10 ) , expected )
eq_ ( resolve ( grid , 26 , 11 ) , expected )
2012-02-24 22:13:56 +01:00
2009-10-21 23:24:44 +02:00
def test_render_points ( ) :
2011-04-14 23:02:31 +02:00
2011-11-23 12:33:58 +01:00
if not mapnik . has_cairo ( ) : return
2011-04-14 23:02:31 +02:00
# create and populate point datasource (WGS84 lat-lon coordinates)
2012-01-24 09:27:44 +01:00
ds = mapnik . MemoryDatasource ( )
context = mapnik . Context ( )
context . push ( ' Name ' )
f = mapnik . Feature ( context , 1 )
f [ ' Name ' ] = ' Westernmost Point '
f . add_geometries_from_wkt ( ' POINT (142.48 -38.38) ' )
ds . add_feature ( f )
f = mapnik . Feature ( context , 2 )
f [ ' Name ' ] = ' Southernmost Point '
2012-02-02 02:03:31 +01:00
f . add_geometries_from_wkt ( ' POINT (143.10 -38.60) ' )
2012-01-24 09:27:44 +01:00
ds . add_feature ( f )
2011-04-14 23:02:31 +02:00
# create layer/rule/style
2011-11-23 12:33:58 +01:00
s = mapnik . Style ( )
r = mapnik . Rule ( )
symb = mapnik . PointSymbolizer ( )
2011-04-14 23:02:31 +02:00
symb . allow_overlap = True
r . symbols . append ( symb )
s . rules . append ( r )
2011-11-23 12:33:58 +01:00
lyr = mapnik . Layer ( ' Places ' , ' +proj=latlon +datum=WGS84 ' )
2012-01-24 09:27:44 +01:00
lyr . datasource = ds
2011-04-14 23:02:31 +02:00
lyr . styles . append ( ' places_labels ' )
# latlon bounding box corners
2011-11-23 12:33:58 +01:00
ul_lonlat = mapnik . Coord ( 142.30 , - 38.20 )
lr_lonlat = mapnik . Coord ( 143.40 , - 38.80 )
2011-04-14 23:02:31 +02:00
# 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 ( ) :
2011-11-23 12:33:58 +01:00
m = mapnik . Map ( 1000 , 500 , projs [ projdescr ] )
2011-04-14 23:02:31 +02:00
m . append_style ( ' places_labels ' , s )
m . layers . append ( lyr )
2011-11-23 12:33:58 +01:00
p = mapnik . Projection ( projs [ projdescr ] )
m . zoom_to_box ( p . forward ( mapnik . Box2d ( ul_lonlat , lr_lonlat ) ) )
2011-04-14 23:02:31 +02:00
# Render to SVG so that it can be checked how many points are there with string comparison
2011-11-30 03:15:25 +01:00
svg_file = os . path . join ( tempfile . gettempdir ( ) , ' %s .svg ' )
2011-11-23 12:33:58 +01:00
mapnik . render_to_file ( m , svg_file )
2012-01-24 09:27:44 +01:00
num_points_present = len ( ds . all_features ( ) )
2011-04-14 23:02:31 +02:00
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-06-03 02:15:17 +02:00
if __name__ == " __main__ " :
2011-08-31 00:51:42 +02:00
setup ( )
[ eval ( run ) ( ) for run in dir ( ) if ' test_ ' in run ]