wkt_grammar - update to use mapnik-geometry and optimise(minimise) memory allocations

This commit is contained in:
artemp 2015-03-11 11:55:43 +01:00
parent c3284aa958
commit f3edf2dfc0
4 changed files with 127 additions and 137 deletions

View file

@ -2,7 +2,7 @@
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 Artem Pavlenko
* 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
@ -23,6 +23,10 @@
#ifndef MAPNIK_WKT_GRAMMAR_HPP
#define MAPNIK_WKT_GRAMMAR_HPP
// mapnik
#include <mapnik/geometry_impl.hpp>
#include <mapnik/geometry_adapters.hpp>
#include <mapnik/geometry_fusion_adapted.hpp>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
@ -31,93 +35,84 @@
#include <boost/spirit/include/phoenix_function.hpp>
#pragma GCC diagnostic pop
// mapnik
#include <mapnik/geometry.hpp>
#include <mapnik/vertex.hpp>
namespace mapnik { namespace wkt {
using namespace boost::spirit;
struct push_vertex
{
template <typename T>
struct result
{
using type = void;
};
namespace detail {
template <typename T0,typename T1, typename T2, typename T3>
void operator() (T0 c, T1 path, T2 x, T3 y) const
struct assign
{
using result_type = void;
template <typename T0, typename T1>
void operator() (T0 & geom, T1 && obj) const
{
BOOST_ASSERT( path!=0 );
path->push_vertex(x,y,c);
geom = std::move(obj);
}
};
struct close_path
struct move_part
{
template <typename T>
struct result
using result_type = void;
template <typename Geometry, typename Part>
void operator() (Geometry & geom, Part && part) const
{
using type = void;
};
template <typename T>
void operator() (T path) const
{
BOOST_ASSERT( path!=0 );
path->close_path();
geom.push_back(std::move(part));
}
};
struct cleanup
struct set_exterior
{
template <typename T0>
struct result
using result_type = void;
template <typename Polygon, typename Ring>
void operator() (Polygon & poly, Ring && ring) const
{
using type = void;
};
template <typename T0>
void operator() (T0 & path) const
{
if (path) delete path,path=0;
poly.set_exterior_ring(std::move(ring));
}
};
struct add_hole
{
using result_type = void;
template <typename Polygon, typename Ring>
void operator() (Polygon & poly, Ring && ring) const
{
poly.add_hole(std::move(ring));
}
};
}
template <typename Iterator>
struct wkt_grammar : qi::grammar<Iterator, mapnik::geometry_container() , ascii::space_type>
struct wkt_grammar : qi::grammar<Iterator, void(mapnik::new_geometry::geometry&) , ascii::space_type>
{
wkt_grammar();
qi::rule<Iterator,mapnik::geometry_container(),ascii::space_type> geometry_tagged_text;
qi::rule<Iterator,qi::locals<geometry_type*>,mapnik::geometry_container(),ascii::space_type> point_tagged_text;
qi::rule<Iterator,qi::locals<geometry_type*>,mapnik::geometry_container(),ascii::space_type> linestring_tagged_text;
qi::rule<Iterator,qi::locals<geometry_type*>,mapnik::geometry_container(),ascii::space_type> polygon_tagged_text;
qi::rule<Iterator,mapnik::geometry_container(),ascii::space_type> multipoint_tagged_text;
qi::rule<Iterator,mapnik::geometry_container(),ascii::space_type> multilinestring_tagged_text;
qi::rule<Iterator,mapnik::geometry_container(),ascii::space_type> multipolygon_tagged_text;
qi::rule<Iterator,void(geometry_type*),ascii::space_type> point_text;
qi::rule<Iterator,void(geometry_type*),ascii::space_type> linestring_text;
qi::rule<Iterator,void(geometry_type*),ascii::space_type> polygon_text;
qi::rule<Iterator, qi::locals<geometry_type*>, mapnik::geometry_container(),ascii::space_type> multipoint_text;
qi::rule<Iterator, qi::locals<geometry_type*>, mapnik::geometry_container(),ascii::space_type> multilinestring_text;
qi::rule<Iterator, qi::locals<geometry_type*>, mapnik::geometry_container(),ascii::space_type> multipolygon_text;
qi::rule<Iterator,void(CommandType,geometry_type*),ascii::space_type> point;
qi::rule<Iterator,qi::locals<CommandType>,void(geometry_type*),ascii::space_type> points;
qi::rule<Iterator, void(mapnik::new_geometry::geometry&), ascii::space_type> geometry_tagged_text;
qi::rule<Iterator, void(mapnik::new_geometry::geometry&), ascii::space_type> point_tagged_text;
qi::rule<Iterator, void(mapnik::new_geometry::geometry&), ascii::space_type> linestring_tagged_text;
qi::rule<Iterator, void(mapnik::new_geometry::geometry&), ascii::space_type> polygon_tagged_text;
qi::rule<Iterator, void(mapnik::new_geometry::geometry&), ascii::space_type> multipoint_tagged_text;
qi::rule<Iterator, void(mapnik::new_geometry::geometry&), ascii::space_type> multilinestring_tagged_text;
qi::rule<Iterator, void(mapnik::new_geometry::geometry&), ascii::space_type> multipolygon_tagged_text;
qi::rule<Iterator, void(mapnik::new_geometry::geometry&), ascii::space_type> geometrycollection_tagged_text;
qi::rule<Iterator, mapnik::new_geometry::point(), ascii::space_type> point_text;
qi::rule<Iterator, mapnik::new_geometry::line_string(), ascii::space_type> linestring_text;
qi::rule<Iterator, mapnik::new_geometry::linear_ring(), ascii::space_type> linearring_text;
qi::rule<Iterator, mapnik::new_geometry::polygon(), ascii::space_type> polygon_text;
qi::rule<Iterator, mapnik::new_geometry::multi_point(), ascii::space_type> multipoint_text;
qi::rule<Iterator, mapnik::new_geometry::multi_line_string(), ascii::space_type> multilinestring_text;
qi::rule<Iterator, mapnik::new_geometry::multi_polygon(), ascii::space_type> multipolygon_text;
qi::rule<Iterator, qi::locals<mapnik::new_geometry::geometry>,
mapnik::new_geometry::geometry_collection(), ascii::space_type> geometrycollection_text;
qi::rule<Iterator, mapnik::new_geometry::point(), ascii::space_type> point;
qi::rule<Iterator, mapnik::new_geometry::line_string(), ascii::space_type> points;
qi::rule<Iterator, mapnik::new_geometry::linear_ring(), ascii::space_type> ring_points;
qi::rule<Iterator,ascii::space_type> empty_set;
boost::phoenix::function<push_vertex> push_vertex_;
boost::phoenix::function<close_path> close_path_;
boost::phoenix::function<cleanup> cleanup_;
};
template <typename Iterator>
struct wkt_collection_grammar : qi::grammar<Iterator, mapnik::geometry_container(), ascii::space_type>
{
wkt_collection_grammar();
qi::rule<Iterator,mapnik::geometry_container(),ascii::space_type> start;
wkt_grammar<Iterator> wkt;
boost::phoenix::function<detail::assign> assign;
boost::phoenix::function<detail::move_part> move_part;
boost::phoenix::function<detail::set_exterior> set_exterior;
boost::phoenix::function<detail::add_hole> add_hole;
};
}}

View file

@ -2,7 +2,7 @@
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 Artem Pavlenko
* 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
@ -23,10 +23,10 @@
#include <mapnik/config.hpp>
#include <mapnik/wkt/wkt_grammar.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/phoenix/object/construct.hpp>
namespace mapnik { namespace wkt {
@ -36,115 +36,113 @@ template <typename Iterator>
wkt_grammar<Iterator>::wkt_grammar()
: wkt_grammar::base_type(geometry_tagged_text)
{
qi::_r1_type _r1;
qi::_r2_type _r2;
qi::_pass_type _pass;
qi::eps_type eps;
qi::_r1_type _r1;
qi::_val_type _val;
qi::lit_type lit;
qi::no_case_type no_case;
qi::double_type double_;
qi::_1_type _1;
qi::_2_type _2;
qi::_a_type _a;
using boost::phoenix::push_back;
using boost::phoenix::new_;
geometry_tagged_text = point_tagged_text
| linestring_tagged_text
| polygon_tagged_text
| multipoint_tagged_text
| multilinestring_tagged_text
| multipolygon_tagged_text
using boost::phoenix::construct;
using boost::phoenix::at_c;
geometry_tagged_text = point_tagged_text(_r1)
| linestring_tagged_text(_r1)
| polygon_tagged_text(_r1)
| multipoint_tagged_text(_r1)
| multilinestring_tagged_text(_r1)
| multipolygon_tagged_text(_r1)
| geometrycollection_tagged_text(_r1)
;
// <point tagged text> ::= point <point text>
point_tagged_text = no_case[lit("POINT")] [ _a = new_<geometry_type>(geometry_type::types::Point) ]
>> ( point_text(_a) [push_back(_val,_a)]
| eps[cleanup_(_a)][_pass = false])
point_tagged_text = no_case[lit("POINT")] >> point_text[assign(_r1,_1)]
;
// <point text> ::= <empty set> | <left paren> <point> <right paren>
point_text = (lit("(") >> point(SEG_MOVETO,_r1) >> lit(')'))
point_text = (lit("(") >> point >> lit(')'))
| empty_set
;
// <linestring tagged text> ::= linestring <linestring text>
linestring_tagged_text = no_case[lit("LINESTRING")] [ _a = new_<geometry_type>(geometry_type::types::LineString) ]
>> (linestring_text(_a)[push_back(_val,_a)]
| eps[cleanup_(_a)][_pass = false])
//<linestring tagged text> ::= linestring <linestring text>
linestring_tagged_text = no_case[lit("LINESTRING")]
>> linestring_text[assign(_r1,_1)]
;
// <linestring text> ::= <empty set> | <left paren> <point> {<comma> <point>}* <right paren>
linestring_text = points(_r1) | empty_set
linestring_text = points | empty_set
;
// <polygon tagged text> ::= polygon <polygon text>
polygon_tagged_text = no_case[lit("POLYGON")] [ _a = new_<geometry_type>(geometry_type::types::Polygon) ]
>> ( polygon_text(_a)[push_back(_val,_a)]
| eps[cleanup_(_a)][_pass = false])
polygon_tagged_text = no_case[lit("POLYGON")]
>> polygon_text[assign(_r1,_1)]
;
// <polygon text> ::= <empty set> | <left paren> <linestring text> {<comma> <linestring text>}* <right paren>
polygon_text = (lit('(') >> linestring_text(_r1)[close_path_(_r1)] % lit(',') >> lit(')')) | empty_set;
polygon_text =
(lit('(') >> linearring_text[set_exterior(_val,_1)] >> *(lit(',') >> linearring_text[add_hole(_val,_1)]) >> lit(')'))
|
empty_set
;
linearring_text = ring_points | empty_set
;
//<multipoint tagged text> ::= multipoint <multipoint text>
multipoint_tagged_text = no_case[lit("MULTIPOINT")]
>> multipoint_text
>> multipoint_text[assign(_r1,_1)]
;
// <multipoint text> ::= <empty set> | <left paren> <point text> {<comma> <point text>}* <right paren>
multipoint_text = (lit('(')
>> ((eps[_a = new_<geometry_type>(geometry_type::types::Point)]
>> (point_text(_a) | empty_set) [push_back(_val,_a)]
| eps[cleanup_(_a)][_pass = false]) % lit(','))
>> lit(')')) | empty_set
>> point_text % lit(',')
>> lit(')'))
|
empty_set
;
// <multilinestring tagged text> ::= multilinestring <multilinestring text>
multilinestring_tagged_text = no_case[lit("MULTILINESTRING")]
>> multilinestring_text ;
>> multilinestring_text[assign(_r1,_1)] ;
// <multilinestring text> ::= <empty set> | <left paren> <linestring text> {<comma> <linestring text>}* <right paren>
multilinestring_text = (lit('(')
>> ((eps[_a = new_<geometry_type>(geometry_type::types::LineString)]
>> ( points(_a)[push_back(_val,_a)]
| eps[cleanup_(_a)][_pass = false]))
% lit(','))
>> points[move_part(_val,_1)] % lit(',')
>> lit(')')) | empty_set;
// <multipolygon tagged text> ::= multipolygon <multipolygon text>
multipolygon_tagged_text = no_case[lit("MULTIPOLYGON")]
>> multipolygon_text ;
// <multipolygon text> ::= <empty set> | <left paren> <polygon text> {<comma> <polygon text>}* <right paren>
>> multipolygon_text[assign(_r1,_1)] ;
//<multipolygon text> ::= <empty set> | <left paren> <polygon text> {<comma> <polygon text>}* <right paren>
multipolygon_text = (lit('(')
>> ((eps[_a = new_<geometry_type>(geometry_type::types::Polygon)]
>> ( polygon_text(_a)[push_back(_val,_a)]
| eps[cleanup_(_a)][_pass = false]))
% lit(','))
>> lit(')')) | empty_set;
>> polygon_text[move_part(_val,_1)] % lit(',')
>> lit(')'))
|
empty_set;
// geometry collection tagged text
geometrycollection_tagged_text = no_case[lit("GEOMETRYCOLLECTION")]
>> geometrycollection_text[assign(_r1,_1)]
;
// geometry collection text
geometrycollection_text = (lit('(')
>> ( eps[_a = construct<new_geometry::geometry>()]
>> geometry_tagged_text(_a)[move_part(_val,_a)] % lit(','))
>> lit(')'))
|
empty_set;
// points
points = lit('(')[_a = SEG_MOVETO] >> point (_a,_r1) % lit(',') [_a = SEG_LINETO] >> lit(')');
points = lit('(') >> point % lit(',') >> lit(')')
;
// ring points
ring_points = lit('(') >> point % lit(',') >> lit(')')
;
// point
point = (double_ >> double_) [push_vertex_(_r1,_r2,_1,_2)];
point = double_ >> double_
;
// <empty set>
empty_set = no_case[lit("EMPTY")];
}
template <typename Iterator>
wkt_collection_grammar<Iterator>::wkt_collection_grammar()
: wkt_collection_grammar::base_type(start)
{
qi::lit_type lit;
qi::no_case_type no_case;
start = wkt | no_case[lit("GEOMETRYCOLLECTION")]
>> (lit("(") >> wkt % lit(",") >> lit(")"));
}
}}

View file

@ -20,16 +20,14 @@
*
*****************************************************************************/
#include <mapnik/geometry.hpp>
#include <mapnik/geometry_impl.hpp>
#include <mapnik/wkt/wkt_generator_grammar_impl.hpp>
#include <string>
namespace mapnik { namespace wkt {
#if 0
//using sink_type = std::back_insert_iterator<std::string>;
//template struct wkt_generator<sink_type, mapnik::geometry_type>;
//template struct wkt_multi_generator<sink_type, mapnik::geometry_container>;
using sink_type = std::back_insert_iterator<std::string>;
template struct wkt_generator<sink_type, mapnik::geometry_type>;
template struct wkt_multi_generator<sink_type, mapnik::geometry_container>;
#endif
}}

View file

@ -2,7 +2,7 @@
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 Artem Pavlenko
* 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
@ -20,13 +20,12 @@
*
*****************************************************************************/
#if 0 // FIXME
#include <mapnik/wkt/wkt_grammar_impl.hpp>
#include <string>
namespace mapnik { namespace wkt {
using iterator_type = std::string::const_iterator;
template struct wkt_collection_grammar<iterator_type>;
}}
#endif
using iterator_type = std::string::const_iterator;
template struct wkt_grammar<iterator_type>;
}}