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