From b78b8e735b09715135f19d98376a346ba6cf1914 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Mon, 5 Dec 2011 14:46:38 -0800 Subject: [PATCH] add ability to have typed arbitrary parameters in xml - refs #977 --- src/load_map.cpp | 27 ++++++++++++++-- src/save_map.cpp | 32 ++++++++++++++++++- .../extra_arbitary_map_parameters.xml | 12 ++++--- tests/python_tests/extra_map_props_test.py | 15 ++++++++- 4 files changed, 77 insertions(+), 9 deletions(-) diff --git a/src/load_map.cpp b/src/load_map.cpp index c20125502..49913ef2b 100644 --- a/src/load_map.cpp +++ b/src/load_map.cpp @@ -403,9 +403,30 @@ void map_parser::parse_map_include( Map & map, ptree const & include ) if (paramIter->first == "Parameter") { std::string name = get_attr(param, "name"); - std::string value = get_value( param, - "parameter"); - params[name] = value; + bool is_string = true; + boost::optional type = get_opt_attr(param, "type"); + if (type) + { + if (*type == "int") + { + is_string = false; + int value = get_value( param,"parameter"); + params[name] = value; + } + else if (*type == "float") + { + is_string = false; + double value = get_value( param,"parameter"); + params[name] = value; + } + } + + if (is_string) + { + std::string value = get_value( param, + "parameter"); + params[name] = value; + } } else if( paramIter->first != "" && paramIter->first != "" ) diff --git a/src/save_map.cpp b/src/save_map.cpp index 48d2f9099..41496a882 100644 --- a/src/save_map.cpp +++ b/src/save_map.cpp @@ -761,6 +761,36 @@ void serialize_datasource( ptree & layer_node, datasource_ptr datasource) } } +class serialize_type : public boost::static_visitor<> +{ + public: + serialize_type( boost::property_tree::ptree & node): + node_(node) {} + + void operator () ( int val ) const + { + node_.put(".type", "int" ); + } + + void operator () ( double val ) const + { + node_.put(".type", "float" ); + } + + void operator () ( std::string const& val ) const + { + node_.put(".type", "string" ); + } + + void operator () ( mapnik::value_null val ) const + { + node_.put(".type", "string" ); + } + + private: + boost::property_tree::ptree & node_; +}; + void serialize_parameters( ptree & map_node, mapnik::parameters const& params) { ptree & params_node = map_node.push_back( @@ -775,7 +805,7 @@ void serialize_parameters( ptree & map_node, mapnik::parameters const& params) boost::property_tree::ptree()))->second; param_node.put(".name", it->first ); param_node.put_value( it->second ); - + boost::apply_visitor(serialize_type(param_node),it->second); } } diff --git a/tests/data/good_maps/extra_arbitary_map_parameters.xml b/tests/data/good_maps/extra_arbitary_map_parameters.xml index d4377cc50..e6bd5d444 100644 --- a/tests/data/good_maps/extra_arbitary_map_parameters.xml +++ b/tests/data/good_maps/extra_arbitary_map_parameters.xml @@ -10,11 +10,15 @@ --> - + - - - + + + + + + + \ No newline at end of file diff --git a/tests/python_tests/extra_map_props_test.py b/tests/python_tests/extra_map_props_test.py index 737bab03f..47f61c8d4 100644 --- a/tests/python_tests/extra_map_props_test.py +++ b/tests/python_tests/extra_map_props_test.py @@ -28,12 +28,25 @@ def test_arbitrary_parameters_attached_to_map(): attr = m.extra_attributes eq_(len(attr),0) - eq_(len(m.params),3) + eq_(len(m.params),6) eq_(m.params['key'],'value2') eq_(m.params['key3'],'value3') eq_(m.params['unicode'],u'iván') + eq_(m.params['integer'],10) + eq_(m.params['decimal'],.999) + eq_(m.params['number-as-string'],u'.9998') +def test_serializing_arbitrary_parameters(): + m = mapnik.Map(256,256) + m.params.append(mapnik.Parameter('width',m.width)) + m.params.append(mapnik.Parameter('height',m.height)) + + m2 = mapnik.Map(1,1) + mapnik.load_map_from_string(m2,mapnik.save_map_to_string(m)) + eq_(m2.params['width'],m.width) + eq_(m2.params['height'],m.height) + if __name__ == "__main__": setup() [eval(run)() for run in dir() if 'test_' in run]