Mysql
 sql >> Base de données >  >> RDS >> Mysql

Trouver N LineString le plus proche à partir d'un point à l'aide des extensions spatiales MySQL

C'était une réponse très utile pour moi, mais j'utilise MySQL 5.7.18, qui a des fonctions de requête géographique plus avancées ou simplement différentes. La fonction de distance publiée n'est plus nécessaire - utilisez ST_Distance_Sphere. Voici donc une mise à jour du même code pour rendre DistanceFromLine compatible avec MySQL moderne (5.7.6+)...

DROP function IF EXISTS `DistanceFromLine`;
delimiter //
    CREATE FUNCTION `DistanceFromLine`(
    route LINESTRING, point1 POINT
    ) RETURNS INT DETERMINISTIC
        BEGIN
        DECLARE a INT Default 0 ;
        DECLARE minDistance INT Default 0;
        DECLARE currentDistance INT Default 0;
        DECLARE currentpoint point ;
        DECLARE size INT Default 0 ;
        SET size =  ST_NumPoints(route);
              simple_loop: LOOP
       SET a = a+1;
       SET currentpoint = ST_PointN(route,a);
       SET currentDistance = ST_Distance_Sphere(point1,currentpoint);

       IF a = 1 THEN
        SET minDistance = currentDistance;
           END IF;

       IF currentDistance < minDistance THEN
        SET minDistance = currentDistance;
       END IF;
       IF a=size THEN
                 LEAVE simple_loop;
       END IF;
          END LOOP simple_loop;
     RETURN (minDistance);
 END//