smarter type handling for Map parameters

This commit is contained in:
Dane Springmeyer 2014-10-27 09:24:33 -07:00
parent aa85a4ff1e
commit 808e820164
3 changed files with 28 additions and 52 deletions

View file

@ -377,25 +377,15 @@ void map_parser::parse_map_include(Map & map, xml_node const& node)
{
if (p.is("Parameter"))
{
bool is_string = true;
boost::optional<std::string> type = p.get_opt_attr<std::string>("type");
if (type)
{
if (*type == "int")
{
is_string = false;
params[p.get_attr<std::string>("name")] = p.get_value<mapnik::value_integer>();
}
else if (*type == "float")
{
is_string = false;
params[p.get_attr<std::string>("name")] = p.get_value<mapnik::value_double>();
}
}
if (is_string)
{
params[p.get_attr<std::string>("name")] = p.get_text();
}
std::string val = p.get_text();
std::string key = p.get_attr<std::string>("name");
mapnik::value_bool b;
mapnik::value_integer i;
mapnik::value_double d;
if (mapnik::util::string2bool(val,b)) params[key] = b;
else if (mapnik::util::string2int(val,i)) params[key] = i;
else if (mapnik::util::string2double(val,d)) params[key] = d;
else params[key] = val;
}
}
}

View file

@ -488,36 +488,6 @@ void serialize_datasource( ptree & layer_node, datasource_ptr datasource)
}
}
class serialize_type : public util::static_visitor<>
{
public:
serialize_type( boost::property_tree::ptree & node):
node_(node) {}
void operator () ( mapnik::value_integer ) const
{
node_.put("<xmlattr>.type", "int" );
}
void operator () ( mapnik::value_double ) const
{
node_.put("<xmlattr>.type", "float" );
}
void operator () ( std::string const& ) const
{
node_.put("<xmlattr>.type", "string" );
}
void operator () ( mapnik::value_null ) const
{
node_.put("<xmlattr>.type", "string" );
}
private:
boost::property_tree::ptree & node_;
};
void serialize_parameters( ptree & map_node, mapnik::parameters const& params)
{
if (params.size()) {
@ -531,7 +501,6 @@ void serialize_parameters( ptree & map_node, mapnik::parameters const& params)
boost::property_tree::ptree()))->second;
param_node.put("<xmlattr>.name", p.first );
param_node.put_value( p.second );
util::apply_visitor(serialize_type(param_node),p.second);
}
}
}

View file

@ -13,13 +13,30 @@ def setup():
def test_arbitrary_parameters_attached_to_map():
m = mapnik.Map(256,256)
mapnik.load_map(m,'../data/good_maps/extra_arbitary_map_parameters.xml')
eq_(len(m.parameters),6)
eq_(len(m.parameters),5)
eq_(m.parameters['key'],'value2')
eq_(m.parameters['key3'],'value3')
eq_(m.parameters['unicode'],u'iván')
eq_(m.parameters['integer'],10)
eq_(m.parameters['decimal'],.999)
eq_(m.parameters['number-as-string'],u'.9998')
m2 = mapnik.Map(256,256)
for k,v in m.parameters:
m2.parameters.append(mapnik.Parameter(k,v))
eq_(len(m2.parameters),5)
eq_(m2.parameters['key'],'value2')
eq_(m2.parameters['key3'],'value3')
eq_(m2.parameters['unicode'],u'iván')
eq_(m2.parameters['integer'],10)
eq_(m2.parameters['decimal'],.999)
map_string = mapnik.save_map_to_string(m)
m3 = mapnik.Map(256,256)
mapnik.load_map_from_string(m3,map_string)
eq_(len(m3.parameters),5)
eq_(m3.parameters['key'],'value2')
eq_(m3.parameters['key3'],'value3')
eq_(m3.parameters['unicode'],u'iván')
eq_(m3.parameters['integer'],10)
eq_(m3.parameters['decimal'],.999)
def test_serializing_arbitrary_parameters():