2010-03-19 13:42:25 +00:00
|
|
|
#encoding: utf8
|
|
|
|
import itertools
|
|
|
|
import unittest
|
2011-10-27 16:00:16 +00:00
|
|
|
from nose.tools import *
|
2010-03-19 13:42:25 +00:00
|
|
|
|
2011-11-23 11:33:58 +00:00
|
|
|
import mapnik
|
2011-10-27 16:00:16 +00:00
|
|
|
from binascii import unhexlify
|
2010-03-19 13:42:25 +00:00
|
|
|
from utilities import Todo
|
|
|
|
|
|
|
|
class FeatureTest(unittest.TestCase):
|
|
|
|
def makeOne(self, *args, **kw):
|
2011-11-23 11:33:58 +00:00
|
|
|
return mapnik.Feature(*args, **kw)
|
2011-09-13 13:32:42 +00:00
|
|
|
|
2010-03-19 13:42:25 +00:00
|
|
|
def test_default_constructor(self):
|
|
|
|
f = self.makeOne(1)
|
|
|
|
self.failUnless(f is not None)
|
|
|
|
|
2010-03-19 18:14:46 +00:00
|
|
|
def test_python_extended_constructor(self):
|
2011-09-13 13:32:42 +00:00
|
|
|
f = self.makeOne(1, 'POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30))', foo="bar")
|
2010-03-19 18:14:46 +00:00
|
|
|
self.failUnlessEqual(f['foo'], 'bar')
|
2011-11-23 11:33:58 +00:00
|
|
|
self.failUnlessEqual(f.envelope(),mapnik.Box2d(10.0,10.0,45.0,45.0))
|
2011-09-13 13:32:42 +00:00
|
|
|
|
2010-03-19 13:42:25 +00:00
|
|
|
def test_set_get_properties(self):
|
|
|
|
f = self.makeOne(1)
|
|
|
|
counter = itertools.count(0)
|
|
|
|
def test_val(expected):
|
|
|
|
key = 'prop%d'%counter.next()
|
|
|
|
try:
|
|
|
|
f[key] = expected
|
|
|
|
except TypeError:
|
|
|
|
self.fail("%r (%s)"%(expected, type(expected)))
|
|
|
|
self.failUnlessEqual(f[key], expected)
|
2010-03-19 13:42:58 +00:00
|
|
|
for v in (1, True, 1.4, "foo", u"avión"):
|
2010-03-19 13:42:25 +00:00
|
|
|
test_val(v)
|
2010-03-19 13:42:41 +00:00
|
|
|
|
2011-09-13 13:32:42 +00:00
|
|
|
def test_add_wkt_geometry(self):
|
2011-09-13 14:06:17 +00:00
|
|
|
def add_geom_wkt(wkt):
|
2010-03-19 18:14:46 +00:00
|
|
|
f = self.makeOne(1)
|
2011-09-13 13:32:42 +00:00
|
|
|
self.failUnlessEqual(len(f.geometries()), 0)
|
|
|
|
f.add_geometries_from_wkt(wkt)
|
|
|
|
self.failUnlessEqual(len(f.geometries()), 3)
|
2011-11-23 11:33:58 +00:00
|
|
|
e = mapnik.Box2d()
|
2011-09-13 13:32:42 +00:00
|
|
|
self.failUnlessEqual(e.valid(), False)
|
|
|
|
for g in f.geometries():
|
|
|
|
if not e.valid():
|
|
|
|
e = g.envelope()
|
|
|
|
else:
|
|
|
|
e +=g.envelope()
|
|
|
|
|
|
|
|
self.failUnlessEqual(e, f.envelope())
|
2011-10-27 16:00:16 +00:00
|
|
|
|
2011-09-13 14:06:17 +00:00
|
|
|
def add_geom_wkb(wkb):
|
|
|
|
f = self.makeOne(1)
|
|
|
|
self.failUnlessEqual(len(f.geometries()), 0)
|
|
|
|
f.add_geometries_from_wkb(unhexlify(wkb))
|
|
|
|
self.failUnlessEqual(len(f.geometries()), 1)
|
2011-11-23 11:33:58 +00:00
|
|
|
e = mapnik.Box2d()
|
2011-09-13 14:06:17 +00:00
|
|
|
self.failUnlessEqual(e.valid(), False)
|
|
|
|
for g in f.geometries():
|
|
|
|
if not e.valid():
|
|
|
|
e = g.envelope()
|
|
|
|
else:
|
|
|
|
e +=g.envelope()
|
|
|
|
|
|
|
|
self.failUnlessEqual(e, f.envelope())
|
2011-10-27 16:00:16 +00:00
|
|
|
|
2011-09-13 14:06:17 +00:00
|
|
|
def run() :
|
|
|
|
add_geom_wkt('GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10),POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10)))')
|
|
|
|
add_geom_wkb('010300000001000000050000000000000000003e4000000000000024400000000000002440000000000000344000000000000034400000000000004440000000000000444000000000000044400000000000003e400000000000002440') # POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))
|
|
|
|
run()
|
2011-10-27 16:00:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_feature_expression_evaluation():
|
2011-11-23 11:33:58 +00:00
|
|
|
f = mapnik.Feature(1)
|
2011-10-27 16:00:16 +00:00
|
|
|
f['name'] = 'a'
|
2011-10-27 19:01:58 +00:00
|
|
|
eq_(f['name'],u'a')
|
2011-11-23 11:33:58 +00:00
|
|
|
expr = mapnik.Expression("[name]='a'")
|
2011-10-27 16:00:16 +00:00
|
|
|
evaluated = expr.evaluate(f)
|
|
|
|
eq_(evaluated,True)
|
|
|
|
num_attributes = len(f)
|
|
|
|
eq_(num_attributes,1)
|
|
|
|
eq_(f.id(),1)
|
|
|
|
|
|
|
|
# https://github.com/mapnik/mapnik/issues/933
|
|
|
|
def test_feature_expression_evaluation_missing_attr():
|
2011-11-23 11:33:58 +00:00
|
|
|
f = mapnik.Feature(1)
|
2011-10-27 19:01:58 +00:00
|
|
|
f['name'] = u'a'
|
|
|
|
eq_(f['name'],u'a')
|
2011-11-23 11:33:58 +00:00
|
|
|
expr = mapnik.Expression("[fielddoesnotexist]='a'")
|
2011-10-27 16:00:16 +00:00
|
|
|
evaluated = expr.evaluate(f)
|
2011-10-27 19:01:58 +00:00
|
|
|
eq_(evaluated,False)
|
2011-10-27 16:00:16 +00:00
|
|
|
num_attributes = len(f)
|
|
|
|
eq_(num_attributes,1)
|
|
|
|
eq_(f.id(),1)
|
|
|
|
|
|
|
|
# https://github.com/mapnik/mapnik/issues/934
|
|
|
|
def test_feature_expression_evaluation_attr_with_spaces():
|
2011-11-23 11:33:58 +00:00
|
|
|
f = mapnik.Feature(1)
|
2011-10-27 19:01:58 +00:00
|
|
|
f['name with space'] = u'a'
|
|
|
|
eq_(f['name with space'],u'a')
|
2011-11-23 11:33:58 +00:00
|
|
|
expr = mapnik.Expression("[name with space]='a'")
|
2011-10-29 00:06:23 +00:00
|
|
|
eq_(str(expr),"([name with space]='a')")
|
2011-10-27 16:00:16 +00:00
|
|
|
eq_(expr.evaluate(f),True)
|
|
|
|
|
2011-08-30 22:51:42 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
[eval(run)() for run in dir() if 'test_' in run]
|