Created
April 12, 2015 20:15
-
-
Save shayanjm/39418c8425c2a66d480f to your computer and use it in GitHub Desktop.
Haversine Formula implementation in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Haversine formula | |
; a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2) | |
; c = 2 ⋅ atan2( √a, √(1−a) ) | |
; d = R ⋅ c | |
; where φ is latitude, λ is longitude, R is earth’s radius (mean radius = 6,371km); | |
(defn haversine | |
"Implementation of Haversine formula. Takes two sets of latitude/longitude pairs and returns the shortest great circle distance between them (in km)" | |
[{lon1 :lng lat1 :lat} {lon2 :lng lat2 :lat}] | |
(let [R 6378.137 ; Radius of Earth in km | |
dlat (Math/toRadians (- lat2 lat1)) | |
dlon (Math/toRadians (- lon2 lon1)) | |
lat1 (Math/toRadians lat1) | |
lat2 (Math/toRadians lat2) | |
a (+ (* (Math/sin (/ dlat 2)) (Math/sin (/ dlat 2))) (* (Math/sin (/ dlon 2)) (Math/sin (/ dlon 2)) (Math/cos lat1) (Math/cos lat2)))] | |
(* R 2 (Math/asin (Math/sqrt a))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment