hi Jon-Paul,
thanks for this great post!
below is some syntax for a haversine db function for hana for testing. it's not 100% due to the inaccuracies of the calc, but it does get close with 966 and change km for the above example.
you can change the units of output as per the syntax below as well.
cheers,
jamie
-- units to use: e.g. use 3956 for output in miles
-- rPd:= 0.017453293; // rad per degree (PI/180 where PI = 3.1415926535)
-- rMi:= 3956; // radius in miles
-- rKm:= 6371; // radius in kilometers
-- rFt:= 20895592 (rMi * 5282); // radius in feet
-- rM:= 6371000 (rKm * 1000); // radius in meters
CREATE FUNCTION DISTBYUNIT(LAT1 DECIMAL(13,10), LON1 DECIMAL(13,10), LAT2 DECIMAL(13,10), LON2 DECIMAL(13,10), UNIT FLOAT)
RETURNS DIST DOUBLE
LANGUAGE SQLSCRIPT READS SQL DATA AS
BEGIN
DIST := :UNIT * 2 * ATAN(SQRT(SIN((LAT2-LAT1)*0.017453293/2) * SIN((LAT2-LAT1)*0.017453293/2) + COS(LAT1*0.017453293) * COS(LAT1*0.017453293) * SIN((LON2-LON1)*0.017453293/2) * SIN((LON2-LON1)*0.017453293/2)) / SQRT(1-SIN((LAT2-LAT1)*0.017453293/2) * SIN((LAT2-LAT1)*0.017453293/2) + COS(LAT1*0.017453293) * COS(LAT2*0.017453293) * SIN((LON2-LON1)*0.017453293/2) * SIN((LON2-LON1)*0.017453293/2)));
END;
-- function input example
DISTBYUNIT(58.641759, -3.068672, 50.05935, -5.708054, 6371) AS DIST_KM
--result
966.3523928691291