通过经纬度计算地图上两点的距离及方位角,百度的结果是许多个人写的函数公式,但是python的包那么多,不可能没有这种计算,自建的函数肯定不如公用的包,后来搜到一个https://stackoverflow.com/questions/17624310/geopy-calculating-gps-heading-bearing
Use the geographiclib package for python. This computes distances and bearings on the ellipsoid and much more. (You can interpolate paths, measure areas, etc.) For example, after
pip install geographiclib
you can do
>>> from geographiclib.geodesic import Geodesic
>>> Geodesic.WGS84.Inverse(-41.32, 174.81, 40.96, -5.50)
{'lat1': -41.32, 'a12': 179.6197069334283, 's12': 19959679.26735382, 'lat2': 40.96, 'azi2': 18.825195123248392, 'azi1': 161.06766998615882, 'lon1': 174.81, 'lon2': -5.5}
This computes the geodesic from Wellington, New Zealand (41.32S 174.81E) to Salamanca, Spain (40.96N 5.50W). The distance is given by s12 (19959679 meters) and the initial azimuth (bearing) is given by azi1 (161.067... degrees clockwise from north).
找到了geographiclib的主页:
https://geographiclib.sourceforge.io/html/python/code.html#module-geographiclib.geodesic
用法很简单:
>>> from geographiclib.geodesic import Geodesic
>>> # The geodesic inverse problem
... Geodesic.WGS84.Inverse(-41.32, 174.81, 40.96, -5.50)
{'lat1': -41.32,
'a12': 179.6197069334283,
's12': 19959679.26735382,
'lat2': 40.96,
'azi2': 18.825195123248392,
'azi1': 161.06766998615882,
'lon1': 174.81,
'lon2': -5.5}
按照上述命令输入即得结果,其中函数定义:
Inverse
(lat1, lon1, lat2, lon2, outmask=1929)[source]
Solve the inverse geodesic problem
Parameters: |
|
---|---|
Returns: | a Geodesic dictionary |
The results returned byGeodesic.Direct
,Geodesic.Inverse
,GeodesicLine.Position
,etc., return a dictionary with some of the following 12 fields set:
注意:
azi1 与azi2 基本同向,有时差了很小的数,可能是零点几,产生两个疑问:
1、为什么从第1个经纬度点到第2个经纬度点的方位角azi1,与从2点到1点的方位角azi2 相同?
答:我们理解错了,azi2 不是从2点到1点的方位角,而是从1点到2点的连线在2点的方位角。正好与反向方位角差180度。
2、为什么azi1、azi2不是完全相等,而是相差一个微小的度数?
答:因为地球是曲面的,虽然从1点到2点的连线方向没有变,但是这条线的方向在1点的方位角与在2点的方位角确实有细微差别。
Geodesic dictionary中的每个项都是浮点数,可以直接用于加减运算。
from geographiclib.geodesic import Geodesic
>>> Geodesic.WGS84.Inverse(-41.32, 174.81, 40.96, -5.50)
{'lat1': -41.32, 'lon1': 174.81, 'lat2': 40.96, 'lon2': -5.5, 'a12': 179.6197069334283, 's12': 19959679.26735382, 'azi1': 161.0676699861599, 'azi2': 18.825195123247305}
>>> geodict = Geodesic.WGS84.Inverse(-41.32, 174.81, 40.96, -5.50)
>>> geodict['azi2']+180
198.8251951232473
>>> type(geodict['azi2'])