I'd like to get the nearest feature to a given point in Spatialite using a spatial SQL query. I'd like to speed it up using index table. Spatial index boundary should be calculated from defined point and a given tolerance, all features that are totally/partly within rtree bounding box should be used in the query.
I have tried several way, but I had always problem with spatial index boundary.
Like this:
SELECT *, Distance(GeomFromText('POINT(19.02658 47.51574)'),mo_utak_polyline.Geometry) as distance FROM mo_utak_polyline WHERE ROWID IN (SELECT pkid FROM idx_mo_utak_polyline_Geometry WHERE xmin > 19.01408 AND xmax < 19.03908 AND ymin > 47.50324 AND ymax < 47.52824) AND distance <=0.0025) ORDER by distance
Or this:
SELECT *,Intersects(GeomFromText('POINT(19.02658 47.51574)'), megyehatar_region.Geometry) as intersects FROM megyehatar_region WHERE ROWID IN (SELECT pkid FROM idx_megyehatar_region_Geometry WHERE xmin > 14.02658 AND xmax < 24.02658 AND ymin > 42.51574 AND ymax < 52.51574) AND intersects=1
I can always get features that are totally contained by my index defined bounding box. It causes a huge problem for me, e.g. when I try to query line features, when their length is totally different, it can be 1 cm or even 1000 km, so it is hard to set the size of spatial index bounding box.
Which do you think the best way to do it?
How to change this part of the query
SELECT pkid FROM idx_mo_utak_polyline_Geometry WHERE xmin > 19.01408 AND xmax < 19.03908 AND ymin > 47.50324 AND ymax < 47.52824)
to return not only the features that are contained by the bounding box, but also those intersects with it?
Thanks in advance!