read.hpp 25 KB

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