2012-07-19 20:06:44 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import math
|
|
|
|
import mapnik
|
|
|
|
import sys
|
|
|
|
from utilities import execution_path
|
|
|
|
from nose.tools import *
|
|
|
|
|
|
|
|
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('.'))
|
|
|
|
|
|
|
|
class PointDatasource(mapnik.PythonDatasource):
|
|
|
|
def __init__(self):
|
|
|
|
super(PointDatasource, self).__init__(
|
2013-05-30 18:16:03 +02:00
|
|
|
geometry_type = mapnik.DataGeometryType.Point,
|
|
|
|
envelope = mapnik.Box2d(0,-10,100,110),
|
|
|
|
data_type = mapnik.DataType.Vector
|
2012-07-19 20:06:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def features(self, query):
|
2012-08-18 18:17:14 +02:00
|
|
|
return mapnik.PythonDatasource.wkt_features(
|
2012-09-05 13:53:37 +02:00
|
|
|
keys = ('label',),
|
2012-07-19 20:06:44 +02:00
|
|
|
features = (
|
2012-09-05 13:53:37 +02:00
|
|
|
( 'POINT (5 6)', { 'label': 'foo-bar'} ),
|
|
|
|
( 'POINT (60 50)', { 'label': 'buzz-quux'} ),
|
2012-07-19 20:06:44 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
class ConcentricCircles(object):
|
|
|
|
def __init__(self, centre, bounds, step=1):
|
|
|
|
self.centre = centre
|
|
|
|
self.bounds = bounds
|
|
|
|
self.step = step
|
|
|
|
|
|
|
|
class Iterator(object):
|
|
|
|
def __init__(self, container):
|
|
|
|
self.container = container
|
|
|
|
|
|
|
|
centre = self.container.centre
|
|
|
|
bounds = self.container.bounds
|
|
|
|
step = self.container.step
|
|
|
|
|
2012-08-18 18:17:14 +02:00
|
|
|
self.radius = step
|
2012-07-19 20:06:44 +02:00
|
|
|
|
|
|
|
def next(self):
|
2012-08-18 18:17:14 +02:00
|
|
|
points = []
|
|
|
|
for alpha in xrange(0, 361, 5):
|
|
|
|
x = math.sin(math.radians(alpha)) * self.radius + self.container.centre[0]
|
|
|
|
y = math.cos(math.radians(alpha)) * self.radius + self.container.centre[1]
|
|
|
|
points.append('%s %s' % (x,y))
|
|
|
|
circle = 'POLYGON ((' + ','.join(points) + '))'
|
2012-07-19 20:06:44 +02:00
|
|
|
|
|
|
|
# has the circle grown so large that the boundary is entirely within it?
|
2012-08-18 18:17:14 +02:00
|
|
|
tl = (self.container.bounds.maxx, self.container.bounds.maxy)
|
|
|
|
tr = (self.container.bounds.maxx, self.container.bounds.maxy)
|
|
|
|
bl = (self.container.bounds.minx, self.container.bounds.miny)
|
|
|
|
br = (self.container.bounds.minx, self.container.bounds.miny)
|
|
|
|
def within_circle(p):
|
|
|
|
delta_x = p[0] - self.container.centre[0]
|
|
|
|
delta_y = p[0] - self.container.centre[0]
|
|
|
|
return delta_x*delta_x + delta_y*delta_y < self.radius*self.radius
|
|
|
|
|
|
|
|
if all(within_circle(p) for p in (tl,tr,bl,br)):
|
2012-07-19 20:06:44 +02:00
|
|
|
raise StopIteration()
|
|
|
|
|
2012-08-18 18:17:14 +02:00
|
|
|
self.radius += self.container.step
|
|
|
|
return ( circle, { } )
|
2012-07-19 20:06:44 +02:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return ConcentricCircles.Iterator(self)
|
|
|
|
|
|
|
|
class CirclesDatasource(mapnik.PythonDatasource):
|
|
|
|
def __init__(self, centre_x=-20, centre_y=0, step=10):
|
|
|
|
super(CirclesDatasource, self).__init__(
|
2013-05-30 18:16:03 +02:00
|
|
|
geometry_type = mapnik.DataGeometryType.Polygon,
|
|
|
|
envelope = mapnik.Box2d(-180, -90, 180, 90),
|
|
|
|
data_type = mapnik.DataType.Vector
|
2012-07-19 20:06:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# note that the plugin loader will set all arguments to strings and will not try to parse them
|
|
|
|
centre_x = int(centre_x)
|
|
|
|
centre_y = int(centre_y)
|
|
|
|
step = int(step)
|
|
|
|
|
|
|
|
self.centre_x = centre_x
|
|
|
|
self.centre_y = centre_y
|
|
|
|
self.step = step
|
|
|
|
|
|
|
|
def features(self, query):
|
2012-08-18 18:17:14 +02:00
|
|
|
centre = (self.centre_x, self.centre_y)
|
2012-07-19 20:06:44 +02:00
|
|
|
|
2012-08-18 18:17:14 +02:00
|
|
|
return mapnik.PythonDatasource.wkt_features(
|
2012-07-19 20:06:44 +02:00
|
|
|
keys = (),
|
2012-08-18 18:17:14 +02:00
|
|
|
features = ConcentricCircles(centre, query.bbox, self.step)
|
2012-07-19 20:06:44 +02:00
|
|
|
)
|
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
if 'python' in mapnik.DatasourceCache.plugin_names():
|
2012-07-19 20:06:44 +02:00
|
|
|
# make sure we can load from ourself as a module
|
|
|
|
sys.path.append(execution_path('.'))
|
|
|
|
|
|
|
|
def test_python_point_init():
|
|
|
|
ds = mapnik.Python(factory='python_plugin_test:PointDatasource')
|
|
|
|
e = ds.envelope()
|
|
|
|
|
|
|
|
assert_almost_equal(e.minx, 0, places=7)
|
|
|
|
assert_almost_equal(e.miny, -10, places=7)
|
|
|
|
assert_almost_equal(e.maxx, 100, places=7)
|
|
|
|
assert_almost_equal(e.maxy, 110, places=7)
|
|
|
|
|
|
|
|
def test_python_circle_init():
|
|
|
|
ds = mapnik.Python(factory='python_plugin_test:CirclesDatasource')
|
|
|
|
e = ds.envelope()
|
|
|
|
|
|
|
|
assert_almost_equal(e.minx, -180, places=7)
|
|
|
|
assert_almost_equal(e.miny, -90, places=7)
|
|
|
|
assert_almost_equal(e.maxx, 180, places=7)
|
|
|
|
assert_almost_equal(e.maxy, 90, places=7)
|
|
|
|
|
|
|
|
def test_python_circle_init_with_args():
|
|
|
|
ds = mapnik.Python(factory='python_plugin_test:CirclesDatasource', centre_x=40, centre_y=7)
|
|
|
|
e = ds.envelope()
|
|
|
|
|
|
|
|
assert_almost_equal(e.minx, -180, places=7)
|
|
|
|
assert_almost_equal(e.miny, -90, places=7)
|
|
|
|
assert_almost_equal(e.maxx, 180, places=7)
|
|
|
|
assert_almost_equal(e.maxy, 90, places=7)
|
|
|
|
|
|
|
|
def test_python_point_rendering():
|
|
|
|
m = mapnik.Map(512,512)
|
2012-08-18 18:34:58 +02:00
|
|
|
mapnik.load_map(m,'../data/python_plugin/python_point_datasource.xml')
|
2012-07-19 20:06:44 +02:00
|
|
|
m.zoom_all()
|
|
|
|
im = mapnik.Image(512,512)
|
|
|
|
mapnik.render(m,im)
|
|
|
|
actual = '/tmp/mapnik-python-point-render1.png'
|
|
|
|
expected = 'images/support/mapnik-python-point-render1.png'
|
|
|
|
im.save(actual)
|
|
|
|
expected_im = mapnik.Image.open(expected)
|
|
|
|
eq_(im.tostring(),expected_im.tostring(),
|
|
|
|
'failed comparing actual (%s) and expected (%s)' % (actual,'tests/python_tests/'+ expected))
|
|
|
|
|
|
|
|
def test_python_circle_rendering():
|
|
|
|
m = mapnik.Map(512,512)
|
2012-08-18 18:34:58 +02:00
|
|
|
mapnik.load_map(m,'../data/python_plugin/python_circle_datasource.xml')
|
2012-07-19 20:06:44 +02:00
|
|
|
m.zoom_all()
|
|
|
|
im = mapnik.Image(512,512)
|
|
|
|
mapnik.render(m,im)
|
|
|
|
actual = '/tmp/mapnik-python-circle-render1.png'
|
|
|
|
expected = 'images/support/mapnik-python-circle-render1.png'
|
|
|
|
im.save(actual)
|
|
|
|
expected_im = mapnik.Image.open(expected)
|
|
|
|
eq_(im.tostring(),expected_im.tostring(),
|
|
|
|
'failed comparing actual (%s) and expected (%s)' % (actual,'tests/python_tests/'+ expected))
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
setup()
|
|
|
|
[eval(run)() for run in dir() if 'test_' in run]
|