I can't seem to find an efficient way to iterate over a boost R-tree (boost::geometry::index::rtree). The only method I have come up with so far is to perform a query using a very large bounding box so that a copy of all the elements is returned in a vector, however this is obviously neither space-efficient nor time-efficient. Ideally I'd just like to use an STL-style iterator to iterate over the tree in the usual way, but this doesn't seem to be possible ?
Since 1.59.0
begin() and end() member functions are defined in bgi::rtree, returning a const_iterator. So it's possible to iterate over all elements without the techniques described below. In C++11:
for(auto const& v: rtree)
/* do something with v */
Before 1.59.0
As others have said to iterate over all elements stored in the rtree you could use query iterator. However there is no need to perform an actual spatial query (pass bounds, etc.). You could pass a dummy UnaryPredicate always returning true wrapped with bgi::satisfies(). In C++11:
std::for_each(rtree.qbegin(bgi::satisfies([](Value const&){ return true; })),
rtree.qend(),
[](Value const& v){
/* do something with v */
});
Non-iterative queries could also be used for this purpose but it'd require a special output iterator, e.g. boost::function_output_iterator implemented in Boost.Iterator library (see http://www.boost.org/doc/libs/1_57_0/libs/iterator/doc/function_output_iterator.html). In C++11:
rtree.query(bgi::satisfies([](Value const&){ return true; }),
boost::make_function_output_iterator([](Value const& v){
/* do something with v */
}));
Side notes:
namespace bgi = boost::geometry::indexValue is a type of objects stored in the bgi::rtreeboost::function_output_iterator requires #include <boost/function_output_iterator.hpp>If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With