Skip to main content

Coordinate Conversion

Geoffrey Hunter
mbedded.ninja Author

Haversine Formula

The Haversine formula can be used to find the shortest distance (great circle distance) between two points on the earth's surface, given their latitude and longitude coordinates.

d=2rarcsin(sin2(φ2φ12)+cos(φ1)cos(φ2)sin2(λ2λ12))d = 2r \cdot arcsin(\sqrt{sin^2(\frac{\varphi_2 - \varphi_1}{2}) + cos(\varphi_1)cos(\varphi_2)sin^2(\frac{\lambda_2 - \lambda_1}{2})})

where:
dd is the shortest (great circle) distance between the two points, in meters
rr radius of the earth, in meters *(see below)
φ1,λ1\varphi_1, \lambda_1 is the latitude and longitude of point 1, in radians
φ2,λ2\varphi_2, \lambda_2 is the latitude and longitude of point 2, in radians

The typical value used for rr is 6731×103m6731 \times 10^3 m, which is the mean radius of the earth.

The code for this formula in many different languages can be found at https://rosettacode.org/wiki/Haversine_formula.

You may also see this Haversine distance formula written as:

a=sin2(φ2φ12)+cos(φ1)cos(φ2)sin2(λ2λ12)c=2atan2(a,1a)d=rca = sin^{2}(\frac{\varphi_2 - \varphi_1}{2}) + cos(\varphi_1) \cdot cos(\varphi_2) \cdot sin^{2}(\frac{\lambda_2 - \lambda_1}{2}) \\ c = 2 \cdot atan2(\sqrt{a}, \sqrt{1 - a}) \\ d = r \cdot c

where all the symbols have the same meaning as above

These two formulas are equivalent!

Bearing

This formula calculates the initial bearing, given a start and end co-ordinate.

θ=atan2(sin(λ2λ1)cosφ2cosφ1sinφ2sinφ1cosφ2cos(λ2λ1))\theta = atan2(sin(\lambda_2 - \lambda_1) \cdot cos \varphi_2 \cdot cos \varphi_1 \cdot sin \varphi_2 - \\ sin \varphi_1 \cdot cos \varphi_2 \cdot cos(\lambda_2 - \lambda_1))

where:
θ\theta is the initial bearing, in radians, from π-\pi to +π+\pi
φ1,λ1\varphi_1, \lambda_1 is the latitude and longitude of point 1, in radians
φ2,λ2\varphi_2, \lambda_2 is the latitude and longitude of point 2, in radians

Note that this calculates the initial bearing, which is the bearing you would have to be pointing in at the first co-ordinate to travel to the second co-ordinate along a great circle (shortest path on the sphere). As you travel there, the bearing is likely to change (there are a few cases in where it wouldn't change, one being if you were travelling exactly North).

Destination Coordinate Given Distance And Bearing From Start Coordinate

The following formula allows you to calculate a destination coordinate (lat/lon) if you know a starting coordinate (again, in lat/lon), ground distance (great circle distance) and initial bearing.

δ=dRp2,lat=arcsin(sinp1,latcosδ+cosp1,latsinδcosθ)p2,lon=p1,lon+arctan2(sinθsinδcosp1,lat,cosδsinp1,latsinp2,lat)\delta = \frac{d}{R} \\ p_{2,lat} = \arcsin(\sin p_{1,lat} \cdot \cos \delta + \cos p_{1,lat} \cdot \sin \delta \cdot \cos \theta ) \\ p_{2,lon} = p_{1,lon} + \arctan2(\sin \theta \cdot sin \delta \cdot \cos p_{1,lat}, \cos \delta - \sin p_{1,lat} \cdot \sin p_{2,lat})

where:
δ\delta = angular distance, in radians
dd = ground (great circle) distance between start coordinate and destination coordinate, in meters
RR = radius of the earth, in meters (6871×103m)(6871 \times 10^3m)
θ\theta = initial bearing from start to destination coordinate (clockwise from North), measured in radians
p1p_1 = starting coordinate, latitude and longitude measured in radians
p2p_2 = destination coordinate, latitude and longitude measured in radians

Note that p2,lonp_{2,lon} depends on p2,latp_{2,lat}, so you have to calculate the latitude of p2 before you can calculate the longitude.