read.hpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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 = typename coordinate_type<Point>::type;
  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, typename geometry::point_type<Geometry>::type 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 = typename geometry::point_type<Geometry>::type;
  237. using size_type = typename boost::range_size
  238. <
  239. typename util::remove_cptrref<Geometry>::type
  240. >::type;
  241. BOOST_STATIC_ASSERT((util::is_ring<Geometry>::value));
  242. inline stateful_range_appender()
  243. : pt_index(0)
  244. {}
  245. // NOTE: Geometry is a reference
  246. inline void append(Geometry geom, point_type const& point, bool is_next_expected)
  247. {
  248. bool should_append = true;
  249. if (pt_index == 0)
  250. {
  251. first_point = point;
  252. }
  253. else
  254. {
  255. // NOTE: if there are not enough Points, they're always appended
  256. should_append
  257. = is_next_expected
  258. || pt_index < core_detail::closure::minimum_ring_size<open>::value
  259. || disjoint(point, first_point);
  260. }
  261. ++pt_index;
  262. if (should_append)
  263. {
  264. geometry::append(geom, point);
  265. }
  266. }
  267. private:
  268. static inline bool disjoint(point_type const& p1, point_type const& p2)
  269. {
  270. // TODO: pass strategy
  271. using strategy_type = typename strategies::io::services::default_strategy
  272. <
  273. point_type
  274. >::type;
  275. return detail::disjoint::disjoint_point_point(p1, p2, strategy_type());
  276. }
  277. size_type pt_index;
  278. point_type first_point;
  279. };
  280. // Geometry is a value-type or reference-type
  281. template <typename Geometry>
  282. struct container_appender
  283. {
  284. using point_type = typename geometry::point_type<Geometry>::type;
  285. template <typename TokenizerIterator>
  286. static inline void apply(TokenizerIterator& it,
  287. TokenizerIterator const& end,
  288. std::string const& wkt,
  289. Geometry out)
  290. {
  291. handle_open_parenthesis(it, end, wkt);
  292. stateful_range_appender<Geometry> appender;
  293. // Parse points until closing parenthesis
  294. while (it != end && *it != ")")
  295. {
  296. point_type point;
  297. parsing_assigner<point_type>::apply(it, end, point, wkt);
  298. bool const is_next_expected = it != end && *it == ",";
  299. appender.append(out, point, is_next_expected);
  300. if (is_next_expected)
  301. {
  302. ++it;
  303. }
  304. }
  305. handle_close_parenthesis(it, end, wkt);
  306. }
  307. };
  308. /*!
  309. \brief Internal, parses a point from a string like this "(x y)"
  310. \note used for parsing points and multi-points
  311. */
  312. template <typename P>
  313. struct point_parser
  314. {
  315. template <typename TokenizerIterator>
  316. static inline void apply(TokenizerIterator& it,
  317. TokenizerIterator const& end,
  318. std::string const& wkt,
  319. P& point)
  320. {
  321. handle_open_parenthesis(it, end, wkt);
  322. parsing_assigner<P>::apply(it, end, point, wkt);
  323. handle_close_parenthesis(it, end, wkt);
  324. }
  325. };
  326. template <typename Geometry>
  327. struct linestring_parser
  328. {
  329. template <typename TokenizerIterator>
  330. static inline void apply(TokenizerIterator& it,
  331. TokenizerIterator const& end,
  332. std::string const& wkt,
  333. Geometry& geometry)
  334. {
  335. container_appender<Geometry&>::apply(it, end, wkt, geometry);
  336. }
  337. };
  338. template <typename Ring>
  339. struct ring_parser
  340. {
  341. template <typename TokenizerIterator>
  342. static inline void apply(TokenizerIterator& it,
  343. TokenizerIterator const& end,
  344. std::string const& wkt,
  345. Ring& ring)
  346. {
  347. // A ring should look like polygon((x y,x y,x y...))
  348. // So handle the extra opening/closing parentheses
  349. // and in between parse using the container-inserter
  350. handle_open_parenthesis(it, end, wkt);
  351. container_appender<Ring&>::apply(it, end, wkt, ring);
  352. handle_close_parenthesis(it, end, wkt);
  353. }
  354. };
  355. /*!
  356. \brief Internal, parses a polygon from a string like this "((x y,x y),(x y,x y))"
  357. \note used for parsing polygons and multi-polygons
  358. */
  359. template <typename Polygon>
  360. struct polygon_parser
  361. {
  362. using ring_return_type = typename ring_return_type<Polygon>::type;
  363. using appender = container_appender<ring_return_type>;
  364. template <typename TokenizerIterator>
  365. static inline void apply(TokenizerIterator& it,
  366. TokenizerIterator const& end,
  367. std::string const& wkt,
  368. Polygon& poly)
  369. {
  370. handle_open_parenthesis(it, end, wkt);
  371. int n = -1;
  372. // Stop at ")"
  373. while (it != end && *it != ")")
  374. {
  375. // Parse ring
  376. if (++n == 0)
  377. {
  378. appender::apply(it, end, wkt, exterior_ring(poly));
  379. }
  380. else
  381. {
  382. typename ring_type<Polygon>::type ring;
  383. appender::apply(it, end, wkt, ring);
  384. range::push_back(geometry::interior_rings(poly), std::move(ring));
  385. }
  386. if (it != end && *it == ",")
  387. {
  388. // Skip "," after ring is parsed
  389. ++it;
  390. }
  391. }
  392. handle_close_parenthesis(it, end, wkt);
  393. }
  394. };
  395. template <typename TokenizerIterator>
  396. inline bool one_of(TokenizerIterator const& it,
  397. std::string const& value,
  398. bool& is_present)
  399. {
  400. if (boost::iequals(*it, value))
  401. {
  402. is_present = true;
  403. return true;
  404. }
  405. return false;
  406. }
  407. template <typename TokenizerIterator>
  408. inline bool one_of(TokenizerIterator const& it,
  409. std::string const& value,
  410. bool& present1,
  411. bool& present2)
  412. {
  413. if (boost::iequals(*it, value))
  414. {
  415. present1 = true;
  416. present2 = true;
  417. return true;
  418. }
  419. return false;
  420. }
  421. template <typename TokenizerIterator>
  422. inline void handle_empty_z_m(TokenizerIterator& it,
  423. TokenizerIterator const& end,
  424. bool& has_empty,
  425. bool& has_z,
  426. bool& has_m)
  427. {
  428. has_empty = false;
  429. has_z = false;
  430. has_m = false;
  431. // WKT can optionally have Z and M (measured) values as in
  432. // POINT ZM (1 1 5 60), POINT M (1 1 80), POINT Z (1 1 5)
  433. // GGL supports any of them as coordinate values, but is not aware
  434. // of any Measured value.
  435. while (it != end
  436. && (one_of(it, "M", has_m)
  437. || one_of(it, "Z", has_z)
  438. || one_of(it, "EMPTY", has_empty)
  439. || one_of(it, "MZ", has_m, has_z)
  440. || one_of(it, "ZM", has_z, has_m)
  441. )
  442. )
  443. {
  444. ++it;
  445. }
  446. }
  447. template <typename Geometry, typename Tag = typename geometry::tag<Geometry>::type>
  448. struct dimension
  449. : geometry::dimension<Geometry>
  450. {};
  451. // TODO: For now assume the dimension of the first type defined for GC
  452. // This should probably be unified for all algorithms
  453. template <typename Geometry>
  454. struct dimension<Geometry, geometry_collection_tag>
  455. : geometry::dimension
  456. <
  457. typename util::sequence_front
  458. <
  459. typename traits::geometry_types<Geometry>::type
  460. >::type
  461. >
  462. {};
  463. /*!
  464. \brief Internal, starts parsing
  465. \param geometry_name string to compare with first token
  466. */
  467. template <typename Geometry, typename TokenizerIterator>
  468. inline bool initialize(TokenizerIterator& it,
  469. TokenizerIterator const& end,
  470. std::string const& wkt,
  471. std::string const& geometry_name)
  472. {
  473. if (it == end || ! boost::iequals(*it++, geometry_name))
  474. {
  475. BOOST_THROW_EXCEPTION(read_wkt_exception(std::string("Should start with '") + geometry_name + "'", wkt));
  476. }
  477. bool has_empty, has_z, has_m;
  478. handle_empty_z_m(it, end, has_empty, has_z, has_m);
  479. // Silence warning C4127: conditional expression is constant
  480. #if defined(_MSC_VER)
  481. #pragma warning(push)
  482. #pragma warning(disable : 4127)
  483. #endif
  484. if (has_z && dimension<Geometry>::value < 3)
  485. {
  486. BOOST_THROW_EXCEPTION(read_wkt_exception("Z only allowed for 3 or more dimensions", wkt));
  487. }
  488. #if defined(_MSC_VER)
  489. #pragma warning(pop)
  490. #endif
  491. if (has_empty)
  492. {
  493. return false;
  494. }
  495. // M is ignored at all.
  496. return true;
  497. }
  498. template <typename Geometry, template<typename> class Parser, typename PrefixPolicy>
  499. struct geometry_parser
  500. {
  501. static inline void apply(std::string const& wkt, Geometry& geometry)
  502. {
  503. geometry::clear(geometry);
  504. auto const tokens{make_tokenizer(wkt)};
  505. auto it = tokens.begin();
  506. auto const end = tokens.end();
  507. apply(it, end, wkt, geometry);
  508. check_end(it, end, wkt);
  509. }
  510. template <typename TokenizerIterator>
  511. static inline void apply(TokenizerIterator& it,
  512. TokenizerIterator const& end,
  513. std::string const& wkt,
  514. Geometry& geometry)
  515. {
  516. if (initialize<Geometry>(it, end, wkt, PrefixPolicy::apply()))
  517. {
  518. Parser<Geometry>::apply(it, end, wkt, geometry);
  519. }
  520. }
  521. };
  522. template <typename MultiGeometry, template<typename> class Parser, typename PrefixPolicy>
  523. struct multi_parser
  524. {
  525. static inline void apply(std::string const& wkt, MultiGeometry& geometry)
  526. {
  527. traits::clear<MultiGeometry>::apply(geometry);
  528. auto const tokens{make_tokenizer(wkt)};
  529. auto it = tokens.begin();
  530. auto const end = tokens.end();
  531. apply(it, end, wkt, geometry);
  532. check_end(it, end, wkt);
  533. }
  534. template <typename TokenizerIterator>
  535. static inline void apply(TokenizerIterator& it,
  536. TokenizerIterator const& end,
  537. std::string const& wkt,
  538. MultiGeometry& geometry)
  539. {
  540. if (initialize<MultiGeometry>(it, end, wkt, PrefixPolicy::apply()))
  541. {
  542. handle_open_parenthesis(it, end, wkt);
  543. // Parse sub-geometries
  544. while(it != end && *it != ")")
  545. {
  546. traits::resize<MultiGeometry>::apply(geometry, boost::size(geometry) + 1);
  547. Parser
  548. <
  549. typename boost::range_value<MultiGeometry>::type
  550. >::apply(it, end, wkt, *(boost::end(geometry) - 1));
  551. if (it != end && *it == ",")
  552. {
  553. // Skip "," after multi-element is parsed
  554. ++it;
  555. }
  556. }
  557. handle_close_parenthesis(it, end, wkt);
  558. }
  559. }
  560. };
  561. template <typename P>
  562. struct noparenthesis_point_parser
  563. {
  564. template <typename TokenizerIterator>
  565. static inline void apply(TokenizerIterator& it,
  566. TokenizerIterator const& end,
  567. std::string const& wkt,
  568. P& point)
  569. {
  570. parsing_assigner<P>::apply(it, end, point, wkt);
  571. }
  572. };
  573. template <typename MultiGeometry, typename PrefixPolicy>
  574. struct multi_point_parser
  575. {
  576. static inline void apply(std::string const& wkt, MultiGeometry& geometry)
  577. {
  578. traits::clear<MultiGeometry>::apply(geometry);
  579. auto const tokens{make_tokenizer(wkt)};
  580. auto it = tokens.begin();
  581. auto const end = tokens.end();
  582. apply(it, end, wkt, geometry);
  583. check_end(it, end, wkt);
  584. }
  585. template <typename TokenizerIterator>
  586. static inline void apply(TokenizerIterator& it,
  587. TokenizerIterator const& end,
  588. std::string const& wkt,
  589. MultiGeometry& geometry)
  590. {
  591. if (initialize<MultiGeometry>(it, end, wkt, PrefixPolicy::apply()))
  592. {
  593. handle_open_parenthesis(it, end, wkt);
  594. // If first point definition starts with "(" then parse points as (x y)
  595. // otherwise as "x y"
  596. bool using_brackets = (it != end && *it == "(");
  597. while(it != end && *it != ")")
  598. {
  599. traits::resize<MultiGeometry>::apply(geometry, boost::size(geometry) + 1);
  600. if (using_brackets)
  601. {
  602. point_parser
  603. <
  604. typename boost::range_value<MultiGeometry>::type
  605. >::apply(it, end, wkt, *(boost::end(geometry) - 1));
  606. }
  607. else
  608. {
  609. noparenthesis_point_parser
  610. <
  611. typename boost::range_value<MultiGeometry>::type
  612. >::apply(it, end, wkt, *(boost::end(geometry) - 1));
  613. }
  614. if (it != end && *it == ",")
  615. {
  616. // Skip "," after point is parsed
  617. ++it;
  618. }
  619. }
  620. handle_close_parenthesis(it, end, wkt);
  621. }
  622. }
  623. };
  624. /*!
  625. \brief Supports box parsing
  626. \note OGC does not define the box geometry, and WKT does not support boxes.
  627. However, to be generic GGL supports reading and writing from and to boxes.
  628. Boxes are outputted as a standard POLYGON. GGL can read boxes from
  629. a standard POLYGON, from a POLYGON with 2 points of from a BOX
  630. \tparam Box the box
  631. */
  632. template <typename Box>
  633. struct box_parser
  634. {
  635. static inline void apply(std::string const& wkt, Box& box)
  636. {
  637. auto const tokens{make_tokenizer(wkt)};
  638. auto it = tokens.begin();
  639. auto const end = tokens.end();
  640. apply(it, end, wkt, box);
  641. check_end(it, end, wkt);
  642. }
  643. template <typename TokenizerIterator>
  644. static inline void apply(TokenizerIterator& it,
  645. TokenizerIterator const& end,
  646. std::string const& wkt,
  647. Box& box)
  648. {
  649. bool should_close = false;
  650. if (it != end && boost::iequals(*it, "POLYGON"))
  651. {
  652. ++it;
  653. bool has_empty, has_z, has_m;
  654. handle_empty_z_m(it, end, has_empty, has_z, has_m);
  655. if (has_empty)
  656. {
  657. assign_zero(box);
  658. return;
  659. }
  660. handle_open_parenthesis(it, end, wkt);
  661. should_close = true;
  662. }
  663. else if (it != end && boost::iequals(*it, "BOX"))
  664. {
  665. ++it;
  666. }
  667. else
  668. {
  669. BOOST_THROW_EXCEPTION(read_wkt_exception("Should start with 'POLYGON' or 'BOX'", wkt));
  670. }
  671. using point_type = typename point_type<Box>::type;
  672. std::vector<point_type> points;
  673. container_inserter<point_type>::apply(it, end, wkt, std::back_inserter(points));
  674. if (should_close)
  675. {
  676. handle_close_parenthesis(it, end, wkt);
  677. }
  678. unsigned int index = 0;
  679. std::size_t n = boost::size(points);
  680. if (n == 2)
  681. {
  682. index = 1;
  683. }
  684. else if (n == 4 || n == 5)
  685. {
  686. // In case of 4 or 5 points, we do not check the other ones, just
  687. // take the opposite corner which is always 2
  688. index = 2;
  689. }
  690. else
  691. {
  692. BOOST_THROW_EXCEPTION(read_wkt_exception("Box should have 2,4 or 5 points", wkt));
  693. }
  694. geometry::detail::assign_point_to_index<min_corner>(points.front(), box);
  695. geometry::detail::assign_point_to_index<max_corner>(points[index], box);
  696. }
  697. };
  698. /*!
  699. \brief Supports segment parsing
  700. \note OGC does not define the segment, and WKT does not support segmentes.
  701. However, it is useful to implement it, also for testing purposes
  702. \tparam Segment the segment
  703. */
  704. template <typename Segment>
  705. struct segment_parser
  706. {
  707. static inline void apply(std::string const& wkt, Segment& segment)
  708. {
  709. auto const tokens{make_tokenizer(wkt)};
  710. auto it = tokens.begin();
  711. auto const end = tokens.end();
  712. apply(it, end, wkt, segment);
  713. check_end(it, end, wkt);
  714. }
  715. template <typename TokenizerIterator>
  716. static inline void apply(TokenizerIterator& it,
  717. TokenizerIterator const& end,
  718. std::string const& wkt,
  719. Segment& segment)
  720. {
  721. if (it != end
  722. && (boost::iequals(*it, prefix_segment::apply())
  723. || boost::iequals(*it, prefix_linestring::apply())))
  724. {
  725. ++it;
  726. }
  727. else
  728. {
  729. BOOST_THROW_EXCEPTION(read_wkt_exception("Should start with 'LINESTRING' or 'SEGMENT'", wkt));
  730. }
  731. using point_type = typename point_type<Segment>::type;
  732. std::vector<point_type> points;
  733. container_inserter<point_type>::apply(it, end, wkt, std::back_inserter(points));
  734. if (boost::size(points) == 2)
  735. {
  736. geometry::detail::assign_point_to_index<0>(points.front(), segment);
  737. geometry::detail::assign_point_to_index<1>(points.back(), segment);
  738. }
  739. else
  740. {
  741. BOOST_THROW_EXCEPTION(read_wkt_exception("Segment should have 2 points", wkt));
  742. }
  743. }
  744. };
  745. struct dynamic_move_assign
  746. {
  747. template <typename DynamicGeometry, typename Geometry>
  748. static void apply(DynamicGeometry& dynamic_geometry, Geometry & geometry)
  749. {
  750. dynamic_geometry = std::move(geometry);
  751. }
  752. };
  753. struct dynamic_move_emplace_back
  754. {
  755. template <typename GeometryCollection, typename Geometry>
  756. static void apply(GeometryCollection& geometry_collection, Geometry & geometry)
  757. {
  758. traits::emplace_back<GeometryCollection>::apply(geometry_collection, std::move(geometry));
  759. }
  760. };
  761. template
  762. <
  763. typename Geometry,
  764. template <typename, typename> class ReadWkt,
  765. typename AppendPolicy
  766. >
  767. struct dynamic_readwkt_caller
  768. {
  769. template <typename TokenizerIterator>
  770. static inline void apply(TokenizerIterator& it,
  771. TokenizerIterator const& end,
  772. std::string const& wkt,
  773. Geometry& geometry)
  774. {
  775. static const char* tag_point = prefix_point::apply();
  776. static const char* tag_linestring = prefix_linestring::apply();
  777. static const char* tag_polygon = prefix_polygon::apply();
  778. static const char* tag_multi_point = prefix_multipoint::apply();
  779. static const char* tag_multi_linestring = prefix_multilinestring::apply();
  780. static const char* tag_multi_polygon = prefix_multipolygon::apply();
  781. static const char* tag_segment = prefix_segment::apply();
  782. static const char* tag_box = prefix_box::apply();
  783. static const char* tag_gc = prefix_geometrycollection::apply();
  784. if (boost::iequals(*it, tag_point))
  785. {
  786. parse_geometry<util::is_point>(tag_point, it, end, wkt, geometry);
  787. }
  788. else if (boost::iequals(*it, tag_multi_point))
  789. {
  790. parse_geometry<util::is_multi_point>(tag_multi_point, it, end, wkt, geometry);
  791. }
  792. else if (boost::iequals(*it, tag_segment))
  793. {
  794. parse_geometry<util::is_segment>(tag_segment, it, end, wkt, geometry);
  795. }
  796. else if (boost::iequals(*it, tag_linestring))
  797. {
  798. parse_geometry<util::is_linestring>(tag_linestring, it, end, wkt, geometry, false)
  799. || parse_geometry<util::is_segment>(tag_linestring, it, end, wkt, geometry);
  800. }
  801. else if (boost::iequals(*it, tag_multi_linestring))
  802. {
  803. parse_geometry<util::is_multi_linestring>(tag_multi_linestring, it, end, wkt, geometry);
  804. }
  805. else if (boost::iequals(*it, tag_box))
  806. {
  807. parse_geometry<util::is_box>(tag_box, it, end, wkt, geometry);
  808. }
  809. else if (boost::iequals(*it, tag_polygon))
  810. {
  811. parse_geometry<util::is_polygon>(tag_polygon, it, end, wkt, geometry, false)
  812. || parse_geometry<util::is_ring>(tag_polygon, it, end, wkt, geometry, false)
  813. || parse_geometry<util::is_box>(tag_polygon, it, end, wkt, geometry);
  814. }
  815. else if (boost::iequals(*it, tag_multi_polygon))
  816. {
  817. parse_geometry<util::is_multi_polygon>(tag_multi_polygon, it, end, wkt, geometry);
  818. }
  819. else if (boost::iequals(*it, tag_gc))
  820. {
  821. parse_geometry<util::is_geometry_collection>(tag_gc, it, end, wkt, geometry);
  822. }
  823. else
  824. {
  825. BOOST_THROW_EXCEPTION(read_wkt_exception(
  826. "Should start with geometry's type, for example 'POINT', 'LINESTRING', 'POLYGON'",
  827. wkt));
  828. }
  829. }
  830. private:
  831. template
  832. <
  833. template <typename> class UnaryPred,
  834. typename TokenizerIterator,
  835. typename Geom = typename util::sequence_find_if
  836. <
  837. typename traits::geometry_types<Geometry>::type, UnaryPred
  838. >::type,
  839. std::enable_if_t<! std::is_void<Geom>::value, int> = 0
  840. >
  841. static bool parse_geometry(const char * ,
  842. TokenizerIterator& it,
  843. TokenizerIterator const& end,
  844. std::string const& wkt,
  845. Geometry& geometry,
  846. bool = true)
  847. {
  848. Geom g;
  849. ReadWkt<Geom, typename tag<Geom>::type>::apply(it, end, wkt, g);
  850. AppendPolicy::apply(geometry, g);
  851. return true;
  852. }
  853. template
  854. <
  855. template <typename> class UnaryPred,
  856. typename TokenizerIterator,
  857. typename Geom = typename util::sequence_find_if
  858. <
  859. typename traits::geometry_types<Geometry>::type, UnaryPred
  860. >::type,
  861. std::enable_if_t<std::is_void<Geom>::value, int> = 0
  862. >
  863. static bool parse_geometry(const char * name,
  864. TokenizerIterator& ,
  865. TokenizerIterator const& ,
  866. std::string const& wkt,
  867. Geometry& ,
  868. bool throw_on_misfit = true)
  869. {
  870. if (throw_on_misfit)
  871. {
  872. std::string msg = std::string("Unable to store '") + name + "' in this geometry";
  873. BOOST_THROW_EXCEPTION(read_wkt_exception(msg, wkt));
  874. }
  875. return false;
  876. }
  877. };
  878. }} // namespace detail::wkt
  879. #endif // DOXYGEN_NO_DETAIL
  880. #ifndef DOXYGEN_NO_DISPATCH
  881. namespace dispatch
  882. {
  883. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  884. struct read_wkt {};
  885. template <typename Point>
  886. struct read_wkt<Point, point_tag>
  887. : detail::wkt::geometry_parser
  888. <
  889. Point,
  890. detail::wkt::point_parser,
  891. detail::wkt::prefix_point
  892. >
  893. {};
  894. template <typename L>
  895. struct read_wkt<L, linestring_tag>
  896. : detail::wkt::geometry_parser
  897. <
  898. L,
  899. detail::wkt::linestring_parser,
  900. detail::wkt::prefix_linestring
  901. >
  902. {};
  903. template <typename Ring>
  904. struct read_wkt<Ring, ring_tag>
  905. : detail::wkt::geometry_parser
  906. <
  907. Ring,
  908. detail::wkt::ring_parser,
  909. detail::wkt::prefix_polygon
  910. >
  911. {};
  912. template <typename Geometry>
  913. struct read_wkt<Geometry, polygon_tag>
  914. : detail::wkt::geometry_parser
  915. <
  916. Geometry,
  917. detail::wkt::polygon_parser,
  918. detail::wkt::prefix_polygon
  919. >
  920. {};
  921. template <typename MultiGeometry>
  922. struct read_wkt<MultiGeometry, multi_point_tag>
  923. : detail::wkt::multi_point_parser
  924. <
  925. MultiGeometry,
  926. detail::wkt::prefix_multipoint
  927. >
  928. {};
  929. template <typename MultiGeometry>
  930. struct read_wkt<MultiGeometry, multi_linestring_tag>
  931. : detail::wkt::multi_parser
  932. <
  933. MultiGeometry,
  934. detail::wkt::linestring_parser,
  935. detail::wkt::prefix_multilinestring
  936. >
  937. {};
  938. template <typename MultiGeometry>
  939. struct read_wkt<MultiGeometry, multi_polygon_tag>
  940. : detail::wkt::multi_parser
  941. <
  942. MultiGeometry,
  943. detail::wkt::polygon_parser,
  944. detail::wkt::prefix_multipolygon
  945. >
  946. {};
  947. // Box (Non-OGC)
  948. template <typename Box>
  949. struct read_wkt<Box, box_tag>
  950. : detail::wkt::box_parser<Box>
  951. {};
  952. // Segment (Non-OGC)
  953. template <typename Segment>
  954. struct read_wkt<Segment, segment_tag>
  955. : detail::wkt::segment_parser<Segment>
  956. {};
  957. template <typename DynamicGeometry>
  958. struct read_wkt<DynamicGeometry, dynamic_geometry_tag>
  959. {
  960. static inline void apply(std::string const& wkt, DynamicGeometry& dynamic_geometry)
  961. {
  962. auto tokens{detail::wkt::make_tokenizer(wkt)};
  963. auto it = tokens.begin();
  964. auto const end = tokens.end();
  965. if (it == end)
  966. {
  967. BOOST_THROW_EXCEPTION(read_wkt_exception(
  968. "Should start with geometry's type, for example 'POINT', 'LINESTRING', 'POLYGON'",
  969. wkt));
  970. }
  971. detail::wkt::dynamic_readwkt_caller
  972. <
  973. DynamicGeometry, dispatch::read_wkt, detail::wkt::dynamic_move_assign
  974. >::apply(it, end, wkt, dynamic_geometry);
  975. detail::wkt::check_end(it, end, wkt);
  976. }
  977. };
  978. template <typename Geometry>
  979. struct read_wkt<Geometry, geometry_collection_tag>
  980. {
  981. static inline void apply(std::string const& wkt, Geometry& geometry)
  982. {
  983. range::clear(geometry);
  984. auto tokens{detail::wkt::make_tokenizer(wkt)};
  985. auto it = tokens.begin();
  986. auto const end = tokens.end();
  987. apply(it, end, wkt, geometry);
  988. detail::wkt::check_end(it, end, wkt);
  989. }
  990. template <typename TokenizerIterator>
  991. static inline void apply(TokenizerIterator& it,
  992. TokenizerIterator const& end,
  993. std::string const& wkt,
  994. Geometry& geometry)
  995. {
  996. if (detail::wkt::initialize<Geometry>(it, end, wkt,
  997. detail::wkt::prefix_geometrycollection::apply()))
  998. {
  999. detail::wkt::handle_open_parenthesis(it, end, wkt);
  1000. // Stop at ")"
  1001. while (it != end && *it != ")")
  1002. {
  1003. detail::wkt::dynamic_readwkt_caller
  1004. <
  1005. Geometry, dispatch::read_wkt, detail::wkt::dynamic_move_emplace_back
  1006. >::apply(it, end, wkt, geometry);
  1007. if (it != end && *it == ",")
  1008. {
  1009. // Skip "," after geometry is parsed
  1010. ++it;
  1011. }
  1012. }
  1013. detail::wkt::handle_close_parenthesis(it, end, wkt);
  1014. }
  1015. }
  1016. };
  1017. } // namespace dispatch
  1018. #endif // DOXYGEN_NO_DISPATCH
  1019. /*!
  1020. \brief Parses OGC Well-Known Text (\ref WKT) into a geometry (any geometry)
  1021. \ingroup wkt
  1022. \tparam Geometry \tparam_geometry
  1023. \param wkt string containing \ref WKT
  1024. \param geometry \param_geometry output geometry
  1025. \ingroup wkt
  1026. \qbk{[include reference/io/read_wkt.qbk]}
  1027. */
  1028. template <typename Geometry>
  1029. inline void read_wkt(std::string const& wkt, Geometry& geometry)
  1030. {
  1031. geometry::concepts::check<Geometry>();
  1032. dispatch::read_wkt<Geometry>::apply(wkt, geometry);
  1033. }
  1034. /*!
  1035. \brief Parses OGC Well-Known Text (\ref WKT) into a geometry (any geometry) and returns it
  1036. \ingroup wkt
  1037. \tparam Geometry \tparam_geometry
  1038. \param wkt string containing \ref WKT
  1039. \ingroup wkt
  1040. \qbk{[include reference/io/from_wkt.qbk]}
  1041. */
  1042. template <typename Geometry>
  1043. inline Geometry from_wkt(std::string const& wkt)
  1044. {
  1045. Geometry geometry;
  1046. geometry::concepts::check<Geometry>();
  1047. dispatch::read_wkt<Geometry>::apply(wkt, geometry);
  1048. return geometry;
  1049. }
  1050. }} // namespace boost::geometry
  1051. #endif // BOOST_GEOMETRY_IO_WKT_READ_HPP