# Geometry :

Subject: Re: [geometry] rtree nearest query with custom geometries
Date: 2019-04-16 12:58:46

Hi Andrea,

Andrea Beconcini Via GeometryÂ wrote:
> I am trying to use an rtree as an index to a set of triangles to
> perform some spatial queries.
> The rtree stores std::pair<box, triangles> where box is the boost
> geometry box.
>
> The problem occurs with the nearest query and, in particular, when two
> or more elements have equivalent boxes,
> Â and I look for just one element, the result may be wrong.
>
> Look at the code below; the problem is that the two triangles have
> equivalent bounding boxes, and since
> the query returns just one element, the result is the one I inserted
> first. Even though the bounding boxes
> are the same, the two elements are not, I'd like the query to returns
>
> I think I need to provide an IndexableGetter to my rtree, but according to

> the returned value has to be one of the Indexable concepts (Point,
> Box, or Segment) and none of them seems to satisfy
> my requirements.
>
> Do I need to make my triangle Indexable? I could not find any
> reference on how to do that and what methods are required
> for a type to be indexable; can anyone provide any resources that may
> be useful?

No, only boxes should be used for indexing.

You have to iterate the query iterator and check a condition until
you're satisfied with the result, e.g.:

double distance_to_closest_triangle = MAX;
for(auto it = rtree.qbegin(nearest(pt, rtree.size())) ; it !=
rtree.qend() ; ++it) {
Â Â Â  if (distance_to_closest_triangle <= distance(pt, it->first))
Â Â Â Â Â Â Â  break;
Â Â Â  distance_to_closest_triangle = min(distance(pt,
triangles[it->second]), distance_to_closest_triangle);
}