SHARE
TWEET

Clojure cities csv

a guest Nov 15th, 2019 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (comment
  2. (ns query-1
  3.   (:require [ca-cities :as cal]))
  4. )
  5.  
  6. ;;java -cp clojure-1.8.0.jar clojure.main query_1.clj
  7.  
  8. (ns query-1 ;;ns is namespace
  9.   (:gen-class)
  10.   (:require [clojure.java.io :as io]
  11.             [clojure.string :as string]
  12.         ))
  13. ; check if the city has more than 0.5E6 people
  14.  
  15.  
  16. (def csv-file "data/ca.csv")
  17. (defn- rad [degree] (* Math/PI (/ degree 180.)));;reading csv file into hashmap clojure
  18.  
  19. (defn reader []
  20.     (io/reader "data/ca.csv"))
  21.    
  22. ; Parse a string as a float
  23. (defn parse-float [s]
  24.     (Float/parseFloat s))
  25.    
  26. ; Parse a string as an integer
  27. (defn parse-int [s]
  28.     (Integer/parseInt s))
  29.  
  30. (defn parse-record [line]
  31.     (def x line)
  32.     (def y (string/split x #","))
  33.    
  34.     (into {}  [[:city (y 0)]
  35.                [:lat (y 1)]
  36.                [:long (y 2)]
  37.                [:country (y 3)]
  38.                [:iso2 (y 4)]
  39.                [:admin (y 5)]
  40.                [:capital (y 6)]
  41.                [:pop (y 7)]
  42.                [:popproper (y 8)]]
  43.     )          
  44. )
  45. (println "Parse-Record Function")
  46. (print(parse-record "Toronto,43.666667,-79.416667,Canada,CA,Ontario,admin,5213000,3934421"))
  47. (println " ")
  48.  
  49. (defn cities []
  50.   (with-open [rdr (reader)]
  51.     (vec
  52.         (for [line (drop 1 (line-seq rdr))]
  53.             (parse-record line)))))
  54.        
  55. (println " ")
  56. (println "Cities Function")
  57. (println (cities))
  58. (println " ")
  59.  
  60. (defn city [name]
  61.   (with-open [rdr (reader)]
  62.     (first
  63.         (for [line (drop 1 (line-seq rdr))
  64.             :let [y (string/split line #",")]
  65.             :when (= name (y 0))]
  66.         line))))
  67.        
  68. (println "Cities Function")
  69. (println (city "Toronto"))
  70. (println " ")
  71.  
  72. (defn latReturn [line]
  73.     (def x line)
  74.     (def y (string/split x #","))
  75.     (def my-map { :city (y 0)
  76.                :lat (y 1)
  77.                :long (y 2)
  78.                :country (y 3)
  79.                :iso2 (y 4)
  80.                :admin (y 5)
  81.                :capital (y 6)
  82.                :pop (y 7)
  83.                :popproper (y 8)
  84.     })
  85.     (read-string (my-map :lat))
  86. )
  87.  
  88. (defn longReturn [line]
  89.     (def x line)
  90.     (def y (string/split x #","))
  91.     (def my-map { :city (y 0)
  92.                :lat (y 1)
  93.                :long (y 2)
  94.                :country (y 3)
  95.                :iso2 (y 4)
  96.                :admin (y 5)
  97.                :capital (y 6)
  98.                :pop (y 7)
  99.                :popproper (y 8)
  100.     })
  101.     (read-string (my-map :long))
  102. )
  103. (def lool (+ (longReturn (city "Toronto")) 1))
  104. (println lool)
  105.  
  106. (defn distance [record1 record2]
  107.   (let [R 6371
  108.         dlat (Math/toRadians (- (latReturn record2) (latReturn record1)))
  109.         dlon (Math/toRadians (- (longReturn record2) (longReturn record1)))
  110.         lolnice (Math/toRadians (latReturn record1))
  111.         dnice (Math/toRadians (latReturn record2))
  112.         a (+ (* (Math/sin (/ dlat 2)) (Math/sin (/ dlat 2))) (* (Math/sin (/ dlon 2)) (Math/sin (/ dlon 2)) (Math/cos lolnice) (Math/cos dnice)))]
  113.     (* R 2 (Math/asin (Math/sqrt a)))))
  114.  
  115. (defn large [citylol]
  116.     (def x (city citylol))
  117.     (def y (string/split x #","))
  118.     (def my-map { :city (y 0)
  119.                :lat (y 1)
  120.                :long (y 2)
  121.                :country (y 3)
  122.                :iso2 (y 4)
  123.                :admin (y 5)
  124.                :capital (y 6)
  125.                :pop (y 7)
  126.                :popproper (y 8)
  127.     })
  128.     (def pop (read-string (my-map :pop)))
  129.     (> pop 0.5E6)
  130.   )
  131. (println (large "Toronto"))
  132.  
  133. ; check if the two cities are no more than 600 km apart
  134. (defn close [c1 c2]
  135.   (def dist (distance (city c1) (city c2)))
  136.   (< dist 600)
  137.   )
  138. (println(close "Toronto" "La Ronge"))
  139.  
  140.  
  141. (comment
  142. ; find distinct pairs of city names of *large* cities
  143. ; that are close.
  144. (defn closest-city-pairs []
  145.     (def newNewVec [])
  146.     (with-open [rdr (reader)]
  147.         (vec
  148.             (for [line (drop 1 (line-seq rdr))
  149.                 :let [y (string/split line #",")]
  150.                 :when (= true (large(y 0)))]
  151.                 (let [newVec (conj [] (y 0))]
  152.                 (for[x newVec
  153.                      y newVec
  154.                      :when (and (= true (close x y)) (not= x y))]
  155.                      ([newNewVec (conj [] [x y])])
  156.             ))))))
  157.  
  158. (print(closest-city-pairs))
  159. )
  160.  
  161.  
  162. (defn test []
  163.     (with-open [rdr (reader)]
  164.         (vec
  165.             (for [line (drop 1 (line-seq rdr))
  166.                 :let [y (string/split line #",")]
  167.                 :when (= true (large(y 0)))]
  168.                 (let [newVec (conj [] (y 0))
  169.                       close-pairs
  170.                       (vec
  171.                       (distinct
  172.                         (for [x newVec
  173.                               y newVec
  174.                               :when (and (not= x y)
  175.                                       (close x y))]
  176.                           #{x y})))])))))
  177.    
  178. (print(test))
  179.  
  180. ;;(apply + {map :population (cities))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top