Ellipsoidal Distance Algorithm: Precise Geographic Distance Calculation
February 9, 2024 • ☕️ 4 min read • 🏷 computer, software, algorithm
Translated by author into: English
Geographic distance calculations are important to accurately calculate the distance between points, taking into account the complexity and curvature of the earth’s surface. For such precise calculations Lambert’s ellipsoidal distance algorithm is often preferred. Lambert’s algorithm calculates the distance between two points, taking into account the ellipsoidal shape of the earth’s surface. This algorithm is used in many areas such as geographic information systems (GIS), map projects, navigation software and space technologies.
Lambert’s ellipsoidal distance algorithm uses mathematical formulas to calculate the distance and direction between two points. These formulas more accurately calculate the distance by taking into account the curve and ellipsoid shape of the earth’s surface. The complexity of the algorithm is generally fixed and the calculated distance can be used in various applications. For example, popular mapping platforms such as Google Maps and ArcGIS can use this algorithm in distance calculations. It is also widely used in areas such as planning ship and aircraft routes, land mapping and geodesy studies.
Working Steps
- Getting Coordinates of Points: The latitude and longitude coordinates of two points are taken. These coordinates are usually expressed in degrees and then converted to radians.
- Calculating the Distance Between Meridian Circles: The distance between the meridian circles of two points is calculated. It starts with taking the absolute value of the longitude difference of these two points.
- Calculation of Azimuth Angles: Azimuth angles of two points are calculated. It is derived from the coordinates of two points and is usually calculated using trigonometric formulas.
- Calculation of Distance: The distance between two points is calculated according to a specific ellipsoid model. This is accomplished using complex mathematical formulas such as the Vincenty formula. These formulas provide more accurate results by taking into account the curve and ellipsoid shape of the earth’s surface.
- Conversion of Results: The resulting distance is converted, if necessary, to be presented in the desired format. For example, the distance may be calculated in meters, but it can be converted into kilometers and presented to the user.
Algorithm Complexity
The complexity of Lambert’s ellipsoidal distance algorithm is usually O(1) or constant. This means that the algorithm takes a constant time regardless of the input size. However, some calculation steps may not be constant, especially calculating azimuth angles between points.
Usage areas
Lambert’s algorithm is used in various fields such as geographic information systems (GIS), mapping projects, navigation software and space technologies.
Ellipsoidal Distance Algorithm implementation in GoLang:
package main
import (
"fmt"
"math"
)
const (
a = 6378137.0 // semi-major axis (in meters)
f = 1 / 298.257223563 // flattening
b = a * (1 - f) // semi-minor axis (in meters)
)
func main() {
lat1 := 40.7128 // latitude of New York City
lon1 := -74.0060 // longitude of New York City
lat2 := 34.0522 // latitude of Los Angeles
lon2 := -118.2437 // longitude of Los Angeles
distance := calculateDistance(lat1, lon1, lat2, lon2)
fmt.Printf("Distance between New York City and Los Angeles: %.2f km\n", distance/1000)
}
func calculateDistance(lat1, lon1, lat2, lon2 float64) float64 {
phi1 := toRadians(lat1)
lambda1 := toRadians(lon1)
phi2 := toRadians(lat2)
lambda2 := toRadians(lon2)
deltaLambda := math.Abs(lambda1 - lambda2)
sigma := math.Atan(math.Sqrt(math.Pow(math.Cos(phi2)*math.Sin(deltaLambda), 2) +
math.Pow(math.Cos(phi1)*math.Sin(phi2)-math.Sin(phi1)*math.Cos(phi2)*math.Cos(deltaLambda), 2)) /
(math.Sin(phi1)*math.Sin(phi2) + math.Cos(phi1)*math.Cos(phi2)*math.Cos(deltaLambda)))
alpha := math.Atan2(math.Cos(phi1)*math.Sin(phi2)-math.Sin(phi1)*math.Cos(phi2)*math.Cos(deltaLambda),
math.Sin(deltaLambda)*math.Cos(phi2))
if alpha < 0 {
alpha += 2 * math.Pi
}
u2 := math.Pow(math.Cos(alpha), 2) * ((math.Pow(a, 2) - math.Pow(b, 2)) / math.Pow(b, 2))
A := 1 + (u2 / 16384) * (4096 + u2 * (-768 + u2 * (320 - 175 * u2)))
B := (u2 / 1024) * (256 + u2 * (-128 + u2 * (74 - 47 * u2)))
deltaSigma := B * math.Sin(sigma) * (math.Cos(2 * sigma) + (B / 4) * (math.Cos(sigma) * (-1 + 2 * math.Pow(math.Cos(2 * sigma), 2)) -
(B / 6) * math.Cos(4 * sigma) * (-3 + 4 * math.Pow(math.Sin(sigma), 2)) * (-3 + 4 * math.Pow(math.Cos(2 * sigma), 2))))
s := b * A * (sigma - deltaSigma)
return s
}
func toRadians(degrees float64) float64 {
return degrees * (math.Pi / 180)
}When the program is run, the output will be as follows.
Distance between New York City and Los Angeles: 3931.50 kmThe working version of the program can be accessed from here.
The algorithm performs distance calculations taking into account the curvature and ellipsoid shape of the earth’s surface. It uses mathematical formulas such as the Vincenty formula when calculating the distance between two points. These formulas include various trigonometric and geometric calculations to more accurately calculate the curve of the earth’s surface.
Areas of use include geographic information systems (GIS), map projects, navigation software and space technologies. For example; This algorithm can be used in land mapping and geodesy studies, while planning the routes of ships and aircraft. This algorithm is also valuable in any field where geographic distances need to be calculated accurately.
Resources
- https://python.algorithmexamples.com/web/geodesy/lamberts_ellipsoidal_distance.html
- https://community.esri.com/t5/coordinate-reference-systems-blog/distance-on-an-ellipsoid-vincenty-s-formulae/ba-p/902053
- https://en.wikipedia.org/wiki/Vincenty%27s_formulae
- https://www.sciencedirect.com/science/article/abs/pii/S0021999122001620