# Geometry :

Subject: Re: [geometry] rtree nearest query with custom geometries
From: Andrea Beconcini (andrea.beconcini_at_[hidden])
Date: 2019-04-17 16:11:20

I see.
Thank you very much.

On Tue, Apr 16, 2019 at 2:58 PM Adam Wulkiewicz via Geometry <
geometry_at_[hidden]> wrote:

> 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 t2
>
> 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);
> }
>