Welcome to
aleprojects.com

Simple geo calculations

All calculations below are made in assumption of spherical shape of the Earth. This allows to simplify formulas and accelerate calculations, what is especially important for mobile devices because of low performance CPU’s and power consumption. The precision is sufficient for most applications.

Distance between two locations with given longitude and latitude

The idea behind the calculation is to define the cosine of the angle between two vectors, starting at the center of the Earth and ending on the Earth’s surface in given locations. Vectors are of the same length that is equal to the Earth’s mean radius. Knowing the cosine of the angle and, therefore, the angle itself (in radians) and also the Earth’s radius the distance is calculated as the arc length of the sector formed by these vectors: L = R∙γ, where R - Earth’s radius, γ – angle in radians.

distance between two location with given longitudes and latitudes

cos γ = cos α1 ∙ cos α2 ∙ (cos(β1 - β2) - 1) + cos(α1 - α2)

where α1, α2 – latitudes, β1, β2 – longitudes.

L = R ∙ acos cos γ

Explanation of this formula:

Initial heading from one location with given longitude and latitude to another

Heading is set in degrees in range 0-360° (0° – heading to the North). Its calculation is based on spherical law of cosines. A calculation of angle between vectors A and B is also required. Joining these calculations in one function gives benefit in performance when both distance and heading are needed.

heading from one location with given latitude and longitude to another

Angle φ is a heading from location A to location B. α1, α2 – angles between vectors formed by A and B and Earth’s axis (they are equal to π/2 minus latitude of locations), γ – angle between vectors A and B, N – Earth’s pole.

In these notations, spherical law of cosines looks like this:

cos α2 = cos α1 ∙ cos γ + sin α1 ∙ sin γ ∙ cos φ

cos φ = (cos α2 - cos α1 ∙ cos γ) / (sin α1 ∙ sin γ)

For calculation of cos γ see above.

Special cases

sin α1 = 0 – this means that location A is at the Earth’s pole. If latitude of A is 90° (The North Pole), every direction from this location is to the South. Therefore φ = 180°. If latitude of A is -90° (The South Pole), every direction from this location is to the North. Therefore φ = 0°.

sin γ = 0 – this means that locations A and B coincide or that location B is exactly on the opposite side of the Earth. All directions are equal and angle φ can be arbitrary. Let it be a 0°.

In some cases, a calculated cos φ may be greater in absolute value than 1 for a very small value. So, Math.Acos(cos_phi) will return Double.NaN. Do the following check to avoid this:

if (Math.Abs(cos_phi) > 1) cos_phi = Math.Truncate(cos_phi);

Math.Acos(double) returns value in [0, π] interval. To get heading in [0, 2π] (or in 0-360°) it is necessary to do:

// lonA, lonB - longitudes of A and B in radians
...
double heading = Math.Acos(cos_phi);

if (Math.Abs(lonB - lonA) > Math.PI)
{
	if (lonB > 0) heading = 2 * Math.PI - heading;
}
else if (lonB < lonA) heading = 2 * Math.PI - heading;
...