bamcensus-acs 0.1.0

Interaction with the American Community Survey API
Documentation
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use bamcensus_core::model::identifier::{fips, Geoid, GeoidType, HasGeoidString};
use std::rc::Rc;

use super::DeserializeGeoidFn;

/// enumeration representing the scopes of various ACS queries.
///
/// when running an ACS query at a given GEOID hierarchical level, there are a set
/// of required (aka, not `Option`al) components which can be coupled with `Option`al
/// (wildcard) components to construct a query.
#[derive(Debug, Clone)]
pub enum AcsGeoidQuery {
    State(Option<fips::State>),
    County(Option<fips::State>, Option<fips::County>),
    CountySubdivision(
        fips::State,
        Option<fips::County>,
        Option<fips::CountySubdivision>,
    ),
    Place(Option<fips::State>, Option<fips::Place>),
    CensusTract(fips::State, Option<fips::County>, Option<fips::CensusTract>),
    BlockGroup(
        fips::State,
        Option<fips::County>,
        Option<fips::CensusTract>,
        Option<fips::BlockGroup>,
    ),
}

impl AcsGeoidQuery {
    /// # Examples
    ///
    /// when no wildcard is provided, the query is constructed for an exact location, which
    /// should return a single result.
    /// ```rust
    /// use bamcensus_core::model::identifier::{fips, Geoid, GeoidType};
    /// use bamcensus_acs::model::AcsGeoidQuery;;
    ///
    /// let geoid = Geoid::County(fips::State(8), fips::County(1));
    /// let query = AcsGeoidQuery::new(Some(geoid), None).unwrap();
    /// let key = query.to_query_key();
    /// assert_eq!(key, String::from("&for=county:001&in=state:08"));
    /// ```
    ///
    /// some combinations simply append a wildcard one level below the geoid, for example,
    /// tacking a county=* on top of state=08.
    /// ```rust
    /// use bamcensus_core::model::identifier::{fips, Geoid, GeoidType};
    /// use bamcensus_acs::model::AcsGeoidQuery;;
    ///
    /// let geoid = Geoid::State(fips::State(8));
    /// let wildcard = GeoidType::County;
    /// let query = AcsGeoidQuery::new(Some(geoid), Some(wildcard)).unwrap();
    /// let key = query.to_query_key();
    /// assert_eq!(key, String::from("&for=county:*&in=state:08"));
    /// ```
    ///
    /// some interpolate the wildcard into a geoid in ways that do not require being
    /// reported in the query, such as adding a wildcard to a tract query. in this case,
    /// the query simply drops the county wildcard, as it is implied.
    /// ```rust
    /// use bamcensus_core::model::identifier::{fips, Geoid, GeoidType};
    /// use bamcensus_acs::model::AcsGeoidQuery;;
    ///
    /// let geoid = Geoid::CensusTract(fips::State(8), fips::County(1), fips::CensusTract(1));
    /// let wildcard = GeoidType::County;
    /// let query = AcsGeoidQuery::new(Some(geoid), Some(wildcard)).unwrap();
    /// let key = query.to_query_key();
    /// assert_eq!(key, String::from("&for=tract:000001&in=state:08"));
    /// ```
    ///
    /// # Returns
    ///
    /// URL query string for calls to the US Census ACS API "for" section, which set the
    /// spatial scope and granularity of the query result.
    pub fn new(geoid: Option<Geoid>, wildcard: Option<GeoidType>) -> Result<AcsGeoidQuery, String> {
        use Geoid as G;
        use GeoidType as GT;

        match (geoid, wildcard) {
            // ~~ errors ~~
            // - invalid combinations of geoid/wildcard values
            (None, None) => Err(String::from(
                "cannot create query without at least a geoid or wildcard",
            )),
            (None, Some(GT::CountySubdivision)) => Err(String::from(
                "cannot create county subdivision query without State Geoid",
            )),
            (None, Some(GT::CensusTract)) => Err(String::from(
                "cannot create census tract query without State Geoid",
            )),
            (None, Some(GT::BlockGroup)) => Err(String::from(
                "cannot create block group query without State + County Geoids",
            )),
            (_, Some(GT::Block)) => Err(String::from("acs does not support block-level queries")),
            (Some(G::Block(_, _, _, _)), _) => {
                Err(String::from("acs does not support block-level queries"))
            }

            (Some(Geoid::State(_)), Some(GT::BlockGroup)) => Err(String::from(
                "cannot create block group query without County Geoid",
            )),
            (Some(Geoid::County(_, _)), Some(GT::Place)) => Err(String::from(
                "cannot append a 'Place' wildcard to a County Geoid",
            )),
            (Some(G::CountySubdivision(_, _, _)), Some(GT::Place)) => Err(String::from(
                "cannot append a 'Place' wildcard to a CountySubdivision Geoid",
            )),
            (Some(G::CountySubdivision(_, _, _)), Some(GT::CensusTract)) => Err(String::from(
                "cannot append a 'CensusTract' wildcard to a CountySubdivision Geoid",
            )),
            (Some(G::CountySubdivision(_, _, _)), Some(GT::BlockGroup)) => Err(String::from(
                "cannot append a 'BlockGroup' wildcard to a CountySubdivision Geoid",
            )),
            (Some(Geoid::Place(_, _)), Some(GT::County)) => Err(String::from(
                "cannot append a 'County' wildcard to a Place Geoid",
            )),
            (Some(Geoid::Place(_, _)), Some(GT::CountySubdivision)) => Err(String::from(
                "cannot append a 'CountySubdivision' wildcard to a Place Geoid",
            )),
            (Some(Geoid::Place(_, _)), Some(GT::CensusTract)) => Err(String::from(
                "cannot append a 'CensusTract' wildcard to a Place Geoid",
            )),
            (Some(Geoid::Place(_, _)), Some(GT::BlockGroup)) => Err(String::from(
                "cannot append a 'BlockGroup' wildcard to a Place Geoid",
            )),
            (Some(Geoid::CensusTract(_, _, _)), Some(GT::State)) => Err(String::from(
                "cannot append a 'State' wildcard to a CensusTract Geoid",
            )),
            (Some(Geoid::CensusTract(_, _, _)), Some(GT::CountySubdivision)) => Err(String::from(
                "cannot append a 'CountySubdivision' wildcard to a CensusTract Geoid",
            )),
            (Some(Geoid::CensusTract(_, _, _)), Some(GT::Place)) => Err(String::from(
                "cannot append a 'Place' wildcard to a CensusTract Geoid",
            )),
            (Some(Geoid::BlockGroup(_, _, _, _)), Some(GT::State)) => Err(String::from(
                "cannot append a 'State' wildcard to a BlockGroup Geoid",
            )),
            (Some(Geoid::BlockGroup(_, _, _, _)), Some(GT::CountySubdivision)) => Err(
                String::from("cannot append a 'CountySubdivision' wildcard to a BlockGroup Geoid"),
            ),
            (Some(Geoid::BlockGroup(_, _, _, _)), Some(GT::Place)) => Err(String::from(
                "cannot append a 'Place' wildcard to a BlockGroup Geoid",
            )),

            // ~~ wildcard-only queries for different GEOID levels ~~
            (None, Some(GT::State)) => Ok(AcsGeoidQuery::State(None)),
            (None, Some(GT::County)) => Ok(AcsGeoidQuery::County(None, None)),
            (None, Some(GT::Place)) => Ok(AcsGeoidQuery::Place(None, None)),

            // ~~ queries for wildcards inserted into specific geoids ~~
            // - STATE -
            (Some(Geoid::State(_)), Some(GT::State)) => Ok(AcsGeoidQuery::State(None)),
            (Some(Geoid::State(s)), Some(GT::County)) => Ok(AcsGeoidQuery::County(Some(s), None)),
            (Some(Geoid::State(s)), Some(GT::CountySubdivision)) => {
                Ok(AcsGeoidQuery::CountySubdivision(s, None, None))
            }
            (Some(Geoid::State(s)), Some(GT::Place)) => Ok(AcsGeoidQuery::Place(Some(s), None)),
            (Some(Geoid::State(s)), Some(GT::CensusTract)) => {
                Ok(AcsGeoidQuery::CensusTract(s, None, None))
            }

            // - COUNTY -
            (Some(Geoid::County(_, c)), Some(GT::State)) => {
                Ok(AcsGeoidQuery::County(None, Some(c)))
            }
            (Some(Geoid::County(s, _)), Some(GT::County)) => {
                Ok(AcsGeoidQuery::County(Some(s), None))
            }
            (Some(Geoid::County(s, c)), Some(GT::CountySubdivision)) => {
                Ok(AcsGeoidQuery::CountySubdivision(s, Some(c), None))
            }
            (Some(Geoid::County(s, c)), Some(GT::CensusTract)) => {
                Ok(AcsGeoidQuery::CensusTract(s, Some(c), None))
            }
            (Some(Geoid::County(s, c)), Some(GT::BlockGroup)) => {
                Ok(AcsGeoidQuery::BlockGroup(s, Some(c), None, None))
            }

            // - COUNTY SUBDIVISION -
            (Some(G::CountySubdivision(st, ct, cs)), Some(GT::State)) => {
                Ok(AcsGeoidQuery::CountySubdivision(st, Some(ct), Some(cs)))
            }
            (Some(G::CountySubdivision(s, _, cs)), Some(GT::County)) => {
                Ok(AcsGeoidQuery::CountySubdivision(s, None, Some(cs)))
            }
            (Some(G::CountySubdivision(s, ct, _)), Some(GT::CountySubdivision)) => {
                Ok(AcsGeoidQuery::CountySubdivision(s, Some(ct), None))
            }

            // - PLACE -
            (Some(Geoid::Place(_, p)), Some(GT::State)) => Ok(AcsGeoidQuery::Place(None, Some(p))),
            (Some(Geoid::Place(s, _)), Some(GT::Place)) => Ok(AcsGeoidQuery::Place(Some(s), None)),

            // - CENSUS TRACT -
            (Some(Geoid::CensusTract(s, _, t)), Some(GT::County)) => {
                Ok(AcsGeoidQuery::CensusTract(s, None, Some(t)))
            }
            (Some(Geoid::CensusTract(s, c, _)), Some(GT::CensusTract)) => {
                Ok(AcsGeoidQuery::CensusTract(s, Some(c), None))
            }
            (Some(Geoid::CensusTract(s, c, t)), Some(GT::BlockGroup)) => {
                Ok(AcsGeoidQuery::BlockGroup(s, Some(c), Some(t), None))
            }

            // - BLOCK GROUP -
            (Some(Geoid::BlockGroup(s, _, t, b)), Some(GT::County)) => {
                Ok(AcsGeoidQuery::BlockGroup(s, None, Some(t), Some(b)))
            }
            (Some(Geoid::BlockGroup(s, c, _, b)), Some(GT::CensusTract)) => {
                Ok(AcsGeoidQuery::BlockGroup(s, Some(c), None, Some(b)))
            }
            (Some(Geoid::BlockGroup(s, c, t, _)), Some(GT::BlockGroup)) => {
                Ok(AcsGeoidQuery::BlockGroup(s, Some(c), Some(t), None))
            }

            // ~~ queries for specific geoids (no wildcards) ~~
            (Some(Geoid::State(s)), None) => Ok(AcsGeoidQuery::State(Some(s))),
            (Some(Geoid::County(s, c)), None) => Ok(AcsGeoidQuery::County(Some(s), Some(c))),
            (Some(Geoid::CountySubdivision(s, ct, cs)), None) => {
                Ok(AcsGeoidQuery::CountySubdivision(s, Some(ct), Some(cs)))
            }
            (Some(Geoid::Place(s, p)), None) => Ok(AcsGeoidQuery::Place(Some(s), Some(p))),
            (Some(Geoid::CensusTract(s, c, t)), None) => {
                Ok(AcsGeoidQuery::CensusTract(s, Some(c), Some(t)))
            }
            (Some(Geoid::BlockGroup(s, c, t, b)), None) => {
                Ok(AcsGeoidQuery::BlockGroup(s, Some(c), Some(t), Some(b)))
            }
        }
    }

