Skip to content

Instantly share code, notes, and snippets.

@scottdw
Created September 30, 2010 21:29
Show Gist options
  • Save scottdw/605354 to your computer and use it in GitHub Desktop.
Save scottdw/605354 to your computer and use it in GitHub Desktop.
Based on http://nakkaya.com/2010/08/17/visualising-the-wikileaks-war-logs-using-clojure using http://en.wikipedia.org/wiki/File:United_Kingdom_location_map.svg 500px png. Draws a red marker (16px square) at each of the long/latitude pairs provided to the
(import
[java.awt.geom Rectangle2D$Double]
[java.awt Dimension Color]
[javax.swing JFrame JLabel]
[java.io File]
[javax.imageio ImageIO])
(defn translate-scale-fn [min max target]
(let [scale-factor (/ target (- max min))]
(fn [v]
(* (- v min) scale-factor))))
;; Latitude - N/S => y
;; Longitude - E/W => x
;; Canterbury 51°16'N 01°06'E, 51.276681, 1.074767
(defn convert-fn [width height [lat-min lat-max] [long-min long-max]]
(let [lat-ts-fn (translate-scale-fn lat-min lat-max height)
long-ts-fn (translate-scale-fn long-min long-max width)]
(fn [[lat long]]
[(lat-ts-fn lat) (long-ts-fn long)])))
(let [image (ImageIO/read (File. "/home/scottdw/src/postcode-map/500px-United_Kingdom_location_map.svg.png"))
width (.getWidth image)
height (.getHeight image)
transf (convert-fn width height [61 49] [-11 2.2])
length 4
offset 2]
(defn frame [data]
(let [marker (Rectangle2D$Double. 0 0 length length)]
(doto (JFrame. "UK Map")
(.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
(.add (proxy [JLabel] []
(paintComponent [g]
(.drawImage g image 0 0 this)
(.setColor g Color/RED)
(doseq [[y x] (map transf data)]
(set! (.x marker) (- x offset))
(set! (.y marker) (- y offset))
(.fill g marker)))
(getPreferredSize []
(Dimension. width height))))
(.pack)
(.show)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment