I have a Linestring layer for which I have build a QgsSpatialIndex
with the following code:
my_linestrings_lyr_idx = QgsSpatialindex()
for f in my_linetraings_lyr.getFeatures():
my_linestrings_lyr_idx.insertFeature(f)
Now I want to find the nearest neighbor for a QgsPoint
that is definitly located on one of the linestrings layers features, code:
#assume pt is of type QgsPoint
fid = my_linestrings_lyr_index.nearestNeighbor(pt, 1)
r = QgsFeatureRequest(fid[0])
nearest_feat = my_linestrings_lyr.getFeatures(r).next()
I expect nearest_feat
to be the feature on which pt
lies in any case (because this is definitly the nearest neighbor to pt
), but in some cases that I can't exactly specify, nearest_feat
does contain other features, which does not contain pt
.
Do I use the nearestNeighbor()
method incorrectly in some way?