Boost logo

Geometry :

Subject: Re: [geometry] 答复: How could I get nodes(MBRs) of the R Tree
From: Adam Wulkiewicz (adam.wulkiewicz_at_[hidden])
Date: 2018-01-26 15:50:07


Hi again,

Li, Richie [Student] wrote:
> Thanks for that. But I'm still a bit confused. Could you give me an
> example code of getting all the MBRs from level2 to the leaf nodes?

I decided to write a simplified version of the visitor. It only prints
boxes/values and traverses the R-tree depth-first. Here is complete program:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <iostream>

namespace bg = boost::geometry;
namespace bgi = bg::index;
namespace bgid = bgi::detail;

template <typename Value, typename Options, typename Translator,
typename Box, typename Allocators>
class test_visitor
 Â Â Â  : public bgid::rtree::visitor<Value, typename
Options::parameters_type, Box, Allocators, typename Options::node_tag,
true>::type
{
 Â Â Â  typedef typename bgid::rtree::internal_node<Value, typename
Options::parameters_type, Box, Allocators, typename
Options::node_tag>::type internal_node;
 Â Â Â  typedef typename bgid::rtree::leaf<Value, typename
Options::parameters_type, Box, Allocators, typename
Options::node_tag>::type leaf;

public:
 Â Â Â  void operator()(internal_node const& n)
 Â Â Â  {
 Â Â Â Â Â Â Â  typedef typename
bgid::rtree::elements_type<internal_node>::type elements_type;
 Â Â Â Â Â Â Â  elements_type const& elements = bgid::rtree::elements(n);

 Â Â Â Â Â Â Â  for ( typename elements_type::const_iterator it = elements.begin();
 Â Â Â Â Â Â Â Â Â Â Â Â Â  it != elements.end() ; ++it)
 Â Â Â Â Â Â Â  {
 Â Â Â Â Â Â Â Â Â Â Â  handle_box_or_value(it->first);

 Â Â Â Â Â Â Â Â Â Â Â  bgid::rtree::apply_visitor(*this, *(it->second));
 Â Â Â Â Â Â Â  }
 Â Â Â  }

 Â Â Â  void operator()(leaf const& n)
 Â Â Â  {
 Â Â Â Â Â Â Â  typedef typename bgid::rtree::elements_type<leaf>::type
elements_type;
 Â Â Â Â Â Â Â  elements_type const& elements = bgid::rtree::elements(n);

 Â Â Â Â Â Â Â  for ( typename elements_type::const_iterator it = elements.begin();
 Â Â Â Â Â Â Â Â Â Â Â Â Â  it != elements.end() ; ++it)
 Â Â Â Â Â Â Â  {
 Â Â Â Â Â Â Â Â Â Â Â  handle_box_or_value(*it);
 Â Â Â Â Â Â Â  }
 Â Â Â  }

 Â Â Â  template <typename BoxOrValue>
 Â Â Â  void handle_box_or_value(BoxOrValue const& b)
 Â Â Â  {
 Â Â Â Â Â Â Â  std::cout << bg::dsv(b) << std::endl;
 Â Â Â  }
};

template <typename Rtree>
inline void test(Rtree const& tree)
{
 Â Â Â  typedef bgid::rtree::utilities::view<Rtree> RTV;
 Â Â Â  RTV rtv(tree);

 Â Â Â  test_visitor<
 Â Â Â Â Â Â Â  typename RTV::value_type,
 Â Â Â Â Â Â Â  typename RTV::options_type,
 Â Â Â Â Â Â Â  typename RTV::translator_type,
 Â Â Â Â Â Â Â  typename RTV::box_type,
 Â Â Â Â Â Â Â  typename RTV::allocators_type
 Â Â Â  > v;

 Â Â Â  rtv.apply_visitor(v);
}

int main()
{
 Â Â Â  typedef bg::model::point<float, 2, bg::cs::cartesian> point;
 Â Â Â  typedef bg::model::box<point> box;
 Â Â Â  typedef bgi::rtree<box, bgi::rstar<4> > rtree;

 Â Â Â  rtree rt;
 Â Â Â  rt.insert(box(point(0, 0), point(1, 1)));
 Â Â Â  rt.insert(box(point(1, 1), point(2, 2)));
 Â Â Â  rt.insert(box(point(2, 2), point(3, 3)));
 Â Â Â  rt.insert(box(point(3, 3), point(4, 4)));
 Â Â Â  rt.insert(box(point(4, 4), point(5, 5)));
 Â Â Â  rt.insert(box(point(5, 5), point(6, 6)));

 Â Â Â  test(rt);
}

Regards,
Adam



Geometry list run by mateusz at loskot.net