    /// a query key for a unique data row in the census API. depending on the AcsGeoidQuery
    /// and the presence/absence of FIPS values, wildcards ("*") will be inserted at any level.
    pub fn to_query_key(&self) -> String {
        use AcsGeoidQuery as G;
        match self {
            G::State(state) => match state {
                None => String::from("&for=state:*"),
                Some(s) => format!("&for=state:{}", s.geoid_string()),
            },
            G::County(state, county) => match (state, county) {
                (None, None) => String::from("&for=county:*"),
                (None, Some(c)) => format!("&for=county:{}", c.geoid_string()),
                (Some(s), None) => format!("&for=county:*&in=state:{}", s.geoid_string()),
                (Some(s), Some(c)) => format!(
                    "&for=county:{}&in=state:{}",
                    c.geoid_string(),
                    s.geoid_string()
                ),
            },
            G::CountySubdivision(state, county, cousub) => match (county, cousub) {
                (None, None) => format!(
                    "&for=county%20subdivision:*&in=state:{}&in=county:*",
                    state.geoid_string(),
                ),
                (None, Some(cs)) => format!(
                    "&for=county%20subdivision:{}&in=state:{}&in=county:*",
                    cs.geoid_string(),
                    state.geoid_string(),
                ),
                (Some(co), None) => format!(
                    "&for=county%20subdivision:*&in=state:{}&in=county:{}",
                    state.geoid_string(),
                    co.geoid_string(),
                ),
                (Some(co), Some(cs)) => format!(
                    "&for=county%20subdivision:{}&in=state:{}&in=county:{}",
                    cs.geoid_string(),
                    state.geoid_string(),
                    co.geoid_string()
                ),
            },
            G::Place(state, place) => match (state, place) {
                (None, None) => String::from("&for=place:*"),
                (None, Some(pl)) => format!("&for=place:{}&in=state:*", pl.geoid_string()),
                (Some(st), None) => format!("&for=place:*&in=state:{}", st.geoid_string()),
                (Some(st), Some(pl)) => format!(
                    "&for=place:{}&in=state:{}",
                    pl.geoid_string(),
                    st.geoid_string()
                ),
            },
            G::CensusTract(state, county, tract) => match (county, tract) {
                (None, None) => format!("&for=tract:*&in=state:{}", state.geoid_string()),
                (None, Some(tr)) => format!(
                    "&for=tract:{}&in=state:{}",
                    tr.geoid_string(),
                    state.geoid_string()
                ),
                (Some(co), None) => format!(
                    "&for=tract:*&in=state:{}&in=county:{}",
                    state.geoid_string(),
                    co.geoid_string()
                ),
                (Some(co), Some(tr)) => format!(
                    "&for=tract:{}&in=state:{}&in=county:{}",
                    tr.geoid_string(),
                    state.geoid_string(),
                    co.geoid_string()
                ),
            },
            G::BlockGroup(state, county, tract, block_group) => {
                match (county, tract, block_group) {
                    (None, None, None) => format!(
                        "&for=block%20group:*&in=state:{}&in=county:*&in=tract:*",
                        state.geoid_string()
                    ),
                    (None, None, Some(b)) => format!(
                        "&for=block%20group:{}&in=state:{}&in=county:*&in=tract:*",
                        b.geoid_string(),
                        state.geoid_string()
                    ),
                    (None, Some(t), None) => format!(
                        "&for=block%20group:*&in=state:{}&in=county:*&in=tract:{}",
                        state.geoid_string(),
                        t.geoid_string(),
                    ),
                    (None, Some(t), Some(b)) => format!(
                        "&for=block%20group:{}&in=state:{}&in=county:*&in=tract:{}",
                        b.geoid_string(),
                        state.geoid_string(),
                        t.geoid_string(),
                    ),
                    (Some(c), None, None) => format!(
                        "&for=block%20group:*&in=state:{}&in=county:{}&in=tract:*",
                        state.geoid_string(),
                        c.geoid_string()
                    ),
                    (Some(c), None, Some(b)) => format!(
                        "&for=block%20group:{}&in=state:{}&in=county:{}&in=tract:*",
                        b.geoid_string(),
                        state.geoid_string(),
                        c.geoid_string(),
                    ),
                    (Some(c), Some(t), None) => format!(
                        "&for=block%20group:*&in=state:{}&in=county:{}&in=tract:{}",
                        state.geoid_string(),
                        c.geoid_string(),
                        t.geoid_string(),
                    ),
                    (Some(c), Some(t), Some(b)) => format!(
                        "&for=block%20group:{}&in=state:{}&in=county:{}&in=tract:{}",
                        b.geoid_string(),
                        state.geoid_string(),
                        c.geoid_string(),
                        t.geoid_string()
                    ),
                }
            }
        }
    }

