read.hpp 36 KB

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