append.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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-2023.
  6. // Modifications copyright (c) 2014-2023, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  8. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  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_ALGORITHMS_APPEND_HPP
  16. #define BOOST_GEOMETRY_ALGORITHMS_APPEND_HPP
  17. #include <boost/range/begin.hpp>
  18. #include <boost/range/end.hpp>
  19. #include <boost/range/value_type.hpp>
  20. #include <boost/geometry/algorithms/num_interior_rings.hpp>
  21. #include <boost/geometry/algorithms/detail/convert_point_to_point.hpp>
  22. #include <boost/geometry/algorithms/detail/signed_size_type.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. if (ring_index == -1)
  76. {
  77. auto&& ext_ring = exterior_ring(polygon);
  78. to_range_point::apply<ring_type, Point>(ext_ring, point);
  79. }
  80. else if (ring_index < signed_size_type(num_interior_rings(polygon)))
  81. {
  82. auto&& int_rings = interior_rings(polygon);
  83. to_range_point::apply<ring_type, Point>(range::at(int_rings, ring_index), point);
  84. }
  85. }
  86. };
  87. struct to_polygon_range
  88. {
  89. template <typename Polygon, typename Range>
  90. static inline void apply(Polygon& polygon, Range const& range,
  91. signed_size_type ring_index, signed_size_type = 0)
  92. {
  93. using ring_type = typename ring_type<Polygon>::type;
  94. using exterior_ring_type = typename ring_return_type<Polygon>::type;
  95. using interior_ring_range_type = typename interior_return_type<Polygon>::type;
  96. if (ring_index == -1)
  97. {
  98. exterior_ring_type ext_ring = exterior_ring(polygon);
  99. to_range_range::apply<ring_type, Range>(ext_ring, range);
  100. }
  101. else if (ring_index < signed_size_type(num_interior_rings(polygon)))
  102. {
  103. interior_ring_range_type int_rings = interior_rings(polygon);
  104. to_range_range::apply<ring_type, Range>(range::at(int_rings, ring_index), range);
  105. }
  106. }
  107. };
  108. template <typename Policy>
  109. struct to_multigeometry
  110. {
  111. template <typename MultiGeometry, typename RangeOrPoint>
  112. static inline void apply(MultiGeometry& multigeometry,
  113. RangeOrPoint const& range_or_point,
  114. signed_size_type ring_index, signed_size_type multi_index)
  115. {
  116. Policy::template apply
  117. <
  118. typename boost::range_value<MultiGeometry>::type,
  119. RangeOrPoint
  120. >(range::at(multigeometry, multi_index), range_or_point, ring_index);
  121. }
  122. };
  123. }} // namespace detail::append
  124. #endif // DOXYGEN_NO_DETAIL
  125. #ifndef DOXYGEN_NO_DISPATCH
  126. namespace dispatch
  127. {
  128. template
  129. <
  130. typename Geometry,
  131. typename RangeOrPoint,
  132. typename Tag = typename geometry::tag<Geometry>::type,
  133. typename OtherTag = typename geometry::tag<RangeOrPoint>::type
  134. >
  135. struct append
  136. : detail::append::append_no_action
  137. {};
  138. template <typename Geometry, typename Point>
  139. struct append<Geometry, Point, linestring_tag, point_tag>
  140. : detail::append::to_range_point
  141. {};
  142. template <typename Geometry, typename Point>
  143. struct append<Geometry, Point, ring_tag, point_tag>
  144. : detail::append::to_range_point
  145. {};
  146. template <typename Polygon, typename Point>
  147. struct append<Polygon, Point, polygon_tag, point_tag>
  148. : detail::append::to_polygon_point
  149. {};
  150. template <typename Geometry, typename Range, typename RangeTag>
  151. struct append<Geometry, Range, linestring_tag, RangeTag>
  152. : detail::append::to_range_range
  153. {};
  154. template <typename Geometry, typename Range, typename RangeTag>
  155. struct append<Geometry, Range, ring_tag, RangeTag>
  156. : detail::append::to_range_range
  157. {};
  158. template <typename Polygon, typename Range, typename RangeTag>
  159. struct append<Polygon, Range, polygon_tag, RangeTag>
  160. : detail::append::to_polygon_range
  161. {};
  162. template <typename Geometry, typename Point>
  163. struct append<Geometry, Point, multi_point_tag, point_tag>
  164. : detail::append::to_range_point
  165. {};
  166. template <typename Geometry, typename Range, typename RangeTag>
  167. struct append<Geometry, Range, multi_point_tag, RangeTag>
  168. : detail::append::to_range_range
  169. {};
  170. template <typename MultiGeometry, typename Point>
  171. struct append<MultiGeometry, Point, multi_linestring_tag, point_tag>
  172. : detail::append::to_multigeometry<detail::append::to_range_point>
  173. {};
  174. template <typename MultiGeometry, typename Range, typename RangeTag>
  175. struct append<MultiGeometry, Range, multi_linestring_tag, RangeTag>
  176. : detail::append::to_multigeometry<detail::append::to_range_range>
  177. {};
  178. template <typename MultiGeometry, typename Point>
  179. struct append<MultiGeometry, Point, multi_polygon_tag, point_tag>
  180. : detail::append::to_multigeometry<detail::append::to_polygon_point>
  181. {};
  182. template <typename MultiGeometry, typename Range, typename RangeTag>
  183. struct append<MultiGeometry, Range, multi_polygon_tag, RangeTag>
  184. : detail::append::to_multigeometry<detail::append::to_polygon_range>
  185. {};
  186. template <typename Geometry, typename RangeOrPoint, typename OtherTag>
  187. struct append<Geometry, RangeOrPoint, dynamic_geometry_tag, OtherTag>
  188. {
  189. static inline void apply(Geometry& geometry,
  190. RangeOrPoint const& range_or_point,
  191. signed_size_type ring_index, signed_size_type multi_index)
  192. {
  193. traits::visit<Geometry>::apply([&](auto & g)
  194. {
  195. append
  196. <
  197. std::remove_reference_t<decltype(g)>, RangeOrPoint
  198. >::apply(g, range_or_point, ring_index, multi_index);
  199. }, geometry);
  200. }
  201. };
  202. // TODO: It's unclear how append should work for GeometryCollection because
  203. // it can hold multiple different geometries.
  204. } // namespace dispatch
  205. #endif // DOXYGEN_NO_DISPATCH
  206. /*!
  207. \brief Appends one or more points to a linestring, ring, polygon, multi-geometry
  208. \ingroup append
  209. \tparam Geometry \tparam_geometry
  210. \tparam RangeOrPoint Either a range or a point, fullfilling Boost.Range concept or Boost.Geometry Point Concept
  211. \param geometry \param_geometry
  212. \param range_or_point The point or range to add
  213. \param ring_index The index of the ring in case of a polygon:
  214. exterior ring (-1, the default) or interior ring index
  215. \param multi_index The index of the geometry to which the points are appended
  216. \qbk{[include reference/algorithms/append.qbk]}
  217. }
  218. */
  219. template <typename Geometry, typename RangeOrPoint>
  220. inline void append(Geometry& geometry, RangeOrPoint const& range_or_point,
  221. signed_size_type ring_index = -1, signed_size_type multi_index = 0)
  222. {
  223. concepts::check<Geometry>();
  224. dispatch::append
  225. <
  226. Geometry, RangeOrPoint
  227. >::apply(geometry, range_or_point, ring_index, multi_index);
  228. }
  229. }} // namespace boost::geometry
  230. #endif // BOOST_GEOMETRY_ALGORITHMS_APPEND_HPP