read.hpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2022 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-2023 Adam Wulkiewicz, Lodz, Poland.
  6. // Copyright (c) 2020 Baidyanath Kundu, Haldia, India
  7. // This file was modified by Oracle on 2014-2021.
  8. // Modifications copyright (c) 2014-2021 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/algorithm/string/predicate.hpp>
  20. #include <boost/lexical_cast.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/tokenizer.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/geometry_types.hpp>
  37. #include <boost/geometry/core/interior_rings.hpp>
  38. #include <boost/geometry/core/mutable_range.hpp>
  39. #include <boost/geometry/core/point_type.hpp>
  40. #include <boost/geometry/core/tag.hpp>
  41. #include <boost/geometry/core/tags.hpp>
  42. #include <boost/geometry/geometries/adapted/boost_variant.hpp> // For consistency with other functions
  43. #include <boost/geometry/geometries/concepts/check.hpp>
  44. #include <boost/geometry/io/wkt/detail/prefix.hpp>
  45. #include <boost/geometry/strategies/io/cartesian.hpp>
  46. #include <boost/geometry/strategies/io/geographic.hpp>
  47. #include <boost/geometry/strategies/io/spherical.hpp>
  48. #include <boost/geometry/util/coordinate_cast.hpp>
  49. #include <boost/geometry/util/range.hpp>
  50. #include <boost/geometry/util/sequence.hpp>
  51. #include <boost/geometry/util/type_traits.hpp>
  52. namespace boost { namespace geometry
  53. {
  54. /*!
  55. \brief Exception showing things wrong with WKT parsing
  56. \ingroup wkt
  57. */
  58. struct read_wkt_exception : public geometry::exception
  59. {
  60. template <typename Iterator>
  61. read_wkt_exception(std::string const& msg,
  62. Iterator const& it,
  63. Iterator const& end,
  64. std::string const& wkt)
  65. : message(msg)
  66. , wkt(wkt)
  67. {
  68. if (it != end)
  69. {
  70. source = " at '";
  71. source += it->c_str();
  72. source += "'";
  73. }
  74. complete = message + source + " in '" + wkt.substr(0, 100) + "'";
  75. }
  76. read_wkt_exception(std::string const& msg, std::string const& wkt)
  77. : message(msg)
  78. , wkt(wkt)
  79. {
  80. complete = message + "' in (" + wkt.substr(0, 100) + ")";
  81. }
  82. const char* what() const noexcept override
  83. {
  84. return complete.c_str();
  85. }
  86. private :
  87. std::string source;
  88. std::string message;
  89. std::string wkt;
  90. std::string complete;
  91. };
  92. #ifndef DOXYGEN_NO_DETAIL
  93. // (wkt: Well Known Text, defined by OGC for all geometries and implemented by e.g. databases (MySQL, PostGIS))
  94. namespace detail { namespace wkt
  95. {
  96. inline auto make_tokenizer(std::string const& wkt)
  97. {
  98. using separator = boost::char_separator<char>;
  99. using tokenizer = boost::tokenizer<separator>;
  100. const tokenizer tokens(wkt, separator(" \n\t\r", ",()"));
  101. return tokens;
  102. }
  103. template <typename Point,
  104. std::size_t Dimension = 0,
  105. std::size_t DimensionCount = geometry::dimension<Point>::value>
  106. struct parsing_assigner
  107. {
  108. template <typename TokenizerIterator>
  109. static inline void apply(TokenizerIterator& it,
  110. TokenizerIterator const& end,
  111. Point& point,
  112. std::string const& wkt)
  113. {
  114. using coordinate_type = coordinate_type_t<Point>;
  115. // Stop at end of tokens, or at "," ot ")"
  116. bool finished = (it == end || *it == "," || *it == ")");
  117. try
  118. {
  119. // Initialize missing coordinates to default constructor (zero)
  120. // OR
  121. // Use lexical_cast for conversion to double/int
  122. // Note that it is much slower than atof. However, it is more standard
  123. // and in parsing the change in performance falls probably away against
  124. // the tokenizing
  125. set<Dimension>(point, finished
  126. ? coordinate_type()
  127. : coordinate_cast<coordinate_type>::apply(*it));
  128. }
  129. catch(boost::bad_lexical_cast const& blc)
  130. {
  131. BOOST_THROW_EXCEPTION(read_wkt_exception(blc.what(), it, end, wkt));
  132. }
  133. catch(std::exception const& e)
  134. {
  135. BOOST_THROW_EXCEPTION(read_wkt_exception(e.what(), it, end, wkt));
  136. }
  137. catch(...)
  138. {
  139. BOOST_THROW_EXCEPTION(read_wkt_exception("", it, end, wkt));
  140. }
  141. parsing_assigner<Point, Dimension + 1, DimensionCount>::apply(
  142. (finished ? it : ++it), end, point, wkt);
  143. }
  144. };
  145. template <typename Point, std::size_t DimensionCount>
  146. struct parsing_assigner<Point, DimensionCount, DimensionCount>
  147. {
  148. template <typename TokenizerIterator>
  149. static inline void apply(TokenizerIterator&,
  150. TokenizerIterator const&,
  151. Point&,
  152. std::string const&)
  153. {
  154. }
  155. };
  156. template <typename Iterator>
  157. inline void handle_open_parenthesis(Iterator& it,
  158. Iterator const& end,
  159. std::string const& wkt)
  160. {
  161. if (it == end || *it != "(")
  162. {
  163. BOOST_THROW_EXCEPTION(read_wkt_exception("Expected '('", it, end, wkt));
  164. }
  165. ++it;
  166. }
  167. template <typename Iterator>
  168. inline void handle_close_parenthesis(Iterator& it,
  169. Iterator const& end,
  170. std::string const& wkt)
  171. {
  172. if (it != end && *it == ")")
  173. {
  174. ++it;
  175. }
  176. else
  177. {
  178. BOOST_THROW_EXCEPTION(read_wkt_exception("Expected ')'", it, end, wkt));
  179. }
  180. }
  181. template <typename Iterator>
  182. inline void check_end(Iterator& it,
  183. Iterator const& end,
  184. std::string const& wkt)
  185. {
  186. if (it != end)
  187. {
  188. BOOST_THROW_EXCEPTION(read_wkt_exception("Too many tokens", it, end, wkt));
  189. }
  190. }
  191. /*!
  192. \brief Internal, parses coordinate sequences, strings are formated like "(1 2,3 4,...)"
  193. \param it token-iterator, should be pre-positioned at "(", is post-positions after last ")"
  194. \param end end-token-iterator
  195. \param out Output itererator receiving coordinates
  196. */
  197. template <typename Point>
  198. struct container_inserter
  199. {
  200. // Version with output iterator
  201. template <typename TokenizerIterator, typename OutputIterator>
  202. static inline void apply(TokenizerIterator& it,
  203. TokenizerIterator const& end,
  204. std::string const& wkt,
  205. OutputIterator out)
  206. {
  207. handle_open_parenthesis(it, end, wkt);
  208. Point point;
  209. // Parse points until closing parenthesis
  210. while (it != end && *it != ")")
  211. {
  212. parsing_assigner<Point>::apply(it, end, point, wkt);
  213. out = point;
  214. ++out;
  215. if (it != end && *it == ",")
  216. {
  217. ++it;
  218. }
  219. }
  220. handle_close_parenthesis(it, end, wkt);
  221. }
  222. };
  223. template <typename Geometry,
  224. closure_selector Closure = closure<Geometry>::value>
  225. struct stateful_range_appender
  226. {
  227. // NOTE: Geometry is a reference
  228. inline void append(Geometry geom, geometry::point_type_t<Geometry> const& point, bool)
  229. {
  230. geometry::append(geom, point);
  231. }
  232. };
  233. template <typename Geometry>
  234. struct stateful_range_appender<Geometry, open>
  235. {
  236. using point_type = geometry::point_type_t<Geometry>;
  237. using size_type = typename boost::range_size<util::remove_cptrref_t<Geometry>>::type;
  238. BOOST_STATIC_ASSERT((util::is_ring<Geometry>::value));
  239. inline stateful_range_appender()
  240. : pt_index(0)
  241. {}
  242. // NOTE: Geometry is a reference
  243. inline void append(Geometry geom, point_type const& point, bool is_next_expected)
  244. {
  245. bool should_append = true;
  246. if (pt_index == 0)
  247. {
  248. first_point = point;
  249. }
  250. else
  251. {
  252. // NOTE: if there are not enough Points, they're always appended
  253. should_append
  254. = is_next_expected
  255. || pt_index < core_detail::closure::minimum_ring_size<open>::value
  256. || disjoint(point, first_point);
  257. }
  258. ++pt_index;
  259. if (should_append)
  260. {
  261. geometry::append(geom, point);
  262. }
  263. }
  264. private:
  265. static inline bool disjoint(point_type const& p1, point_type const& p2)
  266. {
  267. // TODO: pass strategy
  268. using strategy_type = typename strategies::io::services::default_strategy
  269. <
  270. point_type
  271. >::type;
  272. return detail::disjoint::disjoint_point_point(p1, p2, strategy_type());
  273. }
  274. size_type pt_index;
  275. point_type first_point;
  276. };
  277. // Geometry is a value-type or reference-type
  278. template <typename Geometry>
  279. struct container_appender
  280. {
  281. using point_type = geometry::point_type_t<Geometry>;
  282. template <typename TokenizerIterator>
  283. static inline void apply(TokenizerIterator& it,
  284. TokenizerIterator const& end,
  285. std::string const& wkt,
  286. Geometry out)
  287. {
  288. handle_open_parenthesis(it, end, wkt);
  289. stateful_range_appender<Geometry> appender;
  290. // Parse points until closing parenthesis
  291. while (it != end && *it != ")")
  292. {
  293. point_type point;
  294. parsing_assigner<point_type>::apply(it, end, point, wkt);
  295. bool const is_next_expected = it != end && *it == ",";
  296. appender.append(out, point, is_next_expected);
  297. if (is_next_expected)
  298. {
  299. ++it;
  300. }
  301. }
  302. handle_close_parenthesis(it, end, wkt);
  303. }
  304. };
  305. /*!
  306. \brief Internal, parses a point from a string like this "(x y)"
  307. \note used for parsing points and multi-points
  308. */
  309. template <typename P>
  310. struct point_parser
  311. {
  312. template <typename TokenizerIterator>
  313. static inline void apply(TokenizerIterator& it,
  314. TokenizerIterator const& end,
  315. std::string const& wkt,
  316. P& point)
  317. {
  318. handle_open_parenthesis(it, end, wkt);
  319. parsing_assigner<P>::apply(it, end, point, wkt);
  320. handle_close_parenthesis(it, end, wkt);
  321. }
  322. };
  323. template <typename Geometry>
  324. struct linestring_parser
  325. {
  326. template <typename TokenizerIterator>
  327. static inline void apply(TokenizerIterator& it,
  328. TokenizerIterator const& end,
  329. std::string const& wkt,
  330. Geometry& geometry)
  331. {
  332. container_appender<Geometry&>::apply(it, end, wkt, geometry);
  333. }
  334. };
  335. template <typename Ring>
  336. struct ring_parser
  337. {
  338. template <typename TokenizerIterator>
  339. static inline void apply(TokenizerIterator& it,
  340. TokenizerIterator const& end,
  341. std::string const& wkt,
  342. Ring& ring)
  343. {
  344. // A ring should look like polygon((x y,x y,x y...))
  345. // So handle the extra opening/closing parentheses
  346. // and in between parse using the container-inserter
  347. handle_open_parenthesis(it, end, wkt);
  348. container_appender<Ring&>::apply(it, end, wkt, ring);
  349. handle_close_parenthesis(it, end, wkt);
  350. }
  351. };
  352. /*!
  353. \brief Internal, parses a polygon from a string like this "((x y,x y),(x y,x y))"
  354. \note used for parsing polygons and multi-polygons
  355. */
  356. template <typename Polygon>
  357. struct polygon_parser
  358. {
  359. using appender = container_appender<ring_return_type_t<Polygon>>;
  360. template <typename TokenizerIterator>
  361. static inline void apply(TokenizerIterator& it,
  362. TokenizerIterator const& end,
  363. std::string const& wkt,
  364. Polygon& poly)
  365. {
  366. handle_open_parenthesis(it, end, wkt);
  367. int n = -1;
  368. // Stop at ")"
  369. while (it != end && *it != ")")
  370. {
  371. // Parse ring
  372. if (++n == 0)
  373. {
  374. appender::apply(it, end, wkt, exterior_ring(poly));
  375. }
  376. else
  377. {
  378. ring_type_t<Polygon> ring;
  379. appender::apply(it, end, wkt, ring);
  380. range::push_back(geometry::interior_rings(poly), std::move(ring));
  381. }
  382. if (it != end && *it == ",")
  383. {
  384. // Skip "," after ring is parsed
  385. ++it;
  386. }
  387. }
  388. handle_close_parenthesis(it, end, wkt);
  389. }
  390. };
  391. template<typename PolyhedralSurface>
  392. struct polyhedral_surface_parser
  393. {
  394. using polygon_t = typename PolyhedralSurface::polygon_type;
  395. template <typename TokenizerIterator>
  396. static inline void apply(TokenizerIterator& it,
  397. TokenizerIterator const& end,
  398. std::string const& wkt,
  399. PolyhedralSurface& polyhedral)
  400. {
  401. handle_open_parenthesis(it, end, wkt);
  402. // Parse polygons
  403. while (it != end && *it != ")")
  404. {
  405. traits::resize<PolyhedralSurface>::apply(polyhedral, boost::size(polyhedral) + 1);
  406. polygon_parser<polygon_t>::apply(it, end, wkt, *(boost::end(polyhedral) - 1));
  407. if (it != end && *it == ",")
  408. {
  409. // Skip "," after multi-element is parsed
  410. ++it;
  411. }
  412. }
  413. handle_close_parenthesis(it, end, wkt);
  414. }
  415. };
  416. template <typename TokenizerIterator>
  417. inline bool one_of(TokenizerIterator const& it,
  418. std::string const& value,
  419. bool& is_present)
  420. {
  421. if (boost::iequals(*it, value))
  422. {
  423. is_present = true;
  424. return true;
  425. }
  426. return false;
  427. }
  428. template <typename TokenizerIterator>
  429. inline bool one_of(TokenizerIterator const& it,
  430. std::string const& value,
  431. bool& present1,
  432. bool& present2)
  433. {
  434. if (boost::iequals(*it, value))
  435. {
  436. present1 = true;
  437. present2 = true;
  438. return true;
  439. }
  440. return false;
  441. }
  442. template <typename TokenizerIterator>
  443. inline void handle_empty_z_m(TokenizerIterator& it,
  444. TokenizerIterator const& end,
  445. bool& has_empty,
  446. bool& has_z,
  447. bool& has_m)
  448. {
  449. has_empty = false;
  450. has_z = false;
  451. has_m = false;
  452. // WKT can optionally have Z and M (measured) values as in
  453. // POINT ZM (1 1 5 60), POINT M (1 1 80), POINT Z (1 1 5)
  454. // GGL supports any of them as coordinate values, but is not aware
  455. // of any Measured value.
  456. while (it != end
  457. && (one_of(it, "M", has_m)
  458. || one_of(it, "Z", has_z)
  459. || one_of(it, "EMPTY", has_empty)
  460. || one_of(it, "MZ", has_m, has_z)
  461. || one_of(it, "ZM", has_z, has_m)
  462. )
  463. )
  464. {
  465. ++it;
  466. }
  467. }
  468. template <typename Geometry, typename Tag = geometry::tag_t<Geometry>>
  469. struct dimension
  470. : geometry::dimension<Geometry>
  471. {};
  472. // TODO: For now assume the dimension of the first type defined for GC
  473. // This should probably be unified for all algorithms
  474. template <typename Geometry>
  475. struct dimension<Geometry, geometry_collection_tag>
  476. : geometry::dimension
  477. <
  478. typename util::sequence_front
  479. <
  480. typename traits::geometry_types<Geometry>::type
  481. >::type
  482. >
  483. {};
  484. /*!
  485. \brief Internal, starts parsing
  486. \param geometry_name string to compare with first token
  487. */
  488. template <typename Geometry, typename TokenizerIterator>
  489. inline bool initialize(TokenizerIterator& it,
  490. TokenizerIterator const& end,
  491. std::string const& wkt,
  492. std::string const& geometry_name)
  493. {
  494. if (it == end || ! boost::iequals(*it++, geometry_name))
  495. {
  496. BOOST_THROW_EXCEPTION(read_wkt_exception(std::string("Should start with '") + geometry_name + "'", wkt));
  497. }
  498. bool has_empty, has_z, has_m;
  499. handle_empty_z_m(it, end, has_empty, has_z, has_m);
  500. // Silence warning C4127: conditional expression is constant
  501. #if defined(_MSC_VER)
  502. #pragma warning(push)
  503. #pragma warning(disable : 4127)
  504. #endif
  505. if (has_z && dimension<Geometry>::value < 3)
  506. {
  507. BOOST_THROW_EXCEPTION(read_wkt_exception("Z only allowed for 3 or more dimensions", wkt));
  508. }
  509. #if defined(_MSC_VER)
  510. #pragma warning(pop)
  511. #endif
  512. if (has_empty)
  513. {
  514. return false;
  515. }
  516. // M is ignored at all.
  517. return true;
  518. }
  519. template <typename Geometry, template<typename> class Parser, typename PrefixPolicy>
  520. struct geometry_parser
  521. {
  522. static inline void apply(std::string const& wkt, Geometry& geometry)
  523. {
  524. geometry::clear(geometry);
  525. auto const tokens{make_tokenizer(wkt)};
  526. auto it = tokens.begin();
  527. auto const end = tokens.end();
  528. apply(it, end, wkt, geometry);
  529. check_end(it, end, wkt);
  530. }
  531. template <typename TokenizerIterator>
  532. static inline void apply(TokenizerIterator& it,
  533. TokenizerIterator const& end,
  534. std::string const& wkt,
  535. Geometry& geometry)
  536. {
  537. if (initialize<Geometry>(it, end, wkt, PrefixPolicy::apply()))
  538. {
  539. Parser<Geometry>::apply(it, end, wkt, geometry);
  540. }
  541. }
  542. };
  543. template <typename MultiGeometry, template<typename> class Parser, typename PrefixPolicy>
  544. struct multi_parser
  545. {
  546. static inline void apply(std::string const& wkt, MultiGeometry& geometry)
  547. {
  548. traits::clear<MultiGeometry>::apply(geometry);
  549. auto const tokens{make_tokenizer(wkt)};
  550. auto it = tokens.begin();
  551. auto const end = tokens.end();
  552. apply(it, end, wkt, geometry);
  553. check_end(it, end, wkt);
  554. }
  555. template <typename TokenizerIterator>
  556. static inline void apply(TokenizerIterator& it,
  557. TokenizerIterator const& end,
  558. std::string const& wkt,
  559. MultiGeometry& geometry)
  560. {
  561. if (initialize<MultiGeometry>(it, end, wkt, PrefixPolicy::apply()))
  562. {
  563. handle_open_parenthesis(it, end, wkt);
  564. // Parse sub-geometries
  565. while(it != end && *it != ")")
  566. {
  567. traits::resize<MultiGeometry>::apply(geometry, boost::size(geometry) + 1);
  568. Parser
  569. <
  570. typename boost::range_value<MultiGeometry>::type
  571. >::apply(it, end, wkt, *(boost::end(geometry) - 1));
  572. if (it != end && *it == ",")
  573. {
  574. // Skip "," after multi-element is parsed
  575. ++it;
  576. }
  577. }
  578. handle_close_parenthesis(it, end, wkt);
  579. }
  580. }
  581. };
  582. template <typename P>
  583. struct noparenthesis_point_parser
  584. {
  585. template <typename TokenizerIterator>
  586. static inline void apply(TokenizerIterator& it,
  587. TokenizerIterator const& end,
  588. std::string const& wkt,
  589. P& point)
  590. {
  591. parsing_assigner<P>::apply(it, end, point, wkt);
  592. }
  593. };
  594. template <typename MultiGeometry, typename PrefixPolicy>
  595. struct multi_point_parser
  596. {
  597. static inline void apply(std::string const& wkt, MultiGeometry& geometry)
  598. {
  599. traits::clear<MultiGeometry>::apply(geometry);
  600. auto const tokens{make_tokenizer(wkt)};
  601. auto it = tokens.begin();
  602. auto const end = tokens.end();
  603. apply(it, end, wkt, geometry);
  604. check_end(it, end, wkt);
  605. }
  606. template <typename TokenizerIterator>
  607. static inline void apply(TokenizerIterator& it,
  608. TokenizerIterator const& end,
  609. std::string const& wkt,
  610. MultiGeometry& geometry)
  611. {
  612. if (initialize<MultiGeometry>(it, end, wkt, PrefixPolicy::apply()))
  613. {
  614. handle_open_parenthesis(it, end, wkt);
  615. // If first point definition starts with "(" then parse points as (x y)
  616. // otherwise as "x y"
  617. bool using_brackets = (it != end && *it == "(");
  618. while(it != end && *it != ")")
  619. {
  620. traits::resize<MultiGeometry>::apply(geometry, boost::size(geometry) + 1);
  621. if (using_brackets)
  622. {
  623. point_parser
  624. <
  625. typename boost::range_value<MultiGeometry>::type
  626. >::apply(it, end, wkt, *(boost::end(geometry) - 1));
  627. }
  628. else
  629. {
  630. noparenthesis_point_parser
  631. <
  632. typename boost::range_value<MultiGeometry>::type
  633. >::apply(it, end, wkt, *(boost::end(geometry) - 1));
  634. }
  635. if (it != end && *it == ",")
  636. {
  637. // Skip "," after point is parsed
  638. ++it;
  639. }
  640. }
  641. handle_close_parenthesis(it, end, wkt);
  642. }
  643. }
  644. };
  645. /*!
  646. \brief Supports box parsing
  647. \note OGC does not define the box geometry, and WKT does not support boxes.
  648. However, to be generic GGL supports reading and writing from and to boxes.
  649. Boxes are outputted as a standard POLYGON. GGL can read boxes from
  650. a standard POLYGON, from a POLYGON with 2 points of from a BOX
  651. \tparam Box the box
  652. */
  653. template <typename Box>
  654. struct box_parser
  655. {
  656. static inline void apply(std::string const& wkt, Box& box)
  657. {
  658. auto const tokens{make_tokenizer(wkt)};
  659. auto it = tokens.begin();
  660. auto const end = tokens.end();
  661. apply(it, end, wkt, box);
  662. check_end(it, end, wkt);
  663. }
  664. template <typename TokenizerIterator>
  665. static inline void apply(TokenizerIterator& it,
  666. TokenizerIterator const& end,
  667. std::string const& wkt,
  668. Box& box)
  669. {
  670. bool should_close = false;
  671. if (it != end && boost::iequals(*it, "POLYGON"))
  672. {
  673. ++it;
  674. bool has_empty, has_z, has_m;
  675. handle_empty_z_m(it, end, has_empty, has_z, has_m);
  676. if (has_empty)
  677. {
  678. assign_zero(box);
  679. return;
  680. }
  681. handle_open_parenthesis(it, end, wkt);
  682. should_close = true;
  683. }
  684. else if (it != end && boost::iequals(*it, "BOX"))
  685. {
  686. ++it;
  687. }
  688. else
  689. {
  690. BOOST_THROW_EXCEPTION(read_wkt_exception("Should start with 'POLYGON' or 'BOX'", wkt));
  691. }
  692. using point_type = point_type_t<Box>;
  693. std::vector<point_type> points;
  694. container_inserter<point_type>::apply(it, end, wkt, std::back_inserter(points));
  695. if (should_close)
  696. {
  697. handle_close_parenthesis(it, end, wkt);
  698. }
  699. unsigned int index = 0;
  700. std::size_t n = boost::size(points);
  701. if (n == 2)
  702. {
  703. index = 1;
  704. }
  705. else if (n == 4 || n == 5)
  706. {
  707. // In case of 4 or 5 points, we do not check the other ones, just
  708. // take the opposite corner which is always 2
  709. index = 2;
  710. }
  711. else
  712. {
  713. BOOST_THROW_EXCEPTION(read_wkt_exception("Box should have 2,4 or 5 points", wkt));
  714. }
  715. geometry::detail::assign_point_to_index<min_corner>(points.front(), box);
  716. geometry::detail::assign_point_to_index<max_corner>(points[index], box);
  717. }
  718. };
  719. /*!
  720. \brief Supports segment parsing
  721. \note OGC does not define the segment, and WKT does not support segmentes.
  722. However, it is useful to implement it, also for testing purposes
  723. \tparam Segment the segment
  724. */
  725. template <typename Segment>
  726. struct segment_parser
  727. {
  728. static inline void apply(std::string const& wkt, Segment& segment)
  729. {
  730. auto const tokens{make_tokenizer(wkt)};
  731. auto it = tokens.begin();
  732. auto const end = tokens.end();
  733. apply(it, end, wkt, segment);
  734. check_end(it, end, wkt);
  735. }
  736. template <typename TokenizerIterator>
  737. static inline void apply(TokenizerIterator& it,
  738. TokenizerIterator const& end,
  739. std::string const& wkt,
  740. Segment& segment)
  741. {
  742. if (it != end
  743. && (boost::iequals(*it, prefix_segment::apply())
  744. || boost::iequals(*it, prefix_linestring::apply())))
  745. {
  746. ++it;
  747. }
  748. else
  749. {
  750. BOOST_THROW_EXCEPTION(read_wkt_exception("Should start with 'LINESTRING' or 'SEGMENT'", wkt));
  751. }
  752. using point_type = point_type_t<Segment>;
  753. std::vector<point_type> points;
  754. container_inserter<point_type>::apply(it, end, wkt, std::back_inserter(points));
  755. if (boost::size(points) == 2)
  756. {
  757. geometry::detail::assign_point_to_index<0>(points.front(), segment);
  758. geometry::detail::assign_point_to_index<1>(points.back(), segment);
  759. }
  760. else
  761. {
  762. BOOST_THROW_EXCEPTION(read_wkt_exception("Segment should have 2 points", wkt));
  763. }
  764. }
  765. };
  766. struct dynamic_move_assign
  767. {
  768. template <typename DynamicGeometry, typename Geometry>
  769. static void apply(DynamicGeometry& dynamic_geometry, Geometry & geometry)
  770. {
  771. dynamic_geometry = std::move(geometry);
  772. }
  773. };
  774. struct dynamic_move_emplace_back
  775. {
  776. template <typename GeometryCollection, typename Geometry>
  777. static void apply(GeometryCollection& geometry_collection, Geometry & geometry)
  778. {
  779. traits::emplace_back<GeometryCollection>::apply(geometry_collection, std::move(geometry));
  780. }
  781. };
  782. template
  783. <
  784. typename Geometry,
  785. template <typename, typename> class ReadWkt,
  786. typename AppendPolicy
  787. >
  788. struct dynamic_readwkt_caller
  789. {
  790. template <typename TokenizerIterator>
  791. static inline void apply(TokenizerIterator& it,
  792. TokenizerIterator const& end,
  793. std::string const& wkt,
  794. Geometry& geometry)
  795. {
  796. static const char* tag_point = prefix_point::apply();
  797. static const char* tag_linestring = prefix_linestring::apply();
  798. static const char* tag_polygon = prefix_polygon::apply();
  799. static const char* tag_multi_point = prefix_multipoint::apply();
  800. static const char* tag_multi_linestring = prefix_multilinestring::apply();
  801. static const char* tag_multi_polygon = prefix_multipolygon::apply();
  802. static const char* tag_segment = prefix_segment::apply();
  803. static const char* tag_box = prefix_box::apply();
  804. static const char* tag_gc = prefix_geometrycollection::apply();
  805. if (boost::iequals(*it, tag_point))
  806. {
  807. parse_geometry<util::is_point>(tag_point, it, end, wkt, geometry);
  808. }
  809. else if (boost::iequals(*it, tag_multi_point))
  810. {
  811. parse_geometry<util::is_multi_point>(tag_multi_point, it, end, wkt, geometry);
  812. }
  813. else if (boost::iequals(*it, tag_segment))
  814. {
  815. parse_geometry<util::is_segment>(tag_segment, it, end, wkt, geometry);
  816. }
  817. else if (boost::iequals(*it, tag_linestring))
  818. {
  819. parse_geometry<util::is_linestring>(tag_linestring, it, end, wkt, geometry, false)
  820. || parse_geometry<util::is_segment>(tag_linestring, it, end, wkt, geometry);
  821. }
  822. else if (boost::iequals(*it, tag_multi_linestring))
  823. {
  824. parse_geometry<util::is_multi_linestring>(tag_multi_linestring, it, end, wkt, geometry);
  825. }
  826. else if (boost::iequals(*it, tag_box))
  827. {
  828. parse_geometry<util::is_box>(tag_box, it, end, wkt, geometry);
  829. }
  830. else if (boost::iequals(*it, tag_polygon))
  831. {
  832. parse_geometry<util::is_polygon>(tag_polygon, it, end, wkt, geometry, false)
  833. || parse_geometry<util::is_ring>(tag_polygon, it, end, wkt, geometry, false)
  834. || parse_geometry<util::is_box>(tag_polygon, it, end, wkt, geometry);
  835. }
  836. else if (boost::iequals(*it, tag_multi_polygon))
  837. {
  838. parse_geometry<util::is_multi_polygon>(tag_multi_polygon, it, end, wkt, geometry);
  839. }
  840. else if (boost::iequals(*it, tag_gc))
  841. {
  842. parse_geometry<util::is_geometry_collection>(tag_gc, it, end, wkt, geometry);
  843. }
  844. else
  845. {
  846. BOOST_THROW_EXCEPTION(read_wkt_exception(
  847. "Should start with geometry's type, for example 'POINT', 'LINESTRING', 'POLYGON'",
  848. wkt));
  849. }
  850. }
  851. private:
  852. template
  853. <
  854. template <typename> class UnaryPred,
  855. typename TokenizerIterator,
  856. typename Geom = typename util::sequence_find_if
  857. <
  858. typename traits::geometry_types<Geometry>::type, UnaryPred
  859. >::type,
  860. std::enable_if_t<! std::is_void<Geom>::value, int> = 0
  861. >
  862. static bool parse_geometry(const char * ,
  863. TokenizerIterator& it,
  864. TokenizerIterator const& end,
  865. std::string const& wkt,
  866. Geometry& geometry,
  867. bool = true)
  868. {
  869. Geom g;
  870. ReadWkt<Geom, tag_t<Geom>>::apply(it, end, wkt, g);
  871. AppendPolicy::apply(geometry, g);
  872. return true;
  873. }
  874. template
  875. <
  876. template <typename> class UnaryPred,
  877. typename TokenizerIterator,
  878. typename Geom = typename util::sequence_find_if
  879. <
  880. typename traits::geometry_types<Geometry>::type, UnaryPred
  881. >::type,
  882. std::enable_if_t<std::is_void<Geom>::value, int> = 0
  883. >
  884. static bool parse_geometry(const char * name,
  885. TokenizerIterator& ,
  886. TokenizerIterator const& ,
  887. std::string const& wkt,
  888. Geometry& ,
  889. bool throw_on_misfit = true)
  890. {
  891. if (throw_on_misfit)
  892. {
  893. std::string msg = std::string("Unable to store '") + name + "' in this geometry";
  894. BOOST_THROW_EXCEPTION(read_wkt_exception(msg, wkt));
  895. }
  896. return false;
  897. }
  898. };
  899. }} // namespace detail::wkt
  900. #endif // DOXYGEN_NO_DETAIL
  901. #ifndef DOXYGEN_NO_DISPATCH
  902. namespace dispatch
  903. {
  904. template <typename Geometry, typename Tag = tag_t<Geometry>>
  905. struct read_wkt {};
  906. template <typename Point>
  907. struct read_wkt<Point, point_tag>
  908. : detail::wkt::geometry_parser
  909. <
  910. Point,
  911. detail::wkt::point_parser,
  912. detail::wkt::prefix_point
  913. >
  914. {};
  915. template <typename L>
  916. struct read_wkt<L, linestring_tag>
  917. : detail::wkt::geometry_parser
  918. <
  919. L,
  920. detail::wkt::linestring_parser,
  921. detail::wkt::prefix_linestring
  922. >
  923. {};
  924. template <typename Ring>
  925. struct read_wkt<Ring, ring_tag>
  926. : detail::wkt::geometry_parser
  927. <
  928. Ring,
  929. detail::wkt::ring_parser,
  930. detail::wkt::prefix_polygon
  931. >
  932. {};
  933. template <typename Geometry>
  934. struct read_wkt<Geometry, polygon_tag>
  935. : detail::wkt::geometry_parser
  936. <
  937. Geometry,
  938. detail::wkt::polygon_parser,
  939. detail::wkt::prefix_polygon
  940. >
  941. {};
  942. template <typename Geometry>
  943. struct read_wkt<Geometry, polyhedral_surface_tag>
  944. : detail::wkt::geometry_parser
  945. <
  946. Geometry,
  947. detail::wkt::polyhedral_surface_parser,
  948. detail::wkt::prefix_polyhedral_surface
  949. >
  950. {};
  951. template <typename MultiGeometry>
  952. struct read_wkt<MultiGeometry, multi_point_tag>
  953. : detail::wkt::multi_point_parser
  954. <
  955. MultiGeometry,
  956. detail::wkt::prefix_multipoint
  957. >
  958. {};
  959. template <typename MultiGeometry>
  960. struct read_wkt<MultiGeometry, multi_linestring_tag>
  961. : detail::wkt::multi_parser
  962. <
  963. MultiGeometry,
  964. detail::wkt::linestring_parser,
  965. detail::wkt::prefix_multilinestring
  966. >
  967. {};
  968. template <typename MultiGeometry>
  969. struct read_wkt<MultiGeometry, multi_polygon_tag>
  970. : detail::wkt::multi_parser
  971. <
  972. MultiGeometry,
  973. detail::wkt::polygon_parser,
  974. detail::wkt::prefix_multipolygon
  975. >
  976. {};
  977. // Box (Non-OGC)
  978. template <typename Box>
  979. struct read_wkt<Box, box_tag>
  980. : detail::wkt::box_parser<Box>
  981. {};
  982. // Segment (Non-OGC)
  983. template <typename Segment>
  984. struct read_wkt<Segment, segment_tag>
  985. : detail::wkt::segment_parser<Segment>
  986. {};
  987. template <typename DynamicGeometry>
  988. struct read_wkt<DynamicGeometry, dynamic_geometry_tag>
  989. {
  990. static inline void apply(std::string const& wkt, DynamicGeometry& dynamic_geometry)
  991. {
  992. auto tokens{detail::wkt::make_tokenizer(wkt)};
  993. auto it = tokens.begin();
  994. auto const end = tokens.end();
  995. if (it == end)
  996. {
  997. BOOST_THROW_EXCEPTION(read_wkt_exception(
  998. "Should start with geometry's type, for example 'POINT', 'LINESTRING', 'POLYGON'",
  999. wkt));
  1000. }
  1001. detail::wkt::dynamic_readwkt_caller
  1002. <
  1003. DynamicGeometry, dispatch::read_wkt, detail::wkt::dynamic_move_assign
  1004. >::apply(it, end, wkt, dynamic_geometry);
  1005. detail::wkt::check_end(it, end, wkt);
  1006. }
  1007. };
  1008. template <typename Geometry>
  1009. struct read_wkt<Geometry, geometry_collection_tag>
  1010. {
  1011. static inline void apply(std::string const& wkt, Geometry& geometry)
  1012. {
  1013. range::clear(geometry);
  1014. auto tokens{detail::wkt::make_tokenizer(wkt)};
  1015. auto it = tokens.begin();
  1016. auto const end = tokens.end();
  1017. apply(it, end, wkt, geometry);
  1018. detail::wkt::check_end(it, end, wkt);
  1019. }
  1020. template <typename TokenizerIterator>
  1021. static inline void apply(TokenizerIterator& it,
  1022. TokenizerIterator const& end,
  1023. std::string const& wkt,
  1024. Geometry& geometry)
  1025. {
  1026. if (detail::wkt::initialize<Geometry>(it, end, wkt,
  1027. detail::wkt::prefix_geometrycollection::apply()))
  1028. {
  1029. detail::wkt::handle_open_parenthesis(it, end, wkt);
  1030. // Stop at ")"
  1031. while (it != end && *it != ")")
  1032. {
  1033. detail::wkt::dynamic_readwkt_caller
  1034. <
  1035. Geometry, dispatch::read_wkt, detail::wkt::dynamic_move_emplace_back
  1036. >::apply(it, end, wkt, geometry);
  1037. if (it != end && *it == ",")
  1038. {
  1039. // Skip "," after geometry is parsed
  1040. ++it;
  1041. }
  1042. }
  1043. detail::wkt::handle_close_parenthesis(it, end, wkt);
  1044. }
  1045. }
  1046. };
  1047. } // namespace dispatch
  1048. #endif // DOXYGEN_NO_DISPATCH
  1049. /*!
  1050. \brief Parses OGC \well_known_text (\wkt) into a geometry (any geometry)
  1051. \ingroup wkt
  1052. \tparam Geometry \tparam_geometry
  1053. \param wkt string containing \wkt
  1054. \param geometry \param_geometry output geometry
  1055. \ingroup wkt
  1056. \qbk{[include reference/io/read_wkt.qbk]}
  1057. */
  1058. template <typename Geometry>
  1059. inline void read_wkt(std::string const& wkt, Geometry& geometry)
  1060. {
  1061. geometry::concepts::check<Geometry>();
  1062. dispatch::read_wkt<Geometry>::apply(wkt, geometry);
  1063. }
  1064. /*!
  1065. \brief Parses OGC \well_known_text (\wkt) into a geometry (any geometry) and returns it
  1066. \ingroup wkt
  1067. \tparam Geometry \tparam_geometry
  1068. \param wkt string containing \wkt
  1069. \ingroup wkt
  1070. \qbk{[include reference/io/from_wkt.qbk]}
  1071. */
  1072. template <typename Geometry>
  1073. inline Geometry from_wkt(std::string const& wkt)
  1074. {
  1075. Geometry geometry;
  1076. geometry::concepts::check<Geometry>();
  1077. dispatch::read_wkt<Geometry>::apply(wkt, geometry);
  1078. return geometry;
  1079. }
  1080. }} // namespace boost::geometry
  1081. #endif // BOOST_GEOMETRY_IO_WKT_READ_HPP