Skip to content

Instantly share code, notes, and snippets.

@gtrak
Last active December 11, 2015 04:58
Show Gist options
  • Save gtrak/4548456 to your computer and use it in GitHub Desktop.
Save gtrak/4548456 to your computer and use it in GitHub Desktop.
(ns xml.test
(:require [clojure.java.io :as io]
[clojure.data.xml :as xml]))
;; wget
;; download.geofabrik.de/openstreetmap/north-america/us/maryland.osm.bz2
(def xml-seq
#(-> (io/file "/home/gary/dev/maryland.osm")
io/input-stream
xml/parse
(->> (tree-seq :content :content)
(drop 1))))
(defn ways
[]
(filter #(= (:tag %) :way) (xml-seq)))
(defn get-kv
[elt the-key]
(->> (:content elt)
(some (fn [{{:keys [k v]} :attrs tag :tag}]
(if (and (= tag :tag) (= k the-key))
v)))))
(defn baltimore-ways
[]
(filter #(->> % :content
(some (fn [{{:keys [k v]} :attrs tag :tag}]
(and (= tag :tag)
(= k "tiger:county")
(.contains v "Baltimore")))))
(ways)))
(def the-ways (delay (baltimore-ways)))
(defn get-nodes
[elt]
(filter #(-> % :tag (= :nd)) (:content elt)))
;; (count (set (mapcat get-nodes @the-ways)))
;; 82725
(def nodes-ways
(delay (reduce (fn [acc elt]
(let [nodes (get-nodes elt)
maps (for [node nodes] {node [elt]})]
(apply merge-with concat acc maps)))
{}
@the-ways)))
;; (frequencies (map (comp count val) @nodes-ways))
;; {1 63598, 3 1619, 2 16962, 4 506, 5 36, 6 3, 7 1}
;; (count (distinct (mapcat val @nodes-ways)))
;; 11762
(defn name-contains-fn?
[subs]
(fn [elt]
(if-let [n (get-kv elt "name")]
(.contains n subs))))
(defn intersecting
[road-name]
(->> @nodes-ways
(filter #(some (name-contains-fn? road-name) (val %)))
(map val)))
(defn names
[intersecting]
(map #(map (fn [elt] (get-kv elt "name")) %)
intersecting))
(defn nil-name
[intersecting]
(map #(remove (fn [elt] (get-kv elt "name")) %)
intersecting))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment