2012-06-22 14:15:10 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2015-02-02 18:31:16 +00:00
|
|
|
|
|
|
|
from nose.tools import eq_,raises
|
|
|
|
from utilities import run_all
|
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
|
|
|
|
2012-01-24 08:27:44 +00:00
|
|
|
def test_default_constructor():
|
|
|
|
f = mapnik.Feature(mapnik.Context(),1)
|
|
|
|
eq_(f is not None,True)
|
2010-03-19 13:42:25 +00:00
|
|
|
|
2013-09-18 23:08:34 +00:00
|
|
|
def test_feature_geo_interface():
|
|
|
|
ctx = mapnik.Context()
|
|
|
|
feat = mapnik.Feature(ctx,1)
|
2015-03-23 11:17:09 +00:00
|
|
|
feat.geometry = mapnik.Geometry.from_wkt('Point (0 0)')
|
2013-09-18 23:08:34 +00:00
|
|
|
eq_(feat.__geo_interface__['geometry'],{u'type': u'Point', u'coordinates': [0, 0]})
|
|
|
|
|
2012-01-24 08:27:44 +00:00
|
|
|
def test_python_extended_constructor():
|
|
|
|
context = mapnik.Context()
|
|
|
|
context.push('foo')
|
|
|
|
context.push('foo')
|
|
|
|
f = mapnik.Feature(context,1)
|
|
|
|
wkt = 'POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30))'
|
2015-03-23 11:17:09 +00:00
|
|
|
f.geometry = mapnik.Geometry.from_wkt(wkt)
|
2012-01-24 08:27:44 +00:00
|
|
|
f['foo'] = 'bar'
|
|
|
|
eq_(f['foo'], 'bar')
|
|
|
|
eq_(f.envelope(),mapnik.Box2d(10.0,10.0,45.0,45.0))
|
|
|
|
# reset
|
|
|
|
f['foo'] = u"avión"
|
|
|
|
eq_(f['foo'], u"avión")
|
|
|
|
f['foo'] = 1.4
|
|
|
|
eq_(f['foo'], 1.4)
|
|
|
|
f['foo'] = True
|
|
|
|
eq_(f['foo'], True)
|
2011-10-27 16:00:16 +00:00
|
|
|
|
2012-01-24 08:27:44 +00:00
|
|
|
def test_add_geom_wkb():
|
|
|
|
# POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))
|
|
|
|
wkb = '010300000001000000050000000000000000003e4000000000000024400000000000002440000000000000344000000000000034400000000000004440000000000000444000000000000044400000000000003e400000000000002440'
|
|
|
|
context = mapnik.Context()
|
|
|
|
f = mapnik.Feature(context,1)
|
2015-03-23 11:17:09 +00:00
|
|
|
f.geometry = mapnik.Geometry.from_wkb(unhexlify(wkb))
|
|
|
|
eq_(f.geometry.is_valid(), True)
|
|
|
|
eq_(f.geometry.is_simple(), True)
|
|
|
|
eq_(f.geometry.envelope(), f.envelope())
|
2011-10-27 16:00:16 +00:00
|
|
|
|
|
|
|
def test_feature_expression_evaluation():
|
2012-01-24 08:27:44 +00:00
|
|
|
context = mapnik.Context()
|
|
|
|
context.push('name')
|
|
|
|
f = mapnik.Feature(context,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():
|
2012-01-24 08:27:44 +00:00
|
|
|
context = mapnik.Context()
|
|
|
|
context.push('name')
|
|
|
|
f = mapnik.Feature(context,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'")
|
2012-02-14 22:57:36 +00:00
|
|
|
eq_(f.has_key('fielddoesnotexist'),False)
|
|
|
|
try:
|
2015-02-02 18:31:16 +00:00
|
|
|
expr.evaluate(f)
|
2012-02-14 22:57:36 +00:00
|
|
|
except Exception, e:
|
|
|
|
eq_("Key does not exist" in str(e),True)
|
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():
|
2012-01-24 08:27:44 +00:00
|
|
|
context = mapnik.Context()
|
|
|
|
context.push('name with space')
|
|
|
|
f = mapnik.Feature(context,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)
|
|
|
|
|
2014-08-25 19:04:35 +00:00
|
|
|
# https://github.com/mapnik/mapnik/issues/2390
|
2014-09-14 01:22:17 +00:00
|
|
|
@raises(RuntimeError)
|
2014-08-25 19:04:35 +00:00
|
|
|
def test_feature_from_geojson():
|
|
|
|
ctx = mapnik.Context()
|
|
|
|
inline_string = """
|
|
|
|
{
|
|
|
|
"geometry" : {
|
|
|
|
"coordinates" : [ 0,0 ]
|
|
|
|
"type" : "Point"
|
|
|
|
},
|
|
|
|
"type" : "Feature",
|
|
|
|
"properties" : {
|
|
|
|
"this":"that"
|
|
|
|
"known":"nope because missing comma"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2015-02-02 18:31:16 +00:00
|
|
|
mapnik.Feature.from_geojson(inline_string,ctx)
|
2014-08-25 19:04:35 +00:00
|
|
|
|
2011-08-30 22:51:42 +00:00
|
|
|
if __name__ == "__main__":
|
2014-07-14 16:34:20 +00:00
|
|
|
exit(run_all(eval(x) for x in dir() if x.startswith("test_")))
|