2D 距离(“<->”运算符;几何)和 ST_Distance(地理)排序顺序之间的 PostGIS 不匹配
PostGIS mismatch between 2D Distance ("<->" operator; geometry) and ST_Distance (geography) sorting orders
使用 PostgreSQL 9.5.2、PostGIS 2.2,给定以下地理坐标:
'SRID=4326;POINT(24.8713597 60.1600568)' -- Origin
'SRID=4326;POINT(24.87717970 60.19824480)' -- Destination A
'SRID=4326;POINT(24.91281220 60.15821350)' -- Destination B
'SRID=4326;POINT(24.91404950 60.16373390)' -- Destination C
'SRID=4326;POINT(24.91552820 60.16129280)' -- Destination D
按 "geographical" 距离排序 (ST_Distance method):
select name, st_distance('SRID=4326;POINT(24.8713597 60.1600568)'::geography, geo)
from (select 'SRID=4326;POINT(24.87717970 60.19824480)'::geography as geo, 'A' as name
union select 'SRID=4326;POINT(24.91281220 60.15821350)'::geography as geo, 'B' as name
union select 'SRID=4326;POINT(24.91404950 60.16373390)'::geography as geo, 'C' as name
union select 'SRID=4326;POINT(24.91552820 60.16129280)'::geography as geo, 'D' as name) tmp
order by 2;
结果:
B,2311.075069284
C,2405.58508757
D,2456.504535795
A,4266.971129052
而按 "geometrical" 二维距离 (<-> Operator) 排序:
select name, 'SRID=4326;POINT(24.8713597 60.1600568)'::geometry <-> geom
from (select 'SRID=4326;POINT(24.87717970 60.19824480)'::geometry as geom, 'A' as name
union select 'SRID=4326;POINT(24.91281220 60.15821350)'::geometry as geom, 'B' as name
union select 'SRID=4326;POINT(24.91404950 60.16373390)'::geometry as geom, 'C' as name
union select 'SRID=4326;POINT(24.91552820 60.16129280)'::geometry as geom, 'D' as name) tmp
order by 2;
结果:
A,0.03862894955858695
B,0.041493463474867306
C,0.042847871457636175
D,0.04418579056948194
...我希望顺序完全相同。
我错过了什么?
您正在 60 度纬度处进行计算。这里的纬度比经度大得多。具体地,在60度纬度处,纬度为111.412km,而经度为55.800km。这意味着经度值的分离比纬度值的分离重要得多。
A 24.87717970 - 24.8713597 = 0.006...
B 24.91281220 - ... = 0.041...
C 24.91404950 - ... = 0.043...
D 24.91552820 - ... = 0.044
这与您的结果非常吻合。
使用 PostgreSQL 9.5.2、PostGIS 2.2,给定以下地理坐标:
'SRID=4326;POINT(24.8713597 60.1600568)' -- Origin
'SRID=4326;POINT(24.87717970 60.19824480)' -- Destination A
'SRID=4326;POINT(24.91281220 60.15821350)' -- Destination B
'SRID=4326;POINT(24.91404950 60.16373390)' -- Destination C
'SRID=4326;POINT(24.91552820 60.16129280)' -- Destination D
按 "geographical" 距离排序 (ST_Distance method):
select name, st_distance('SRID=4326;POINT(24.8713597 60.1600568)'::geography, geo)
from (select 'SRID=4326;POINT(24.87717970 60.19824480)'::geography as geo, 'A' as name
union select 'SRID=4326;POINT(24.91281220 60.15821350)'::geography as geo, 'B' as name
union select 'SRID=4326;POINT(24.91404950 60.16373390)'::geography as geo, 'C' as name
union select 'SRID=4326;POINT(24.91552820 60.16129280)'::geography as geo, 'D' as name) tmp
order by 2;
结果:
B,2311.075069284
C,2405.58508757
D,2456.504535795
A,4266.971129052
而按 "geometrical" 二维距离 (<-> Operator) 排序:
select name, 'SRID=4326;POINT(24.8713597 60.1600568)'::geometry <-> geom
from (select 'SRID=4326;POINT(24.87717970 60.19824480)'::geometry as geom, 'A' as name
union select 'SRID=4326;POINT(24.91281220 60.15821350)'::geometry as geom, 'B' as name
union select 'SRID=4326;POINT(24.91404950 60.16373390)'::geometry as geom, 'C' as name
union select 'SRID=4326;POINT(24.91552820 60.16129280)'::geometry as geom, 'D' as name) tmp
order by 2;
结果:
A,0.03862894955858695
B,0.041493463474867306
C,0.042847871457636175
D,0.04418579056948194
...我希望顺序完全相同。
我错过了什么?
您正在 60 度纬度处进行计算。这里的纬度比经度大得多。具体地,在60度纬度处,纬度为111.412km,而经度为55.800km。这意味着经度值的分离比纬度值的分离重要得多。
A 24.87717970 - 24.8713597 = 0.006...
B 24.91281220 - ... = 0.041...
C 24.91404950 - ... = 0.043...
D 24.91552820 - ... = 0.044
这与您的结果非常吻合。