2010-12-21 18:49:49 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2011-08-30 22:51:42 +00:00
|
|
|
import os
|
2015-02-02 18:31:16 +00:00
|
|
|
from nose.tools import eq_
|
2013-06-03 02:28:24 +00:00
|
|
|
from utilities import execution_path, run_all
|
2011-11-23 11:33:58 +00:00
|
|
|
import mapnik
|
2010-12-21 18:49:49 +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('.'))
|
|
|
|
|
2010-12-21 18:49:49 +00:00
|
|
|
def test_adding_datasource_to_layer():
|
|
|
|
map_string = '''<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<Map>
|
|
|
|
|
|
|
|
<Layer name="world_borders">
|
|
|
|
<StyleName>world_borders_style</StyleName>
|
|
|
|
<StyleName>point_style</StyleName>
|
|
|
|
<!-- leave datasource empty -->
|
|
|
|
<!--
|
|
|
|
<Datasource>
|
|
|
|
<Parameter name="file">../data/shp/world_merc.shp</Parameter>
|
|
|
|
<Parameter name="type">shape</Parameter>
|
|
|
|
</Datasource>
|
|
|
|
-->
|
|
|
|
</Layer>
|
|
|
|
|
|
|
|
</Map>
|
|
|
|
'''
|
2011-11-23 11:33:58 +00:00
|
|
|
m = mapnik.Map(256, 256)
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-10-29 00:06:23 +00:00
|
|
|
try:
|
2011-11-23 11:33:58 +00:00
|
|
|
mapnik.load_map_from_string(m, map_string)
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-10-29 00:06:23 +00:00
|
|
|
# validate it loaded fine
|
|
|
|
eq_(m.layers[0].styles[0],'world_borders_style')
|
|
|
|
eq_(m.layers[0].styles[1],'point_style')
|
|
|
|
eq_(len(m.layers),1)
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-10-29 00:06:23 +00:00
|
|
|
# also assign a variable reference to that layer
|
|
|
|
# below we will test that this variable references
|
|
|
|
# the same object that is attached to the map
|
|
|
|
lyr = m.layers[0]
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-10-29 00:06:23 +00:00
|
|
|
# ensure that there was no datasource for the layer...
|
|
|
|
eq_(m.layers[0].datasource,None)
|
|
|
|
eq_(lyr.datasource,None)
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-10-29 00:06:23 +00:00
|
|
|
# also note that since the srs was black it defaulted to wgs84
|
|
|
|
eq_(m.layers[0].srs,'+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
|
|
|
|
eq_(lyr.srs,'+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-10-29 00:06:23 +00:00
|
|
|
# now add a datasource one...
|
2011-11-23 11:33:58 +00:00
|
|
|
ds = mapnik.Shapefile(file='../data/shp/world_merc.shp')
|
2011-10-29 00:06:23 +00:00
|
|
|
m.layers[0].datasource = ds
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-10-29 00:06:23 +00:00
|
|
|
# now ensure it is attached
|
2012-01-12 04:03:47 +00:00
|
|
|
eq_(m.layers[0].datasource.describe()['name'],"shape")
|
|
|
|
eq_(lyr.datasource.describe()['name'],"shape")
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-10-29 00:06:23 +00:00
|
|
|
# and since we have now added a shapefile in spherical mercator, adjust the projection
|
|
|
|
lyr.srs = '+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs'
|
2012-02-24 21:13:56 +00:00
|
|
|
|
2011-10-29 00:06:23 +00:00
|
|
|
# test that assignment
|
|
|
|
eq_(m.layers[0].srs,'+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')
|
|
|
|
eq_(lyr.srs,'+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')
|
|
|
|
except RuntimeError, e:
|
|
|
|
# only test datasources that we have installed
|
|
|
|
if not 'Could not create datasource' in str(e):
|
|
|
|
raise RuntimeError(e)
|
2011-08-30 22:51:42 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
setup()
|
2014-07-14 16:34:20 +00:00
|
|
|
exit(run_all(eval(x) for x in dir() if x.startswith("test_")))
|