Parsing / Geocoding in Clojure


/ Published in: Clojure
Save to your folder(s)



Copy this code and paste it in your HTML
  1. (ns parsing2
  2. (:use clojure.contrib.str-utils)
  3. ;(:use com.ashafa.clutch)
  4. (:import java.io.File
  5. java.util.Date
  6. java.text.DateFormat
  7. jxl.Workbook
  8. jxl.Sheet
  9. jxl.Range
  10. jxl.Cell
  11. org.geonames.ToponymSearchCriteria
  12. org.geonames.ToponymSearchResult
  13. org.geonames.WebService
  14. org.jdom.input.SAXBuilder))
  15.  
  16.  
  17. (defstruct flight :day :rf :aircraft :from :etd :to :eta)
  18. (defstruct map-point :name :country :latitude :longitude)
  19. (def *days* ["SUNDAY" "MONDAY" "TUESDAY" "WEDNESDAY" "THURSDAY" "FRIDAY" "SATURDAY"])
  20. (def *curr-day* (atom "None"))
  21. (def *curr-rf* (atom "None"))
  22. (def *curr-aircraft* (atom "None"))
  23. (def *countries-ck* ["Sudan" "Uganda"])
  24.  
  25. (defn in-list? )
  26.  
  27. ;(geo-code "Khartoum" "AFRICA")
  28. (defn geo-code [loc area]
  29. (let [tsc (doto (ToponymSearchCriteria. )
  30. (.setQ (str loc "," area))
  31. (.setMaxRows 5))
  32. result (WebService/search tsc)]
  33. (doseq [r (.getToponyms result)]
  34. (let [n (.getName r)
  35. c (.getCountryName r)
  36. lat (.getLatitude r)
  37. lon (.getLongitude r)
  38. m (some #{c} *countries-ck*)]
  39. (when m
  40. (let [s
  41. (struct map-point n m lat lon )] (prn s)))))))
  42.  
  43. (defn get-rf-ac [flight]
  44. (let [rf (get flight :rf)
  45. ac (get flight :aircraft)]
  46. (when (> (.length rf) 0)
  47. (do (reset! *curr-rf* rf)
  48. (reset! *curr-aircraft* (.trim ac))
  49. (prn *curr-rf* *curr-aircraft*)))
  50. (let [flt (assoc flight :rf (deref *curr-rf*) :aircraft (deref *curr-aircraft*))]
  51. (prn flt))))
  52.  
  53. (defn get-day [{from :from to :to :as flight}]
  54. (let [match (some (set *days*) (vals flight))
  55. from-s (geo-code from "AFRICA")
  56. to-s (geo-code to "AFRICA")]
  57. (when (and from (> (.length from) 0))
  58. (when match
  59. (reset! *curr-day* (.toUpperCase match)))
  60. (prn from-s)
  61. (get-rf-ac (assoc flight :day (deref *curr-day*) :to to-s :from from-s)))))
  62.  
  63. (defn format-it [r]
  64. (let [[day rf aircraft from etd to eta] (map #(.getContents %) r)]
  65. (let [f (struct flight day rf aircraft from etd to eta)]
  66. (get-day f))))
  67.  
  68. ;;(parse-it "src/data/01_04_2009_UNMIS_1.xls")
  69. (defn parse-it [file]
  70. (let [wb (Workbook/getWorkbook (File. file))]
  71. (doseq [sheet (.getSheets wb)]
  72. (let [num-rows (range 0 (.getRows sheet))]
  73. (doseq [r num-rows]
  74. (let [row (.getRow sheet r)]
  75. (prn (format-it row))))))))

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.