Calculating distance between two points using latitude longitude and altitude (elevation)

99封情书 提交于 2019-11-29 02:46:22

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;//redius of earth in meaters

       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));

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)
    

EDIT 2018: 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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!