clojure-geohash / src / geohash / core.clj

Diff from to

src/geohash/core.clj

 
 (defn- do-decode-hashcode
   [bits hash-code geocode-data]
-  (if (< bits 0) geocode-data
-    (if (true? (:islon geocode-data))
-      (let [
-            local-bits (dec bits)
-            bit (bit-and (bit-shift-right hash-code local-bits) 1)
-            mid (/ (+ (:maxlon geocode-data) (:minlon geocode-data)) 2)
-            minlon (if (= bit 1) mid (:minlon geocode-data))
-            maxlon (if (= bit 1) (:maxlon geocode-data) mid)]
-        (recur local-bits hash-code
-          (assoc geocode-data
-            :minlon minlon
-            :maxlon maxlon
-            :islon (not (:islon geocode-data)))))
-      (let [
-            local-bits (dec bits)
-            bit (bit-and (bit-shift-right hash-code local-bits) 1)
-            mid (/ (+ (:maxlat geocode-data) (:minlat geocode-data)) 2)
-            minlat (if (= bit 1) mid (:minlat geocode-data))
-            maxlat (if (= bit 1) (:maxlat geocode-data) mid)]
-        (recur local-bits hash-code
-          (assoc geocode-data
-            :minlat minlat
-            :maxlat maxlat
-            :islon (not (:islon geocode-data))))))))
+  (loop [bits 4 geocode-data geocode-data]
+    (if (< bits 0) geocode-data
+      (if (true? (:islon geocode-data))
+        (let [
+              bit (bit-and (bit-shift-right hash-code bits) 1)
+              mid (/ (+ (:maxlon geocode-data) (:minlon geocode-data)) 2)
+              minlon (if (= bit 1) mid (:minlon geocode-data))
+              maxlon (if (= bit 1) (:maxlon geocode-data) mid)]
+          (recur (dec bits)
+            (assoc geocode-data
+              :minlon minlon
+              :maxlon maxlon
+              :islon (not (:islon geocode-data)))))
+        (let [
+              bit (bit-and (bit-shift-right hash-code bits) 1)
+              mid (/ (+ (:maxlat geocode-data) (:minlat geocode-data)) 2)
+              minlat (if (= bit 1) mid (:minlat geocode-data))
+              maxlat (if (= bit 1) (:maxlat geocode-data) mid)]
+          (recur (dec bits)
+            (assoc geocode-data
+              :minlat minlat
+              :maxlat maxlat
+              :islon (not (:islon geocode-data)))))))))
 
-(defn- do-decode [hash-codes geocode-data]
+(defn- do-decode-bbox [hash-codes geocode-data]
   (if (empty? hash-codes)
-    (do (println geocode-data)
-      geocode-data)
+    geocode-data
     (let [local-geocode-data (do-decode-hashcode 5 (first hash-codes) geocode-data)]
       (recur (rest hash-codes) local-geocode-data))))
 
+(defn decode-bbox [hashstring]
+  (let [
+        hashcodes (map base32-index hashstring)
+        bbox (do-decode-bbox hashcodes (struct geocode-data 180.0 -180.0 90.0 -90.0 true nil nil nil))]
+    (dissoc bbox :islon :bits :hashcode :hashstring)))
+
 (defn decode [hashstring]
   (let [
         hashcodes (map base32-index hashstring)
-        bbox (do-decode hashcodes (struct geocode-data 180.0 -180.0 90.0 -90.0 true nil nil nil))
+        bbox (do-decode-bbox hashcodes (struct geocode-data 180.0 -180.0 90.0 -90.0 true nil nil nil))
         lat (/ (+ (:maxlat bbox) (:minlat bbox)) 2)
-        lon (/ (+ (:maxlon bbox) (:minlon bbox)) 2)]
-    {:lat lat :lon lon}))
+        lon (/ (+ (:maxlon bbox) (:minlon bbox)) 2)
+        lat-err (/ (- (:maxlat bbox) (:minlat bbox)) 2)
+        lon-err (/ (- (:maxlon bbox) (:minlon bbox)) 2)]
+    {:lat lat :lon lon :lat-err lat-err :lon-err lon-err}))
 
 (defn encode [lat lon prec]
   (do-encode lat lon prec 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.