Python-1-geopy.distance

Geopy模块

当我们需要计算A,B两点之间的最短距离时,可以使用geopy模块的distance.distance函数(可参见官方文档说明)。

Geopy可以用 geodesic distancegreat-circle distance ,默认测地线距离作为函数 geopy.distance.distance .

大圆距离( great_circle )使用地球的球形模型,使用国际大地测量学和地球物理学联合会定义的平均地球半径6371.0087714150598公里,约6371.009公里(WGS-84),误差高达0.5%。半径值存储在 distance.EARTH_RADIUS ,因此可以对其进行自定义(但是,它应该始终以公里为单位)。

具体而言,以下是以下示例 distance.distance 用法,取对 (lat, lon) 元组:

1
2
3
4
5
6
7
8
9
10
>>> from geopy import distance
>>> newport_ri = (41.49008, -71.312796)
>>> cleveland_oh = (41.499498, -81.695391)
>>> print(distance.distance(newport_ri, cleveland_oh).miles)
538.39044536

>>> wellington = (-41.32, 174.81)
>>> salamanca = (40.96, -5.50)
>>> print(distance.distance(wellington, salamanca).km)
19959.6792674

说明:

  • 目前所有的算法都假设点的高度要么为零(如上面的例子中所示)要么相等,并且相对较小。因此,海拔高度不会影响结果距离:

  • 如果需要使用高程计算距离,则对于短距离 Euclidean distance 公式可以给出一个合适的近似值:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    >>> import math
    >>> from geopy import distance
    >>> p1 = (43.668613, 40.258916, 0.976)
    >>> p2 = (43.658852, 40.250839, 1.475)
    >>> flat_distance = distance.distance(p1[:2], p2[:2]).km
    >>> print(flat_distance)
    1.265133525952866
    >>> euclidian_distance = math.sqrt(flat_distance**2 + (p2[2] - p1[2])**2)
    >>> print(euclidian_distance)
    1.359986705262199