+ 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:
parent
03315ff893
commit
42bcf4ea98
3 changed files with 71 additions and 54 deletions
|
@ -40,7 +40,7 @@ bool to_wkt(std::string & wkt, mapnik::geometry_type const& geom)
|
||||||
{
|
{
|
||||||
typedef std::back_insert_iterator<std::string> sink_type;
|
typedef std::back_insert_iterator<std::string> sink_type;
|
||||||
sink_type sink(wkt);
|
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);
|
bool result = karma::generate(sink, generator, geom);
|
||||||
return result;
|
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;
|
typedef std::back_insert_iterator<std::string> sink_type;
|
||||||
sink_type sink(wkt);
|
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);
|
bool result = karma::generate(sink, generator, geom);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,6 @@
|
||||||
|
|
||||||
// mapnik
|
// mapnik
|
||||||
#include <mapnik/global.hpp>
|
#include <mapnik/global.hpp>
|
||||||
#include <mapnik/geometry.hpp>
|
|
||||||
|
|
||||||
// boost
|
// boost
|
||||||
#include <boost/tuple/tuple.hpp>
|
#include <boost/tuple/tuple.hpp>
|
||||||
|
@ -36,17 +35,17 @@
|
||||||
#include <boost/spirit/include/phoenix_function.hpp>
|
#include <boost/spirit/include/phoenix_function.hpp>
|
||||||
#include <boost/spirit/home/phoenix/statement/if.hpp>
|
#include <boost/spirit/home/phoenix/statement/if.hpp>
|
||||||
#include <boost/fusion/include/boost_tuple.hpp>
|
#include <boost/fusion/include/boost_tuple.hpp>
|
||||||
|
#include <boost/type_traits/remove_pointer.hpp>
|
||||||
|
|
||||||
//#define BOOST_SPIRIT_USE_PHOENIX_V3 1
|
//#define BOOST_SPIRIT_USE_PHOENIX_V3 1
|
||||||
|
|
||||||
namespace boost { namespace spirit { namespace traits {
|
namespace boost { namespace spirit { namespace traits {
|
||||||
|
|
||||||
// make gcc and darwin toolsets happy.
|
// make gcc and darwin toolsets happy.
|
||||||
template <>
|
//template <>
|
||||||
struct is_container<mapnik::geometry_container>
|
//struct is_container<mapnik::geometry_container>
|
||||||
: mpl::false_
|
// : mpl::false_
|
||||||
{};
|
//{};
|
||||||
|
|
||||||
}}}
|
}}}
|
||||||
|
|
||||||
|
@ -58,34 +57,41 @@ namespace phoenix = boost::phoenix;
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
|
template <typename Geometry>
|
||||||
struct get_type
|
struct get_type
|
||||||
{
|
{
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct result { typedef int type; };
|
struct result { typedef int type; };
|
||||||
|
|
||||||
int operator() (geometry_type const& geom) const
|
int operator() (Geometry const& geom) const
|
||||||
{
|
{
|
||||||
return static_cast<int>(geom.type());
|
return static_cast<int>(geom.type());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
struct get_first
|
struct get_first
|
||||||
{
|
{
|
||||||
template <typename T>
|
typedef T geometry_type;
|
||||||
struct result { typedef geometry_type::value_type const 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;
|
typename geometry_type::value_type coord;
|
||||||
boost::get<0>(coord) = geom.vertex(0,&boost::get<1>(coord),&boost::get<2>(coord));
|
geom.rewind(0);
|
||||||
|
boost::get<0>(coord) = geom.vertex(&boost::get<1>(coord),&boost::get<2>(coord));
|
||||||
return coord;
|
return coord;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
struct multi_geometry_
|
struct multi_geometry_
|
||||||
{
|
{
|
||||||
template <typename T>
|
typedef T geometry_container;
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
struct result { typedef bool type; };
|
struct result { typedef bool type; };
|
||||||
|
|
||||||
bool operator() (geometry_container const& geom) const
|
bool operator() (geometry_container const& geom) const
|
||||||
|
@ -94,9 +100,12 @@ struct multi_geometry_
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
struct multi_geometry_type
|
struct multi_geometry_type
|
||||||
{
|
{
|
||||||
template <typename T>
|
typedef T geometry_container;
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
struct result { typedef boost::tuple<unsigned,bool> type; };
|
struct result { typedef boost::tuple<unsigned,bool> type; };
|
||||||
|
|
||||||
boost::tuple<unsigned,bool> operator() (geometry_container const& geom) const;
|
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 :
|
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);
|
wkt_generator(bool single = false);
|
||||||
// rules
|
// rules
|
||||||
karma::rule<OutputIterator, geometry_type const& ()> wkt;
|
karma::rule<OutputIterator, geometry_type const& ()> wkt;
|
||||||
|
@ -126,33 +138,35 @@ struct wkt_generator :
|
||||||
|
|
||||||
karma::rule<OutputIterator, geometry_type const& ()> coords;
|
karma::rule<OutputIterator, geometry_type const& ()> coords;
|
||||||
karma::rule<OutputIterator, karma::locals<unsigned>, geometry_type const& ()> coords2;
|
karma::rule<OutputIterator, karma::locals<unsigned>, geometry_type const& ()> coords2;
|
||||||
karma::rule<OutputIterator, geometry_type::value_type ()> point_coord;
|
karma::rule<OutputIterator, coord_type ()> point_coord;
|
||||||
karma::rule<OutputIterator, geometry_type::value_type (unsigned& )> polygon_coord;
|
karma::rule<OutputIterator, coord_type (unsigned& )> polygon_coord;
|
||||||
|
|
||||||
// phoenix functions
|
// phoenix functions
|
||||||
phoenix::function<detail::get_type > _type;
|
phoenix::function<detail::get_type<geometry_type> > _type;
|
||||||
phoenix::function<detail::get_first> _first;
|
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 :
|
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();
|
wkt_multi_generator();
|
||||||
// rules
|
// rules
|
||||||
karma::rule<OutputIterator, karma::locals<boost::tuple<unsigned,bool> >, geometry_container const& ()> wkt;
|
karma::rule<OutputIterator, karma::locals<boost::tuple<unsigned,bool> >, GeometryContainer const& ()> wkt;
|
||||||
karma::rule<OutputIterator, geometry_container const& ()> geometry;
|
karma::rule<OutputIterator, GeometryContainer const& ()> geometry;
|
||||||
karma::rule<OutputIterator, geometry_type const& ()> single_geometry;
|
karma::rule<OutputIterator, geometry_type const& ()> single_geometry;
|
||||||
karma::rule<OutputIterator, geometry_container const& ()> multi_geometry;
|
karma::rule<OutputIterator, GeometryContainer const& ()> multi_geometry;
|
||||||
wkt_generator<OutputIterator> path;
|
wkt_generator<OutputIterator, geometry_type > path;
|
||||||
// phoenix
|
// phoenix
|
||||||
phoenix::function<detail::multi_geometry_> is_multi;
|
phoenix::function<detail::multi_geometry_<GeometryContainer> > is_multi;
|
||||||
phoenix::function<detail::multi_geometry_type> _multi_type;
|
phoenix::function<detail::multi_geometry_type<GeometryContainer> > _multi_type;
|
||||||
phoenix::function<detail::get_type > _type;
|
phoenix::function<detail::get_type<geometry_type> > _type;
|
||||||
//
|
//
|
||||||
karma::symbols<unsigned, char const*> geometry_types;
|
karma::symbols<unsigned, char const*> geometry_types;
|
||||||
};
|
};
|
||||||
|
|
|
@ -24,19 +24,22 @@
|
||||||
|
|
||||||
#if BOOST_VERSION >= 104700
|
#if BOOST_VERSION >= 104700
|
||||||
|
|
||||||
|
#include <mapnik/geometry.hpp>
|
||||||
#include <mapnik/util/geometry_wkt_generator.hpp>
|
#include <mapnik/util/geometry_wkt_generator.hpp>
|
||||||
#include <mapnik/util/vertex_iterator.hpp>
|
#include <mapnik/util/vertex_iterator.hpp>
|
||||||
#include <mapnik/util/container_adapter.hpp>
|
#include <mapnik/util/container_adapter.hpp>
|
||||||
|
|
||||||
namespace mapnik { namespace util {
|
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;
|
unsigned type = 0u;
|
||||||
bool collection = false;
|
bool collection = false;
|
||||||
|
|
||||||
geometry_container::const_iterator itr = geom.begin();
|
typename geometry_container::const_iterator itr = geom.begin();
|
||||||
geometry_container::const_iterator end = geom.end();
|
typename geometry_container::const_iterator end = geom.end();
|
||||||
|
|
||||||
for ( ; itr != end; ++itr)
|
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);
|
return boost::tuple<unsigned,bool>(type, collection);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputIterator>
|
template <typename OutputIterator, typename Geometry>
|
||||||
wkt_generator<OutputIterator>::wkt_generator(bool single)
|
wkt_generator<OutputIterator, Geometry>::wkt_generator(bool single)
|
||||||
: wkt_generator::base_type(wkt)
|
: wkt_generator::base_type(wkt)
|
||||||
{
|
{
|
||||||
using boost::spirit::karma::uint_;
|
using boost::spirit::karma::uint_;
|
||||||
|
@ -86,15 +89,15 @@ wkt_generator<OutputIterator>::wkt_generator(bool single)
|
||||||
<< lit("))")
|
<< lit("))")
|
||||||
;
|
;
|
||||||
|
|
||||||
point_coord = &uint_ << coord_type << lit(' ') << coord_type
|
point_coord = &uint_ << coordinate << lit(' ') << coordinate
|
||||||
;
|
;
|
||||||
|
|
||||||
polygon_coord %= ( &uint_(mapnik::SEG_MOVETO) << eps[_r1 += 1]
|
polygon_coord %= ( &uint_(mapnik::SEG_MOVETO) << eps[_r1 += 1]
|
||||||
<< string[ if_ (_r1 > 1) [_1 = "),("]
|
<< string[ if_ (_r1 > 1) [_1 = "),("]
|
||||||
.else_[_1 = "("] ] | &uint_ << ",")
|
.else_[_1 = "("] ] | &uint_ << ",")
|
||||||
<< coord_type
|
<< coordinate
|
||||||
<< lit(' ')
|
<< lit(' ')
|
||||||
<< coord_type
|
<< coordinate
|
||||||
;
|
;
|
||||||
|
|
||||||
coords2 %= *polygon_coord(_a)
|
coords2 %= *polygon_coord(_a)
|
||||||
|
@ -104,8 +107,8 @@ wkt_generator<OutputIterator>::wkt_generator(bool single)
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputIterator>
|
template <typename OutputIterator, typename GeometryContainer>
|
||||||
wkt_multi_generator<OutputIterator>::wkt_multi_generator()
|
wkt_multi_generator<OutputIterator, GeometryContainer>::wkt_multi_generator()
|
||||||
: wkt_multi_generator::base_type(wkt)
|
: wkt_multi_generator::base_type(wkt)
|
||||||
{
|
{
|
||||||
using boost::spirit::karma::lit;
|
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_generator<std::back_insert_iterator<std::string>, mapnik::geometry_type>;
|
||||||
template struct mapnik::util::wkt_multi_generator<std::back_insert_iterator<std::string> >;
|
template struct mapnik::util::wkt_multi_generator<std::back_insert_iterator<std::string>, mapnik::geometry_container >;
|
||||||
|
|
||||||
|
|
||||||
}}
|
}}
|
||||||
|
|
Loading…
Reference in a new issue