From fa052c50211f051c415c174423e56cce1eb60469 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Wed, 25 Jul 2012 14:43:32 -0700 Subject: [PATCH] amend 516f7c7 to suport filtering on collections and rename line to linestring for consistency - refs #546 --- include/mapnik/attribute.hpp | 14 +++++---- include/mapnik/expression_grammar.hpp | 3 +- tests/python_tests/filter_test.py | 41 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/include/mapnik/attribute.hpp b/include/mapnik/attribute.hpp index a9ae97169..9ff0ab063 100644 --- a/include/mapnik/attribute.hpp +++ b/include/mapnik/attribute.hpp @@ -26,8 +26,7 @@ // mapnik #include #include -// boost -#include + // stl #include @@ -53,15 +52,18 @@ struct geometry_type_attribute template V value(F const& f) const { - int result = 0; - + int type = 0; geometry_container::const_iterator itr = f.paths().begin(); geometry_container::const_iterator end = f.paths().end(); for ( ; itr != end; ++itr) { - result = itr->type(); + if (type != 0 && itr->type() != type) + { + return 4; // Collection + } + type = itr->type(); } - return result; + return type; } }; diff --git a/include/mapnik/expression_grammar.hpp b/include/mapnik/expression_grammar.hpp index 0fe062923..31f6e912e 100644 --- a/include/mapnik/expression_grammar.hpp +++ b/include/mapnik/expression_grammar.hpp @@ -114,8 +114,9 @@ struct geometry_types : qi::symbols { add ("point",1) - ("line", 2) + ("linestring", 2) ("polygon",3) + ("collection",4) ; } }; diff --git a/tests/python_tests/filter_test.py b/tests/python_tests/filter_test.py index f28153abd..3480d7d24 100644 --- a/tests/python_tests/filter_test.py +++ b/tests/python_tests/filter_test.py @@ -92,6 +92,47 @@ def test_filter_init(): eq_(s.filter_mode,mapnik.filter_mode.FIRST) +def test_geometry_type_eval(): + # clashing field called 'mapnik::geometry' + context2 = mapnik.Context() + context2.push('mapnik::geometry_type') + f = mapnik.Feature(context2,0) + f["mapnik::geometry_type"] = 'sneaky' + expr = mapnik.Expression("[mapnik::geometry_type]") + eq_(expr.evaluate(f),0) + + expr = mapnik.Expression("[mapnik::geometry_type]") + context = mapnik.Context() + + # no geometry + f = mapnik.Feature(context,0) + eq_(expr.evaluate(f),0) + eq_(mapnik.Expression("[mapnik::geometry_type]=0").evaluate(f),True) + + # POINT = 1 + f = mapnik.Feature(context,0) + f.add_geometries_from_wkt('POINT(10 40)') + eq_(expr.evaluate(f),1) + eq_(mapnik.Expression("[mapnik::geometry_type]=point").evaluate(f),True) + + # LINESTRING = 2 + f = mapnik.Feature(context,0) + f.add_geometries_from_wkt('LINESTRING (30 10, 10 30, 40 40)') + eq_(expr.evaluate(f),2) + eq_(mapnik.Expression("[mapnik::geometry_type]=linestring").evaluate(f),True) + + # POLYGON = 3 + f = mapnik.Feature(context,0) + f.add_geometries_from_wkt('POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))') + eq_(expr.evaluate(f),3) + eq_(mapnik.Expression("[mapnik::geometry_type]=polygon").evaluate(f),True) + + # COLLECTION = 4 + f = mapnik.Feature(context,0) + f.add_geometries_from_wkt('GEOMETRYCOLLECTION(POLYGON((1 1,2 1,2 2,1 2,1 1)),POINT(2 3),LINESTRING(2 3,3 4))') + eq_(expr.evaluate(f),4) + eq_(mapnik.Expression("[mapnik::geometry_type]=collection").evaluate(f),True) + def test_regex_match(): context = mapnik.Context() context.push('name')