1

I posted this question originally Using nearest neighbor query in SpatiaLite?

Using the answer given I was able to get the query working on some test data. However I am having trouble applying a spatial index (which is a must as I have over 1 million points and I am trying to find the closest line in a layer that has over 300,000 lines). I've tried the query below on a test data set, but it runs slower than the query statement without the spatial index. What am I doing wrong?

UPDATE UgandaBikeTaxis_test SET dist=(SELECT 
 ST_Distance(p.geometry, l.geometry) AND
p.ROWID IN
(SELECT ROWID FROM SpatialIndex WHERE f_table_name= "ugandabiketaxis_test" AND search_frame=l.Geometry)
 FROM osm_filtered_prj_test AS l, ugandabiketaxis_test AS p
 WHERE p.uniqueID = ugandabiketaxis_test.uniqueID
 ORDER BY ST_Distance(p.geometry, l.geometry) LIMIT 1);
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jul 26, 2015 at 15:47
6
  • Can you give a reasonable maximum distance from biketaxi stations to nearest roads? Could it be 100 meters or 1 km? Commented Jul 26, 2015 at 16:21
  • Yes I definitely could but where do I apply that distance? Commented Jul 26, 2015 at 16:25
  • I would build a search_frame around biketaxe stations by using the BuildCircleMBR function gaia-gis.it/gaia-sins/spatialite-sql-latest.html and radius of the max distance. ST_x and ST_y should be used for getting x and y coordinates for BuildCircleMBR. For easier reading I would put AND p.ROWID IN (SELECT ROWID FROM SpatialIndex WHERE f_table_name= "ugandabiketaxis_test" AND search_frame=BuildCircleMBR...) last in the query. Commented Jul 26, 2015 at 16:31
  • So you mean just replace search_frame=l.Geometry with search_frame=BuildCircleMBR...? I don't quite understand. I am new with these queries. Commented Jul 26, 2015 at 16:39
  • Well, your query looks a bit odd to me but without your data it would take me some time to make test layers to play with and I feel a bit lazy right now. Anyway, you query as it stands now is using the geometries of linestrings (actually envelopes of the geometries) as search_frame. That may lead to no hit at all if linestring is very short or to all too many hits if linestring is very long. So I would first use fixed distance from biketaxi stations as a search_frame for having a better control on what happens. Commented Jul 26, 2015 at 16:47

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.