    pub fn response_geoid_type(&self) -> GeoidType {
        use AcsGeoidQuery as G;
        match self {
            G::State(_) => GeoidType::State,
            G::County(_, _) => GeoidType::County,
            G::CountySubdivision(_, _, _) => GeoidType::CountySubdivision,
            G::Place(_, _) => GeoidType::Place,
            G::CensusTract(_, _, _) => GeoidType::CensusTract,
            G::BlockGroup(_, _, _, _) => GeoidType::BlockGroup,
        }
    }

    pub fn response_column_names(&self) -> Vec<String> {
        use AcsGeoidQuery as G;
        match self {
            G::State(_) => vec![String::from("state")],
            G::County(_, _) => vec![String::from("state"), String::from("county")],
            G::CountySubdivision(_, _, _) => vec![
                String::from("state"),
                String::from("county"),
                String::from("county subdivision"),
            ],
            G::Place(_, _) => vec![String::from("state"), String::from("place")],
            G::CensusTract(_, _, _) => vec![
                String::from("state"),
                String::from("county"),
                String::from("tract"),
            ],
            G::BlockGroup(_, _, _, _) => vec![
                String::from("state"),
                String::from("county"),
                String::from("tract"),
                String::from("block group"),
            ],
        }
    }

    pub fn response_column_count(&self) -> usize {
        match self {
            AcsGeoidQuery::State(_) => 1,
            AcsGeoidQuery::County(_, _) => 2,
            AcsGeoidQuery::CountySubdivision(_, _, _) => 3,
            AcsGeoidQuery::Place(_, _) => 2,
            AcsGeoidQuery::CensusTract(_, _, _) => 3,
            AcsGeoidQuery::BlockGroup(_, _, _, _) => 4,
        }
    }

