3

I'm trying to calculate distance between two points, using latitude longitude and altitude (elevation).

I was using euklides formula in order to get my distance:

D=√((Long1-Long2)²+(Lat1-Lat2)²+(Alt1-Alt2)²)

My points are geographical coordinates and ofcourse altitude is my height above the sea. I only have lat and lng, I'm using GOOGLE API Elevation to get my altitude.

I'm developing an application which calculates my traveled distance (on my skis). Every application which I have used, gets distance traveled with included altitude. Like #Endomondo or #Garmin I cannot get my distance in 2D space because true distances are going to vary from the ones I've returned.

Which formula would be the best to calculate my distance ? Ofcourse with included altitude.

I'm writing my app in Python, with PostGis.

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
sebb
  • 1,956
  • 1
  • 16
  • 28
  • 2
    Why don't you store the coordinates in Postgis and use the ST_distance function? – Tom-db Oct 14 '15 at 07:15
  • There are several articles that dispute the claim that Garmin and others use altitude in their calculations. http://www.trailhunger.com/info/articles/garmin-distance-calculation . In the equation you have there you are combining degrees and meters. – e4c5 Oct 16 '15 at 02:11
  • Hey, @sebb did you find any of the answers helpful? – John Moutafis Dec 19 '19 at 14:56

3 Answers3

4

You can calculate distance between flat coordinates in, say, meters by using geopy package or Vincenty's formula, pasting coordinates directly. Suppose the result is d meters. Then the total distance travelled is sqrt(d**2 + h**2) where h is the change in elevation in meters.

hilberts_drinking_problem
  • 11,322
  • 3
  • 22
  • 51
3

EDIT 2019: Since this answer, I composed a Q&A style example to answer similar questions (including this one as an example): How to calculate 3D distance (including altitude) between two points in GeoDjango.

In sort:

We need to calculate the 2D great-circle distance between 2 points using either the Haversine formula or the Vicenty formula and then we can combine it with the difference (delta) in altitude between the 2 points to calculate the Euclidean distance between them as follows:

dist = sqrt(great_circle((lat_1, lon_1), (lat_2, lon_2)).m**2, (alt_1 - alt_2)**2)

The solution assumes that the altitude is in meters and thus converts the great_circle's result into meters as well.


You can get the correct calculation by translating your coordinates from Polar (long, lat, alt) to Cartesian (x, y, z):

  • Let:
    polar_point_1 = (long_1, lat_1, alt_1)
    and
    polar_point_2 = (long_2, lat_2, alt_2)
  • Translate each point to it's Cartesian equivalent by utilizing this formula:

    x = alt * cos(lat) * sin(long)
    y = alt * sin(lat)
    z = alt * cos(lat) * cos(long)
    

    and you will have p_1 = (x_1, y_1, z_1) and p_2 = (x_2, y_2, z_2) points respectively.

  • Finally use the Euclidean formula:

    dist = sqrt((x_2-x_1)**2 + (y_2-y_1)**2 + (z_2-z_1)**2)
    

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
  • what unit of measure would this distance be in and how would you go about converting it back into say meters?? – AngryDuck Nov 09 '17 at 15:01
  • @AngryDuck Since we are using Google Elevation API, `alt` is in meters, therefore the `dist` will be in meters. – John Moutafis Nov 09 '17 at 15:48
  • ok cool so as long as my altitude is in meters i can just take the value of `dist` and it will be in meters? – AngryDuck Nov 09 '17 at 15:49
  • @AngryDuck Yes that is the case (You will have the same units as your `alt` variable)! – John Moutafis Nov 09 '17 at 16:05
  • Shouldn't the altitude be modified to account for earth radius when converting to Cartesian? I.e. use alt+earth_radius in the conversion equations? – ja.ro Sep 29 '19 at 00:48
  • @ja.ro Since the initial answer I have strudied better and came up with a better solution. Have a look at the edited answer and the linked Q&A as well. – John Moutafis Dec 19 '19 at 14:55
3

I used the solution provided by John Moutafis but I didn't get a right answer.The formula needs some corrections. You will get the conversion of coordinates from Polar to Cartesian (x, y, z) at http://electron9.phys.utk.edu/vectors/3dcoordinates.htm. Use the above formula to convert spherical coordinates(Polar) to Cartesian and calculate Euclidean distance.

I used the following c# in a console app. Considering following dummy lat long

       double lat_1 = 18.457793 * (Math.PI / 180);
       double lon_1 = 73.3951930277778 *(Math.PI/180);
       double alt_1 = 270.146;

       double lat_2 = 18.4581253333333 * (Math.PI / 180);
       double lon_2 = 73.3963755277778 * (Math.PI / 180);
       double alt_2 = 317.473;

       const Double r = 6376.5 *1000; // Radius of Earth in metres

       double x_1 = r * Math.Sin(lon_1) * Math.Cos(lat_1);
       double y_1 = r * Math.Sin(lon_1) * Math.Sin(lat_1);
       double z_1 = r * Math.Cos(lon_1);

       double x_2 = r * Math.Sin(lon_2) * Math.Cos(lat_2);
       double y_2 = r * Math.Sin(lon_2) * Math.Sin(lat_2);
       double z_2 = r * Math.Cos(lon_2);

       double dist = Math.Sqrt((x_2 - x_1) * (x_2 - x_1) + (y_2 - y_1) *    
                               (y_2 - y_1) + (z_2 - z_1) * (z_2 - z_1));
Richie Bendall
  • 7,738
  • 4
  • 38
  • 58
  • This seems to only account for the radius of Earth in `r`, but should also include the elevation above Earth's surface for each point, e.g. r should be different for points 1 and 2 such that `r1 = r + alt1` and `r2 = r + alt2`. With that modification, this seems more intuitive than the @John Moutafis's answer (being most directly comparable to the Haversine calculation for XY due to the assumption of a spherical Earth in this answer), although they may both end up being mathematically correct. – emigre459 Jun 17 '20 at 14:12