Skip to content

Instantly share code, notes, and snippets.

@damionjunk
Last active August 29, 2015 14:26
Show Gist options
  • Save damionjunk/f799a10e5bfbb678d8bd to your computer and use it in GitHub Desktop.
Save damionjunk/f799a10e5bfbb678d8bd to your computer and use it in GitHub Desktop.
;; https://en.wikipedia.org/wiki/Maidenhead_Locator_System
;; http://no.nonsense.ee/qth/map.html
;; http://ham.stackexchange.com/questions/221/how-can-one-convert-from-lat-long-to-grid-square
(defn to-maidenhead
[lat long]
(let [long (-> long (+ 180) (/ 2))
lat (-> lat (+ 90))
funs [#(* 10 (mod % 1)) #(* 24 (mod % 1)) #(* 10 (mod % 1)) #(* 24 (mod % 1))]
cars [\A \0 \a \0 \a]]
(map (fn [geo]
(map (fn [n car] (char (+ (int car) n)))
(reductions (fn [n fun] (fun n))
(/ geo 10)
funs)
cars))
[long lat])))
(def to-maidenhead-str (comp #(apply str %) #(apply interleave %) to-maidenhead))
(comment
(apply str
(apply interleave
(to-maidenhead 36.165926 -86.723285)))
;; => "EM66pd39et"
(and
(= "EM66pd39et" (to-maidenhead-str 36.165926 -86.723285))
(= "OF86cx76ql" (to-maidenhead-str -33.014673 116.230695))
(= "FD54oq44oh" (to-maidenhead-str -55.315349 -68.794971))
(= "PM85ge79vh" (to-maidenhead-str 35.205535 136.56579)))
;; => true
;; Just a simple test at various coordinates around the globe.
;; Maidenhead Locator taken from http://no.nonsense.ee/qth/map.html
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment