move symbolizer to json into core

symbolizer json grammar (needs more work)
This commit is contained in:
artemp 2013-12-10 15:50:03 -05:00
parent 329224d272
commit b7dda95d84
3 changed files with 210 additions and 25 deletions

View File

@ -124,33 +124,9 @@ boost::python::object __getitem__(mapnik::symbolizer_base const& sym, std::strin
return boost::python::object();
}
struct symbolizer_to_json : public boost::static_visitor<std::string>
{
typedef std::string result_type;
template <typename T>
auto operator() (T const& sym) const -> result_type
{
std::stringstream ss;
ss << "{\"type\":\"" << mapnik::symbolizer_traits<T>::name() << "\",";
ss << "\"properties\":{";
bool first = true;
for (auto const& prop : sym.properties)
{
auto const& meta = mapnik::get_meta(prop.first);
if (first) first = false;
else ss << ",";
ss << "\"" << std::get<0>(meta) << "\":";
ss << boost::apply_visitor(mapnik::symbolizer_property_value_string<mapnik::property_meta_type>(meta),prop.second);
}
ss << "}}";
return ss.str();
}
};
std::string __str__(mapnik::symbolizer const& sym)
{
return boost::apply_visitor(symbolizer_to_json(), sym);
return boost::apply_visitor(mapnik::symbolizer_to_json(), sym);
}
std::string get_symbolizer_type(symbolizer const& sym)

View File

@ -0,0 +1,184 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2013 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_SYMBOLIZER_GRAMMAR_HPP
#define MAPNIK_SYMBOLIZER_GRAMMAR_HPP
// boost
#define BOOST_SPIRIT_USE_PHOENIX_V3 1
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
// mapnik
#include <mapnik/symbolizer.hpp>
#include <mapnik/json/generic_json.hpp>
namespace mapnik { namespace json {
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
namespace fusion = boost::fusion;
namespace standard_wide = boost::spirit::standard_wide;
using standard_wide::space_type;
template <typename T>
struct put_property_visitor : boost::static_visitor<>
{
typedef T value_type;
put_property_visitor(mapnik::keys key, value_type const& val)
: key_(key), val_(val) {}
template <typename Symbolizer>
void operator() (Symbolizer & sym) const
{
std::cerr << std::get<0>(get_meta(key_)) << ":" << val_ << std::endl;
// put<value_type>(sym, key_, val_);
}
keys key_;
value_type const& val_;
};
struct put_property
{
typedef void result_type;
//explicit put_property(mapnik::transcoder const& tr)
// : tr_(tr) {}
template <typename T0,typename T1, typename T2>
result_type operator() (T0 & sym, T1 const& name, T2 const& val) const
{
try
{
boost::apply_visitor(put_property_visitor<T2>(get_key(name),val), sym);
}
catch (std::runtime_error const& err)
{
std::cerr << err.what() << std::endl;
}
}
// mapnik::transcoder const& tr_;
};
template <typename Iterator>
struct symbolizer_grammar : qi::grammar<Iterator, space_type, symbolizer()>
{
symbolizer_grammar(generic_json<Iterator> & json)
: symbolizer_grammar::base_type(sym, "symbolizer"),
json_(json)
{
using qi::lit;
using qi::double_;
using qi::int_;
using qi::no_skip;
using qi::omit;
using qi::_val;
using qi::_a;
using qi::_r1;
using qi::_1;
using qi::_2;
using qi::_3;
using qi::_4;
using qi::fail;
using qi::on_error;
using standard_wide::char_;
using phoenix::construct;
// generic json types
json_.value = json_.object | json_.array | json_.string_
| json_.number
;
json_.pairs = json_.key_value % lit(',')
;
json_.key_value = (json_.string_ >> lit(':') >> json_.value)
;
json_.object = lit('{')
>> *json_.pairs
>> lit('}')
;
json_.array = lit('[')
>> json_.value >> *(lit(',') >> json_.value)
>> lit(']')
;
json_.number %= json_.strict_double
| json_.int__
| lit("true") [_val = true]
| lit ("false") [_val = false]
| lit("null")[_val = construct<value_null>()]
;
json_.unesc_char.add
("\\\"", '\"') // quotation mark
("\\\\", '\\') // reverse solidus
("\\/", '/') // solidus
("\\b", '\b') // backspace
("\\f", '\f') // formfeed
("\\n", '\n') // newline
("\\r", '\r') // carrige return
("\\t", '\t') // tab
;
json_.string_ %= lit('"') >> no_skip[*(json_.unesc_char | "\\u" >> json_.hex4 | (char_ - lit('"')))] >> lit('"')
;
sym = lit('{')
>> lit("\"type\"") >> lit(':')
>> (lit("\"PointSymbolizer\"")[_val = construct<point_symbolizer>()]
|
lit("\"LineSymbolizer\"")[_val = construct<line_symbolizer>()]
|
lit("\"PolygonSymbolizer\"")[_val = construct<polygon_symbolizer>()]
)
>> lit(',')
>> lit("\"properties\"") >> lit(':')
>> ((lit('{') >> *property(_val) >> lit('}')) | lit("null"))
>> lit('}')
;
property = (json_.string_ [_a = _1] >> lit(':') >> property_value [put_property_(_r1,_a,_1)]) % lit(',')
;
property_value %= json.number | json.string_ ;
}
// generic JSON
generic_json<Iterator> & json_;
// symbolizer
qi::rule<Iterator, space_type, mapnik::symbolizer()> sym;
qi::rule<Iterator,qi::locals<std::string>, void(mapnik::symbolizer&),space_type> property;
qi::rule<Iterator,boost::variant<value_null,value_bool,value_integer,value_double, std::string>, space_type> property_value;
phoenix::function<put_property> put_property_;
// error
//boost::phoenix::function<where_message> where_message_;
};
}}
#endif // MAPNIK_FEATURE_GRAMMAR_HPP

View File

@ -215,6 +215,31 @@ private:
Meta const& meta_;
};
struct symbolizer_to_json : public boost::static_visitor<std::string>
{
typedef std::string result_type;
template <typename T>
auto operator() (T const& sym) const -> result_type
{
std::stringstream ss;
ss << "{\"type\":\"" << mapnik::symbolizer_traits<T>::name() << "\",";
ss << "\"properties\":{";
bool first = true;
for (auto const& prop : sym.properties)
{
auto const& meta = mapnik::get_meta(prop.first);
if (first) first = false;
else ss << ",";
ss << "\"" << std::get<0>(meta) << "\":";
ss << boost::apply_visitor(symbolizer_property_value_string<property_meta_type>(meta),prop.second);
}
ss << "}}";
return ss.str();
}
};
}
#endif // MAPNIK_SYMBOLIZER_UTILS_HPP