-
Notifications
You must be signed in to change notification settings - Fork 183
/
core.cljc
238 lines (213 loc) · 9.05 KB
/
core.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
(ns census.wmsAPI.core
(:require
#?(:cljs [cljs.core.async :refer [>! <! chan promise-chan close! take! put! to-chan!
timeout]
:refer-macros [go alt!]]
:clj [clojure.core.async :refer [>! <! chan promise-chan close! take! put! to-chan!
timeout go alt!]])
;#?(:cljs [com.rpl.specter :refer [MAP-VALS MAP-KEYS ALL]
; :refer-macros [select transform traverse setval]]
; :clj [com.rpl.specter :refer [MAP-VALS MAP-KEYS ALL select transform traverse setval]])
[clojure.set :refer [map-invert]]
[cuerdas.core :refer [join]]
[linked.core :as -=-]
[census.utils.core :refer [=O?>-cb $GET$
amap-type vec-type throw-err ->args
URL-WMS URL-GEOKEYMAP]]))
(defn $g$->wms-cfg
"
Creates a configuration map for the WMS url-builder from the geoHierarchy map.
if : lookup key is a vec -> direct looked up
else: lookup at :id<-json key
($g$->wms-cfg
$g$
{:vintage 2014,
:geoHierarchy {:state {:lat 28.2639, :lng -80.7214}, :county '*'}})
;=>
{:vintage 2014,
:layers ['84'],
:cur-layer-idx 0,
:lat 28.2639,
:lng -80.7214,
:sub-level [:county '*'],
:geo [:STATE],
:looked-up-in :2010}
"
([$g$ args] ($g$->wms-cfg $g$ args 0))
([$g$ {:keys [geoHierarchy vintage]} server-index]
(let [[[scope {:keys [lat lng]}] sub-level] (vec geoHierarchy)
{:keys [lookup layers]} (get-in $g$ [scope (keyword (str vintage)) :wms])
config {:vintage vintage
:layers layers
:cur-layer-idx server-index
:lat lat
:lng lng
:sub-level sub-level}]
(if (instance? vec-type lookup)
(merge-with assoc config
{:geo lookup
:looked-up-in (keyword vintage)})
(merge-with assoc config
{:geo (get-in $g$ [scope lookup :id<-json])
; lookup-up-in
:looked-up-in lookup})))))
(defn lookup-id->match?
"
:id<-json
Looks in a single entry from the inverted geoKeyMap for a matching geoKey via
`some`ing through each of its vintages for a match with a provided WMS
geographic identifier.
(lookup-id->match? :CONCITY ;; ↓ seq'd inverted geoKeyMap | looks up ↓
[{:2017 {:wms {:layers ['24'], :lookup [:STATE :CONCITY]}}
:2016 {:wms {:layers ['24'], :lookup [:STATE :CONCITY]}}}
:consolidated-cities
{:2014 {:wms {:layers ['24'], :lookup [:BLOOP]}}
:2016 {:wms {:layers ['24'], :lookup [:BLOOP]}}}
:something-else])
; => :consolidated-cities
"
[GEO [geo-val geo-key]]
(let [vins (map (fn [[_ {:keys [id<-json] {:keys [lookup]} :wms}]]
(if (instance? vec-type lookup)
(last lookup)
(last id<-json)))
(vec geo-val))]
(if (some #(= GEO %) vins)
geo-key
nil)))
(defn search-id->match?
"
Searches the entire geoKeyMap (inverted) for a geo key match provided a given
WMS geographic identifier. Returned values are used in combination with a
response from the TigerWeb WMS geocoding response to determine the geographic
hierarchy of a geography for filling-in the data API request geography for
geocoding requests
(search-id->match? $g$ :CONCITY)
; => :consolidated-cities
"
[$g$ GEO]
(let [inverted-geoKeyMap (seq (map-invert $g$))]
(remove nil?
(map #(lookup-id->match? GEO %)
inverted-geoKeyMap))))
; https://tigerweb.geo.census.gov/arcgis/rest/services/TIGERweb/tigerWMS_ACS2012/MapServer
; https://tigerweb.geo.census.gov/arcgis/rest/services/Census2020/tigerWMS_Census2000/MapServer
(defn C->GIS-url
"
Constructs a URL for the TigerWeb Web Mapping Service (WMS) using a lookup
from the geoKeyMap configuration file cross-referenced against the users args.
"
([$g$ args] (C->GIS-url $g$ args 0))
([$g$ args server-index]
(let [{:keys [vintage layers cur-layer-idx lat lng geo]}
($g$->wms-cfg $g$ args server-index)]
(str URL-WMS
(cond
(= 0 (mod vintage 10)) (str "Census2020/tigerWMS_Census" vintage)
:else (str "TIGERweb/tigerWMS_ACS" vintage))
"/MapServer/"
(get layers cur-layer-idx)
"/query?"
(join "&"
(map #(join "=" %)
[["geometry" (str lng "," lat)]
["geometryType" "esriGeometryPoint"]
["inSR" "4269"]
["spatialRel" "esriSpatialRelIntersects"]
["returnGeometry" "false"]
["f" "pjson"]
["outFields" (join "," (map name geo))]]))))))
(defn configed-map
"IMPORTANT!
The :id<-json key in index.edn is double loaded, to both pull ids from GeoJSON
as well as configure the API call
(configed-map $g$ {:STATE '51', :COUNTY '013'})
;=> {:STATE {:state '51'}, :COUNTY {:county '013'}}
Takes the geoKeyMap configuration and the attributes from the WMS service
API (js->cljs response) and returns a config map (:key = attribute ; value =
corresponding configured map with (:geography 'value') needed to call Census'
data API).
"
[$g$ attrs]
(let [wms-keys (into [] (keys attrs))
wms-vals (into [] (vals attrs))
geo-keys (map #(search-id->match? $g$ %) wms-keys)]
(loop [idx 0
result {}]
(if (= nil (get wms-keys idx))
result
(recur (inc idx)
(assoc result ; TODO: `revert` if no bueno
(get wms-keys idx)
;; returns an empty map ({}) if invalid
{(get (mapv #(first %) geo-keys) idx)
(get wms-vals idx)}))))))
(def $url$ (atom ""))
(def $res$ (atom []))
(def $err$ (atom {}))
(def $GET$-wms ($GET$ :json "Census FIPS Geocoding" $url$ $res$ $err$))
(defn try-census-wms
"
Takes the geoKeyMap with the users' arguments, a current WMS server index (used
for retrying if more than one exists for a given geography in WMS) and a
channel that will convey the result. Tries to cal the WMS and puts the
`configed-map` into the channel if successful.
"
[$g$ args server-idx =res=]
(let [=args=> (chan 1 (map #(configed-map $g$ (get-in % [:features 0 :attributes]))))
url (C->GIS-url $g$ args server-idx)]
($GET$-wms (to-chan! [url]) =args=> =args=>)
(take! =args=> (fn [args->] (do (put! =res= args->)
(close! =args=>))))))
(defn wms-engage?
"
Engages the wms-service workflow if the first element in the geoHierarchy
contains a map argument, which implies that the user doesn't have a GEOID handy.
"
[{:keys [geoHierarchy]}]
(let [[_ geo-val] (first geoHierarchy)]
(if (instance? amap-type geo-val)
true
false)))
; TODO: can this be cleaned up?
(defn =>args=GIS=args=>
"
Tries to find the appropriate geographic identifiers for a provided
geoHierarchy argument, which contains a {:lat <float> :lng <float>} coordinate
instead of an actual FIPS code set. If FIPS are already provided, this step is
skipped. If not, the users' arguments are augmented with FIPS codes from the
Census Tiger WMS.
"
[$g$]
(fn [=>args= =args=>]
(go (let [->args (<! =>args=)
=res= (chan 1)]
(if (not (wms-engage? ->args))
(do (>! =args=> ->args)
(close! =res=))
(loop [args ->args
idx 0]
(try-census-wms $g$ args idx =res=)
(let [{:keys [layers sub-level]} ($g$->wms-cfg $g$ args)
res (<! =res=)]
(cond
(not (empty? res))
(do (>! =args=>
(merge args
(assoc {} :geoHierarchy
(conj (-=-/map)
(into (-=-/map) (vals res))
(into (-=-/map) [sub-level])))))
(close! =res=))
; if another layer is available: recur
(and (empty? res) (not (nil? (get layers (inc idx)))))
(recur ->args (inc idx))
:else
(do (>! =args=> "No FIPS (Census geocodes) found for given arguments")
(close! =res=))))))))))
(defn I-<wms=I=
"Provides a synchronous input to a function that accepts a channel for args
and calls the Census WMS for geocoding; providing the results to the channel"
[$g$]
(fn [I =args=>]
((=>args=GIS=args=> $g$) (to-chan! [(->args I)]) =args=>)))