json - split/refactor geojson grammars more

This commit is contained in:
artemp 2016-02-09 13:21:20 +01:00
parent 6f33b8a5f1
commit 8102827215
10 changed files with 250 additions and 87 deletions

View file

@ -0,0 +1,78 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 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
*
*****************************************************************************/
/*****************************************************************************
*
* 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_FEATURE_CALLBACK_HPP
#define MAPNIK_FEATURE_CALLBACK_HPP
// mapnik
#include <mapnik/feature.hpp>
#include <vector>
namespace mapnik { namespace json {
struct default_feature_callback
{
default_feature_callback(std::vector<feature_ptr> & features)
: features_(features) {}
void operator() (feature_ptr const& feature)
{
features_.push_back(feature);
}
std::vector<feature_ptr> & features_;
};
struct apply_feature_callback
{
using result_type = void;
template <typename Callback, typename Feature>
void operator() (Callback & callback, Feature const& feature) const
{
callback(feature);
}
};
}}
#endif // MAPNIK_FEATURE_CALLBACK_HPP

View file

@ -0,0 +1,63 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 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_FEATURE_CALLBACK_GRAMMAR_HPP
#define MAPNIK_FEATURE_CALLBACK_GRAMMAR_HPP
// mapnik
#include <mapnik/unicode.hpp>
#include <mapnik/json/geometry_grammar.hpp>
#include <mapnik/json/feature_grammar.hpp>
#include <mapnik/json/feature_callback.hpp>
#include <mapnik/feature.hpp>
// spirit::qi
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace mapnik { namespace json {
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
template <typename Iterator, typename FeatureType, typename FeatureCallback = default_feature_callback, typename ErrorHandler = error_handler<Iterator> >
struct feature_grammar_callback :
qi::grammar<Iterator, void(context_ptr const&, std::size_t&, FeatureCallback &), space_type>
{
feature_grammar_callback(mapnik::transcoder const& tr);
// grammars
feature_grammar<Iterator, FeatureType> feature_g;
geometry_grammar<Iterator> geometry_g;
// rules
qi::rule<Iterator, void(context_ptr const&, std::size_t&, FeatureCallback&), space_type> start; // START
qi::rule<Iterator, qi::locals<feature_ptr,int>, void(context_ptr const& ctx, std::size_t, FeatureCallback&), space_type> feature;
qi::rule<Iterator, qi::locals<feature_ptr,int>, void(context_ptr const& ctx, std::size_t, FeatureCallback&), space_type> feature_from_geometry;
// phoenix functions
phoenix::function<json::set_geometry_impl> set_geometry;
phoenix::function<apply_feature_callback> on_feature;
// error handler
boost::phoenix::function<ErrorHandler> const error_handler;
};
}}
#endif // MAPNIK_FEATURE_CALLBACK_GRAMMAR_HPP

View file

@ -0,0 +1,74 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 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
*
*****************************************************************************/
// mapnik
#include <mapnik/json/error_handler.hpp>
#include <mapnik/json/feature_callback_grammar.hpp>
// spirit::qi
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace mapnik { namespace json {
template <typename Iterator, typename FeatureType, typename FeatureCallback, typename ErrorHandler>
feature_grammar_callback<Iterator,FeatureType, FeatureCallback,ErrorHandler>::feature_grammar_callback(mapnik::transcoder const& tr)
: feature_grammar_callback::base_type(start,"start"),
feature_g(tr)
{
qi::lit_type lit;
qi::eps_type eps;
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
qi::_a_type _a;
qi::_r1_type _r1;
qi::_r2_type _r2;
qi::_r3_type _r3;
using phoenix::construct;
using phoenix::new_;
using phoenix::val;
using qi::on_error;
using qi::fail;
start = feature_from_geometry(_r1, _r2, _r3) | feature(_r1, _r2, _r3)
;
feature = eps[_a = phoenix::construct<mapnik::feature_ptr>(new_<mapnik::feature_impl>(_r1, _r2))]
>> feature_g(*_a)[on_feature(_r3,_a)]
;
feature_from_geometry =
eps[_a = phoenix::construct<mapnik::feature_ptr>(new_<mapnik::feature_impl>(_r1, _r2))]
>> geometry_g[set_geometry(*_a, _1)] [on_feature(_r3, _a)]
;
start.name("start");
feature.name("feature");
feature_from_geometry.name("feature-from-geometry");
feature_g.name("feature-grammar");
geometry_g.name("geometry-grammar");
on_error<fail>(feature, error_handler(_1, _2, _3, _4));
}
}}

View file

