gc_make_rtree.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Boost.Geometry
  2. // Copyright (c) 2022, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  4. // Licensed under the Boost Software License version 1.0.
  5. // http://www.boost.org/users/license.html
  6. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_GC_MAKE_RTREE_HPP
  7. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_GC_MAKE_RTREE_HPP
  8. #include <vector>
  9. #include <boost/range/begin.hpp>
  10. #include <boost/range/end.hpp>
  11. #include <boost/range/size.hpp>
  12. #include <boost/geometry/algorithms/detail/expand_by_epsilon.hpp>
  13. #include <boost/geometry/algorithms/detail/visit.hpp>
  14. #include <boost/geometry/algorithms/envelope.hpp>
  15. #include <boost/geometry/index/rtree.hpp>
  16. #include <boost/geometry/strategies/index/services.hpp>
  17. #include <boost/geometry/views/detail/random_access_view.hpp>
  18. namespace boost { namespace geometry
  19. {
  20. #ifndef DOXYGEN_NO_DETAIL
  21. namespace detail
  22. {
  23. template <typename GC>
  24. using gc_make_rtree_box_t = geometry::model::box
  25. <
  26. geometry::model::point
  27. <
  28. typename geometry::coordinate_type<GC>::type,
  29. geometry::dimension<GC>::value,
  30. typename geometry::coordinate_system<GC>::type
  31. >
  32. >;
  33. template <typename GC, typename Strategy>
  34. inline auto gc_make_rtree_iterators(GC& gc, Strategy const& strategy)
  35. {
  36. using box_t = gc_make_rtree_box_t<GC>;
  37. using iter_t = typename boost::range_iterator<GC>::type;
  38. using rtree_param_t = index::rstar<4>;
  39. using rtree_parameters_t = index::parameters<rtree_param_t, Strategy>;
  40. using value_t = std::pair<box_t, iter_t>;
  41. using rtree_t = index::rtree<value_t, rtree_parameters_t>;
  42. // TODO: get rid of the temporary vector
  43. auto const size = boost::size(gc);
  44. std::vector<value_t> values;
  45. values.reserve(size);
  46. visit_breadth_first_impl<true>::apply([&](auto const& g, auto iter)
  47. {
  48. box_t b = geometry::return_envelope<box_t>(g, strategy);
  49. detail::expand_by_epsilon(b);
  50. values.emplace_back(b, iter);
  51. return true;
  52. }, gc);
  53. return rtree_t(values.begin(), values.end(), rtree_parameters_t(rtree_param_t(), strategy));
  54. }
  55. template <typename GCView, typename Strategy>
  56. inline auto gc_make_rtree_indexes(GCView const& gc, Strategy const& strategy)
  57. {
  58. // Alternatively only take random_access_view<GC>
  59. static const bool is_random_access = is_random_access_range<GCView>::value;
  60. static const bool is_not_recursive = ! is_geometry_collection_recursive<GCView>::value;
  61. BOOST_GEOMETRY_STATIC_ASSERT((is_random_access && is_not_recursive),
  62. "This algorithm requires random-access, non-recursive geometry collection or view.",
  63. GCView);
  64. using box_t = gc_make_rtree_box_t<GCView>;
  65. using rtree_param_t = index::rstar<4>;
  66. using rtree_parameters_t = index::parameters<rtree_param_t, Strategy>;
  67. using value_t = std::pair<box_t, std::size_t>;
  68. using rtree_t = index::rtree<value_t, rtree_parameters_t>;
  69. // TODO: get rid of the temporary vector
  70. std::size_t const size = boost::size(gc);
  71. std::vector<value_t> values;
  72. values.reserve(size);
  73. auto const begin = boost::begin(gc);
  74. for (std::size_t i = 0; i < size; ++i)
  75. {
  76. traits::iter_visit<GCView>::apply([&](auto const& g)
  77. {
  78. box_t b = geometry::return_envelope<box_t>(g, strategy);
  79. detail::expand_by_epsilon(b);
  80. values.emplace_back(b, i);
  81. }, begin + i);
  82. }
  83. return rtree_t(values.begin(), values.end(), rtree_parameters_t(rtree_param_t(), strategy));
  84. }
  85. } // namespace detail
  86. #endif // DOXYGEN_NO_DETAIL
  87. }} // namespace boost::geometry
  88. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_GC_MAKE_RTREE_HPP