I am attempting to calculate the bearing between two lat/long.
I don't have a question regarding the function/formula per se,
provided:
def get_bearing[lat1, long1, lat2, long2]:
dLon = [long2 - long1]
y = math.sin[dLon] * math.cos[lat2]
x = math.cos[lat1] * math.sin[lat2] - math.sin[lat1] * math.cos[lat2] * math.cos[dLon]
brng = math.atan2[y, x]
brng = np.rad2deg[brng]
return brng
the problem is that the result isn't what is expected.
The intended usage of the function returns the bearing between two lat/long pairs in a [very long] list i.e.
lat1 = path[int[len[path] * location / 1000]][0]
lat2 = path[int[len[path] * location / 1000] + 1][0]
lng1 = path[int[len[path] * location / 1000]][1]
lng2 = path[int[len[path] * location / 1000] + 1][1]
The bearing result then alters the view orientation of the plot where bearing can assume a value in the range [-180, 180]. Ideally, the result would appear such that the line formed between lat1, lng1 and lat2, lng2 is perfectly "vertical" in the plot [lat/lon annotations are switched in plot], see below
I am hoping that someone might be able to deduce the problem from the bearing returned from the function and what the expected bearing should be. A few instances below:
Current Location: 30.07134 -97.23076
Next in path: 30.0709 -97.22907
Calculated Bearing: 88.39967863143139
Expected Bearing: ~-70.67
Current Location: 29.91581 -96.85068
Next in path: 29.91556 -96.85021
Calculated Bearing: 118.9170342272798
Expected Bearing: ~122.67
Current Location: 29.69419 -96.53487
Next in path: 29.69432 -96.53466
Calculated Bearing 141.0271357781952
Expected Bearing: ~56
Current Location: 29.77357 -96.07924
Next in path: 29.77349 -96.07876
Calculated Bearing 165.24612555483893
Expected Bearing: ~104
Happy to provide additional information, thanks in advance for any/all help.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# LICENSE: public domain | |
def calculate_initial_compass_bearing[pointA, pointB]: | |
""" | |
Calculates the bearing between two points. | |
The formulae used is the following: | |
θ = atan2[sin[Δlong].cos[lat2], | |
cos[lat1].sin[lat2] − sin[lat1].cos[lat2].cos[Δlong]] | |
:Parameters: | |
- `pointA: The tuple representing the latitude/longitude for the | |
first point. Latitude and longitude must be in decimal degrees | |
- `pointB: The tuple representing the latitude/longitude for the | |
second point. Latitude and longitude must be in decimal degrees | |
:Returns: | |
The bearing in degrees | |
:Returns Type: | |
float | |
""" | |
if [type[pointA] != tuple] or [type[pointB] != tuple]: | |
raise TypeError["Only tuples are supported as arguments"] | |
lat1 = math.radians[pointA[0]] | |
lat2 = math.radians[pointB[0]] | |
diffLong = math.radians[pointB[1] - pointA[1]] | |
x = math.sin[diffLong] * math.cos[lat2] | |
y = math.cos[lat1] * math.sin[lat2] - [math.sin[lat1] | |
* math.cos[lat2] * math.cos[diffLong]] | |
initial_bearing = math.atan2[x, y] | |
# Now we have the initial bearing but math.atan2 return values | |
# from -180° to + 180° which is not what we want for a compass bearing | |
# The solution is to normalize the initial bearing as shown below | |
initial_bearing = math.degrees[initial_bearing] | |
compass_bearing = [initial_bearing + 360] % 360 | |
return compass_bearing |