append.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2014-2021.
  6. // Modifications copyright (c) 2014-2021, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_ALGORITHMS_APPEND_HPP
  15. #define BOOST_GEOMETRY_ALGORITHMS_APPEND_HPP
  16. #include <boost/range/begin.hpp>
  17. #include <boost/range/end.hpp>
  18. #include <boost/range/value_type.hpp>
  19. #include <boost/geometry/algorithms/num_interior_rings.hpp>
  20. #include <boost/geometry/algorithms/detail/convert_point_to_point.hpp>
  21. #include <boost/geometry/algorithms/detail/signed_size_type.hpp>
  22. #include <boost/geometry/core/access.hpp>
  23. #include <boost/geometry/core/mutable_range.hpp>
  24. #include <boost/geometry/core/point_type.hpp>
  25. #include <boost/geometry/core/tags.hpp>
  26. #include <boost/geometry/core/visit.hpp>
  27. #include <boost/geometry/geometries/adapted/boost_variant.hpp> // for backward compatibility
  28. #include <boost/geometry/geometries/concepts/check.hpp>
  29. #include <boost/geometry/util/range.hpp>
  30. namespace boost { namespace geometry
  31. {
  32. #ifndef DOXYGEN_NO_DETAIL
  33. namespace detail { namespace append
  34. {
  35. struct append_no_action
  36. {
  37. template <typename Geometry, typename Point>
  38. static inline void apply(Geometry& , Point const& ,
  39. signed_size_type = -1, signed_size_type = 0)
  40. {
  41. }
  42. };
  43. struct to_range_point
  44. {
  45. template <typename Geometry, typename Point>
  46. static inline void apply(Geometry& geometry, Point const& point,
  47. signed_size_type = -1, signed_size_type = 0)
  48. {
  49. typename geometry::point_type<Geometry>::type copy;
  50. geometry::detail::conversion::convert_point_to_point(point, copy);
  51. traits::push_back<Geometry>::apply(geometry, copy);
  52. }
  53. };
  54. struct to_range_range
  55. {
  56. template <typename Geometry, typename Range>
  57. static inline void apply(Geometry& geometry, Range const& range,
  58. signed_size_type = -1, signed_size_type = 0)
  59. {
  60. using point_type = typename boost::range_value<Range>::type;
  61. auto const end = boost::end(range);
  62. for (auto it = boost::begin(range); it != end; ++it)
  63. {
  64. to_range_point::apply<Geometry, point_type>(geometry, *it);
  65. }
  66. }
  67. };
  68. struct to_polygon_point
  69. {
  70. template <typename Polygon, typename Point>
  71. static inline void apply(Polygon& polygon, Point const& point,
  72. signed_size_type ring_index, signed_size_type = 0)
  73. {
  74. using ring_type = typename ring_type<Polygon>::type;
  75. using exterior_ring_type = typename ring_return_type<Polygon>::type;
  76. using interior_ring_range_type = typename interior_return_type<Polygon>::type;
  77. if (ring_index == -1)
  78. {
  79. exterior_ring_type ext_ring = exterior_ring(polygon);
  80. to_range_point::apply<ring_type, Point>(ext_ring, point);
  81. }
  82. else if (ring_index < signed_size_type(num_interior_rings(polygon)))
  83. {
  84. interior_ring_range_type int_rings = interior_rings(polygon);
  85. to_range_point::apply<ring_type, Point>(range::at(int_rings, ring_index), point);
  86. }
  87. }
  88. };
  89. struct to_polygon_range
  90. {
  91. template <typename Polygon, typename Range>
  92. static inline void apply(Polygon& polygon, Range const& range,
  93. signed_size_type ring_index, signed_size_type = 0)
  94. {
  95. using ring_type = typename ring_type<Polygon>::type;
  96. using exterior_ring_type = typename ring_return_type<Polygon>::type;
  97. using interior_ring_range_type = typename interior_return_type<Polygon>::type;
  98. if (ring_index == -1)
  99. {
  100. exterior_ring_type ext_ring = exterior_ring(polygon);
  101. to_range_range::apply<ring_type, Range>(ext_ring, range);
  102. }
  103. else if (ring_index < signed_size_type(num_interior_rings(polygon)))
  104. {
  105. interior_ring_range_type int_rings = interior_rings(polygon);
  106. to_range_range::apply<ring_type, Range>(range::at(int_rings, ring_index), range);
  107. }
  108. }
  109. };
  110. template <typename Policy>
  111. struct to_multigeometry
  112. {
  113. template <typename MultiGeometry, typename RangeOrPoint>
  114. static inline void apply(MultiGeometry& multigeometry,
  115. RangeOrPoint const& range_or_point,
  116. signed_size_type ring_index, signed_size_type multi_index)
  117. {
  118. Policy::template apply
  119. <
  120. typename boost::range_value<MultiGeometry>::type,
  121. RangeOrPoint
  122. >(range::at(multigeometry, multi_index), range_or_point, ring_index);
  123. }
  124. };
  125. }} // namespace detail::append
  126. #endif // DOXYGEN_NO_DETAIL
  127. #ifndef DOXYGEN_NO_DISPATCH
  128. namespace dispatch
  129. {
  130. template
  131. <
  132. typename Geometry,
  133. typename RangeOrPoint,
  134. typename Tag = typename geometry::tag<Geometry>::type,
  135. typename OtherTag = typename geometry::tag<RangeOrPoint>::type
  136. >
  137. struct append
  138. : detail::append::append_no_action
  139. {};
  140. template <typename Geometry, typename Point>
  141. struct append<Geometry, Point, linestring_tag, point_tag>
  142. : detail::append::to_range_point
  143. {};
  144. template <typename Geometry, typename Point>
  145. struct append<Geometry, Point, ring_tag, point_tag>
  146. : detail::append::to_range_point
  147. {};
  148. template <typename Polygon, typename Point>
  149. struct append<Polygon, Point, polygon_tag, point_tag>
  150. : detail::append::to_polygon_point
  151. {};
  152. template <typename Geometry, typename Range, typename RangeTag>
  153. struct append<Geometry, Range, linestring_tag, RangeTag>
  154. : detail::append::to_range_range
  155. {};
  156. template <typename Geometry, typename Range, typename RangeTag>
  157. struct append<Geometry, Range, ring_tag, RangeTag>
  158. : detail::append::to_range_range
  159. {};
  160. template <typename Polygon, typename Range, typename RangeTag>
  161. struct append<Polygon, Range, polygon_tag, RangeTag>
  162. : detail::append::to_polygon_range
  163. {};
  164. template <typename Geometry, typename Point>
  165. struct append<Geometry, Point, multi_point_tag, point_tag>
  166. : detail::append::to_range_point
  167. {};
  168. template <typename Geometry, typename Range, typename RangeTag>
  169. struct append<Geometry, Range, multi_point_tag, RangeTag>
  170. : detail::append::to_range_range
  171. {};
  172. template <typename MultiGeometry, typename Point>
  173. struct append<MultiGeometry, Point, multi_linestring_tag, point_tag>
  174. : detail::append::to_multigeometry<detail::append::to_range_point>
  175. {};
  176. template <typename MultiGeometry, typename Range, typename RangeTag>
  177. struct append<MultiGeometry, Range, multi_linestring_tag, RangeTag>
  178. : detail::append::to_multigeometry<detail::append::to_range_range>
  179. {};
  180. template <typename MultiGeometry, typename Point>
  181. struct append<MultiGeometry, Point, multi_polygon_tag, point_tag>
  182. : detail::append::to_multigeometry<detail::append::to_polygon_point>
  183. {};
  184. template <typename MultiGeometry, typename Range, typename RangeTag>
  185. struct append<MultiGeometry, Range, multi_polygon_tag, RangeTag>
  186. : detail::append::to_multigeometry<detail::append::to_polygon_range>
  187. {};
  188. template <typename Geometry, typename RangeOrPoint, typename OtherTag>
  189. struct append<Geometry, RangeOrPoint, dynamic_geometry_tag, OtherTag>
  190. {
  191. static inline void apply(Geometry& geometry,
  192. RangeOrPoint const& range_or_point,
  193. signed_size_type ring_index, signed_size_type multi_index)
  194. {
  195. traits::visit<Geometry>::apply([&](auto & g)
  196. {
  197. append
  198. <
  199. std::remove_reference_t<decltype(g)>, RangeOrPoint
  200. >::apply(g, range_or_point, ring_index, multi_index);
  201. }, geometry);
  202. }
  203. };
  204. // TODO: It's unclear how append should work for GeometryCollection because
  205. // it can hold multiple different geometries.
  206. } // namespace dispatch
  207. #endif // DOXYGEN_NO_DISPATCH
  208. /*!
  209. \brief Appends one or more points to a linestring, ring, polygon, multi-geometry
  210. \ingroup append
  211. \tparam Geometry \tparam_geometry
  212. \tparam RangeOrPoint Either a range or a point, fullfilling Boost.Range concept or Boost.Geometry Point Concept
  213. \param geometry \param_geometry
  214. \param range_or_point The point or range to add
  215. \param ring_index The index of the ring in case of a polygon:
  216. exterior ring (-1, the default) or interior ring index
  217. \param multi_index The index of the geometry to which the points are appended
  218. \qbk{[include reference/algorithms/append.qbk]}
  219. }
  220. */
  221. template <typename Geometry, typename RangeOrPoint>
  222. inline void append(Geometry& geometry, RangeOrPoint const& range_or_point,
  223. signed_size_type ring_index = -1, signed_size_type multi_index = 0)
  224. {
  225. concepts::check<Geometry>();
  226. dispatch::append
  227. <
  228. Geometry, RangeOrPoint
  229. >::apply(geometry, range_or_point, ring_index, multi_index);
  230. }
  231. }} // namespace boost::geometry
  232. #endif // BOOST_GEOMETRY_ALGORITHMS_APPEND_HPP