pythongeopypyproj

Why do the Pyproj and Haversine methods to calculate heading from two lat/lon points differ from what Google Earth displays?


I am trying to implement a short python function to calculate the heading between two points. In other words if I am at point one, what heading relative to true north would I need to take to get to point two. I have used the following Python implementation which gives me essentially the same results.

from pyproj import Geod
lat1 = 42.73864
lon1 = 111.8052
lat2 = 43.24844
lon2 = 110.6083
geod = Geod(ellps='WGS84')
# This implements the highly accurate Vincenty method
bearing = geod.inv(lon1, lat1, lon2, lat2)[0]
# >>> 60.31358

I have also used the following code that uses a Haversine method

from math import degrees, radians, sin, cos, atan2

def bearing(lat1, lon1, lat2, lon2):
    lat, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon1])
    dLon = lon2 - lon1
    y = sin(dLon) * cos(lat2)
    x = cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(dLon)
    brng = degrees(atan2(y, x))
    if brng < 0: brng += 360.0
    return brng

With the same inputs from the previous implementation I get a result of 60.313 degrees, which matches the first implementation. However, when I use the Ruler function in google earth I get a result of 15.71 degrees. Furthermore when I activate the grid on google earth that shows the lines of longitude as a reference, 15.71 degrees makes far more sense. Why does the Google Earth implementation differ from the Python implementations?


Solution

  • the outputs of your geod code part are correct. try to get behind it using easy examples (see below):

    this means that there was probably a problem entering the coordinates into google earth or setting up the ruler but not in your code.

    lat1 = 40
    lon1 = 40
    lat2 = 39
    lon2 = 40
    
    #output 180.0
    

    or

    #Kansas City:
    lat1 = 39.099912 
    lon1 = -94.581213
    #St Louis:
    lat2 = 38.627089
    lon2 = -90.200203
    
    #output 96.4809
    

    the second example can be confirmed on this page: https://www.igismap.com/formula-to-find-bearing-or-heading-angle-between-two-points-latitude-longitude/