Skip to content

Instantly share code, notes, and snippets.

@rcampbell
Created May 10, 2011 17:59
Show Gist options
  • Save rcampbell/964996 to your computer and use it in GitHub Desktop.
Save rcampbell/964996 to your computer and use it in GitHub Desktop.
Conversion from Decimal Degree to DMS (Degrees, Minutes, Seconds) in Clojure
;; See http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
(defn dms [type degrees]
{:pre [(#{:latitude :longitude} type)]}
"Conversion from Decimal Degree to DMS (Degrees, Minutes, Seconds)"
(let [total-sec (Math/round (* (Math/abs degrees) 60 60))
total-min (quot total-sec 60)
sec (rem total-sec 60)
min (rem total-min 60)
deg (quot total-min 60)]
{:direction (case type
:latitude (if (neg? degrees) :south :north)
:longitude (if (neg? degrees) :west :east ))
:degrees deg
:minutes min
:seconds sec}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment