+ make WKT generator templated on geometry type - the goal is to be able

to use generator for any type that implements 'vertex' concept
This commit is contained in:
artemp 2012-09-27 16:04:29 +01:00
parent 03315ff893
commit 42bcf4ea98
3 changed files with 71 additions and 54 deletions

View file

@ -40,7 +40,7 @@ bool to_wkt(std::string & wkt, mapnik::geometry_type const& geom)
{
typedef std::back_insert_iterator<std::string> sink_type;
sink_type sink(wkt);
wkt_generator<sink_type> generator(true);
wkt_generator<sink_type, mapnik::geometry_type> generator(true);
bool result = karma::generate(sink, generator, geom);
return result;
}
@ -49,7 +49,7 @@ bool to_wkt(std::string & wkt, mapnik::geometry_container const& geom)
{
typedef std::back_insert_iterator<std::string> sink_type;
sink_type sink(wkt);
wkt_multi_generator<sink_type> generator;
wkt_multi_generator<sink_type, mapnik::geometry_container> generator;
bool result = karma::generate(sink, generator, geom);
return result;
}

View file

@ -25,7 +25,6 @@
// mapnik
#include <mapnik/global.hpp>
#include <mapnik/geometry.hpp>
// boost
#include <boost/tuple/tuple.hpp>
@ -36,17 +35,17 @@
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/home/phoenix/statement/if.hpp>
#include <boost/fusion/include/boost_tuple.hpp>
#include <boost/type_traits/remove_pointer.hpp>
//#define BOOST_SPIRIT_USE_PHOENIX_V3 1
namespace boost { namespace spirit { namespace traits {
// make gcc and darwin toolsets happy.
template <>
struct is_container<mapnik::geometry_container>
: mpl::false_
{};
//template <>
//struct is_container<mapnik::geometry_container>
// : mpl::false_
//{};
}}}
@ -58,34 +57,41 @@ namespace phoenix = boost::phoenix;
namespace detail {
template <typename Geometry>
struct get_type
{
template <typename T>
struct result { typedef int type; };
int operator() (geometry_type const& geom) const
int operator() (Geometry const& geom) const
{
return static_cast<int>(geom.type());
}
};
template <typename T>
struct get_first
{
template <typename T>
struct result { typedef geometry_type::value_type const type; };
typedef T geometry_type;
geometry_type::value_type const operator() (geometry_type const& geom) const
template <typename U>
struct result { typedef typename geometry_type::value_type const type; };
typename geometry_type::value_type const operator() (geometry_type const& geom) const
{
geometry_type::value_type coord;
boost::get<0>(coord) = geom.vertex(0,&boost::get<1>(coord),&boost::get<2>(coord));
typename geometry_type::value_type coord;
geom.rewind(0);
boost::get<0>(coord) = geom.vertex(&boost::get<1>(coord),&boost::get<2>(coord));
return coord;
}
};
template <typename T>
struct multi_geometry_
{
template <typename T>
typedef T geometry_container;
template <typename U>
struct result { typedef bool type; };
bool operator() (geometry_container const& geom) const
@ -94,9 +100,12 @@ struct multi_geometry_
}
};
template <typename T>
struct multi_geometry_type
{
template <typename T>
typedef T geometry_container;
template <typename U>
struct result { typedef boost::tuple<unsigned,bool> type; };
boost::tuple<unsigned,bool> operator() (geometry_container const& geom) const;
@ -113,10 +122,13 @@ struct wkt_coordinate_policy : karma::real_policies<T>
}
template <typename OutputIterator>
template <typename OutputIterator, typename Geometry>
struct wkt_generator :
karma::grammar<OutputIterator, geometry_type const& ()>
karma::grammar<OutputIterator, Geometry const& ()>
{
typedef Geometry geometry_type;
typedef typename boost::remove_pointer<typename geometry_type::value_type>::type coord_type;
wkt_generator(bool single = false);
// rules
karma::rule<OutputIterator, geometry_type const& ()> wkt;
@ -126,33 +138,35 @@ struct wkt_generator :
karma::rule<OutputIterator, geometry_type const& ()> coords;
karma::rule<OutputIterator, karma::locals<unsigned>, geometry_type const& ()> coords2;
karma::rule<OutputIterator, geometry_type::value_type ()> point_coord;
karma::rule<OutputIterator, geometry_type::value_type (unsigned& )> polygon_coord;
karma::rule<OutputIterator, coord_type ()> point_coord;
karma::rule<OutputIterator, coord_type (unsigned& )> polygon_coord;
// phoenix functions
phoenix::function<detail::get_type > _type;
phoenix::function<detail::get_first> _first;
phoenix::function<detail::get_type<geometry_type> > _type;
phoenix::function<detail::get_first<geometry_type> > _first;
//
karma::real_generator<double, detail::wkt_coordinate_policy<double> > coord_type;
karma::real_generator<double, detail::wkt_coordinate_policy<double> > coordinate;
};
template <typename OutputIterator>
template <typename OutputIterator, typename GeometryContainer>
struct wkt_multi_generator :
karma::grammar<OutputIterator, karma::locals< boost::tuple<unsigned,bool> >, geometry_container const& ()>
karma::grammar<OutputIterator, karma::locals< boost::tuple<unsigned,bool> >, GeometryContainer const& ()>
{
typedef GeometryContainer geometry_contaner;
typedef boost::remove_pointer<typename geometry_container::value_type>::type geometry_type;
wkt_multi_generator();
// rules
karma::rule<OutputIterator, karma::locals<boost::tuple<unsigned,bool> >, geometry_container const& ()> wkt;
karma::rule<OutputIterator, geometry_container const& ()> geometry;
karma::rule<OutputIterator, karma::locals<boost::tuple<unsigned,bool> >, GeometryContainer const& ()> wkt;
karma::rule<OutputIterator, GeometryContainer const& ()> geometry;
karma::rule<OutputIterator, geometry_type const& ()> single_geometry;
karma::rule<OutputIterator, geometry_container const& ()> multi_geometry;
wkt_generator<OutputIterator> path;
karma::rule<OutputIterator, GeometryContainer const& ()> multi_geometry;
wkt_generator<OutputIterator, geometry_type > path;
// phoenix
phoenix::function<detail::multi_geometry_> is_multi;
phoenix::function<detail::multi_geometry_type> _multi_type;
phoenix::function<detail::get_type > _type;
phoenix::function<detail::multi_geometry_<GeometryContainer> > is_multi;
phoenix::function<detail::multi_geometry_type<GeometryContainer> > _multi_type;
phoenix::function<detail::get_type<geometry_type> > _type;
//
karma::symbols<unsigned, char const*> geometry_types;
};

View file

@ -24,19 +24,22 @@
#if BOOST_VERSION >= 104700
#include <mapnik/geometry.hpp>
#include <mapnik/util/geometry_wkt_generator.hpp>
#include <mapnik/util/vertex_iterator.hpp>
#include <mapnik/util/container_adapter.hpp>
namespace mapnik { namespace util {
boost::tuple<unsigned,bool> detail::multi_geometry_type::operator() (geometry_container const& geom) const
template <typename T>
boost::tuple<unsigned,bool> detail::multi_geometry_type<T>::operator() (T const& geom) const
{
typedef T geometry_container;
unsigned type = 0u;
bool collection = false;
geometry_container::const_iterator itr = geom.begin();
geometry_container::const_iterator end = geom.end();
typename geometry_container::const_iterator itr = geom.begin();
typename geometry_container::const_iterator end = geom.end();
for ( ; itr != end; ++itr)
{
@ -50,8 +53,8 @@ boost::tuple<unsigned,bool> detail::multi_geometry_type::operator() (geometry_co
return boost::tuple<unsigned,bool>(type, collection);
}
template <typename OutputIterator>
wkt_generator<OutputIterator>::wkt_generator(bool single)
template <typename OutputIterator, typename Geometry>
wkt_generator<OutputIterator, Geometry>::wkt_generator(bool single)
: wkt_generator::base_type(wkt)
{
using boost::spirit::karma::uint_;
@ -86,15 +89,15 @@ wkt_generator<OutputIterator>::wkt_generator(bool single)
<< lit("))")
;
point_coord = &uint_ << coord_type << lit(' ') << coord_type
point_coord = &uint_ << coordinate << lit(' ') << coordinate
;
polygon_coord %= ( &uint_(mapnik::SEG_MOVETO) << eps[_r1 += 1]
<< string[ if_ (_r1 > 1) [_1 = "),("]
.else_[_1 = "("] ] | &uint_ << ",")
<< coord_type
<< coordinate
<< lit(' ')
<< coord_type
<< coordinate
;
coords2 %= *polygon_coord(_a)
@ -104,8 +107,8 @@ wkt_generator<OutputIterator>::wkt_generator(bool single)
;
}
template <typename OutputIterator>
wkt_multi_generator<OutputIterator>::wkt_multi_generator()
template <typename OutputIterator, typename GeometryContainer>
wkt_multi_generator<OutputIterator, GeometryContainer>::wkt_multi_generator()
: wkt_multi_generator::base_type(wkt)
{
using boost::spirit::karma::lit;
@ -138,8 +141,8 @@ wkt_multi_generator<OutputIterator>::wkt_multi_generator()
}
template struct mapnik::util::wkt_generator<std::back_insert_iterator<std::string> >;
template struct mapnik::util::wkt_multi_generator<std::back_insert_iterator<std::string> >;
template struct mapnik::util::wkt_generator<std::back_insert_iterator<std::string>, mapnik::geometry_type>;
template struct mapnik::util::wkt_multi_generator<std::back_insert_iterator<std::string>, mapnik::geometry_container >;
}}