On Mon, 18 Mar 2024 05:43:09 -0700 (PDT), Elham Peiravian wrote:
> Hello Everyone,
>
> I am using the following query 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?
>
Hi Elham,
It seems like you're running that query cyclically as the
vehicle moves.
and that you therefore know perfectly the street it was on
in the previous iteration.
in the examples you provided we find a potentially ambiguous
situation, because it is not clear whether the car turned at
the intersection or continued straight.
only with the subsequent iteration it will become clear
which of the two roads was actually followed.
conclusion: I really don't think you can do anything
at the SQL query level.
you should instead address the problem of road intersections
in the code that handles your queries, where it shouldn't be
too difficult to understand whether after the intersection
we are still on the old road or there has been a turn.
bye Sandro