【问题标题】:MYSQL/PHP/Postgis: Check if a coordinate is within the radius of another coordinate( little twist )MYSQL/PHP/Postgis:检查一个坐标是否在另一个坐标的半径内(小扭曲)
【发布时间】:2012-06-16 14:46:53
【问题描述】:

是的,我想知道如何检查某个坐标是否在另一个坐标半径内。不过,这可能是一个小差异,因为 lat,long,radius 存储在数据库中,我们要检查的只是给定的坐标。

例子

数据库表

name    |   lat         |    long        |   radius(m) |
loc1    |   15.3333     |    120.312     |    100      |
loc2    |   120.321     |    -15.5436    |    123      |

我的坐标是16 lat, 120 long 假设该表中有一堆数据,具有不同的坐标和/或不同的半径,如何检查该坐标是否会落在 loc1 的 100m 半径内。 我想专门使用 PostgreSQL 来了解这一点,但对于 MYSQL 来说可能是一个好的开始

非常感谢。

【问题讨论】:

    标签: php mysql sql postgresql postgis


    【解决方案1】:

    使用 PostGIS,您可以在地理类型上使用ST_DWithin function,使用平面公制缓冲区(或“半径”)距离和地理长/纬度数据。不需要小插曲,因为这种类型的查询在 PostGIS 中很常见。

    例如,创建表格(使用 PostGIS 2.0 typmod 语法,对于 PostGIS 1.5,您需要使用其他函数来创建地理列):

    CREATE TABLE db_table
    (
      gid serial primary key,
      name character varying(100),
      geog geography(Point,4326),
      radius_m numeric
    );
    
    CREATE INDEX db_table_idx ON db_table USING gist (geog);
    
    INSERT INTO db_table(name, geog, radius_m)
    VALUES
     ('loc1', ST_MakePoint( 20.312, 15.333), 100),
     ('loc2', ST_MakePoint(-15.543, 20.312), 123);
    

    对于您的问题,在“16 lat, 120 long”处查找点 100 内的所有 db_table 行:

    SELECT *
    FROM db_table
    WHERE ST_DWithin(geog, ST_MakePoint(120, 16), radius_m + 100);
    

    另一个例子,如果你有一个带有地理列的someother_table,你可以对两者进行地理空间查询:

    SELECT a.name, b.other_attr, ST_Distance(a.geog, b.geog) AS distance_m
    FROM db_table a
    JOIN someother_table b ON ST_DWithin(a.geog, b.geog, a.raidus_m + 100)
    ORDER BY ST_Distance(a.geog, b.geog);
    

    最后,我很确定你不能用 MySQL 做任何这些,因为它无法将坐标从 Lat/Long 转换为以米为单位的距离。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 2011-05-31
      • 2012-02-16
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多