python 计算地球上两点距离和方位角(bearing)的包geographiclib

通过经纬度计算地图上两点的距离及方位角,百度的结果是许多个人写的函数公式,但是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:
  • lat1 – latitude of the first point in degrees
  • lon1 – longitude of the first point in degrees
  • lat2 – latitude of the second point in degrees
  • lon2 – longitude of the second point in degrees
  • outmask – the output mask
Returns:

a Geodesic dictionary

返回结果:

Geodesic dictionary

The results returned byGeodesic.Direct,Geodesic.Inverse,GeodesicLine.Position,etc., return a dictionary with some of the following 12 fields set:

  • lat1 = φ1, latitude of point 1 (degrees) 第一个点的纬度
  • lon1 = λ1, longitude of point 1 (degrees)第一个点的经度
  • azi1 = α1, azimuth of line at point 1 (degrees) 在第一个点的方位角
  • lat2 = φ2, latitude of point 2 (degrees)第2个点的纬度
  • lon2 = λ2, longitude of point 2 (degrees)第2个点的经度
  • azi2 = α2, (forward) azimuth of line at point 2 (degrees)在第2个点的方位角
  • s12 = s12, distance from 1 to 2 (meters) 两点间距离
  • a12 = σ12, arc length on auxiliary sphere from 1 to 2 (degrees) 地球球面弧度
  • m12 = m12, reduced length of geodesic (meters)
  • M12 = M12, geodesic scale at 2 relative to 1 (dimensionless)
  • M21 = M21, geodesic scale at 1 relative to 2 (dimensionless)
  • S12 = S12, area between geodesic and equator (meters2)

注意:

azi1azi2 基本同向,有时差了很小的数,可能是零点几,产生两个疑问:

1、为什么从第1个经纬度点到第2个经纬度点的方位角azi1,与从2点到1点的方位角azi2 相同?

答:我们理解错了,azi2 不是从2点到1点的方位角,而是从1点到2点的连线在2点的方位角。正好与反向方位角差180度。

 

2、为什么azi1azi2不是完全相等,而是相差一个微小的度数?

答:因为地球是曲面的,虽然从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'])

 

你可能感兴趣的:(python 计算地球上两点距离和方位角(bearing)的包geographiclib)