Consider the Earth as a perfect sphere. If we wanted to measure the distance between two points on this sphere, we would simply draw a straight line between the coordinates φ1λ1 and φ2λ2, right?
Actually, not exactly. When trying to calculate this distance, we should keep in mind the angle formed by the sphere's radius, which is represented by the unit θ. By ignoring this angle, we would consider the Earth as a flat surface. Recognizing this complexity, some mathematicians developed the Haversine Formula over the years based on astronomical observations. This formula calculates the shortest distance between two points on a sphere using its radius. Nowadays, it is widely used in navigation and geolocation applications, as it allows for precise calculations of distances on the Earth's surface.
Image 1: Graphical representation of the trigonometric functions.
φ1λ1 represents the latitude and longitude of the first coordinate, and φ2λ2 represents the latitude and longitude of the second coordinate.
To understand the concept of Haversine, visualize a sphere and a triangle inscribed in it, whose vertex is located at the center of the sphere (O) and whose base connects the points φ1λ1 and φ2λ2 on the surface of the sphere. From O, draw a segment that extends to touch the surface of the sphere at a specific angle, known as the central angle (θ). The distance between the center of the sphere (O) and the point where this segment touches the surface is related to the Versine function.
The Haversine is an essential tool for calculating the orthodromic distance, which is the shortest distance between two points on the surface of a sphere. This distance is determined between the points φ1λ1 and φ2λ2 and is expressed using the trigonometric function of the sine. Essentially, the haversine(θ) is half of a versine(θ). The reason for using the Haversine instead of the Versine is that it provides more accurate calculations for short distances. When the distance between φ1λ1 and φ2λ2 is very small, the value of versine(θ) approaches 1, becoming less precise. The Haversine, on the other hand, remains effective in representing that small distance accurately.
Essentially, the Haversine is half of a Versine.
Given this explanation, the central angle (θ) is given by the relationship of the distance between the two points on the sphere divided by the radius of the sphere:
python
1defcentral_angle(d, r):2"""
3 Calculates the central angle of a circle given the distance and
4 radius.
56 Args:
7 d (float): Distance
8 r (float): Radius
910 Returns:
11 float: Central angle
12 """13return d / r
Where:
d is the distance between the two points on the sphere;
r is the radius of the sphere.
You can see in Image 1 that the value of versine(θ) is 1-cos(θ) on the trigonometric circle, and this equates to 2sin^2(θ/2) using the trigonometric identities. As previously mentioned, the haversine(θ) will be exactly half of this (sin^2(θ/2)):
python
1import math
23defhav(θ):4"""
5 Calculates the haversine of an angle.
67 Args:
8 θ (float): Angle in radians
910 Returns:
11 float: Haversine of angle.
12 """13returnpow(math.sin(θ /2),2)
As we need the haversine(θ) in a spherical system, it will be necessary to bring in the coordinates of the points φ1λ1 and φ2λ2 and calculate the haversine of the central difference between the two geographic points:
python
1defhaversine_difference(φ1, φ2, λ1, λ2):2"""
3 Calculates the haversine of the central difference between
4 two geographic points.
56 Args:
7 φ1 (float): Latitude of first point in radians.
8 φ2 (float): Latitude of second point in radians.
9 λ1 (float): Longitude of first point in radians.
10 λ2 (float): Longitude of second point in radians.
1112 Returns:
13 float: Haversine of the central difference.
14 """15return hav(φ2- φ1)+(math.cos(φ1)* math.cos(φ2)* hav(λ2- λ1))
Where:
φ1 and φ2 are the latitudes of each point in radians;
λ1 and λ2 are the longitudes of each point in radians.
Now that we have the main tools to calculate the central angle and the distance of the geographic points, we can compute the distance between the two points on the coordinate using the inverse sine function (arcsin):
python
1import math
23defhaversine(φ1, φ2, λ1, λ2, rad=6371):4"""
5 Calculates the distance between two points on the Earth's
6 surface given their latitude and longitude in degrees.
78 Args:
9 φ1 (float): Latitude of first point in degrees.
10 φ2 (float): Latitude of second point in degrees.
11 λ1 (float): Longitude of first point in degrees.
12 λ2 (float): Longitude of second point in degrees.
13 rad (int): Radius of the Earth in the desired units (default is 6371 km).
1415 Returns:
16 float: Distance between the two points in the units corresponding to the provided Earth's radius.
17 """1819 φ1, φ2= math.radians(φ1), math.radians(φ2)20 λ1, λ2= math.radians(λ1), math.radians(λ2)2122 central_angle_hav = haversine_difference(φ1, φ2, λ1, λ2)2324return2* rad * math.asin(math.sqrt(central_angle_hav))
Where:
φ1 and φ2 are the latitudes of each point in radians;
λ1 and λ2 are the longitudes of each point in radians;
rad is the radius of the perfect sphere.
Note:
Notice that we are converting the angles of the coordinates into radians, as we are computing trigonometric functions in the radian system.
Real-world Applications
The ability to accurately calculate the distance between two points on the Earth's surface has various uses, such as: GPS navigation, logistics and transport, aviation, geological studies, environmental research, tourism, and more.
Some Considerations
As many of us know, Earth does not have the exact shape of a perfect sphere; it more closely resembles an oblate spheroid, with significant variations due to terrain, gravity, among other factors. The "Haversine distance" method provides us with an approximation of the real distance between two points on the Earth's surface and is sufficiently accurate for many applications that don't require extreme precision. If we need more accurate calculations, we can turn to other methods and formulas, like the Vincenty formula, and in even more specific situations, the Earth Gravitational Model (EGM).