I am using the following query in SpatiaLite to find the closest road segment to a given GPS location:
SELECT id, Distance(segment, GeomFromText('POINT(-79.406166 43.666075)'),1) AS Distance
FROM rs
WHERE ROWID IN (
SELECT ROWID FROM SpatialIndex
WHERE f_table_name = 'rs'
AND f_geometry_column = 'segment'
AND search_frame = ST_Expand(MakePoint(--79.406166, 43.666075, 4326), .001)
LIMIT 10
)
ORDER BY Distance ASC LIMIT 1;
Now I am having trouble at intersections because the GPS points are literarily closer to another segment than the one the car is travelling on. Examples below:
As far as the query goes, what else can I add to it so that even at intersections it will choose the road I am actually traveling on?