2009-04-16 17:22:38 +00:00
#!/usr/bin/env python
2011-05-21 00:20:49 +00:00
# -*- coding: utf-8 -*-
2009-04-16 17:22:38 +00:00
2015-02-02 18:31:16 +00:00
from nose . tools import eq_ , raises
2011-11-30 02:15:25 +00:00
import tempfile
2011-11-23 11:33:58 +00:00
import os , mapnik
2013-06-03 02:28:24 +00:00
from utilities import execution_path , run_all
2009-04-16 17:22:38 +00:00
2011-08-30 22:51:42 +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 ( ' . ' ) )
2009-04-16 17:22:38 +00:00
def test_simplest_render ( ) :
2011-11-23 11:33:58 +00:00
m = mapnik . Map ( 256 , 256 )
2013-02-22 21:40:05 +00:00
im = mapnik . Image ( m . width , m . height )
eq_ ( im . painted ( ) , False )
eq_ ( im . is_solid ( ) , True )
mapnik . render ( m , im )
eq_ ( im . painted ( ) , False )
eq_ ( im . is_solid ( ) , True )
s = im . tostring ( )
2010-09-26 23:41:16 +00:00
eq_ ( s , 256 * 256 * ' \x00 \x00 \x00 \x00 ' )
2009-04-16 17:22:38 +00:00
def test_render_image_to_string ( ) :
2013-02-22 21:40:05 +00:00
im = mapnik . Image ( 256 , 256 )
2015-01-29 20:27:42 +00:00
im . fill ( mapnik . Color ( ' black ' ) )
2013-02-22 21:40:05 +00:00
eq_ ( im . painted ( ) , False )
eq_ ( im . is_solid ( ) , True )
s = im . tostring ( )
2010-09-26 23:41:16 +00:00
eq_ ( s , 256 * 256 * ' \x00 \x00 \x00 \xff ' )
2009-04-16 17:22:38 +00:00
2013-02-22 21:40:05 +00:00
def test_non_solid_image ( ) :
im = mapnik . Image ( 256 , 256 )
2015-01-29 20:27:42 +00:00
im . fill ( mapnik . Color ( ' black ' ) )
2013-02-22 21:40:05 +00:00
eq_ ( im . painted ( ) , False )
eq_ ( im . is_solid ( ) , True )
# set one pixel to a different color
im . set_pixel ( 0 , 0 , mapnik . Color ( ' white ' ) )
eq_ ( im . painted ( ) , False )
eq_ ( im . is_solid ( ) , False )
def test_non_solid_image_view ( ) :
im = mapnik . Image ( 256 , 256 )
2015-01-29 20:27:42 +00:00
im . fill ( mapnik . Color ( ' black ' ) )
2013-02-22 21:40:05 +00:00
view = im . view ( 0 , 0 , 256 , 256 )
eq_ ( view . is_solid ( ) , True )
# set one pixel to a different color
im . set_pixel ( 0 , 0 , mapnik . Color ( ' white ' ) )
eq_ ( im . is_solid ( ) , False )
# view, since it is the exact dimensions of the image
# should also be non-solid
eq_ ( view . is_solid ( ) , False )
# but not a view that excludes the single diff pixel
view2 = im . view ( 1 , 1 , 256 , 256 )
eq_ ( view2 . is_solid ( ) , True )
2009-04-16 17:22:38 +00:00
2010-09-24 16:12:23 +00:00
def test_setting_alpha ( ) :
w , h = 256 , 256
2011-11-23 11:33:58 +00:00
im1 = mapnik . Image ( w , h )
2010-09-24 16:12:23 +00:00
# white, half transparent
2014-01-27 22:21:07 +00:00
c1 = mapnik . Color ( ' rgba(255,255,255,.5) ' )
2015-01-29 20:27:42 +00:00
im1 . fill ( c1 )
2013-02-22 21:40:05 +00:00
eq_ ( im1 . painted ( ) , False )
eq_ ( im1 . is_solid ( ) , True )
2010-09-24 16:12:23 +00:00
# pure white
2011-11-23 11:33:58 +00:00
im2 = mapnik . Image ( w , h )
2014-01-27 22:21:07 +00:00
c2 = mapnik . Color ( ' rgba(255,255,255,1) ' )
2015-01-29 20:27:42 +00:00
im2 . fill ( c2 )
2014-01-27 22:21:07 +00:00
im2 . set_alpha ( c1 . a / 255.0 )
2013-02-22 21:40:05 +00:00
eq_ ( im2 . painted ( ) , False )
eq_ ( im2 . is_solid ( ) , True )
2014-01-27 22:21:07 +00:00
eq_ ( len ( im1 . tostring ( ' png32 ' ) ) , len ( im2 . tostring ( ' png32 ' ) ) )
2010-09-24 16:12:23 +00:00
2009-04-16 17:22:38 +00:00
def test_render_image_to_file ( ) :
2013-02-22 21:40:05 +00:00
im = mapnik . Image ( 256 , 256 )
2015-01-29 20:27:42 +00:00
im . fill ( mapnik . Color ( ' black ' ) )
2011-11-23 11:33:58 +00:00
if mapnik . has_jpeg ( ) :
2013-02-22 21:40:05 +00:00
im . save ( ' test.jpg ' )
im . save ( ' test.png ' , ' png ' )
2009-04-16 17:22:38 +00:00
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 06:10:42 +00:00
def get_paired_images ( w , h , mapfile ) :
tmp_map = ' tmp_map.xml '
2011-11-23 11:33:58 +00:00
m = mapnik . Map ( w , h )
mapnik . load_map ( m , mapfile )
2013-02-22 21:40:05 +00:00
im = mapnik . Image ( w , h )
2009-07-24 06:10:42 +00:00
m . zoom_all ( )
2013-02-22 21:40:05 +00:00
mapnik . render ( m , im )
2011-11-23 11:33:58 +00:00
mapnik . save_map ( m , tmp_map )
m2 = mapnik . Map ( w , h )
mapnik . load_map ( m2 , tmp_map )
2013-02-22 21:40:05 +00:00
im2 = mapnik . Image ( w , h )
2009-07-24 06:10:42 +00:00
m2 . zoom_all ( )
2013-02-22 21:40:05 +00:00
mapnik . render ( m2 , im2 )
2009-07-24 06:10:42 +00:00
os . remove ( tmp_map )
2013-02-22 21:40:05 +00:00
return im , im2
2009-07-24 06:10:42 +00:00
def test_render_from_serialization ( ) :
2011-10-29 00:06:23 +00:00
try :
2013-02-22 21:40:05 +00:00
im , im2 = get_paired_images ( 100 , 100 , ' ../data/good_maps/building_symbolizer.xml ' )
2014-01-27 22:21:07 +00:00
eq_ ( im . tostring ( ' png32 ' ) , im2 . tostring ( ' png32 ' ) )
2012-02-24 21:13:56 +00:00
2013-02-22 21:40:05 +00:00
im , im2 = get_paired_images ( 100 , 100 , ' ../data/good_maps/polygon_symbolizer.xml ' )
2014-01-27 22:21:07 +00:00
eq_ ( im . tostring ( ' png32 ' ) , im2 . tostring ( ' png32 ' ) )
2011-10-29 00:06:23 +00:00
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 19:25:00 +00:00
2009-10-21 21:24:44 +00:00
def test_render_points ( ) :
2011-11-23 11:33:58 +00:00
if not mapnik . has_cairo ( ) : return
2011-04-14 21:02:31 +00:00
# create and populate point datasource (WGS84 lat-lon coordinates)
2012-01-24 08:27:44 +00:00
ds = mapnik . MemoryDatasource ( )
context = mapnik . Context ( )
context . push ( ' Name ' )
f = mapnik . Feature ( context , 1 )
f [ ' Name ' ] = ' Westernmost Point '
2015-03-23 17:04:37 +00:00
f . geometry = mapnik . Geometry . from_wkt ( ' POINT (142.48 -38.38) ' )
2012-01-24 08:27:44 +00:00
ds . add_feature ( f )
f = mapnik . Feature ( context , 2 )
f [ ' Name ' ] = ' Southernmost Point '
2015-03-23 17:04:37 +00:00
f . geometry = mapnik . Geometry . from_wkt ( ' POINT (143.10 -38.60) ' )
2012-01-24 08:27:44 +00:00
ds . add_feature ( f )
2011-04-14 21:02:31 +00:00
# create layer/rule/style
2011-11-23 11:33:58 +00:00
s = mapnik . Style ( )
r = mapnik . Rule ( )
symb = mapnik . PointSymbolizer ( )
2011-04-14 21:02:31 +00:00
symb . allow_overlap = True
r . symbols . append ( symb )
s . rules . append ( r )
2013-01-25 09:36:06 +00:00
lyr = mapnik . Layer ( ' Places ' , ' +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ' )
2012-01-24 08:27:44 +00:00
lyr . datasource = ds
2011-04-14 21:02:31 +00:00
lyr . styles . append ( ' places_labels ' )
# latlon bounding box corners
2011-11-23 11:33:58 +00:00
ul_lonlat = mapnik . Coord ( 142.30 , - 38.20 )
lr_lonlat = mapnik . Coord ( 143.40 , - 38.80 )
2015-03-23 17:04:37 +00:00
# render for different projections
projs = {
2013-01-29 06:04:07 +00:00
' google ' : ' +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over ' ,
2013-01-25 09:36:06 +00:00
' latlon ' : ' +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ' ,
2011-04-14 21:02:31 +00:00
' merc ' : ' +proj=merc +datum=WGS84 +k=1.0 +units=m +over +no_defs ' ,
' utm ' : ' +proj=utm +zone=54 +datum=WGS84 '
}
for projdescr in projs . iterkeys ( ) :
2011-11-23 11:33:58 +00:00
m = mapnik . Map ( 1000 , 500 , projs [ projdescr ] )
2011-04-14 21:02:31 +00:00
m . append_style ( ' places_labels ' , s )
m . layers . append ( lyr )
2013-01-29 06:04:07 +00:00
dest_proj = mapnik . Projection ( projs [ projdescr ] )
src_proj = mapnik . Projection ( ' +init=epsg:4326 ' )
tr = mapnik . ProjTransform ( src_proj , dest_proj )
m . zoom_to_box ( tr . forward ( mapnik . Box2d ( ul_lonlat , lr_lonlat ) ) )
2011-04-14 21:02:31 +00:00
# Render to SVG so that it can be checked how many points are there with string comparison
2012-10-16 16:27:56 +00:00
svg_file = os . path . join ( tempfile . gettempdir ( ) , ' mapnik-render-points- %s .svg ' % projdescr )
2011-11-23 11:33:58 +00:00
mapnik . render_to_file ( m , svg_file )
2012-01-24 08:27:44 +00:00
num_points_present = len ( ds . all_features ( ) )
2011-04-14 21:02:31 +00:00
svg = open ( svg_file , ' r ' ) . read ( )
num_points_rendered = svg . count ( ' <image ' )
2015-03-23 17:04:37 +00:00
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 21:24:44 +00:00
2013-02-21 02:55:03 +00:00
@raises ( RuntimeError )
def test_render_with_scale_factor_zero_throws ( ) :
m = mapnik . Map ( 256 , 256 )
im = mapnik . Image ( 256 , 256 )
mapnik . render ( m , im , 0.0 )
2013-11-13 18:45:23 +00:00
def test_render_with_detector ( ) :
ds = mapnik . MemoryDatasource ( )
context = mapnik . Context ( )
geojson = ' { " type " : " Feature " , " geometry " : { " type " : " Point " , " coordinates " : [ 0, 0 ] } } '
ds . add_feature ( mapnik . Feature . from_geojson ( geojson , context ) )
s = mapnik . Style ( )
r = mapnik . Rule ( )
lyr = mapnik . Layer ( ' point ' )
lyr . datasource = ds
lyr . styles . append ( ' point ' )
symb = mapnik . MarkersSymbolizer ( )
symb . allow_overlap = False
r . symbols . append ( symb )
s . rules . append ( r )
m = mapnik . Map ( 256 , 256 )
m . append_style ( ' point ' , s )
m . layers . append ( lyr )
m . zoom_to_box ( mapnik . Box2d ( - 180 , - 85 , 180 , 85 ) )
im = mapnik . Image ( 256 , 256 )
mapnik . render ( m , im )
expected_file = ' ./images/support/marker-in-center.png '
actual_file = ' /tmp/ ' + os . path . basename ( expected_file )
#im.save(expected_file,'png8')
im . save ( actual_file , ' png8 ' )
actual = mapnik . Image . open ( expected_file )
expected = mapnik . Image . open ( expected_file )
2014-01-27 22:21:07 +00:00
eq_ ( actual . tostring ( ' png32 ' ) , expected . tostring ( ' png32 ' ) , ' failed comparing actual ( %s ) and expected ( %s ) ' % ( actual_file , expected_file ) )
2013-11-13 18:45:23 +00:00
# now render will a collision detector that should
# block out the placement of this point
detector = mapnik . LabelCollisionDetector ( m )
eq_ ( detector . extent ( ) , mapnik . Box2d ( - 0.0 , - 0.0 , m . width , m . height ) )
eq_ ( detector . extent ( ) , mapnik . Box2d ( - 0.0 , - 0.0 , 256.0 , 256.0 ) )
eq_ ( detector . boxes ( ) , [ ] )
detector . insert ( detector . extent ( ) )
eq_ ( detector . boxes ( ) , [ detector . extent ( ) ] )
im2 = mapnik . Image ( 256 , 256 )
mapnik . render_with_detector ( m , im2 , detector )
expected_file_collision = ' ./images/support/marker-in-center-not-placed.png '
#im2.save(expected_file_collision,'png8')
actual_file = ' /tmp/ ' + os . path . basename ( expected_file_collision )
im2 . save ( actual_file , ' png8 ' )
2013-05-22 03:27:00 +00:00
if ' shape ' in mapnik . DatasourceCache . plugin_names ( ) :
def test_render_with_scale_factor ( ) :
m = mapnik . Map ( 256 , 256 )
mapnik . load_map ( m , ' ../data/good_maps/marker-text-line.xml ' )
m . zoom_all ( )
sizes = [ .00001 , .005 , .1 , .899 , 1 , 1.5 , 2 , 5 , 10 , 100 ]
for size in sizes :
im = mapnik . Image ( 256 , 256 )
mapnik . render ( m , im , size )
expected_file = ' ./images/support/marker-text-line-scale-factor- %s .png ' % size
actual_file = ' /tmp/ ' + os . path . basename ( expected_file )
2013-11-13 18:45:23 +00:00
im . save ( actual_file , ' png32 ' )
2014-12-03 18:38:55 +00:00
if os . environ . get ( ' UPDATE ' ) :
im . save ( expected_file , ' png32 ' )
2013-05-22 03:27:00 +00:00
# we save and re-open here so both png8 images are ready as full color png
2013-11-13 18:45:23 +00:00
actual = mapnik . Image . open ( actual_file )
2013-05-22 03:27:00 +00:00
expected = mapnik . Image . open ( expected_file )
2014-01-27 22:21:07 +00:00
eq_ ( actual . tostring ( ' png32 ' ) , expected . tostring ( ' png32 ' ) , ' failed comparing actual ( %s ) and expected ( %s ) ' % ( actual_file , expected_file ) )
2013-02-21 02:55:03 +00:00
2011-06-03 00:15:17 +00:00
if __name__ == " __main__ " :
2011-08-30 22:51:42 +00:00
setup ( )
2014-07-14 16:34:20 +00:00
exit ( run_all ( eval ( x ) for x in dir ( ) if x . startswith ( " test_ " ) ) )