read.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2014, 2015, 2018.
  7. // Modifications copyright (c) 2014-2018 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_IO_WKT_READ_HPP
  15. #define BOOST_GEOMETRY_IO_WKT_READ_HPP
  16. #include <cstddef>
  17. #include <string>
  18. #include <boost/lexical_cast.hpp>
  19. #include <boost/tokenizer.hpp>
  20. #include <boost/algorithm/string.hpp>
  21. #include <boost/mpl/if.hpp>
  22. #include <boost/range/begin.hpp>
  23. #include <boost/range/end.hpp>
  24. #include <boost/range/size.hpp>
  25. #include <boost/range/value_type.hpp>
  26. #include <boost/throw_exception.hpp>
  27. #include <boost/type_traits/is_same.hpp>
  28. #include <boost/type_traits/remove_reference.hpp>
  29. #include <boost/geometry/algorithms/assign.hpp>
  30. #include <boost/geometry/algorithms/append.hpp>
  31. #include <boost/geometry/algorithms/clear.hpp>
  32. #include <boost/geometry/algorithms/detail/disjoint/point_point.hpp>
  33. #include <boost/geometry/core/access.hpp>
  34. #include <boost/geometry/core/coordinate_dimension.hpp>
  35. #include <boost/geometry/core/exception.hpp>
  36. #include <boost/geometry/core/exterior_ring.hpp>
  37. #include <boost/geometry/core/geometry_id.hpp>
  38. #include <boost/geometry/core/interior_rings.hpp>
  39. #include <boost/geometry/core/mutable_range.hpp>
  40. #include <boost/geometry/core/point_type.hpp>
  41. #include <boost/geometry/core/tag_cast.hpp>
  42. #include <boost/geometry/core/tags.hpp>
  43. #include <boost/geometry/geometries/concepts/check.hpp>
  44. #include <boost/geometry/util/coordinate_cast.hpp>
  45. #include <boost/geometry/io/wkt/detail/prefix.hpp>
  46. namespace boost { namespace geometry
  47. {
  48. /*!
  49. \brief Exception showing things wrong with WKT parsing
  50. \ingroup wkt
  51. */
  52. struct read_wkt_exception : public geometry::exception
  53. {
  54. template <typename Iterator>
  55. read_wkt_exception(std::string const& msg,
  56. Iterator const& it,
  57. Iterator const& end,
  58. std::string const& wkt)
  59. : message(msg)
  60. , wkt(wkt)
  61. {
  62. if (it != end)
  63. {
  64. source = " at '";
  65. source += it->c_str();
  66. source += "'";
  67. }
  68. complete = message + source + " in '" + wkt.substr(0, 100) + "'";
  69. }
  70. read_wkt_exception(std::string const& msg, std::string const& wkt)
  71. : message(msg)
  72. , wkt(wkt)
  73. {
  74. complete = message + "' in (" + wkt.substr(0, 100) + ")";
  75. }
  76. virtual ~read_wkt_exception() throw() {}
  77. virtual const char* what() const throw()
  78. {
  79. return complete.c_str();
  80. }
  81. private :
  82. std::string source;
  83. std::string message;
  84. std::string wkt;
  85. std::string complete;
  86. };
  87. #ifndef DOXYGEN_NO_DETAIL
  88. // (wkt: Well Known Text, defined by OGC for all geometries and implemented by e.g. databases (MySQL, PostGIS))
  89. namespace detail { namespace wkt
  90. {
  91. typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
  92. template <typename Point,
  93. std::size_t Dimension = 0,
  94. std::size_t DimensionCount = geometry::dimension<Point>::value>
  95. struct parsing_assigner
  96. {
  97. static inline void apply(tokenizer::iterator& it,
  98. tokenizer::iterator const& end,
  99. Point& point,
  100. std::string const& wkt)
  101. {
  102. typedef typename coordinate_type<Point>::type coordinate_type;
  103. // Stop at end of tokens, or at "," ot ")"
  104. bool finished = (it == end || *it == "," || *it == ")");
  105. try
  106. {
  107. // Initialize missing coordinates to default constructor (zero)
  108. // OR
  109. // Use lexical_cast for conversion to double/int
  110. // Note that it is much slower than atof. However, it is more standard
  111. // and in parsing the change in performance falls probably away against
  112. // the tokenizing
  113. set<Dimension>(point, finished
  114. ? coordinate_type()
  115. : coordinate_cast<coordinate_type>::apply(*it));
  116. }
  117. catch(boost::bad_lexical_cast const& blc)
  118. {
  119. BOOST_THROW_EXCEPTION(read_wkt_exception(blc.what(), it, end, wkt));
  120. }
  121. catch(std::exception const& e)
  122. {
  123. BOOST_THROW_EXCEPTION(read_wkt_exception(e.what(), it, end, wkt));
  124. }
  125. catch(...)
  126. {
  127. BOOST_THROW_EXCEPTION(read_wkt_exception("", it, end, wkt));
  128. }
  129. parsing_assigner<Point, Dimension + 1, DimensionCount>::apply(
  130. (finished ? it : ++it), end, point, wkt);
  131. }
  132. };
  133. template <typename Point, std::size_t DimensionCount>
  134. struct parsing_assigner<Point, DimensionCount, DimensionCount>
  135. {
  136. static inline void apply(tokenizer::iterator&,
  137. tokenizer::iterator const&,
  138. Point&,
  139. std::string const&)
  140. {
  141. }
  142. };
  143. template <typename Iterator>
  144. inline void handle_open_parenthesis(Iterator& it,
  145. Iterator const& end,
  146. std::string const& wkt)
  147. {
  148. if (it == end || *it != "(")
  149. {
  150. BOOST_THROW_EXCEPTION(read_wkt_exception("Expected '('", it, end, wkt));
  151. }
  152. ++it;
  153. }
  154. template <typename Iterator>
  155. inline void handle_close_parenthesis(Iterator& it,
  156. Iterator const& end,
  157. std::string const& wkt)
  158. {
  159. if (it != end && *it == ")")
  160. {
  161. ++it;
  162. }
  163. else
  164. {
  165. BOOST_THROW_EXCEPTION(read_wkt_exception("Expected ')'", it, end, wkt));
  166. }
  167. }
  168. template <typename Iterator>
  169. inline void check_end(Iterator& it,
  170. Iterator const& end,
  171. std::string const& wkt)
  172. {
  173. if (it != end)
  174. {
  175. BOOST_THROW_EXCEPTION(read_wkt_exception("Too many tokens", it, end, wkt));
  176. }
  177. }
  178. /*!
  179. \brief Internal, parses coordinate sequences, strings are formated like "(1 2,3 4,...)"
  180. \param it token-iterator, should be pre-positioned at "(", is post-positions after last ")"
  181. \param end end-token-iterator
  182. \param out Output itererator receiving coordinates
  183. */
  184. template <typename Point>
  185. struct container_inserter
  186. {
  187. // Version with output iterator
  188. template <typename OutputIterator>
  189. static inline void apply(tokenizer::iterator& it,
  190. tokenizer::iterator const& end,
  191. std::string const& wkt,
  192. OutputIterator out)
  193. {
  194. handle_open_parenthesis(it, end, wkt);
  195. Point point;
  196. // Parse points until closing parenthesis
  197. while (it != end && *it != ")")
  198. {
  199. parsing_assigner<Point>::apply(it, end, point, wkt);
  200. out = point;
  201. ++out;
  202. if (it != end && *it == ",")
  203. {
  204. ++it;
  205. }
  206. }
  207. handle_close_parenthesis(it, end, wkt);
  208. }
  209. };
  210. template <typename Geometry,
  211. closure_selector Closure = closure<Geometry>::value>
  212. struct stateful_range_appender
  213. {
  214. typedef typename geometry::point_type<Geometry>::type point_type;
  215. // NOTE: Geometry is a reference
  216. inline void append(Geometry geom, point_type const& point, bool)
  217. {
  218. geometry::append(geom, point);
  219. }
  220. };
  221. template <typename Geometry>
  222. struct stateful_range_appender<Geometry, open>
  223. {
  224. typedef typename geometry::point_type<Geometry>::type point_type;
  225. typedef typename boost::range_size
  226. <
  227. typename util::bare_type<Geometry>::type
  228. >::type size_type;
  229. BOOST_STATIC_ASSERT(( boost::is_same
  230. <
  231. typename tag<Geometry>::type,
  232. ring_tag
  233. >::value ));
  234. inline stateful_range_appender()
  235. : pt_index(0)
  236. {}
  237. // NOTE: Geometry is a reference
  238. inline void append(Geometry geom, point_type const& point, bool is_next_expected)
  239. {
  240. bool should_append = true;
  241. if (pt_index == 0)
  242. {
  243. first_point = point;
  244. //should_append = true;
  245. }
  246. else
  247. {
  248. // NOTE: if there is not enough Points, they're always appended
  249. should_append
  250. = is_next_expected
  251. || pt_index < core_detail::closure::minimum_ring_size<open>::value
  252. || disjoint(point, first_point);
  253. }
  254. ++pt_index;
  255. if (should_append)
  256. {
  257. geometry::append(geom, point);
  258. }
  259. }
  260. private:
  261. static inline bool disjoint(point_type const& p1, point_type const& p2)
  262. {
  263. // TODO: pass strategy
  264. typedef typename strategy::disjoint::services::default_strategy
  265. <
  266. point_type, point_type
  267. >::type strategy_type;
  268. return detail::disjoint::disjoint_point_point(p1, p2, strategy_type());
  269. }
  270. size_type pt_index;
  271. point_type first_point;
  272. };
  273. // Geometry is a value-type or reference-type
  274. template <typename Geometry>
  275. struct container_appender
  276. {
  277. typedef typename geometry::point_type<Geometry>::type point_type;
  278. static inline void apply(tokenizer::iterator& it,
  279. tokenizer::iterator const& end,
  280. std::string const& wkt,
  281. Geometry out)
  282. {
  283. handle_open_parenthesis(it, end, wkt);
  284. stateful_range_appender<Geometry> appender;
  285. // Parse points until closing parenthesis
  286. while (it != end && *it != ")")
  287. {
  288. point_type point;
  289. parsing_assigner<point_type>::apply(it, end, point, wkt);
  290. bool const is_next_expected = it != end && *it == ",";
  291. appender.append(out, point, is_next_expected);
  292. if (is_next_expected)
  293. {
  294. ++it;
  295. }
  296. }
  297. handle_close_parenthesis(it, end, wkt);
  298. }
  299. };
  300. /*!
  301. \brief Internal, parses a point from a string like this "(x y)"
  302. \note used for parsing points and multi-points
  303. */
  304. template <typename P>
  305. struct point_parser
  306. {
  307. static inline void apply(tokenizer::iterator& it,
  308. tokenizer::iterator const& end,
  309. std::string const& wkt,
  310. P& point)
  311. {
  312. handle_open_parenthesis(it, end, wkt);
  313. parsing_assigner<P>::apply(it, end, point, wkt);
  314. handle_close_parenthesis(it, end, wkt);
  315. }
  316. };
  317. template <typename Geometry>
  318. struct linestring_parser
  319. {
  320. static inline void apply(tokenizer::iterator& it,
  321. tokenizer::iterator const& end,
  322. std::string const& wkt,
  323. Geometry& geometry)
  324. {
  325. container_appender<Geometry&>::apply(it, end, wkt, geometry);
  326. }
  327. };
  328. template <typename Ring>
  329. struct ring_parser
  330. {
  331. static inline void apply(tokenizer::iterator& it,
  332. tokenizer::iterator const& end,
  333. std::string const& wkt,
  334. Ring& ring)
  335. {
  336. // A ring should look like polygon((x y,x y,x y...))
  337. // So handle the extra opening/closing parentheses
  338. // and in between parse using the container-inserter
  339. handle_open_parenthesis(it, end, wkt);
  340. container_appender<Ring&>::apply(it, end, wkt, ring);
  341. handle_close_parenthesis(it, end, wkt);
  342. }
  343. };
  344. /*!
  345. \brief Internal, parses a polygon from a string like this "((x y,x y),(x y,x y))"
  346. \note used for parsing polygons and multi-polygons
  347. */
  348. template <typename Polygon>
  349. struct polygon_parser
  350. {
  351. typedef typename ring_return_type<Polygon>::type ring_return_type;
  352. typedef container_appender<ring_return_type> appender;
  353. static inline void apply(tokenizer::iterator& it,
  354. tokenizer::iterator const& end,
  355. std::string const& wkt,
  356. Polygon& poly)
  357. {
  358. handle_open_parenthesis(it, end, wkt);
  359. int n = -1;
  360. // Stop at ")"
  361. while (it != end && *it != ")")
  362. {
  363. // Parse ring
  364. if (++n == 0)
  365. {
  366. appender::apply(it, end, wkt, exterior_ring(poly));
  367. }
  368. else
  369. {
  370. typename ring_type<Polygon>::type ring;
  371. appender::apply(it, end, wkt, ring);
  372. traits::push_back
  373. <
  374. typename boost::remove_reference
  375. <
  376. typename traits::interior_mutable_type<Polygon>::type
  377. >::type
  378. >::apply(interior_rings(poly), ring);
  379. }
  380. if (it != end && *it == ",")
  381. {
  382. // Skip "," after ring is parsed
  383. ++it;
  384. }
  385. }
  386. handle_close_parenthesis(it, end, wkt);
  387. }
  388. };
  389. inline bool one_of(tokenizer::iterator const& it,
  390. std::string const& value,
  391. bool& is_present)
  392. {
  393. if (boost::iequals(*it, value))
  394. {
  395. is_present = true;
  396. return true;
  397. }
  398. return false;
  399. }
  400. inline bool one_of(tokenizer::iterator const& it,
  401. std::string const& value,
  402. bool& present1,
  403. bool& present2)
  404. {
  405. if (boost::iequals(*it, value))
  406. {
  407. present1 = true;
  408. present2 = true;
  409. return true;
  410. }
  411. return false;
  412. }
  413. inline void handle_empty_z_m(tokenizer::iterator& it,
  414. tokenizer::iterator const& end,
  415. bool& has_empty,
  416. bool& has_z,
  417. bool& has_m)
  418. {
  419. has_empty = false;
  420. has_z = false;
  421. has_m = false;
  422. // WKT can optionally have Z and M (measured) values as in
  423. // POINT ZM (1 1 5 60), POINT M (1 1 80), POINT Z (1 1 5)
  424. // GGL supports any of them as coordinate values, but is not aware
  425. // of any Measured value.
  426. while (it != end
  427. && (one_of(it, "M", has_m)
  428. || one_of(it, "Z", has_z)
  429. || one_of(it, "EMPTY", has_empty)
  430. || one_of(it, "MZ", has_m, has_z)
  431. || one_of(it, "ZM", has_z, has_m)
  432. )
  433. )
  434. {
  435. ++it;
  436. }
  437. }
  438. /*!
  439. \brief Internal, starts parsing
  440. \param tokens boost tokens, parsed with separator " " and keeping separator "()"
  441. \param geometry string to compare with first token
  442. */
  443. template <typename Geometry>
  444. inline bool initialize(tokenizer const& tokens,
  445. std::string const& geometry_name,
  446. std::string const& wkt,
  447. tokenizer::iterator& it,
  448. tokenizer::iterator& end)
  449. {
  450. it = tokens.begin();
  451. end = tokens.end();
  452. if (it == end || ! boost::iequals(*it++, geometry_name))
  453. {
  454. BOOST_THROW_EXCEPTION(read_wkt_exception(std::string("Should start with '") + geometry_name + "'", wkt));
  455. }
  456. bool has_empty, has_z, has_m;
  457. handle_empty_z_m(it, end, has_empty, has_z, has_m);
  458. // Silence warning C4127: conditional expression is constant
  459. #if defined(_MSC_VER)
  460. #pragma warning(push)
  461. #pragma warning(disable : 4127)
  462. #endif
  463. if (has_z && dimension<Geometry>::type::value < 3)
  464. {
  465. BOOST_THROW_EXCEPTION(read_wkt_exception("Z only allowed for 3 or more dimensions", wkt));
  466. }
  467. #if defined(_MSC_VER)
  468. #pragma warning(pop)
  469. #endif
  470. if (has_empty)
  471. {
  472. check_end(it, end, wkt);
  473. return false;
  474. }
  475. // M is ignored at all.
  476. return true;
  477. }
  478. template <typename Geometry, template<typename> class Parser, typename PrefixPolicy>
  479. struct geometry_parser
  480. {
  481. static inline void apply(std::string const& wkt, Geometry& geometry)
  482. {
  483. geometry::clear(geometry);
  484. tokenizer tokens(wkt, boost::char_separator<char>(" ", ",()"));
  485. tokenizer::iterator it, end;
  486. if (initialize<Geometry>(tokens, PrefixPolicy::apply(), wkt, it, end))
  487. {
  488. Parser<Geometry>::apply(it, end, wkt, geometry);
  489. check_end(it, end, wkt);
  490. }
  491. }
  492. };
  493. template <typename MultiGeometry, template<typename> class Parser, typename PrefixPolicy>
  494. struct multi_parser
  495. {
  496. static inline void apply(std::string const& wkt, MultiGeometry& geometry)
  497. {
  498. traits::clear<MultiGeometry>::apply(geometry);
  499. tokenizer tokens(wkt, boost::char_separator<char>(" ", ",()"));
  500. tokenizer::iterator it, end;
  501. if (initialize<MultiGeometry>(tokens, PrefixPolicy::apply(), wkt, it, end))
  502. {
  503. handle_open_parenthesis(it, end, wkt);
  504. // Parse sub-geometries
  505. while(it != end && *it != ")")
  506. {
  507. traits::resize<MultiGeometry>::apply(geometry, boost::size(geometry) + 1);
  508. Parser
  509. <
  510. typename boost::range_value<MultiGeometry>::type
  511. >::apply(it, end, wkt, *(boost::end(geometry) - 1));
  512. if (it != end && *it == ",")
  513. {
  514. // Skip "," after multi-element is parsed
  515. ++it;
  516. }
  517. }
  518. handle_close_parenthesis(it, end, wkt);
  519. }
  520. check_end(it, end, wkt);
  521. }
  522. };
  523. template <typename P>
  524. struct noparenthesis_point_parser
  525. {
  526. static inline void apply(tokenizer::iterator& it,
  527. tokenizer::iterator const& end,
  528. std::string const& wkt,
  529. P& point)
  530. {
  531. parsing_assigner<P>::apply(it, end, point, wkt);
  532. }
  533. };
  534. template <typename MultiGeometry, typename PrefixPolicy>
  535. struct multi_point_parser
  536. {
  537. static inline void apply(std::string const& wkt, MultiGeometry& geometry)
  538. {
  539. traits::clear<MultiGeometry>::apply(geometry);
  540. tokenizer tokens(wkt, boost::char_separator<char>(" ", ",()"));
  541. tokenizer::iterator it, end;
  542. if (initialize<MultiGeometry>(tokens, PrefixPolicy::apply(), wkt, it, end))
  543. {
  544. handle_open_parenthesis(it, end, wkt);
  545. // If first point definition starts with "(" then parse points as (x y)
  546. // otherwise as "x y"
  547. bool using_brackets = (it != end && *it == "(");
  548. while(it != end && *it != ")")
  549. {
  550. traits::resize<MultiGeometry>::apply(geometry, boost::size(geometry) + 1);
  551. if (using_brackets)
  552. {
  553. point_parser
  554. <
  555. typename boost::range_value<MultiGeometry>::type
  556. >::apply(it, end, wkt, *(boost::end(geometry) - 1));
  557. }
  558. else
  559. {
  560. noparenthesis_point_parser
  561. <
  562. typename boost::range_value<MultiGeometry>::type
  563. >::apply(it, end, wkt, *(boost::end(geometry) - 1));
  564. }
  565. if (it != end && *it == ",")
  566. {
  567. // Skip "," after point is parsed
  568. ++it;
  569. }
  570. }
  571. handle_close_parenthesis(it, end, wkt);
  572. }
  573. check_end(it, end, wkt);
  574. }
  575. };
  576. /*!
  577. \brief Supports box parsing
  578. \note OGC does not define the box geometry, and WKT does not support boxes.
  579. However, to be generic GGL supports reading and writing from and to boxes.
  580. Boxes are outputted as a standard POLYGON. GGL can read boxes from
  581. a standard POLYGON, from a POLYGON with 2 points of from a BOX
  582. \tparam Box the box
  583. */
  584. template <typename Box>
  585. struct box_parser
  586. {
  587. static inline void apply(std::string const& wkt, Box& box)
  588. {
  589. bool should_close = false;
  590. tokenizer tokens(wkt, boost::char_separator<char>(" ", ",()"));
  591. tokenizer::iterator it = tokens.begin();
  592. tokenizer::iterator end = tokens.end();
  593. if (it != end && boost::iequals(*it, "POLYGON"))
  594. {
  595. ++it;
  596. bool has_empty, has_z, has_m;
  597. handle_empty_z_m(it, end, has_empty, has_z, has_m);
  598. if (has_empty)
  599. {
  600. assign_zero(box);
  601. return;
  602. }
  603. handle_open_parenthesis(it, end, wkt);
  604. should_close = true;
  605. }
  606. else if (it != end && boost::iequals(*it, "BOX"))
  607. {
  608. ++it;
  609. }
  610. else
  611. {
  612. BOOST_THROW_EXCEPTION(read_wkt_exception("Should start with 'POLYGON' or 'BOX'", wkt));
  613. }
  614. typedef typename point_type<Box>::type point_type;
  615. std::vector<point_type> points;
  616. container_inserter<point_type>::apply(it, end, wkt, std::back_inserter(points));
  617. if (should_close)
  618. {
  619. handle_close_parenthesis(it, end, wkt);
  620. }
  621. check_end(it, end, wkt);
  622. unsigned int index = 0;
  623. std::size_t n = boost::size(points);
  624. if (n == 2)
  625. {
  626. index = 1;
  627. }
  628. else if (n == 4 || n == 5)
  629. {
  630. // In case of 4 or 5 points, we do not check the other ones, just
  631. // take the opposite corner which is always 2
  632. index = 2;
  633. }
  634. else
  635. {
  636. BOOST_THROW_EXCEPTION(read_wkt_exception("Box should have 2,4 or 5 points", wkt));
  637. }
  638. geometry::detail::assign_point_to_index<min_corner>(points.front(), box);
  639. geometry::detail::assign_point_to_index<max_corner>(points[index], box);
  640. }
  641. };
  642. /*!
  643. \brief Supports segment parsing
  644. \note OGC does not define the segment, and WKT does not support segmentes.
  645. However, it is useful to implement it, also for testing purposes
  646. \tparam Segment the segment
  647. */
  648. template <typename Segment>
  649. struct segment_parser
  650. {
  651. static inline void apply(std::string const& wkt, Segment& segment)
  652. {
  653. tokenizer tokens(wkt, boost::char_separator<char>(" ", ",()"));
  654. tokenizer::iterator it = tokens.begin();
  655. tokenizer::iterator end = tokens.end();
  656. if (it != end &&
  657. (boost::iequals(*it, "SEGMENT")
  658. || boost::iequals(*it, "LINESTRING") ))
  659. {
  660. ++it;
  661. }
  662. else
  663. {
  664. BOOST_THROW_EXCEPTION(read_wkt_exception("Should start with 'LINESTRING' or 'SEGMENT'", wkt));
  665. }
  666. typedef typename point_type<Segment>::type point_type;
  667. std::vector<point_type> points;
  668. container_inserter<point_type>::apply(it, end, wkt, std::back_inserter(points));
  669. check_end(it, end, wkt);
  670. if (boost::size(points) == 2)
  671. {
  672. geometry::detail::assign_point_to_index<0>(points.front(), segment);
  673. geometry::detail::assign_point_to_index<1>(points.back(), segment);
  674. }
  675. else
  676. {
  677. BOOST_THROW_EXCEPTION(read_wkt_exception("Segment should have 2 points", wkt));
  678. }
  679. }
  680. };
  681. }} // namespace detail::wkt
  682. #endif // DOXYGEN_NO_DETAIL
  683. #ifndef DOXYGEN_NO_DISPATCH
  684. namespace dispatch
  685. {
  686. template <typename Tag, typename Geometry>
  687. struct read_wkt {};
  688. template <typename Point>
  689. struct read_wkt<point_tag, Point>
  690. : detail::wkt::geometry_parser
  691. <
  692. Point,
  693. detail::wkt::point_parser,
  694. detail::wkt::prefix_point
  695. >
  696. {};
  697. template <typename L>
  698. struct read_wkt<linestring_tag, L>
  699. : detail::wkt::geometry_parser
  700. <
  701. L,
  702. detail::wkt::linestring_parser,
  703. detail::wkt::prefix_linestring
  704. >
  705. {};
  706. template <typename Ring>
  707. struct read_wkt<ring_tag, Ring>
  708. : detail::wkt::geometry_parser
  709. <
  710. Ring,
  711. detail::wkt::ring_parser,
  712. detail::wkt::prefix_polygon
  713. >
  714. {};
  715. template <typename Geometry>
  716. struct read_wkt<polygon_tag, Geometry>
  717. : detail::wkt::geometry_parser
  718. <
  719. Geometry,
  720. detail::wkt::polygon_parser,
  721. detail::wkt::prefix_polygon
  722. >
  723. {};
  724. template <typename MultiGeometry>
  725. struct read_wkt<multi_point_tag, MultiGeometry>
  726. : detail::wkt::multi_point_parser
  727. <
  728. MultiGeometry,
  729. detail::wkt::prefix_multipoint
  730. >
  731. {};
  732. template <typename MultiGeometry>
  733. struct read_wkt<multi_linestring_tag, MultiGeometry>
  734. : detail::wkt::multi_parser
  735. <
  736. MultiGeometry,
  737. detail::wkt::linestring_parser,
  738. detail::wkt::prefix_multilinestring
  739. >
  740. {};
  741. template <typename MultiGeometry>
  742. struct read_wkt<multi_polygon_tag, MultiGeometry>
  743. : detail::wkt::multi_parser
  744. <
  745. MultiGeometry,
  746. detail::wkt::polygon_parser,
  747. detail::wkt::prefix_multipolygon
  748. >
  749. {};
  750. // Box (Non-OGC)
  751. template <typename Box>
  752. struct read_wkt<box_tag, Box>
  753. : detail::wkt::box_parser<Box>
  754. {};
  755. // Segment (Non-OGC)
  756. template <typename Segment>
  757. struct read_wkt<segment_tag, Segment>
  758. : detail::wkt::segment_parser<Segment>
  759. {};
  760. } // namespace dispatch
  761. #endif // DOXYGEN_NO_DISPATCH
  762. /*!
  763. \brief Parses OGC Well-Known Text (\ref WKT) into a geometry (any geometry)
  764. \ingroup wkt
  765. \tparam Geometry \tparam_geometry
  766. \param wkt string containing \ref WKT
  767. \param geometry \param_geometry output geometry
  768. \ingroup wkt
  769. \qbk{[include reference/io/read_wkt.qbk]}
  770. */
  771. template <typename Geometry>
  772. inline void read_wkt(std::string const& wkt, Geometry& geometry)
  773. {
  774. geometry::concepts::check<Geometry>();
  775. dispatch::read_wkt<typename tag<Geometry>::type, Geometry>::apply(wkt, geometry);
  776. }
  777. }} // namespace boost::geometry
  778. #endif // BOOST_GEOMETRY_IO_WKT_READ_HPP