The Haversine formula in Firebird SQL => Calculate distance between two WGS84 Points
Hi,
I think might be helpful, today for a customer i do the Haversine formulas for Firebird Sql as a stored procedure.
This formula allow you to calculate the distance in meter between two WGS84 datum points(degree points of Earth).
You can call the procedure like this:
SELECT * FROM HAVERSINE_GENERIC(1, 45,474081, 9,179348, 45,456091, 9)
The result wil be aprox 14Km, the distance from my office and my house aprox.
the parameters of the procedure are:
CORD_FORMAT integer, --> 0 coordinate expressed in degree, 1 expressed in radiants
LAT1 double precision, --> Latitude of Point1
LOG1 double precision, --> Longitude of Point1
LAT2 double precision, --> Latitude of Point2
LOG2 double precision --> Longitude of Point2
The procedure code is:
create procedure HAVERSINE_GENERIC (
CORD_FORMAT integer,
LAT1 double precision,
LOG1 double precision,
LAT2 double precision,
LOG2 double precision)
returns (
DISTANCE_METER double precision)
as
declare variable RADIUS double precision;
declare variable A double precision;
declare variable A0 double precision;
declare variable A1 double precision;
declare variable A2 double precision;
declare variable DISTANCE_KM double precision;
declare variable DLON double precision;
declare variable DLAT double precision;
begin
/* LE COORDINATE IN INGRESSO DOVRANNO ESSERE ESPRESSE IN RADIANTI */
if (CORD_FORMAT = 1) then
BEGIN
LAT1 = LAT1 * (pi()/180);
LOG1 = LOG1 * (pi()/180);
LAT2 = LAT2 * (pi()/180);
LOG2 = LOG2 * (pi()/180);
END
/* RAGGIO DELLA TERRA IN KM */
radius=6378.137;
dlon= (loG2-loG1);
dlat= (lat2-lat1);
a1 = (sin(dlat/2));
a1 = a1 * a1;
a2 = (sin(dlon/2));
a2 = a2 * a2;
a= a1 + cos(lat1)*cos(lat2)*a2;
distance_km= radius*(2*atan2(sqrt(a),sqrt(1-a)));
distance_meter = distance_km*1000;
suspend;
end