json - refactor stringifier and attribute_value_visitor into separate *.hpp and share between geojson and topojson grammars
This commit is contained in:
parent
6bd708af86
commit
21487f36b9
4 changed files with 173 additions and 133 deletions
68
include/mapnik/json/attribute_value_visitor.hpp
Normal file
68
include/mapnik/json/attribute_value_visitor.hpp
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
/*****************************************************************************
|
||||||
|
*
|
||||||
|
* This file is part of Mapnik (c++ mapping toolkit)
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Artem Pavlenko
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef MAPNIK_JSON_ATTRIBUTE_VALUE_VISITOR_HPP
|
||||||
|
#define MAPNIK_JSON_ATTRIBUTE_VALUE_VISITOR_HPP
|
||||||
|
|
||||||
|
// mapnik
|
||||||
|
#include <mapnik/value.hpp>
|
||||||
|
#include <mapnik/unicode.hpp>
|
||||||
|
#include <mapnik/json/stringifier.hpp>
|
||||||
|
#include <mapnik/json/value_converters.hpp>
|
||||||
|
|
||||||
|
namespace mapnik { namespace json {
|
||||||
|
|
||||||
|
struct attribute_value_visitor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
attribute_value_visitor(mapnik::transcoder const& tr)
|
||||||
|
: tr_(tr) {}
|
||||||
|
|
||||||
|
mapnik::value operator()(std::string const& val) const
|
||||||
|
{
|
||||||
|
return mapnik::value(tr_.transcode(val.c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
mapnik::value operator()(std::vector<mapnik::json::json_value> const& array) const
|
||||||
|
{
|
||||||
|
std::string str = stringifier()(array);
|
||||||
|
return mapnik::value(tr_.transcode(str.c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
mapnik::value operator()(std::unordered_map<std::string, mapnik::json::json_value> const& object) const
|
||||||
|
{
|
||||||
|
std::string str = stringifier()(object);
|
||||||
|
return mapnik::value(tr_.transcode(str.c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
mapnik::value operator()(T const& val) const
|
||||||
|
{
|
||||||
|
return mapnik::value(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
mapnik::transcoder const& tr_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}}
|
||||||
|
|
||||||
|
#endif //MAPNIK_JSON_ATTRIBUTE_VALUE_VISITOR_HPP
|
|
@ -24,15 +24,10 @@
|
||||||
#define MAPNIK_FEATURE_GRAMMAR_HPP
|
#define MAPNIK_FEATURE_GRAMMAR_HPP
|
||||||
|
|
||||||
// mapnik
|
// mapnik
|
||||||
#include <mapnik/json/geometry_grammar.hpp>
|
|
||||||
#include <mapnik/value.hpp>
|
#include <mapnik/value.hpp>
|
||||||
#include <mapnik/feature.hpp>
|
#include <mapnik/feature.hpp>
|
||||||
#include <mapnik/unicode.hpp>
|
#include <mapnik/json/geometry_grammar.hpp>
|
||||||
#include <mapnik/value.hpp>
|
#include <mapnik/json/attribute_value_visitor.hpp>
|
||||||
#include <mapnik/json/generic_json.hpp>
|
|
||||||
#include <mapnik/json/value_converters.hpp>
|
|
||||||
#include <mapnik/util/conversions.hpp>
|
|
||||||
|
|
||||||
#pragma GCC diagnostic push
|
#pragma GCC diagnostic push
|
||||||
#include <mapnik/warning_ignore.hpp>
|
#include <mapnik/warning_ignore.hpp>
|
||||||
#include <boost/spirit/include/qi.hpp>
|
#include <boost/spirit/include/qi.hpp>
|
||||||
|
@ -46,103 +41,6 @@ namespace qi = boost::spirit::qi;
|
||||||
namespace phoenix = boost::phoenix;
|
namespace phoenix = boost::phoenix;
|
||||||
namespace fusion = boost::fusion;
|
namespace fusion = boost::fusion;
|
||||||
|
|
||||||
namespace {
|
|
||||||
struct stringifier
|
|
||||||
{
|
|
||||||
std::string operator()(std::string const& val) const
|
|
||||||
{
|
|
||||||
return "\"" + val + "\"";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string operator()(value_null) const
|
|
||||||
{
|
|
||||||
return "null";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string operator()(value_bool val) const
|
|
||||||
{
|
|
||||||
return val ? "true" : "false";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string operator()(value_integer val) const
|
|
||||||
{
|
|
||||||
std::string str;
|
|
||||||
util::to_string(str, val);
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string operator()(value_double val) const
|
|
||||||
{
|
|
||||||
std::string str;
|
|
||||||
util::to_string(str, val);
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string operator()(std::vector<mapnik::json::json_value> const& array) const
|
|
||||||
{
|
|
||||||
std::string str = "[";
|
|
||||||
bool first = true;
|
|
||||||
for (auto const& val : array)
|
|
||||||
{
|
|
||||||
if (first) first = false;
|
|
||||||
else str += ",";
|
|
||||||
str += mapnik::util::apply_visitor(*this, val);
|
|
||||||
}
|
|
||||||
str += "]";
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string operator()(std::unordered_map<std::string, mapnik::json::json_value> const& object) const
|
|
||||||
{
|
|
||||||
std::string str = "{";
|
|
||||||
bool first = true;
|
|
||||||
for (auto const& kv : object)
|
|
||||||
{
|
|
||||||
if (first) first = false;
|
|
||||||
else str += ",";
|
|
||||||
str += kv.first;
|
|
||||||
str += ":";
|
|
||||||
str += mapnik::util::apply_visitor(*this, kv.second);
|
|
||||||
}
|
|
||||||
str += "}";
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // anonymous ns
|
|
||||||
|
|
||||||
struct attribute_value_visitor
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
attribute_value_visitor(mapnik::transcoder const& tr)
|
|
||||||
: tr_(tr) {}
|
|
||||||
|
|
||||||
mapnik::value operator()(std::string const& val) const
|
|
||||||
{
|
|
||||||
return mapnik::value(tr_.transcode(val.c_str()));
|
|
||||||
}
|
|
||||||
|
|
||||||
mapnik::value operator()(std::vector<mapnik::json::json_value> const& array) const
|
|
||||||
{
|
|
||||||
std::string str = stringifier()(array);
|
|
||||||
return mapnik::value(tr_.transcode(str.c_str()));
|
|
||||||
}
|
|
||||||
|
|
||||||
mapnik::value operator()(std::unordered_map<std::string, mapnik::json::json_value> const& object) const
|
|
||||||
{
|
|
||||||
std::string str = stringifier()(object);
|
|
||||||
return mapnik::value(tr_.transcode(str.c_str()));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
mapnik::value operator()(T const& val) const
|
|
||||||
{
|
|
||||||
return mapnik::value(val);
|
|
||||||
}
|
|
||||||
|
|
||||||
mapnik::transcoder const& tr_;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct put_property
|
struct put_property
|
||||||
{
|
{
|
||||||
using result_type = void;
|
using result_type = void;
|
||||||
|
|
100
include/mapnik/json/stringifier.hpp
Normal file
100
include/mapnik/json/stringifier.hpp
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
/*****************************************************************************
|
||||||
|
*
|
||||||
|
* This file is part of Mapnik (c++ mapping toolkit)
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Artem Pavlenko
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef MAPNIK_JSON_STRINGIFIER_HPP
|
||||||
|
#define MAPNIK_JSON_STRINGIFIER_HPP
|
||||||
|
|
||||||
|
// mapnik
|
||||||
|
#include <mapnik/json/generic_json.hpp>
|
||||||
|
#include <mapnik/util/conversions.hpp>
|
||||||
|
// stl
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
namespace mapnik { namespace json {
|
||||||
|
|
||||||
|
struct stringifier
|
||||||
|
{
|
||||||
|
std::string operator()(std::string const& val) const
|
||||||
|
{
|
||||||
|
return "\"" + val + "\"";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string operator()(value_null) const
|
||||||
|
{
|
||||||
|
return "null";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string operator()(value_bool val) const
|
||||||
|
{
|
||||||
|
return val ? "true" : "false";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string operator()(value_integer val) const
|
||||||
|
{
|
||||||
|
std::string str;
|
||||||
|
util::to_string(str, val);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string operator()(value_double val) const
|
||||||
|
{
|
||||||
|
std::string str;
|
||||||
|
util::to_string(str, val);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string operator()(std::vector<mapnik::json::json_value> const& array) const
|
||||||
|
{
|
||||||
|
std::string str = "[";
|
||||||
|
bool first = true;
|
||||||
|
for (auto const& val : array)
|
||||||
|
{
|
||||||
|
if (first) first = false;
|
||||||
|
else str += ",";
|
||||||
|
str += mapnik::util::apply_visitor(*this, val);
|
||||||
|
}
|
||||||
|
str += "]";
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string operator()(std::unordered_map<std::string, mapnik::json::json_value> const& object) const
|
||||||
|
{
|
||||||
|
std::string str = "{";
|
||||||
|
bool first = true;
|
||||||
|
for (auto const& kv : object)
|
||||||
|
{
|
||||||
|
if (first) first = false;
|
||||||
|
else str += ",";
|
||||||
|
str += kv.first;
|
||||||
|
str += ":";
|
||||||
|
str += mapnik::util::apply_visitor(*this, kv.second);
|
||||||
|
}
|
||||||
|
str += "}";
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}}
|
||||||
|
|
||||||
|
|
||||||
|
#endif // MAPNIK_JSON_STRINGIFIER_HPP
|
|
@ -2,7 +2,7 @@
|
||||||
*
|
*
|
||||||
* This file is part of Mapnik (c++ mapping toolkit)
|
* This file is part of Mapnik (c++ mapping toolkit)
|
||||||
*
|
*
|
||||||
* Copyright (C) 2015 Artem Pavlenko
|
* Copyright (C) 2016 Artem Pavlenko
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
@ -27,6 +27,7 @@
|
||||||
#include <mapnik/box2d.hpp>
|
#include <mapnik/box2d.hpp>
|
||||||
#include <mapnik/unicode.hpp>
|
#include <mapnik/unicode.hpp>
|
||||||
#include <mapnik/json/topology.hpp>
|
#include <mapnik/json/topology.hpp>
|
||||||
|
#include <mapnik/json/attribute_value_visitor.hpp>
|
||||||
#include <mapnik/feature_factory.hpp>
|
#include <mapnik/feature_factory.hpp>
|
||||||
#include <mapnik/geometry_adapters.hpp>
|
#include <mapnik/geometry_adapters.hpp>
|
||||||
#include <mapnik/geometry_correct.hpp>
|
#include <mapnik/geometry_correct.hpp>
|
||||||
|
@ -246,33 +247,6 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
struct attribute_value_visitor
|
|
||||||
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
attribute_value_visitor(mapnik::transcoder const& tr)
|
|
||||||
: tr_(tr) {}
|
|
||||||
|
|
||||||
mapnik::value operator()(std::string const& val) const
|
|
||||||
{
|
|
||||||
return mapnik::value(tr_.transcode(val.c_str()));
|
|
||||||
}
|
|
||||||
mapnik::value operator()(std::vector<mapnik::json::json_value> const& arr) const
|
|
||||||
{
|
|
||||||
return mapnik::value(tr_.transcode("FAIL ARRAY"));
|
|
||||||
}
|
|
||||||
mapnik::value operator()(std::unordered_map<std::string, mapnik::json::json_value> const& obj) const
|
|
||||||
{
|
|
||||||
return mapnik::value(tr_.transcode("FAIL OBJECT"));
|
|
||||||
}
|
|
||||||
template <typename T>
|
|
||||||
mapnik::value operator()(T const& val) const
|
|
||||||
{
|
|
||||||
return mapnik::value(val);
|
|
||||||
}
|
|
||||||
|
|
||||||
mapnik::transcoder const& tr_;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void assign_properties(mapnik::feature_impl & feature, T const& geom, mapnik::transcoder const& tr)
|
void assign_properties(mapnik::feature_impl & feature, T const& geom, mapnik::transcoder const& tr)
|
||||||
|
@ -281,7 +255,7 @@ void assign_properties(mapnik::feature_impl & feature, T const& geom, mapnik::tr
|
||||||
{
|
{
|
||||||
for (auto const& p : *geom.props)
|
for (auto const& p : *geom.props)
|
||||||
{
|
{
|
||||||
feature.put_new(std::get<0>(p), mapnik::util::apply_visitor(attribute_value_visitor(tr),std::get<1>(p)));
|
feature.put_new(std::get<0>(p), mapnik::util::apply_visitor(mapnik::json::attribute_value_visitor(tr),std::get<1>(p)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue