/usr/include/mapnik/json/geometry_generator_grammar.hpp is in libmapnik-dev 2.2.0+ds1-7+b2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | /*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2012 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_JSON_GEOMETRY_GENERATOR_GRAMMAR_HPP
#define MAPNIK_JSON_GEOMETRY_GENERATOR_GRAMMAR_HPP
// mapnik
#include <mapnik/global.hpp>
#include <mapnik/geometry.hpp>
#include <mapnik/util/path_iterator.hpp>
#include <mapnik/util/container_adapter.hpp>
#include <mapnik/vertex.hpp> // for CommandType::SEG_MOVETO
// boost
#include <boost/tuple/tuple.hpp>
#include <boost/spirit/include/karma.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_function.hpp>
#include <boost/spirit/include/phoenix_statement.hpp>
#include <boost/fusion/include/boost_tuple.hpp>
#include <boost/math/special_functions/trunc.hpp> // trunc to avoid needing C++11
//#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_
{};
}}}
namespace mapnik { namespace json {
namespace karma = boost::spirit::karma;
namespace phoenix = boost::phoenix;
namespace {
struct get_type
{
template <typename T>
struct result { typedef int type; };
int operator() (geometry_type const& geom) const
{
return static_cast<int>(geom.type());
}
};
struct get_first
{
template <typename T>
struct result { typedef geometry_type::value_type const type; };
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));
return coord;
}
};
struct multi_geometry_type
{
template <typename T>
struct result { typedef boost::tuple<unsigned,bool> type; };
boost::tuple<unsigned,bool> operator() (geometry_container const& geom) const
{
unsigned type = 0u;
bool collection = false;
geometry_container::const_iterator itr = geom.begin();
geometry_container::const_iterator end = geom.end();
for ( ; itr != end; ++itr)
{
if (type != 0u && itr->type() != type)
{
collection = true;
break;
}
type = itr->type();
}
if (geom.size() > 1) type +=3;
return boost::tuple<unsigned,bool>(type, collection);
}
};
template <typename T>
struct json_coordinate_policy : karma::real_policies<T>
{
typedef boost::spirit::karma::real_policies<T> base_type;
static int floatfield(T n) { return base_type::fmtflags::fixed; }
static unsigned precision(T n)
{
if (n == 0.0) return 0;
using namespace boost::spirit;
return static_cast<unsigned>(15 - boost::math::trunc(log10(traits::get_absolute_value(n))));
}
template <typename OutputIterator>
static bool dot(OutputIterator& sink, T n, unsigned precision)
{
if (n == 0) return true;
return base_type::dot(sink, n, precision);
}
template <typename OutputIterator>
static bool fraction_part(OutputIterator& sink, T n
, unsigned adjprec, unsigned precision)
{
if (n == 0) return true;
return base_type::fraction_part(sink, n, adjprec, precision);
}
};
}
template <typename OutputIterator>
struct geometry_generator_grammar :
karma::grammar<OutputIterator, geometry_type const& ()>
{
geometry_generator_grammar()
: geometry_generator_grammar::base_type(coordinates)
{
boost::spirit::karma::uint_type uint_;
boost::spirit::karma::_val_type _val;
boost::spirit::karma::_1_type _1;
boost::spirit::karma::lit_type lit;
boost::spirit::karma::_a_type _a;
boost::spirit::karma::_r1_type _r1;
boost::spirit::karma::eps_type eps;
boost::spirit::karma::string_type kstring;
coordinates = point | linestring | polygon
;
point = &uint_(mapnik::Point)[_1 = _type(_val)]
<< point_coord [_1 = _first(_val)]
;
linestring = &uint_(mapnik::LineString)[_1 = _type(_val)]
<< lit('[')
<< coords
<< lit(']')
;
polygon = &uint_(mapnik::Polygon)[_1 = _type(_val)]
<< lit('[')
<< coords2
<< lit("]]")
;
point_coord = &uint_
<< lit('[')
<< coord_type << lit(',') << coord_type
<< lit("]")
;
polygon_coord %= ( &uint_(mapnik::SEG_MOVETO) << eps[_r1 += 1]
<< kstring[ if_ (_r1 > 1) [_1 = "],["]
.else_[_1 = '[' ]] | &uint_ << lit(','))
<< lit('[') << coord_type
<< lit(',')
<< coord_type << lit(']')
;
coords2 %= *polygon_coord(_a)
;
coords = point_coord % lit(',')
;
}
// rules
karma::rule<OutputIterator, geometry_type const& ()> coordinates;
karma::rule<OutputIterator, geometry_type const& ()> point;
karma::rule<OutputIterator, geometry_type const& ()> linestring;
karma::rule<OutputIterator, geometry_type const& ()> polygon;
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;
// phoenix functions
phoenix::function<get_type > _type;
phoenix::function<get_first> _first;
//
karma::real_generator<double, json_coordinate_policy<double> > coord_type;
};
template <typename OutputIterator>
struct multi_geometry_generator_grammar :
karma::grammar<OutputIterator, karma::locals<boost::tuple<unsigned,bool> >,
geometry_container const& ()>
{
multi_geometry_generator_grammar()
: multi_geometry_generator_grammar::base_type(start)
{
boost::spirit::karma::_val_type _val;
boost::spirit::karma::_1_type _1;
boost::spirit::karma::lit_type lit;
boost::spirit::karma::_a_type _a;
boost::spirit::karma::_r1_type _r1;
boost::spirit::karma::eps_type eps;
boost::spirit::karma::string_type kstring;
geometry_types.add
(mapnik::Point,"\"Point\"")
(mapnik::LineString,"\"LineString\"")
(mapnik::Polygon,"\"Polygon\"")
(mapnik::Point + 3,"\"MultiPoint\"")
(mapnik::LineString + 3,"\"MultiLineString\"")
(mapnik::Polygon + 3,"\"MultiPolygon\"")
;
start %= ( eps(phoenix::at_c<1>(_a))[_a = _multi_type(_val)]
<< lit("{\"type\":\"GeometryCollection\",\"geometries\":[")
<< geometry_collection << lit("]}")
|
geometry)
;
geometry_collection = -(geometry2 % lit(','))
;
geometry = (lit("{\"type\":")
<< geometry_types[_1 = phoenix::at_c<0>(_a)][_a = _multi_type(_val)]
<< lit(",\"coordinates\":")
<< kstring[ if_ (phoenix::at_c<0>(_a) > 3) [_1 = '[']]
<< coordinates
<< kstring[ if_ (phoenix::at_c<0>(_a) > 3) [_1 = ']']]
<< lit('}')) | lit("null")
;
geometry2 = lit("{\"type\":")
<< geometry_types[_1 = _a][_a = type_(_val)]
<< lit(",\"coordinates\":")
<< path
<< lit('}')
;
coordinates %= path % lit(',')
;
}
// rules
karma::rule<OutputIterator, karma::locals<boost::tuple<unsigned,bool> >,
geometry_container const&()> start;
karma::rule<OutputIterator, karma::locals<boost::tuple<unsigned,bool> >,
geometry_container const&()> geometry_collection;
karma::rule<OutputIterator, karma::locals<boost::tuple<unsigned,bool> >,
geometry_container const&()> geometry;
karma::rule<OutputIterator, karma::locals<unsigned>,
geometry_type const&()> geometry2;
karma::rule<OutputIterator, geometry_container const&()> coordinates;
geometry_generator_grammar<OutputIterator> path;
// phoenix
phoenix::function<multi_geometry_type> _multi_type;
phoenix::function<get_type > type_;
// symbols table
karma::symbols<unsigned, char const*> geometry_types;
};
}}
#endif // MAPNIK_JSON_GEOMETRY_GENERATOR_GRAMMAR_HPP
|