【问题标题】:st_distance_sphere from PostGIS returns unexpected value?来自 PostGIS 的 st_distance_sphere 返回意外值?
【发布时间】:2014-02-21 21:21:58
【问题描述】:

我正在使用 PostgreSQL v9.1.11 和 PostGIS 2.1.0。我正在使用 st_distance_sphere 来计算各个点之间的距离,并且得到了不寻常的结果。根据文档,该函数应返回所提供的 2 个点之间的距离(以米为单位)。以下是我运行的两个示例:

select st_distance_sphere(
  st_setsrid(st_makepoint(33.44, -82.60), 4326),  -- location near Warrenton, GA
  st_setsrid(st_makepoint(33.533, -82.517), 4326) -- locaton near Thomson, GA
)

返回 9325.862 米或 5.8 英里,这似乎是正确的

select st_distance_sphere(
  st_setsrid(st_makepoint(33.44, -82.60), 4326),  -- location near Warrenton, GA
  st_setsrid(st_makepoint(32.819, -97.361), 4326) -- location near Forth Worth, TX
)

返回 9873.560 米,即 6.1 英里,甚至还没有接近。

我已阅读并重新阅读 PostGIS 文档,但找不到任何我做错的地方。该功能是否只是行为不端?

【问题讨论】:

标签: postgresql postgis


【解决方案1】:

验证您的查询我收到以下错误:

回顾文档我们可以看到:

ST_Distance_Sphere — Returns linear distance in meters between two lon/la points

这意味着一个点的第一个坐标必须是经度,第二个坐标必须是纬度

因此,您将获得以下正确的位置坐标:

(-82.60, 33.44)   -- location near Warrenton, GA
(-82.517, 33.533  -- locaton near Thomson, GA
(-97.361, 32.819) -- location near Forth Worth, TX

现在,让我们再次运行查询:

select st_distance_sphere(
  st_setsrid(st_makepoint(-82.60, 33.44), 4326),  -- location near Warrenton, GA
  st_setsrid(st_makepoint(-82.517, 33.533), 4326) -- locaton near Thomson, GA
)

结果: 12891.3730143974 meters = 8.01 miles

select st_distance_sphere(
  st_setsrid(st_makepoint(-82.60, 33.44), 4326),  -- location near Warrenton, GA
  st_setsrid(st_makepoint(-97.361, 32.819), 4326) -- location near Forth Worth, TX
)

结果: 1375107.45231354 meters = 854.45 miles

【讨论】:

  • 谢谢索林,我知道这很愚蠢
【解决方案2】:

我发现使用 st_pointgeometry 是一种方法:

select ST_Distance_Sphere(st_point(lng_1, lat_1)::geometry
                         ,st_point(lng_2, lat_2)::geometry) as distance

为存储在我的数据库中的数据工作,问候!

【讨论】:

    猜你喜欢
    • 2012-07-27
    • 1970-01-01
    • 2014-06-21
    • 2020-06-24
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多