title: Get nearest geometry in spatiality using spatial index
body: <p>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.</p>
  
  <p>I have tried several way, but I had always <strong>problem with spatial index boundary</strong>. </p>
  
  <p>Like this:</p>
  
  <p>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 &lt; 19.03908 AND ymin > 47.50324 AND ymax &lt; 47.52824) AND distance &lt;=0.0025) ORDER by distance</p>
  
  <p>Or this:</p>
  
  <p>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 &lt; 24.02658 AND ymin > 42.51574 AND ymax &lt; 52.51574) AND intersects=1</p>
  
  <p><strong>I can always get features that are totally contained by my index defined bounding box.</strong> 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 <strong>the size of spatial index bounding box</strong>.</p>
  
  <p>Which do you think the best way to do it?</p>
  
  <p>How to change this part of the query</p>
  
  <p>SELECT pkid FROM idx_mo_utak_polyline_Geometry WHERE xmin > 19.01408 AND xmax &lt; 19.03908 AND ymin > 47.50324 AND ymax &lt; 47.52824)</p>
  
  <p>to return not only the features that are contained by the bounding box, but also those intersects with it?</p>
  
  <p>Thanks in advance! </p>