Home > Net >  Pascal (Delphi 11) formula for distance in meters, between two decimal GPS points
Pascal (Delphi 11) formula for distance in meters, between two decimal GPS points

Time:09-07

I am using this formula found here Calculating speed from set of longitude and latitudes values obtained in one minute? and then converted it to Delphi (11) Pascal.

My aim is to determine meters traveled per second in a moving car. Therefore the curvature of the earth in relation to this very short distance travelled can be ignored (for my purposes).

My pascal attempt at the above Java code has a flaw in it, because it is calculating the distance at /- twice the actual distance than what it should actually be.

Please help me to correct/improve my Delphi formula that calculates the meters traveled (dDistance) between first point (dLat1, dLon1) and second point (dLat2, dLon2).

I am also wondering if my source (see above link) is actually ideal code, but I don't have the math skill to compare my source formula against other formulas like for instance = Calculate distance between two latitude-longitude points? (Haversine formula)

(Don't worry about the speed calculation, my request is only for the distance calculation please).

const degToRad = System.Pi / 180;
      iEarthRadius = 6371000;// meters
var   dDistance, dSpeed, dLat1, dLon1, dLat2, dLon2  : double;
begin
    dDistance := iEarthRadius * degToRad *
             System.Sqrt(System.Math.Power(System.Math.Cosecant(dLat1 * degToRad )
                          * (dLon1 - dLon2) , 2)   System.Math.Power(dLat1 - dLat2, 2));
end;

CodePudding user response:

Cosecant is not Cosinus! Please use cos().

CodePudding user response:

To calculate a distance (in meters) between any two points (in decimal degrees) on a perfect sphere, you have to use the Haversine formula.

This formula require to convert your points coordinates from degrees to radians, then it will be just mathematical Implementation.

The distance is computed on the surface on the sphere, not a straight line. This is the distance you would experience walking between the two points. But keep in mind that it doesn't take account of the altitude. So if you climb a mountain, the computed distance (in Haversine formula) is shorter that the actual distance.

Here is the complete implementation in Delphi, it is based on Delphi RTL unit "System.Math", so you have to add it to the uses section.

uses    ... , System.Math, ...

function HaversineDist( 
     Lat1 : Extended; // Latitude of point 1 in degrees 
     Lng1 : Extended; // Longitude of point 1 in degrees 
     Lat2 : Extended; // Latitude of point 2 in degrees 
     Lng2 : Extended) // Longitude of point 2 in degrees 
     : Extended; // Distance in meters 

var 
    LatFrom, LatTo, LngDiff: Extended; 
    Dx, Dy, Dz : Extended; 

const
    Diameter = 12,745,600; // Meters 

begin 
    LngDiff := DegToRad(Lng1 - Lng2); 
    LatFrom := DegToRad(Lat1); 
    LatTo := DegToRad(Lat2); 
 
    Dz := Sin(LatFrom) - Sin(LatTo); 
    Dx := Cos(LngDiff) * Cos(LatFrom) - Cos(LatTo); 
    Dy := Sin(LngDiff) * Cos(LatFrom);  
 
    Result := ArcSin(Sqrt(sqr(Dx)   Sqr(Dy)   Sqr(Dz)) / 2) * Diameter; end;
  • Related