MySQL latitude and Longitude table setup

前端 未结 2 852
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 12:01

I want to store latitude and longitude values of places in a mysql database table. With the future in mind I will want to be able to find these places within a certain radiu

相关标签:
2条回答
  • 2020-12-03 12:33

    I've digged few hours through tons of topics and could nowhere find a query, returning points in a radius, defined by km. ST_Distance_Sphere does it, however, the server is MariaDB 5.5, not supporting ST_Distance_Sphere().

    Managed to get something working, so here is my solution, compatible with Doctrine 2.5 and the Doctrine CrEOF Spatial Library:

        $sqlPoint = sprintf('POINT(%f %f)', $lng, $lat);
    
        $rsm = new ResultSetMappingBuilder($this->manager);
        $rsm->addRootEntityFromClassMetadata('ApiBundle\\Entity\\Place', 'p');
    
        $query = $this->manager->createNativeQuery(
            'SELECT p.*, AsBinary(p.location) as location FROM place p ' .
            'WHERE (6371 * acos( cos( radians(Y(ST_GeomFromText(?))) ) ' .
            '* cos( radians( Y(p.location) ) ) * cos( radians( X(p.location) ) ' .
            '- radians(X(ST_GeomFromText(?))) ) + sin( radians(Y(ST_GeomFromText(?))) ) * sin( radians( Y(p.location) ) ) )) <= ?',
            $rsm
        );
    
        $query->setParameter(1, $sqlPoint, 'string');
        $query->setParameter(2, $sqlPoint, 'string');
        $query->setParameter(3, $sqlPoint, 'string');
        $query->setParameter(4, $radius, 'float');
    
        $result = $query->getResult();
    

    Assuming lng and lat is XY of the fixed point, Place is the entity with a "location" field POINT type. I could not use DQL directly due to problems with the param binding of MySQL, that's why the low-level native query. The rsm is required to map results into entity objects. Can live without it, though.

    Feel free to use it. I hope it will save you some time.

    0 讨论(0)
  • 2020-12-03 12:35

    You should store the points in a singe column of datatype Point which you can index with a SPATIAL index (if your table type is MyISAM):

    CREATE SPATIAL INDEX sx_place_location ON place (location)
    
    SELECT  *
    FROM    mytable
    WHERE   MBRContains
                   (
                   LineString
                           (
                           Point($x - $radius, $y - $radius),
                           Point($x + $radius, $y + $radius)
                           )
                   location
                   )
            AND Distance(Point($x, $y), location) <= $radius
    

    This will drastically improve the speed of queries like "find all within a given radius".

    Note that it is better to use plain TM metrical coordinates (easting and northing) instead of polar (latitude and longitude). For small radii, they are accurate enough, and the calculations are simplified greatly. If all your points are in one hemishpere and are far from the poles, you can use a single central meridian.

    You still can use polar coordinates of course, but the formulae for calculating the MBR and the distance will be more complex.

    0 讨论(0)
提交回复
热议问题