Quick quiz: from New York, which direction is Makkah? Look at a standard world map and you'd say east, maybe slightly south-east. The real answer is north-east, about 58°. Same story for flights from Europe to Los Angeles that pass over Greenland.
The culprit is the map projection. Here's the math behind it, and the 20 lines of Python you need to get direction and distance right.
Rhumb lines vs great circles
On a Mercator map, a straight line is a rhumb line: a path that keeps the same compass heading the whole way. It's easy to navigate but it's not the shortest route.
The shortest path between two points on a sphere is a great circle: the intersection of the globe with a plane through its centre. On a flat map it looks curved, bending towards the nearest pole.
When someone asks "which direction is X from here?", they almost always mean the initial bearing of the great circle: which way to face to start on the shortest path.
The initial bearing formula
Given a start point (φ₁, λ₁) and a destination (φ₂, λ₂), latitudes and longitudes in radians:
import math
def initial_bearing(lat1, lon1, lat2, lon2):
"""Degrees clockwise from true north, 0-360."""
phi1, phi2 = math.radians(lat1), math.radians(lat2)
dlon = math.radians(lon2 - lon1)
x = math.sin(dlon) * math.cos(phi2)
y = math.cos(phi1) * math.sin(phi2) - math.sin(phi1) * math.cos(phi2) * math.cos(dlon)
return (math.degrees(math.atan2(x, y)) + 360) % 360
atan2(x, y) (note the argument order: east component first) returns the angle from north, and the modulo turns −90° into 270°.
Distance: the haversine formula
def haversine_km(lat1, lon1, lat2, lon2, r=6371.0088):
phi1, phi2 = math.radians(lat1), math.radians(lat2)
dphi = phi2 - phi1
dlon = math.radians(lon2 - lon1)
a = math.sin(dphi / 2) ** 2 + math.cos(phi1) * math.cos(phi2) * math.sin(dlon / 2) ** 2
return 2 * r * math.asin(math.sqrt(a))
The haversine form stays numerically stable for short distances, where the plain spherical law of cosines suffers from rounding errors.
Trying it out
Destination: the Kaaba in Makkah (21.4225° N, 39.8262° E), a classic example because the direction matters to over a billion people.
KAABA = (21.422487, 39.826206)
cities = {
"Tunis": (36.8065, 10.1815),
"Paris": (48.8566, 2.3522),
"New York": (40.7128, -74.0060),
"Jakarta": (-6.2088, 106.8456),
}
for name, (lat, lon) in cities.items():
b = initial_bearing(lat, lon, *KAABA)
d = haversine_km(lat, lon, *KAABA)
print(f"{name:9} {b:6.1f}° {d:7,.0f} km")
Tunis 112.7° 3,328 km
Paris 119.2° 4,496 km
New York 58.5° 10,306 km
Jakarta 295.2° 7,920 km
New York at 58.5° (north-east) is the counter-intuitive one. On a sphere, the shortest route from New York to Arabia arcs up over the North Atlantic.
Things that trip people up
- Bearing changes along the route. The initial bearing is only valid at the start. Halfway along a great circle, the heading is different. That's fine for "which way do I face", not for "set the autopilot once".
- True north vs magnetic north. The formula gives a bearing from true north. A magnetic compass is off by the local magnetic declination, which can be several degrees. Phones usually correct for it when location is enabled.
-
Sphere vs ellipsoid. The Earth is slightly flattened. For bearings, the spherical model is accurate to a small fraction of a degree. For survey-grade distances, use Vincenty's or Karney's algorithms (the
geographiclibpackage implements Karney's). - Antipodal points. If the destination is exactly opposite you on the globe, every direction is a shortest path and the bearing is undefined.
Wrap-up
Two short functions give you correct direction and distance anywhere on Earth. Next time a flight path looks "wrong" on the seat-back map, you'll know it's the map that's bending, not the plane.
Top comments (0)