gps-wizard

Bearing

It may be handy for Python code to calculate the heading / bearing of the course.

The following code was generated by Google’s AI mode.

“find heading lat lon points python”

Pre-requisites

pip install geopy

Python Code

Slightly different Google searches produced different examples, although very similar in nature.

Example 1

from geopy.distance import geodesic
from geopy.point import Point

point1 = Point(latitude=52.20472, longitude=0.14056)
point2 = Point(latitude=52.20444, longitude=0.36056)

# Example coordinates
# Use a default ellipsoid for greater accuracy, or specify one.
# For simpler spherical calculations, you can omit the ellipsoid argument
# and pass the lat/lon values directly.
bearing = point1.bearing(point2)
print(f"The initial bearing from point1 to point2 is: {bearing:.2f} degrees")

Example 2

from geopy.point import Point
from geopy.distance import geodesic

# Example usage
point1 = Point(40.7128, -74.0060)  # New York City
point2 = Point(34.0522, -118.2437) # Los Angeles

# Get the bearing from point1 to point2
# geopy calculates the bearing as an absolute heading (e.g. North is 0)
bearing = geodesic(point1, point2).degrees[0] # Degrees between the two points
# The second element of the tuple will be the bearing.
# If you want bearing to point2 from point1, you need to adjust as shown below:
diff = point2 - point1
# Compute the heading using the bearing property of the Difference
# point1.bearing(point2) directly computes the heading.
heading = point1.bearing(point2)

print(f"The heading from {point1} to {point2} is: {heading:.2f} degrees")

Notes

The examples do not specify an ellipsoid but will be good enough for this specific purpose.

The destination() method may also be useful, if lead in / out areas are required at either end of the course.