mysqlmysql-spatial

How To find the nearest distance along a polyline Mysql


I am building a ride sharing application.

The application has a feature for searching drivers.

The drivers (employees in a work hub) can choose a route to their destination from the app.

The passengers, who also belong to the work hub, and are within the route, can join their ride.

This is accomplished by search functionality after user enter their destination in the app.

Here is the table structure of rides

id  driver_id path
 1     1       BLOB 

Here driver id is the id of driver and path is the line string from drivers source to destination.For the sake of simplicity I won't include other fields

My task is to find all the drivers who passes through my location with 85% or above ride match

After having a look at Mysql spatial Types https://dev.mysql.com/doc/refman/8.0/en/spatial-types.html I wrote a query of my own which looks partially ok, Please see the query below

set @g1=ST_GeomFromText('LINESTRING(10.013059 76.363075,10.0168011
 76.34592529999999,10.0161831
 76.34368020000001,10.0118 76.3218,9.99848 76.311936,9.9771685 76.2773228
)');
SET @g2 = ST_GeomFromText('Point(10.0141713 76.33320359999999
)');
 
 SELECT MBRCovers(@g1,@g2)

works fine Point(10.0141713 76.33320359999999) is between Point(10.013059 76.363075) and point(9.9771685 76.2773228)

but this won't work

 set @g1=ST_GeomFromText('LINESTRING(10.013059 76.363075,10.0168011
 76.34592529999999,10.0161831
 76.34368020000001,10.0118 76.3218,9.99848 76.311936,9.9771685 76.2773228
)');
SET @g2 = ST_GeomFromText('Point(10.0185876 76.3439941
)');
 
 SELECT MBRCovers(@g1,@g2)

Point(10.0185876 76.3439941) is also between Point(10.013059 76.363075) and point(9.9771685 76.2773228),

I think I need to add some covering distance for the above point to return true which I am not aware about.

I am expecting above 85 % of ride match.

What I mean by that is user is not necessary belongs to the line string the driver chosen, small tolerances are allowed.

I am using mysql version 8.

Please help.


Solution

  • set @g1=ST_GeomFromText('LINESTRING(10.013059  76.363075,
                                        10.0168011 76.34592529999999,
                                        10.0161831 76.34368020000001,
                                        10.0118    76.3218,
                                        9.99848    76.311936,
                                        9.9771685  76.2773228)', 4326);
    SET @g2 = ST_GeomFromText('Point(10.0141713 76.33320359999999)', 4326);
    SELECT ST_Distance(@g1, @g2, 'metre');
    
    | ST_Distance(@g1, @g2, 'metre') |
    | -----------------------------: |
    |              9.402168864952756 |
    
    set @g1=ST_GeomFromText('LINESTRING(10.013059  76.363075,
                                        10.0168011 76.34592529999999,
                                        10.0161831 76.34368020000001,
                                        10.0118    76.3218,
                                        9.99848    76.311936,
                                        9.9771685  76.2773228)', 4326);
    SET @g2 = ST_GeomFromText('Point(10.0185876 76.3439941)', 4326);
    SELECT ST_Distance(@g1, @g2, 'metre');
    
    | ST_Distance(@g1, @g2, 'metre') |
    | -----------------------------: |
    |              247.0472114965831 |
    

    db<>fiddle here