Skip to content

Instantly share code, notes, and snippets.



Created Mar 27, 2014
What would you like to do?
Tile Server Quad Tree Math
(import 'java.lang.Math)
(defn tile-count [z]
(Math/pow 2 z))
(defn deg2num [lat_deg lon_deg z]
(let [lat_rad (Math/toRadians lat_deg)
n (tile-count z)
x (int (* (/ (+ lon_deg 180.0) 360.0) n))
y (int
(- 1.0
(Math/tan lat_rad)
(/ 1.0
(Math/cos lat_rad))))
(list x y z)))
(defn tilelink [lat lon z]
(let [u ""
url (fn [x y z] (str u "/" z "/" x "/" y ".png"))
(apply url (deg2num lat lon z))))
(defn num2deg [x y z]
(let [n (tile-count z)
lon (- (* (/ x n) 360.0) 180.0)
lat_rad (Math/atan
(* Math/PI
(- 1
(/ (* 2 y) n)))))
lat (Math/toDegrees lat_rad)]
(list lat lon)))
(defn num2deg-bb [x y z]
(let [nw (num2deg x y z)
sw (num2deg x (+ 1 y) z)
ne (num2deg (+ 1 x) y z)
se (num2deg (+ 1 x) (+ 1 y) z)]
(list nw sw se ne)))
(defn bb-extents [bb]
(let [lats (map first bb)
lons (map second bb)
minlat (reduce min lats)
maxlat (reduce max lats)
minlon (reduce min lons)
maxlon (reduce max lons)]
(list minlat maxlat minlon maxlon)))
(defn deg2extents [lat lon z]
lat lon z))))
(defn num2extents [x y z]
(num2deg-bb x y z)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment