Calculate Geodistance Between Two Points using the Standard Library

Task

Mathematics / Geographical Calculations / Calculate Geodistance Between Two Points

No packages used

This recipe uses only the OCaml Standard Library.

Code

The Earth surface can be approximated by a 6371 km sphere. The following example computes the distance in kilometers between two points on the Earth with the Haversine formula.

let deg2rad angle =
  angle /. 180. *. Float.pi

let sqr a = a *. a

let haversine_distance lat1 lon1 lat2 lon2 =
  let r = 6371. in
  let dLat = deg2rad(lat2-.lat1) in
  let dLon = deg2rad(lon2-.lon1) in
  let a =
    sqr (sin(dLat/.2.))
    +. cos(deg2rad lat1)
       *. cos(deg2rad lat2)
       *. sqr (sin(dLon/.2.))
  in
  let c = 2. *. atan2 (sqrt a) (sqrt(1.-.a)) in
  r *. c

let deg d m s =
  float_of_int d
  +. float_of_int m/.60.
  +. float_of_int s/.3600.

let lat_paris = deg 48 51 24
let long_paris = deg 2 21 07

let lat_marseille = deg 43 17 47
let long_marseille = deg 5 22 12

let d = haversine_distance
          lat_paris long_paris
          lat_marseille long_marseille

let () = Printf.printf "%f km\n" d

Recipe not working? Comments not clear or out of date?

Open an issue or contribute to this recipe!