boostorg / boostorg/geometry

rtree with qbegin doesn't sort the elements by distance

Open
#1,050 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
517
Forks
232
PR merge metrics
No merged PRs in 30d

Description

Hi,
I just ran into an issue with the order of the returned values with an rtree using segments.

```
using Vector2d = boost::geometry::model::d2::point_xy ;
using seg2d = boost::geometry::model::segment ;
using rtree_seg = pair ;
boost::geometry::index::rtree > global_rtree_segs;
```
I had an issue with the result, so I decided to use an iterative query because it should have guaranteed sorted results from closest for farthest:

```
vector > results;
for ( auto it = global_rtree_segs.qbegin( boost::geometry::index::intersects( seg ) && boost::geometry::index::nearest( seg, 100 ) ); it != global_rtree_segs.qend(); ++it ){

results.push_back( *it );
}
```

and the distance check:
```
for ( u32 idx = -1; auto &e: r ){
idx += 1;
double d = boost::geometry::distance( seg, e.first );
std::cout << idx << " " << boost::geometry::wkt( e.first ) << ", distance= " << d << "\n";

}
```
And I saw that the distance are in random order.
But the Note at [https://www.boost.org/doc/libs/1_79_0/libs/geometry/doc/html/geometry/spatial_indexes/queries.html](url) says

> It's different in case of k-NN queries performed with query iterator returned by qbegin() function which guarantees the iteration over the closest Values first

So, right now, I want all the segments intersecting with a given segment, from closest to farthest, I need to implement a sort afterwards.

If I didn't get something, please tell me, otherwise, I think people should not rely on the note in the doc.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.