Add distance calculation based on haversine
Alire installation and checkup / build (ubuntu-20.04) (push) Failing after 4m0s
Alire installation and checkup / build (ubuntu-20.04) (push) Failing after 4m0s
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
|
||||||
|
|
||||||
package body Grl.Coordinates is
|
package body Grl.Coordinates is
|
||||||
|
|
||||||
function To_DD (DM_Base : Coordinate) return Coordinate is
|
function To_DD (DM_Base : Coordinate) return Coordinate is
|
||||||
@@ -37,4 +39,39 @@ package body Grl.Coordinates is
|
|||||||
return DD_Target;
|
return DD_Target;
|
||||||
end To_DD;
|
end To_DD;
|
||||||
|
|
||||||
|
function Plane_Distance (Origin, Destination : Coordinate) return H is
|
||||||
|
Heuristic : H;
|
||||||
|
A : H;
|
||||||
|
DD_Origin : constant Coordinate := To_DD (Origin);
|
||||||
|
DD_Destination : constant Coordinate := To_DD (Destination);
|
||||||
|
Earth_Radius : constant H := 6_371_000.0; -- M
|
||||||
|
Delta_Phi : constant H := -- Latitude difference
|
||||||
|
H (DD_Destination.DD.Latitude - DD_Origin.DD.Latitude);
|
||||||
|
Delta_Lambda : constant H := -- Longitude difference
|
||||||
|
H (DD_Destination.DD.Longitude - DD_Origin.DD.Longitude);
|
||||||
|
|
||||||
|
-- @name Hav
|
||||||
|
-- @return Float
|
||||||
|
-- @parameter Theta:Float
|
||||||
|
-- @variable Fraction : Float
|
||||||
|
-- @description Compute Haversine function.
|
||||||
|
function Hav (Theta : H) return Float is
|
||||||
|
Fraction : Float;
|
||||||
|
begin
|
||||||
|
Fraction := Float (Theta) / 2.0;
|
||||||
|
return Sin (Fraction)**2;
|
||||||
|
end Hav;
|
||||||
|
begin
|
||||||
|
|
||||||
|
A :=
|
||||||
|
H (Hav (Delta_Phi) +
|
||||||
|
Cos (Float (DD_Origin.DD.Latitude)) *
|
||||||
|
Cos (Float (DD_Destination.DD.Latitude)) * Hav (Delta_Lambda));
|
||||||
|
|
||||||
|
-- Haversine formula
|
||||||
|
Heuristic := Earth_Radius * H (2.0 * Arcsin (Sqrt (Float (A))));
|
||||||
|
|
||||||
|
return Heuristic;
|
||||||
|
end Plane_Distance;
|
||||||
|
|
||||||
end Grl.Coordinates;
|
end Grl.Coordinates;
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ is
|
|||||||
Format_DMS.Cardinal_Lat in North | South
|
Format_DMS.Cardinal_Lat in North | South
|
||||||
and then Format_DMS.Cardinal_Lon in East | West;
|
and then Format_DMS.Cardinal_Lon in East | West;
|
||||||
|
|
||||||
-- DDM
|
-- DDM
|
||||||
type Format_DDM is record
|
type Format_DDM is record
|
||||||
Degree_Lat : DM_Degree_Lat;
|
Degree_Lat : DM_Degree_Lat;
|
||||||
Degree_Lon : DM_Degree_Lon;
|
Degree_Lon : DM_Degree_Lon;
|
||||||
@@ -118,4 +118,12 @@ is
|
|||||||
(DM_Base.Form = DD) or else (DM_Base.Form = DMS)
|
(DM_Base.Form = DD) or else (DM_Base.Form = DMS)
|
||||||
or else (DM_Base.Form = DDM);
|
or else (DM_Base.Form = DDM);
|
||||||
|
|
||||||
|
-- @name Plane_Distance
|
||||||
|
-- @return H
|
||||||
|
-- @parameter Origin:Coordinate
|
||||||
|
-- @parameter Destination:Coordinate
|
||||||
|
-- @variable Heuristic : H
|
||||||
|
-- @description Calculate heuristic between coordinates.
|
||||||
|
function Plane_Distance (Origin, Destination : Coordinate) return H;
|
||||||
|
|
||||||
end Grl.Coordinates;
|
end Grl.Coordinates;
|
||||||
|
|||||||
Reference in New Issue
Block a user