python 笔记 haversine (两个经纬度坐标之间的距离)
Posted UQI-LIUWJ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 笔记 haversine (两个经纬度坐标之间的距离)相关的知识,希望对你有一定的参考价值。
1 安装包
pip install haversine
2 haversine:计算两个点经纬度之间的距离
from haversine import haversine
lyon = (45.7597, 4.8422)
paris = (48.8567, 2.3508)
haversine(lyon, paris)
#392.2172595594006 默认是公里
haversine(lyon, paris, unit=Unit.MILES)
#单位设置为英里
#243.71250609539814
haversine(lyon, paris, unit='mi')
#243.71250609539814
3 Unit——支持的所有单位
from haversine import Unit
tuple(Unit)
'''
(<Unit.KILOMETERS: 'km'>,
<Unit.METERS: 'm'>,
<Unit.MILES: 'mi'>,
<Unit.NAUTICAL_MILES: 'nmi'>,
<Unit.FEET: 'ft'>,
<Unit.INCHES: 'in'>,
<Unit.RADIANS: 'rad'>,
<Unit.DEGREES: 'deg'>)
'''
4 inverse haversine
从给定的向量(距离和方向)和起点计算点。
目前明确支持(北,东,南,西)和(东北,东南,西南,西北)方向。但也允许以弧度表示的角度。
from haversine import inverse_haversine, Direction
from math import pi
paris = (48.8567, 2.3508) # (lat, lon)
inverse_haversine(paris, 32, Direction.WEST)
#(48.85587279023947, 1.9134085092836945)
inverse_haversine(paris, 50, Direction.NORTH, unit=Unit.MILES)
#(49.58035791599536, 2.3508
inverse_haversine(paris, 32, pi * 1.25)
#(48.65279552300661, 2.0427666779658806)
5 haversine_vector
from haversine import haversine_vector, Unit
lyon = (45.7597, 4.8422) # (lat, lon)
paris = (48.8567, 2.3508)
new_york = (40.7033962, -74.2351462)
haversine_vector([lyon, lyon,lyon], [paris, new_york,lyon], Unit.KILOMETERS)
#array([ 392.21725956, 6163.43638211, 0. ])
#分别是 lyon-paris,lyon-new_york,lyon-lyon的距离
5.1 comb=True
from haversine import haversine_vector, Unit
lyon = (45.7597, 4.8422) # (lat, lon)
london = (51.509865, -0.118092)
paris = (48.8567, 2.3508)
new_york = (40.7033962, -74.2351462)
haversine_vector([lyon, london], [paris, new_york], Unit.KILOMETERS, comb=True)
'''
array([[ 392.21725956, 343.37455271],
[6163.43638211, 5586.48447423]])
'''
以上是关于python 笔记 haversine (两个经纬度坐标之间的距离)的主要内容,如果未能解决你的问题,请参考以下文章