    pub fn get_geoid_type(&self) -> GeoidType {
        match self {
            AcsGeoidQuery::State(_) => GeoidType::State,
            AcsGeoidQuery::County(_, _) => GeoidType::County,
            AcsGeoidQuery::CountySubdivision(_, _, _) => GeoidType::CountySubdivision,
            AcsGeoidQuery::Place(_, _) => GeoidType::Place,
            AcsGeoidQuery::CensusTract(_, _, _) => GeoidType::CensusTract,
            AcsGeoidQuery::BlockGroup(_, _, _, _) => GeoidType::BlockGroup,
        }
    }

    /// builds a function that unpacks the ACS query values representing a geoid.
    /// these return as values in an array of different lengths, depending on the scope of
    /// the original query.
    pub fn build_deserialize_geoid_fn(&self) -> DeserializeGeoidFn {
        let geoid_type = self.get_geoid_type();
        let f: DeserializeGeoidFn = Rc::new(move |vals| {
            let strings = as_strings(&vals)?;
            geoid_type.geoid_from_slice_of_strings(&strings)
        });
        f
    }
}

/// helper function to convert a vec of JSON values to their expected String values.
fn as_strings(arr: &[serde_json::Value]) -> Result<Vec<String>, String> {
    arr.iter()
        .map(|v| {
            v.as_str()
                .ok_or_else(|| format!("raw geoid value should be string, found {v}"))
                .map(String::from)
        })
        .collect::<Result<Vec<_>, String>>()
}