代码之家  ›  专栏  ›  技术社区  ›  Niklas

Postgres Postgis ST_D查询不准确

  •  0
  • Niklas  · 技术社区  · 5 月前

    我有下表:

    CREATE TABLE locations (
      id SERIAL PRIMARY KEY,
      name VARCHAR(255),
      location GEOMETRY(Point, 4326)
    );
    

    现在,如果我插入两个:

    INSERT INTO locations (name, location)
    VALUES ('Berlin', ST_SetSRID(ST_MakePoint(13.405, 52.52), 4326));
    
    INSERT INTO locations (name, location)
    VALUES ('Krakow', ST_SetSRID(ST_MakePoint(19.945, 50.0647), 4326));
    

    并使用以下查询:

    select * from locations where ST_DWithin(location::geography, ST_GeographyFromText('SRID=4326;POINT(13.405 52.52)'), 531000);
    

    我只去了柏林。他们之间的距离只有 530100 不过。因此,即使还有900米,它也不会返回。只有当我将其更改为 531500 。但那距离原点大约1400米,太远了。

    我做错什么了吗?

    1 回复  |  直到 5 月前
        1
  •  3
  •   JGH    5 月前

    看起来地理使用的距离是 两者准确。

    st_distance doc :

    对于地理类型,默认返回最小测地线距离 在以米为单位的两个地理区域之间,计算确定的球体 如果use_spheroid为假,则更快的球面计算 使用。

    st_distanceSphere doc :

    返回两个经度/纬度点之间的最小距离(单位:米)。使用a 球形地球和由球体定义的半径 SRID。比ST_DistanceSpheroid更快,但精度较低

    比较使用球体或更精确的球体计算的距离,我们确认 st_dwithin 还使用在球体上计算的距离。你仍然可以强迫 st_dinner 使用球体代替球体,但精度较低。

    SELECT st_distance(Berlin::geography,Krakow::geography) as dist_geog,
        st_distance(Berlin::geography,Krakow::geography, false) as dist_geog_sphere,
        st_distanceSphere(Berlin,Krakow) as dist_sphere,
        st_distanceSpheroid(Berlin,Krakow) as dist_spheroid,
        ST_DWithin(Berlin::geography,Krakow::geography,530200, false) dwithin_sphere
    FROM geo;
    
        dist_geog    | dist_geog_sphere |   dist_sphere   |   dist_spheroid   | dwithin_sphere
    -----------------+------------------+-----------------+-------------------+----------------
     531430.81283859 |  530123.37723304 | 530123.37723304 | 531430.8128385914 | t
    (1 row)