我正在使用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文档,但找不到任何错误。这个功能只是行为不端吗?
答案 0 :(得分: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
答案 1 :(得分:0)
我发现使用 st_point 和 geometry 是执行此操作的一种方式:
select ST_Distance_Sphere(st_point(lng_1, lat_1)::geometry
,st_point(lng_2, lat_2)::geometry) as distance
并为存储在我的数据库中的数据打招呼!