In traditional methods we calculate distance by assuming the earth as a sphere. But the truth is the Earth is neither perfect spherical nor ellipse hence calculating the distance on its surface is a challenging task.

There are many algorithms that deal with the shortest distance problem. Each algorithm is related to a specific shape or representation. In this blog we are going to find more about Haversine and Vincenty formula.

Haversine formula:

By using this formula we can easily compute the great-circle distance (The shortest distance between two points on the surface of a Sphere). – Giving an ‘as-the-crow-flies’ distance between the points (ignoring any hills they fly over, of course!)

Haversine formula mostly used to calculate distances for spherical shape.

Formula:

haversine (d/r) = haversine (Φ2 – Φ1) + cos(Φ1)cos(Φ2)haversine(λ2 1)

Haversine formula

Where d is the distance between two points with longitude and latitude ( λ,Φ ) and r is the radius of the earth.

Java code to calculate the distance between two points using Haversine formula:

public double getDistance(double startLat, double startLong, double endLat, double endLong) {

       double haversine, distance;

       double dLatitude,  dLongitude;

       double DEG_RAD = 0.01745329251994;

       double R_EARTH = 6367.45;

       dLatitude, = (endLat – startLat) * DEG_RAD;

       dLongitude = (endLong – startLong) * DEG_RAD;

       haversine = Math.sin(dLat * 0.5) * Math.sin(dLat * 0.5) +

               Math.sin(dLon * 0.5) * Math.sin(dLon * 0.5) *

                       Math.cos(startLat * DEG_RAD) *

                       Math.cos(destLat * DEG_RAD);

       distance = Math.asin(Math.sqrt(haversine)) * R_EARTH * 2.0;

       return distance;

   }

Why to use it: Haversine provide simpler computation. Also reduces battery drainage when compared to Vincenty.

Why not to use itIt does not provide high accuracy that Vincenty offers.

Vincenty formula :

The formulae were developed by Thaddeus Vincenty(1975a) for calculating geodesic distances between a pair of latitude/longitude points on an ellipsoidal model of the Earth.

Unlike the Haversine method for calculating distance on a sphere, these formulae are an iterative method and assume the Earth is an ellipsoid.

This formula include a direct and an inverse method where:

  1. Direct Method: It computes the location of a point that is a given distance and azimuth from another point
  2. Inverse Method: It computes the geographical distance and azimuth between two given points.

For eg. In the Inverse Method outputs λ after assigning several constants including the length of the semi-major axis, length of the semi-minor axis, flattening, latitude coordinates, reduced latitudes, etc. The aim of the method is to minimise the value of the output λ (i.e. when the results converge to a desired degree of accuracy):

When the difference between the current value of λ and the value of λ from the previous iteration is less than the convergence tolerance then the final stage of the Inverse Method can be executed:

Java code to calculate the distance between two points using Vincenty formula:

public static float vincentyDist(float startLat, float startLong, float endLat, float endLong) {

   double earthRadius = 6371000; //meters

   double dLat = Math.toRadians(endLat-startLat);

   double dLng = Math.toRadians(endLong-startLong);

   double a = Math.sin(dLat/2) * Math.sin(dLat/2) +

              Math.cos(Math.toRadians(startLat)) * Math.cos(Math.toRadians(endLat)) *

              Math.sin(dLng/2) * Math.sin(dLng/2);

   double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

   float dist = (float) (earthRadius * c);

   return dist;

 }

Why to use it: Vincenty is more accurate .

Why not to use it: It is more computationally intensive and will, therefore perform slower and increases battery drainage.

How do you choose which distance calculation to use? A few important factors:

A few important factors to considered when choosing formulas for calculating distance:

  1. What are the requirements and use cases for your application?
  2. How accurate do you need the distance calculation to be?
  3. How fast do you need to do it?
  4. Which method/implementation works best with the data you have?

As with anything “better” is a matter of your particular application. For your application, Vincenty may be an appropriate choice than Haversine. You will have to look at the particulars of your use cases and need of your application.

For Example: Let’s calculate the distance between Pune (PNQ) to Sydney (SYD) using Air Miles Calculator.

distance calculator

Observation:

Here we see that Haversine formula shows 6236.243 miles and Vincenty formula shows 6231.892 miles which is more accurate.

So, which one to use depends on your needs.

Choose the best fit!

References :

  1. https://community.esri.com/groups/coordinate-reference-systems/blog/2017/10/11/vincenty-formula
  2. https://www.airmilescalculator.com/distance/pnq-to-syd/
  3. https://stackoverflow.com/questions/38248046/is-the-haversine-formula-or-the-vincentys-formula-better-for-calculating-distan/38663071

 

Headquarter

4701 Patrick Henry Drive,
Bldg. 16, Suite 106, Santa Clara, California 95054
781-640-0588
sales@neovatechsolutions.com

Development Center

P3-603, Pentagon Tower,
Magarpatta City, Hadapsar, Pune, Maharashtra 411028
020-26899991