MySQL
Haversine Formula
Great Circle Distance
Geospatial Queries
Database Calculations

MySQL Great Circle Distance Haversine formula

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

The Great Circle Distance, often associated with the Haversine formula, is a critical concept in geospatial data analysis. This mathematical formula calculates the shortest distance between two points on the surface of a sphere, which is essential in fields like geography, aviation, and telecommunications. In this article, we'll explore how the MySQL database engine can be used to calculate the Great Circle Distance using the Haversine formula, including examples, subtopics, and a summary table.

Theoretical Background

Great Circle and Haversine Formula

A Great Circle is the largest circle that can be drawn on a sphere, resulting from the intersection of the sphere with a plane passing through its center. On Earth, the shortest path between two points on its surface lies along the Great Circle connecting them.

The Haversine formula is an equation that provides the great-circle distance between two points on a sphere given their longitudes and latitudes. The formula assumes a perfectly spherical shape for Earth, which is approximately valid for many practical applications.

The Haversine formula is expressed as:

d=2rasin(hav(ϕ_2ϕ_1)+cos(ϕ_1)cos(ϕ_2)hav(λ_2λ_1))d = 2r \cdot \text{asin}\left(\sqrt{\text{hav}(\phi\_2 - \phi\_1) + \cos(\phi\_1) \cdot \cos(\phi\_2) \cdot \text{hav}(\lambda\_2 - \lambda\_1)}\right)

Where: • dd is the distance between the two points (along the surface of the sphere), • rr is the radius of the sphere (mean radius of Earth = 6,371 km), • ϕ1\phi_1, ϕ2\phi_2 are the latitudes of point 1 and point 2 (in radians), • λ1\lambda_1, λ2\lambda_2 are the longitudes of point 1 and point 2 (in radians), • hav(θ)=sin2(θ2)\text{hav}(\theta) = \sin^2\left(\frac{\theta}{2}\right) is the haversine function.

Implementing the Haversine Formula in MySQL

To calculate the distance, MySQL can execute SQL queries that implement this formula. Below is an example of how this can be done:

places is a table with columns lat and lon representing the latitude and longitude of points, • :source_id and :destination_id should be replaced with the specific IDs for the source and destination points. • Aviation: Pilots and airlines use Great Circle routes to plan flight paths that minimize fuel consumption and time. • Telecommunications: Providers calculate the distance between towers to optimize network coverage and signal strength. • Logistics and Shipping: Companies optimize delivery routes using these calculations to ensure cost efficiency. • Spherical Assumption: Though the formula assumes Earth is a sphere, the actual shape can introduce slight errors in extensive navigation tasks. • Crossing the 180th Meridian: Handling calculations where longitude measurements cross the International Date Line can require special consideration.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.