By continuing to use this website, you are agreeing to our use of cookies to personalise the services we offer you.
Brand
Welcome, Guest. Please login or register.
Did you miss your activation email?
Geolives forum
News:

Author Topic: What algorithm is used to calculate distance?  (Read 1023 times)

livfoss

  • Jr. Member
  • **
  • Posts: 51
  • Keen not-so-young hiker and cyclist
    • View Profile
What algorithm is used to calculate distance?
« on: March 18, 2015, 11:55:07 am »
I'm doing some analysis of .gpx files, and I realise that knowing two points (latitude, longitude) on the Earth's surface doesn't tell you the distance between them. For that you need a formula (algorithm). A popular one which is fairly accurate for short distances is Haversine. If you don't mind telling me, which algorithm does the Web Editor use? I can't make my distances the same as yours - they are out by quite a large margin, so I'm curious to know.

thanks for any info

livfoss

  • Jr. Member
  • **
  • Posts: 51
  • Keen not-so-young hiker and cyclist
    • View Profile
Re: What algorithm is used to calculate distance?
« Reply #1 on: March 19, 2015, 03:03:09 pm »
Actually, having re-worked my calculations, I see that I am not out all that far: about 5 percent, perhaps. That could be explained by different degrees of accuracy being used in the two calculations, as many individual distances between points are quite small and so are sensitive to variations in precision in the arithmetic.

pscheven

  • Developer and support manager
  • Administrator
  • Sr. Member
  • *****
  • Posts: 458
    • View Profile
Re: What algorithm is used to calculate distance?
« Reply #2 on: March 20, 2015, 08:42:08 am »
Hello.

A simple well-known algorithm is used to calculate distance between two points in most of our applications :

Code: [Select]
public static double DistanceTo(double lat, double lon, double lat2, double lon2)
 {
            double distance = -1;

            int earthRadius = 6371;//km
            double dLat = (lat2 - lat) * (Math.PI / 180);
            double dLon = (lon2 - lon) * (Math.PI / 180);

            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
                Math.Cos(lat * (Math.PI / 180)) * Math.Cos(lat2 * (Math.PI / 180)) *
                Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            distance = earthRadius * c;

            return distance;
}

Best regards.
Patrick Schevenels.
Developer and support manager
Geolives S.A.