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

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英里,甚至都不接近。
我读了又读了博士后的文档,没有发现我做错了什么。这个功能是不是有问题?

最佳答案

验证您的查询我得到以下错误:
回顾文档,我们可以看到:

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

关于postgresql - PostGIS的st_distance_sphere返回意外值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21945428/

10-10 04:47