Commits

Ning Sun committed 8138e71

initial commit

Comments (0)

Files changed (5)

+# osm-api
+
+A demo clojure app
+
+## Usage
+
+lein run
+
+## License
+
+Copyright (C) 2010 Sun Ning
+
+Distributed under the Eclipse Public License, the same as Clojure.
+(defproject osm-api "1.0.0-SNAPSHOT"
+  :description "a demo application running http-client api"
+  :dependencies [[org.clojure/clojure "1.2.0"]
+                 [org.clojure/clojure-contrib "1.2.0"]
+                 [compojure "0.5.3"]
+                 [ring/ring-jetty-adapter "0.3.5"]]
+  :main osm-api.web)
+(ns osm-api.core
+  (:gen-class)
+  (:use [clojure.string :only [join]])
+  (:use [clojure.xml :only [parse]])
+  (:use [clojure.contrib.http.agent :only [http-agent, stream]])
+  (:use [clojure.pprint :only [pprint]]))
+
+(defn load-from-osm
+  [minlon minlat maxlon maxlat]
+  (let [url (str "http://api.openstreetmap.org/api/0.6/map?bbox=" 
+               (join "," (map str [minlon minlat maxlon maxlat])))]
+    (stream (http-agent url))))
+
+(defn parse-osm-xml
+  [xmlstream]
+  (parse xmlstream))
+
+
+(defn get-names
+  [xml-struct]
+  (filter #(not (nil? %))
+    (for [x (xml-seq xml-struct) 
+          :when (= :tag (:tag x))]
+      (if (= "name" (:k (:attrs x))) (:v (:attrs x))))))
+      
+(defn get-ways
+  [xml-struct]
+  (filter #(not (nil? %))
+    (for [x (xml-seq xml-struct) :when (= :way (:tag x))]
+        x)))
+
+(defn get-way-url
+  [way]
+  (str "http://www.openstreetmaps.org/?way=" (:id (:attrs way))))
+
+(defn -main []
+    (let [osmdata (parse-osm-xml(load-from-osm 118.759 32.058 118.765 32.062))]
+        (println (get-names osmdata))
+        (map get-way-url (get-ways osmdata))))
+
+(ns osm-api.web
+    (:gen-class)
+    (:use [osm-api.core :exclude [-main]])
+    (:use compojure.core, ring.adapter.jetty)
+    (:use [clojure.string :only [split,join]]))
+    
+(defn ways [bbox-str]
+    (map get-way-url (get-ways 
+        (parse-osm-xml(apply load-from-osm (split bbox-str #","))))))        
+
+(defn to-link [url]
+    (str "<a href=\"" url "\">" url "</a>"))        
+        
+(defroutes root
+    (GET "/ways" {{bbox "bbox"} :params} 
+        (join "<br/>" (map to-link (ways bbox)))))
+    
+(defn -main []
+    (run-jetty root {:port 8080}))    
+    

test/osm_api/test/core.clj

+(ns osm-api.test.core
+  (:use [osm-api.core] :reload)
+  (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+  (is false "No tests have been written."))