read.hpp 26 KB

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