read.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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.
  7. // Modifications copyright (c) 2014-2015 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/equals/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. || !detail::equals::equals_point_point(point, first_point);
  253. }
  254. ++pt_index;
  255. if (should_append)
  256. {
  257. geometry::append(geom, point);
  258. }
  259. }
  260. private:
  261. size_type pt_index;
  262. point_type first_point;
  263. };
  264. // Geometry is a value-type or reference-type
  265. template <typename Geometry>
  266. struct container_appender
  267. {
  268. typedef typename geometry::point_type<Geometry>::type point_type;
  269. static inline void apply(tokenizer::iterator& it,
  270. tokenizer::iterator const& end,
  271. std::string const& wkt,
  272. Geometry out)
  273. {
  274. handle_open_parenthesis(it, end, wkt);
  275. stateful_range_appender<Geometry> appender;
  276. // Parse points until closing parenthesis
  277. while (it != end && *it != ")")
  278. {
  279. point_type point;
  280. parsing_assigner<point_type>::apply(it, end, point, wkt);
  281. bool const is_next_expected = it != end && *it == ",";
  282. appender.append(out, point, is_next_expected);
  283. if (is_next_expected)
  284. {
  285. ++it;
  286. }
  287. }
  288. handle_close_parenthesis(it, end, wkt);
  289. }
  290. };
  291. /*!
  292. \brief Internal, parses a point from a string like this "(x y)"
  293. \note used for parsing points and multi-points
  294. */
  295. template <typename P>
  296. struct point_parser
  297. {
  298. static inline void apply(tokenizer::iterator& it,
  299. tokenizer::iterator const& end,
  300. std::string const& wkt,
  301. P& point)
  302. {
  303. handle_open_parenthesis(it, end, wkt);
  304. parsing_assigner<P>::apply(it, end, point, wkt);
  305. handle_close_parenthesis(it, end, wkt);
  306. }
  307. };
  308. template <typename Geometry>
  309. struct linestring_parser
  310. {
  311. static inline void apply(tokenizer::iterator& it,
  312. tokenizer::iterator const& end,
  313. std::string const& wkt,
  314. Geometry& geometry)
  315. {
  316. container_appender<Geometry&>::apply(it, end, wkt, geometry);
  317. }
  318. };
  319. template <typename Ring>
  320. struct ring_parser
  321. {
  322. static inline void apply(tokenizer::iterator& it,
  323. tokenizer::iterator const& end,
  324. std::string const& wkt,
  325. Ring& ring)
  326. {
  327. // A ring should look like polygon((x y,x y,x y...))
  328. // So handle the extra opening/closing parentheses
  329. // and in between parse using the container-inserter
  330. handle_open_parenthesis(it, end, wkt);
  331. container_appender<Ring&>::apply(it, end, wkt, ring);
  332. handle_close_parenthesis(it, end, wkt);
  333. }
  334. };
  335. /*!
  336. \brief Internal, parses a polygon from a string like this "((x y,x y),(x y,x y))"
  337. \note used for parsing polygons and multi-polygons
  338. */
  339. template <typename Polygon>
  340. struct polygon_parser
  341. {
  342. typedef typename ring_return_type<Polygon>::type ring_return_type;
  343. typedef container_appender<ring_return_type> appender;
  344. static inline void apply(tokenizer::iterator& it,
  345. tokenizer::iterator const& end,
  346. std::string const& wkt,
  347. Polygon& poly)
  348. {
  349. handle_open_parenthesis(it, end, wkt);
  350. int n = -1;
  351. // Stop at ")"
  352. while (it != end && *it != ")")
  353. {
  354. // Parse ring
  355. if (++n == 0)
  356. {
  357. appender::apply(it, end, wkt, exterior_ring(poly));
  358. }
  359. else
  360. {
  361. typename ring_type<Polygon>::type ring;
  362. appender::apply(it, end, wkt, ring);
  363. traits::push_back
  364. <
  365. typename boost::remove_reference
  366. <
  367. typename traits::interior_mutable_type<Polygon>::type
  368. >::type
  369. >::apply(interior_rings(poly), ring);
  370. }
  371. if (it != end && *it == ",")
  372. {
  373. // Skip "," after ring is parsed
  374. ++it;
  375. }
  376. }
  377. handle_close_parenthesis(it, end, wkt);
  378. }
  379. };
  380. inline bool one_of(tokenizer::iterator const& it,
  381. std::string const& value,
  382. bool& is_present)
  383. {
  384. if (boost::iequals(*it, value))
  385. {
  386. is_present = true;
  387. return true;
  388. }
  389. return false;
  390. }
  391. inline bool one_of(tokenizer::iterator const& it,
  392. std::string const& value,
  393. bool& present1,
  394. bool& present2)
  395. {
  396. if (boost::iequals(*it, value))
  397. {
  398. present1 = true;
  399. present2 = true;
  400. return true;
  401. }
  402. return false;
  403. }
  404. inline void handle_empty_z_m(tokenizer::iterator& it,
  405. tokenizer::iterator const& end,
  406. bool& has_empty,
  407. bool& has_z,
  408. bool& has_m)
  409. {
  410. has_empty = false;
  411. has_z = false;
  412. has_m = false;
  413. // WKT can optionally have Z and M (measured) values as in
  414. // POINT ZM (1 1 5 60), POINT M (1 1 80), POINT Z (1 1 5)
  415. // GGL supports any of them as coordinate values, but is not aware
  416. // of any Measured value.
  417. while (it != end
  418. && (one_of(it, "M", has_m)
  419. || one_of(it, "Z", has_z)
  420. || one_of(it, "EMPTY", has_empty)
  421. || one_of(it, "MZ", has_m, has_z)
  422. || one_of(it, "ZM", has_z, has_m)
  423. )
  424. )
  425. {
  426. ++it;
  427. }
  428. }
  429. /*!
  430. \brief Internal, starts parsing
  431. \param tokens boost tokens, parsed with separator " " and keeping separator "()"
  432. \param geometry string to compare with first token
  433. */
  434. template <typename Geometry>
  435. inline bool initialize(tokenizer const& tokens,
  436. std::string const& geometry_name,
  437. std::string const& wkt,
  438. tokenizer::iterator& it,
  439. tokenizer::iterator& end)
  440. {
  441. it = tokens.begin();
  442. end = tokens.end();
  443. if (it == end || ! boost::iequals(*it++, geometry_name))
  444. {
  445. BOOST_THROW_EXCEPTION(read_wkt_exception(std::string("Should start with '") + geometry_name + "'", wkt));
  446. }
  447. bool has_empty, has_z, has_m;
  448. handle_empty_z_m(it, end, has_empty, has_z, has_m);
  449. // Silence warning C4127: conditional expression is constant
  450. #if defined(_MSC_VER)
  451. #pragma warning(push)
  452. #pragma warning(disable : 4127)
  453. #endif
  454. if (has_z && dimension<Geometry>::type::value < 3)
  455. {
  456. BOOST_THROW_EXCEPTION(read_wkt_exception("Z only allowed for 3 or more dimensions", wkt));
  457. }
  458. #if defined(_MSC_VER)
  459. #pragma warning(pop)
  460. #endif
  461. if (has_empty)
  462. {
  463. check_end(it, end, wkt);
  464. return false;
  465. }
  466. // M is ignored at all.
  467. return true;
  468. }
  469. template <typename Geometry, template<typename> class Parser, typename PrefixPolicy>
  470. struct geometry_parser
  471. {
  472. static inline void apply(std::string const& wkt, Geometry& geometry)
  473. {
  474. geometry::clear(geometry);
  475. tokenizer tokens(wkt, boost::char_separator<char>(" ", ",()"));
  476. tokenizer::iterator it, end;
  477. if (initialize<Geometry>(tokens, PrefixPolicy::apply(), wkt, it, end))
  478. {
  479. Parser<Geometry>::apply(it, end, wkt, geometry);
  480. check_end(it, end, wkt);
  481. }
  482. }
  483. };
  484. template <typename MultiGeometry, template<typename> class Parser, typename PrefixPolicy>
  485. struct multi_parser
  486. {
  487. static inline void apply(std::string const& wkt, MultiGeometry& geometry)
  488. {
  489. traits::clear<MultiGeometry>::apply(geometry);
  490. tokenizer tokens(wkt, boost::char_separator<char>(" ", ",()"));
  491. tokenizer::iterator it, end;
  492. if (initialize<MultiGeometry>(tokens, PrefixPolicy::apply(), wkt, it, end))
  493. {
  494. handle_open_parenthesis(it, end, wkt);
  495. // Parse sub-geometries
  496. while(it != end && *it != ")")
  497. {
  498. traits::resize<MultiGeometry>::apply(geometry, boost::size(geometry) + 1);
  499. Parser
  500. <
  501. typename boost::range_value<MultiGeometry>::type
  502. >::apply(it, end, wkt, *(boost::end(geometry) - 1));
  503. if (it != end && *it == ",")
  504. {
  505. // Skip "," after multi-element is parsed
  506. ++it;
  507. }
  508. }
  509. handle_close_parenthesis(it, end, wkt);
  510. }
  511. check_end(it, end, wkt);
  512. }
  513. };
  514. template <typename P>
  515. struct noparenthesis_point_parser
  516. {
  517. static inline void apply(tokenizer::iterator& it,
  518. tokenizer::iterator const& end,
  519. std::string const& wkt,
  520. P& point)
  521. {
  522. parsing_assigner<P>::apply(it, end, point, wkt);
  523. }
  524. };
  525. template <typename MultiGeometry, typename PrefixPolicy>
  526. struct multi_point_parser
  527. {
  528. static inline void apply(std::string const& wkt, MultiGeometry& geometry)
  529. {
  530. traits::clear<MultiGeometry>::apply(geometry);
  531. tokenizer tokens(wkt, boost::char_separator<char>(" ", ",()"));
  532. tokenizer::iterator it, end;
  533. if (initialize<MultiGeometry>(tokens, PrefixPolicy::apply(), wkt, it, end))
  534. {
  535. handle_open_parenthesis(it, end, wkt);
  536. // If first point definition starts with "(" then parse points as (x y)
  537. // otherwise as "x y"
  538. bool using_brackets = (it != end && *it == "(");
  539. while(it != end && *it != ")")
  540. {
  541. traits::resize<MultiGeometry>::apply(geometry, boost::size(geometry) + 1);
  542. if (using_brackets)
  543. {
  544. point_parser
  545. <
  546. typename boost::range_value<MultiGeometry>::type
  547. >::apply(it, end, wkt, *(boost::end(geometry) - 1));
  548. }
  549. else
  550. {
  551. noparenthesis_point_parser
  552. <
  553. typename boost::range_value<MultiGeometry>::type
  554. >::apply(it, end, wkt, *(boost::end(geometry) - 1));
  555. }
  556. if (it != end && *it == ",")
  557. {
  558. // Skip "," after point is parsed
  559. ++it;
  560. }
  561. }
  562. handle_close_parenthesis(it, end, wkt);
  563. }
  564. check_end(it, end, wkt);
  565. }
  566. };
  567. /*!
  568. \brief Supports box parsing
  569. \note OGC does not define the box geometry, and WKT does not support boxes.
  570. However, to be generic GGL supports reading and writing from and to boxes.
  571. Boxes are outputted as a standard POLYGON. GGL can read boxes from
  572. a standard POLYGON, from a POLYGON with 2 points of from a BOX
  573. \tparam Box the box
  574. */
  575. template <typename Box>
  576. struct box_parser
  577. {
  578. static inline void apply(std::string const& wkt, Box& box)
  579. {
  580. bool should_close = false;
  581. tokenizer tokens(wkt, boost::char_separator<char>(" ", ",()"));
  582. tokenizer::iterator it = tokens.begin();
  583. tokenizer::iterator end = tokens.end();
  584. if (it != end && boost::iequals(*it, "POLYGON"))
  585. {
  586. ++it;
  587. bool has_empty, has_z, has_m;
  588. handle_empty_z_m(it, end, has_empty, has_z, has_m);
  589. if (has_empty)
  590. {
  591. assign_zero(box);
  592. return;
  593. }
  594. handle_open_parenthesis(it, end, wkt);
  595. should_close = true;
  596. }
  597. else if (it != end && boost::iequals(*it, "BOX"))
  598. {
  599. ++it;
  600. }
  601. else
  602. {
  603. BOOST_THROW_EXCEPTION(read_wkt_exception("Should start with 'POLYGON' or 'BOX'", wkt));
  604. }
  605. typedef typename point_type<Box>::type point_type;
  606. std::vector<point_type> points;
  607. container_inserter<point_type>::apply(it, end, wkt, std::back_inserter(points));
  608. if (should_close)
  609. {
  610. handle_close_parenthesis(it, end, wkt);
  611. }
  612. check_end(it, end, wkt);
  613. unsigned int index = 0;
  614. std::size_t n = boost::size(points);
  615. if (n == 2)
  616. {
  617. index = 1;
  618. }
  619. else if (n == 4 || n == 5)
  620. {
  621. // In case of 4 or 5 points, we do not check the other ones, just
  622. // take the opposite corner which is always 2
  623. index = 2;
  624. }
  625. else
  626. {
  627. BOOST_THROW_EXCEPTION(read_wkt_exception("Box should have 2,4 or 5 points", wkt));
  628. }
  629. geometry::detail::assign_point_to_index<min_corner>(points.front(), box);
  630. geometry::detail::assign_point_to_index<max_corner>(points[index], box);
  631. }
  632. };
  633. /*!
  634. \brief Supports segment parsing
  635. \note OGC does not define the segment, and WKT does not support segmentes.
  636. However, it is useful to implement it, also for testing purposes
  637. \tparam Segment the segment
  638. */
  639. template <typename Segment>
  640. struct segment_parser
  641. {
  642. static inline void apply(std::string const& wkt, Segment& segment)
  643. {
  644. tokenizer tokens(wkt, boost::char_separator<char>(" ", ",()"));
  645. tokenizer::iterator it = tokens.begin();
  646. tokenizer::iterator end = tokens.end();
  647. if (it != end &&
  648. (boost::iequals(*it, "SEGMENT")
  649. || boost::iequals(*it, "LINESTRING") ))
  650. {
  651. ++it;
  652. }
  653. else
  654. {
  655. BOOST_THROW_EXCEPTION(read_wkt_exception("Should start with 'LINESTRING' or 'SEGMENT'", wkt));
  656. }
  657. typedef typename point_type<Segment>::type point_type;
  658. std::vector<point_type> points;
  659. container_inserter<point_type>::apply(it, end, wkt, std::back_inserter(points));
  660. check_end(it, end, wkt);
  661. if (boost::size(points) == 2)
  662. {
  663. geometry::detail::assign_point_to_index<0>(points.front(), segment);
  664. geometry::detail::assign_point_to_index<1>(points.back(), segment);
  665. }
  666. else
  667. {
  668. BOOST_THROW_EXCEPTION(read_wkt_exception("Segment should have 2 points", wkt));
  669. }
  670. }
  671. };
  672. }} // namespace detail::wkt
  673. #endif // DOXYGEN_NO_DETAIL
  674. #ifndef DOXYGEN_NO_DISPATCH
  675. namespace dispatch
  676. {
  677. template <typename Tag, typename Geometry>
  678. struct read_wkt {};
  679. template <typename Point>
  680. struct read_wkt<point_tag, Point>
  681. : detail::wkt::geometry_parser
  682. <
  683. Point,
  684. detail::wkt::point_parser,
  685. detail::wkt::prefix_point
  686. >
  687. {};
  688. template <typename L>
  689. struct read_wkt<linestring_tag, L>
  690. : detail::wkt::geometry_parser
  691. <
  692. L,
  693. detail::wkt::linestring_parser,
  694. detail::wkt::prefix_linestring
  695. >
  696. {};
  697. template <typename Ring>
  698. struct read_wkt<ring_tag, Ring>
  699. : detail::wkt::geometry_parser
  700. <
  701. Ring,
  702. detail::wkt::ring_parser,
  703. detail::wkt::prefix_polygon
  704. >
  705. {};
  706. template <typename Geometry>
  707. struct read_wkt<polygon_tag, Geometry>
  708. : detail::wkt::geometry_parser
  709. <
  710. Geometry,
  711. detail::wkt::polygon_parser,
  712. detail::wkt::prefix_polygon
  713. >
  714. {};
  715. template <typename MultiGeometry>
  716. struct read_wkt<multi_point_tag, MultiGeometry>
  717. : detail::wkt::multi_point_parser
  718. <
  719. MultiGeometry,
  720. detail::wkt::prefix_multipoint
  721. >
  722. {};
  723. template <typename MultiGeometry>
  724. struct read_wkt<multi_linestring_tag, MultiGeometry>
  725. : detail::wkt::multi_parser
  726. <
  727. MultiGeometry,
  728. detail::wkt::linestring_parser,
  729. detail::wkt::prefix_multilinestring
  730. >
  731. {};
  732. template <typename MultiGeometry>
  733. struct read_wkt<multi_polygon_tag, MultiGeometry>
  734. : detail::wkt::multi_parser
  735. <
  736. MultiGeometry,
  737. detail::wkt::polygon_parser,
  738. detail::wkt::prefix_multipolygon
  739. >
  740. {};
  741. // Box (Non-OGC)
  742. template <typename Box>
  743. struct read_wkt<box_tag, Box>
  744. : detail::wkt::box_parser<Box>
  745. {};
  746. // Segment (Non-OGC)
  747. template <typename Segment>
  748. struct read_wkt<segment_tag, Segment>
  749. : detail::wkt::segment_parser<Segment>
  750. {};
  751. } // namespace dispatch
  752. #endif // DOXYGEN_NO_DISPATCH
  753. /*!
  754. \brief Parses OGC Well-Known Text (\ref WKT) into a geometry (any geometry)
  755. \ingroup wkt
  756. \tparam Geometry \tparam_geometry
  757. \param wkt string containing \ref WKT
  758. \param geometry \param_geometry output geometry
  759. \ingroup wkt
  760. \qbk{[include reference/io/read_wkt.qbk]}
  761. */
  762. template <typename Geometry>
  763. inline void read_wkt(std::string const& wkt, Geometry& geometry)
  764. {
  765. geometry::concepts::check<Geometry>();
  766. dispatch::read_wkt<typename tag<Geometry>::type, Geometry>::apply(wkt, geometry);
  767. }
  768. }} // namespace boost::geometry
  769. #endif // BOOST_GEOMETRY_IO_WKT_READ_HPP