↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

支持中的版本: 当前版本 (18) / 17 / 16 / 15 / 14
开发中的版本: 19 / 20devel
已结束支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3
历史版本。 PostgreSQL 9.1 已结束支持。 2016-10-27. 请参阅 当前版本手册.

F.13. earthdistance — 计算大圆距离 #

earthdistance 模块提供了两种不同的方法,用于计算地球表面上的大圆距离。首先介绍的一种依赖于 cube 模块(在安装 earthdistance 之前必须先安装该模块)。第二种方法基于内置的 point 数据类型,使用经度和纬度作为坐标。

在这个模块中,地球被假定为完全球形的。(如果这种假设对你而言精度不足,可以参见 PostGIS 项目。)

F.13.1. 基于立方体的地球距离

数据以立方体形式存储,而这些立方体实际上是点(两个角相同),使用 3 个坐标分别表示距地心的 x、y、z 距离。这里还提供了一个基于 cube 的域 earth,其中包含约束检查,用以确认该值满足这些限制,并且与地球实际表面足够接近。

地球半径取自 earth() 函数,其单位为米。不过,只要修改这一个函数,你就可以让该模块改用其他单位,或者使用你认为更合适的半径值。

这个模块在天文数据库中也有用途。天文学家大概会希望修改 earth(),使其返回半径 180/pi(),这样距离单位就是度。

这里提供了一些函数,用于按经纬度(以度为单位)输入、输出经纬度、计算两点之间的大圆距离,以及方便地指定一个可用于索引搜索的边界框。

所提供的函数见表 F.6。

表 F.6. 基于立方体的地球距离函数

FunctionReturnsDescription
earth()float8Returns the assumed radius of the Earth.
sec_to_gc(float8)float8Converts the normal straight line (secant) distance between between two points on the surface of the Earth to the great circle distance between them.
gc_to_sec(float8)float8Converts the great circle distance between two points on the surface of the Earth to the normal straight line (secant) distance between them.
ll_to_earth(float8, float8)earthReturns the location of a point on the surface of the Earth given its latitude (argument 1) and longitude (argument 2) in degrees.
latitude(earth)float8Returns the latitude in degrees of a point on the surface of the Earth.
longitude(earth)float8Returns the longitude in degrees of a point on the surface of the Earth.
earth_distance(earth, earth)float8Returns the great circle distance between two points on the surface of the Earth.
earth_box(earth, float8)cubeReturns a box suitable for an indexed search using the cube @> operator for points within a given great circle distance of a location. Some points in this box are further than the specified great circle distance from the location, so a second check using earth_distance should be included in the query.

F.13.2. 基于点的地球距离

该模块的第二部分依赖于将地球上的位置表示为 point 类型的值,其中第一个分量被视为以度为单位的经度,第二个分量被视为以度为单位的纬度。之所以采用 (longitude, latitude) 而不是相反的顺序,是因为经度更接近 x 轴的直观概念,而纬度更接近 y 轴。

这一部分只提供了一个操作符,见表 F.7。

表 F.7. 基于点的地球距离操作符

OperatorReturnsDescription
point <@> pointfloat8Gives the distance in statute miles between two points on the Earth's surface.

注意,与该模块基于 cube 的部分不同,这里的单位是固定的:修改 earth() 函数不会影响该操作符的结果。

经度/纬度表示的一个缺点是,你需要小心靠近两极以及接近经度 +/- 180 度时的边界情况。基于 cube 的表示可以避免这些不连续性。

报告文档问题

阅读 上游文档. 反馈更正前请先核对 当前版本手册.