@ -27,6 +27,7 @@
#include <mapnik/unicode.hpp>
#include <mapnik/json/geometry_grammar.hpp>
#include <mapnik/json/feature_grammar.hpp>
#include <mapnik/json/feature_callback.hpp>
#include <mapnik/feature.hpp>
// spirit::qi
@ -38,27 +39,6 @@ namespace mapnik { namespace json {
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
struct default_feature_callback
{
default_feature_callback(std::vector<feature_ptr> & features)
: features_(features) {}
void operator() (feature_ptr const& feature)
{
features_.push_back(feature);
}
std::vector<feature_ptr> & features_;
};
struct apply_feature_callback
{
using result_type = void;
template <typename Callback, typename Feature>
void operator() (Callback & callback, Feature const& feature) const
{
callback(feature);
}
};
template <typename Iterator, typename FeatureType, typename FeatureCallback = default_feature_callback, typename ErrorHandler = error_handler<Iterator> >
struct feature_collection_grammar :
qi::grammar<Iterator, void(context_ptr const&, std::size_t&, FeatureCallback &), space_type>
@ -78,26 +58,6 @@ struct feature_collection_grammar :
boost::phoenix::function<ErrorHandler> const error_handler;
};
template <typename Iterator, typename FeatureType, typename FeatureCallback = default_feature_callback, typename ErrorHandler = error_handler<Iterator> >
struct feature_grammar_callback :
qi::grammar<Iterator, void(context_ptr const&, std::size_t&, FeatureCallback &), space_type>
{
feature_grammar_callback(mapnik::transcoder const& tr);
// grammars
feature_grammar<Iterator, FeatureType> feature_g;
geometry_grammar<Iterator> geometry_g;
// rules
qi::rule<Iterator, void(context_ptr const&, std::size_t&, FeatureCallback&), space_type> start; // START
qi::rule<Iterator, qi::locals<feature_ptr,int>, void(context_ptr const& ctx, std::size_t, FeatureCallback&), space_type> feature;
qi::rule<Iterator, qi::locals<feature_ptr,int>, void(context_ptr const& ctx, std::size_t, FeatureCallback&), space_type> feature_from_geometry;
// phoenix functions
phoenix::function<json::set_geometry_impl> set_geometry;
phoenix::function<apply_feature_callback> on_feature;
// error handler
boost::phoenix::function<ErrorHandler> const error_handler;
};
}}
#endif // MAPNIK_FEATURE_COLLECTION_GRAMMAR_HPP

View file

@ -24,7 +24,7 @@
// mapnik
#include <mapnik/json/error_handler.hpp>
#include <mapnik/json/feature_collection_grammar.hpp>
#include <mapnik/json/feature_callback_grammar.hpp>
// spirit::qi
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
@ -78,45 +78,4 @@ feature_collection_grammar<Iterator,FeatureType, FeatureCallback,ErrorHandler>::
on_error<fail>(feature_collection, error_handler(_1, _2, _3, _4));
}
template <typename Iterator, typename FeatureType, typename FeatureCallback, typename ErrorHandler>
feature_grammar_callback<Iterator,FeatureType, FeatureCallback,ErrorHandler>::feature_grammar_callback(mapnik::transcoder const& tr)
: feature_grammar_callback::base_type(start,"start"),
feature_g(tr)
{
qi::lit_type lit;
qi::eps_type eps;
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
qi::_a_type _a;
qi::_r1_type _r1;
qi::_r2_type _r2;
qi::_r3_type _r3;
using phoenix::construct;
using phoenix::new_;
using phoenix::val;
using qi::on_error;
using qi::fail;
start = feature_from_geometry(_r1, _r2, _r3) | feature(_r1, _r2, _r3)
;
feature = eps[_a = phoenix::construct<mapnik::feature_ptr>(new_<mapnik::feature_impl>(_r1, _r2))]
>> feature_g(*_a)[on_feature(_r3,_a)]
;
feature_from_geometry =
eps[_a = phoenix::construct<mapnik::feature_ptr>(new_<mapnik::feature_impl>(_r1, _r2))]
>> geometry_g[set_geometry(*_a, _1)] [on_feature(_r3, _a)]
;
start.name("start");
feature.name("feature");
feature_from_geometry.name("feature-from-geometry");
feature_g.name("feature-grammar");
geometry_g.name("geometry-grammar");
on_error<fail>(feature, error_handler(_1, _2, _3, _4));
}
}}

View file

@ -44,6 +44,7 @@ else:
%(PLUGIN_NAME)s_featureset.cpp
%(PLUGIN_NAME)s_index_featureset.cpp
%(PLUGIN_NAME)s_memory_index_featureset.cpp
%(PLUGIN_NAME)s_extract_bounding_box_grammar.cpp
""" % locals()
)

View file

@ -51,7 +51,8 @@
#include <mapnik/make_unique.hpp>
#include <mapnik/geometry_adapters.hpp>
#include <mapnik/json/feature_collection_grammar.hpp>
#include <mapnik/json/extract_bounding_box_grammar_impl.hpp>
#include <mapnik/json/feature_callback_grammar.hpp>
#include <mapnik/json/extract_bounding_box_grammar.hpp>
#include <mapnik/util/fs.hpp>
#include <mapnik/util/spatial_index.hpp>
#include <mapnik/geom_util.hpp>

View file

@ -0,0 +1,28 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 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
*
*****************************************************************************/
#include <mapnik/json/feature_grammar.hpp>
#include <mapnik/json/extract_bounding_box_grammar_impl.hpp>
using iterator_type = char const*;
template struct mapnik::json::extract_bounding_box_grammar<iterator_type>;

View file

@ -25,5 +25,4 @@
#include <string>
using iterator_type = char const*;
template struct mapnik::json::feature_collection_grammar<iterator_type,mapnik::feature_impl, mapnik::json::default_feature_callback> ;
template struct mapnik::json::feature_grammar_callback<iterator_type,mapnik::feature_impl, mapnik::json::default_feature_callback> ;
template struct mapnik::json::feature_collection_grammar<iterator_type,mapnik::feature_impl, mapnik::json::default_feature_callback>;

View file

@ -39,7 +39,7 @@
#include <mapnik/json/positions_grammar.hpp>
#include <mapnik/json/extract_bounding_box_grammar_impl.hpp>
#include <mapnik/json/feature_collection_grammar_impl.hpp>
#include <mapnik/json/feature_callback_grammar_impl.hpp>
namespace {
struct feature_validate_callback