ST_Within/ST_Contains 如何处理地理数据?

How ST_Within/ST_Contains works with geo data?

问题:什么algorithm/or算法s使用spatial databases来检查地理点(纬度和经度)属于"geo rect"( 4 个由经线和纬线连接的地理点)?

一开始以为是简单的投影加算法 二维平面索引,如 r-tree,但是 那么这些数据库如何处理 south/north 极点 and/or -180 和 180 经度附近的点。

例如我们的点是(0, E 180),矩形是(N 1, W 179), (N 1, E 179), (S 1, E 179), (S 1, E 179)

其中 N = 北,E = 东,W = 西,S = 南。

如果将矩形映射到 Mercator 那么我们得到:

(-126799830, 5434036), (139214148, 6832332),
(-126799830, -16488164) (139214148, -17886459),

我们的观点是 (142452996, -5527064).

并且在这样的投影点不属于矩形, 而它实际上属于。

实际上没有任何投影可以帮助解决这种情况,因为它应该 将地理点映射到几个不同的位置,以处理以下情况 矩形横跨 E 180、W 180、N 90、S 90 以及当矩形不跨越此类边界时。

那么空间数据库如何检查地理点是否属于地理矩形?

在 PostGIS 中,几何和地理之间存在差异。

看看他们的 documentation:

The basis for the PostGIS geometry type is a plane. The shortest path between two points on the plane is a straight line. That means calculations on geometries (areas, distances, lengths, intersections, etc) can be calculated using cartesian mathematics and straight line vectors.

The basis for the PostGIS geographic type is a sphere. The shortest path between two points on the sphere is a great circle arc. That means that calculations on geographies (areas, distances, lengths, intersections, etc) must be calculated on the sphere, using more complicated mathematics.

墨卡托是基于几何的投影。如果您需要使用经纬度坐标,请使用地理投影 WGS-84。

查看postgis源代码,地理类型的"within"计算完成计算对象之间的距离。

这是 geography_dwtithin 函数,由 st_dwithin https://github.com/gravitystorm/postgis/blob/master/postgis/geography_measurement.c#L107

调用

然后 lwgeom_distance_spheroid http://postgis.net/docs/doxygen/2.1/da/de7/liblwgeom_8h_a2aac0f91b6dfd27873ab828a1874805b.html 在测量距离之前比较 bboxes。

距离计算可以在这里找到 http://postgis.net/docs/doxygen/2.1/d2/ddd/lwgeodetic_8c_a56339ad4a240527a078249dc8e56b082.html#a56339ad4a240527a078249dc8e56b082

在第 1756 - 1764 行,您可以找到最简单情况(点对点)的计算。其他情况使用相同的逻辑,但寻找最近的点。

然后你可以在这里找到sphere_distance计算 http://postgis.net/docs/doxygen/2.1/d2/ddd/lwgeodetic_8c_ab9f003c831c66b723beca7103e811785.html#ab9f003c831c66b723beca7103e811785

和这里的spheroid_distance计算 http://postgis.net/docs/doxygen/2.1/d0/d7a/lwgeodetic_8h_a5c2565cd7f88783c32b777ca58d4dbcc.html#a5c2565cd7f88783c32b777ca58d4dbcc