我ST_Distance_Spheroid在 PostgreSQL(使用 Postgis)中使用来计算沃金和爱丁堡之间的距离,如下所示:
CREATE TABLE pointsTable (
id serial NOT NULL,
name varchar(255) NOT NULL,
location Point NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO pointsTable (name, location) VALUES
( 'Woking', '(51.3168, -0.56)' ),
( 'Edinburgh', '(55.9533, -3.1883)' );
SELECT ST_Distance_Spheroid(geometry(a.location), geometry(b.location), 'SPHEROID["WGS 84",6378137,298.257223563]')
FROM pointsTable a, pointsTable b
WHERE a.id=1 AND b.id=2;
Run Code Online (Sandbox Code Playgroud)
我得到了 592 公里(592,053.100454442 米)的结果。
不幸的是,当我使用网络上的各种来源进行相同的计算时,我始终在 543 公里附近,相差 8.2%。
幸运的是,第三个消息来源澄清说他们使用的是半正弦公式。我不确定其他两个来源。
我在查询中做错了什么还是因为使用的公式不同?如果是这样,哪个计算最接近乌鸦可以飞行的最短距离,保持恒定的高度?
您交换了纬度和经度。如果您按正确的顺序排列它们,您将得到 544 430m。距离计算使用大圆弧,这是球体上点之间真正的最短距离。
WITH src AS (
select st_geomfromtext('POINT(-0.56 51.3168)',4326) pt1,
st_geomfromtext('POINT(-3.1883 55.9533)',4326) pt2)
SELECT
ST_DistanceSpheroid(pt1, pt2, 'SPHEROID["WGS 84",6378137,298.257223563]') Dist_sphere,
ST_Distance(pt1::geography, pt2::geography) Dist_great_circle
FROM src;
dist_sphere | dist_great_circle
------------------+-------------------
544430.941199621 | 544430.94119962
(1 row)
Run Code Online (Sandbox Code Playgroud)
在旁注中,有一个警告
ST_Distance_Spheroid 签名在 2.2.0 中已弃用。请使用 ST_DistanceSpheroid
| 归档时间: |
|
| 查看次数: |
1133 次 |
| 最近记录: |