Haversine implementation in ABL

This is the Haversine formula translated into an ABL procedure. The Haversine formula is used for determining the Great-Circle distance in km between two latitude/longitude references.

The formula is based off the Javascript interpretation here: http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a...

Requires Progress STandard Libraries with math_asin function uncommented (see http://www.oehive.org/node/2057)

Code:

/*d.cook 2012-02-06*/

{lib\slib\libmath.i}

&scop R 6371 /*Earth's mean radius in km*/

function Haversine returns dec (lat1 as dec, lon1 as dec, lat2 as dec, lon2 as dec):
   def var dLat   as dec no-undo.
   def var dLon   as dec no-undo.
   def var a      as dec no-undo.
   def var c      as dec no-undo.
   
   dLat = math_deg2rad(lat2 - lat1).
   dLon = math_deg2rad(lon2 - lon1).
   lat1 = math_deg2rad(lat1).
   lat2 = math_deg2rad(lat2).
   
   a = math_sin(dLat / 2) * math_sin(dLat / 2) +
           math_cos(lat1) * math_cos(lat2) *   
           math_sin(dLon / 2) * math_sin(dLon / 2).
   c = 2 * math_asin(sqrt(a)).
   return {&R} * c.
end function.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
alonb's picture

thanks for sharing!btw i'm

thanks for sharing!

btw i'm not sure if it's relevant.

but there is a small google api library.

i would encourage you to extend the library and add some google maps api

that can be used to calculate the distance between two point anywhere in the world, and even how long it will take you.


Thanks for the suggestion

Thanks for the suggestion, I'm actually working with Google Maps API at the moment. If I have time and am able to, I'll think about extending the Google library here.