geocode-csv 0.3.0-beta.2

Unofficial CLI tool to bulk geocode CSV data using the SmartyStreets API
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
436
437
438
439
440
441
//! Types related to addresses.

use csv::StringRecord;
use failure::{format_err, ResultExt};
use serde::{Deserialize, Serialize};
use std::{
    borrow::Cow,
    collections::{HashMap, HashSet},
    fs::File,
    path::Path,
};

use crate::structure::Structure;
use crate::Result;

/// An address record that we can pass to SmartyStreets.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct Address {
    /// Either the street, or the entire address as a string. This must always
    /// be present.
    pub street: String,
    /// The city, if any.
    pub city: Option<String>,
    /// The state, if any.
    pub state: Option<String>,
    /// The zipcode, if any.
    pub zipcode: Option<String>,
}

/// Either a column name, or a list of names.
///
/// `K` is typically either a `String` (for a column name) or a `usize` (for a
/// column index).
#[derive(Debug, Deserialize, Eq, PartialEq)]
#[serde(untagged, deny_unknown_fields)]
pub enum ColumnKeyOrKeys<K: Eq> {
    /// The name of a single column.
    Key(K),
    /// The names of multiple columns, which should be joined using a space.
    Keys(Vec<K>),
}

impl ColumnKeyOrKeys<usize> {
    /// Given a CSV row, extract an `Address` value to send to SmartyStreets.
    pub fn extract_from_record<'a>(
        &self,
        record: &'a StringRecord,
    ) -> Result<Cow<'a, str>> {
        match self {
            ColumnKeyOrKeys::Key(key) => Ok(Cow::Borrowed(&record[*key])),
            ColumnKeyOrKeys::Keys(keys) => {
                // Allocate an empty string with some reserved space so we maybe don't
                // need to reallocate it every time we append.
                let mut extracted = String::with_capacity(40);
                for key in keys {
                    let s = &record[*key];
                    if extracted.is_empty() {
                        extracted.push_str(s);
                    } else if extracted.ends_with(s) {
                        // Already there, so ignore it. This appears in a lot of
                        // real-world databases, for some reason.
                    } else {
                        extracted.push(' ');
                        extracted.push_str(s);
                    }
                }
                Ok(Cow::Owned(extracted))
            }
        }
    }
}

#[test]
fn extract_collapses_duplicate_suffixes() {
    // This seems really arbitrary, but it consistently appears in many
    // real-world databases.
    //
    // I wonder if the equivalent "prefix" case is common?
    use std::iter::FromIterator;
    let record = StringRecord::from_iter(&["100", "Main Street #302", "#302"]);
    let keys = ColumnKeyOrKeys::Keys(vec![0, 1, 2]);
    assert_eq!(
        keys.extract_from_record(&record).unwrap(),
        "100 Main Street #302",
    );
}

/// The column names from a CSV file that we want to use as addresses.
///
/// `K` is typically either a `String` (for a column name) or a `usize` (for a
/// column index).
#[derive(Debug, Deserialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct AddressColumnKeys<K: Default + Eq> {
    /// The name of street column or columns. May also be specified as
    /// "house_number_and_street" or "address".
    #[serde(alias = "house_number_and_street", alias = "address", alias = "glob")]
    pub street: ColumnKeyOrKeys<K>,
    /// The city column, if any.
    #[serde(default)]
    pub city: Option<K>,
    /// The state column, if any.
    #[serde(default)]
    pub state: Option<K>,
    /// The zipcode column, if any. May also be specified as
    /// "postcode".
    #[serde(default, alias = "postcode")]
    pub zipcode: Option<K>,
}

impl AddressColumnKeys<usize> {
    /// Given a CSV row, extract an `Address` value to send to SmartyStreets.
    pub fn extract_address_from_record(
        &self,
        record: &'_ StringRecord,
    ) -> Result<Address> {
        Ok(Address {
            street: self.street.extract_from_record(record)?.into_owned(),
            city: self.city.map(|c| record[c].to_owned()),
            state: self.state.map(|s| record[s].to_owned()),
            zipcode: self.zipcode.map(|z| record[z].to_owned()),
        })
    }
}

#[test]
fn extract_simple_address_from_record() {
    use std::iter::FromIterator;
    let record = StringRecord::from_iter(&[
        "1600 Pennsylvania Avenue NW, Washington DC, 20500",
    ]);
    let keys = AddressColumnKeys {
        street: ColumnKeyOrKeys::Key(0),
        city: None,
        state: None,
        zipcode: None,
    };
    assert_eq!(
        keys.extract_address_from_record(&record).unwrap(),
        Address {
            street: "1600 Pennsylvania Avenue NW, Washington DC, 20500".to_owned(),
            city: None,
            state: None,
            zipcode: None,
        },
    );
}

#[test]
fn extract_complex_address_from_record() {
    use std::iter::FromIterator;
    let record = StringRecord::from_iter(&[
        "1600",
        "Pennsylvania Avenue NW",
        "Washington",
        "DC",
        "20500",
    ]);
    let keys = AddressColumnKeys {
        street: ColumnKeyOrKeys::Keys(vec![0, 1]),
        city: Some(2),
        state: Some(3),
        zipcode: Some(4),
    };
    assert_eq!(
        keys.extract_address_from_record(&record).unwrap(),
        Address {
            street: "1600 Pennsylvania Avenue NW".to_owned(),
            city: Some("Washington".to_owned()),
            state: Some("DC".to_owned()),
            zipcode: Some("20500".to_owned()),
        },
    );
}

/// A map from column prefixes (e.g. "home", "work") to address column keys.
///
/// `K` is typically either a `String` (for a column name) or a `usize` (for a
/// column index).
#[derive(Debug, Deserialize, Eq, PartialEq)]
pub struct AddressColumnSpec<Key: Default + Eq> {
    /// A map from output column prefixes to address column keys.
    #[serde(flatten)]
    address_columns_by_prefix: HashMap<String, AddressColumnKeys<Key>>,
}

impl<Key: Default + Eq> AddressColumnSpec<Key> {
    /// The number of prefixes we want to include in our output.
    pub fn prefix_count(&self) -> usize {
        self.address_columns_by_prefix.len()
    }

    /// The address prefixes we want to include in our output.
    ///
    /// This **MUST** return the prefixes in the same order every time or our
    /// output will be corrupted.
    pub fn prefixes(&self) -> Vec<&str> {
        let mut prefixes = self
            .address_columns_by_prefix
            .keys()
            .map(|k| &k[..])
            .collect::<Vec<_>>();
        // Do not remove this `sort`! This can be unstable because strings give
        // the same result with stable and unstable sorts.
        prefixes.sort_unstable();
        prefixes
    }

    /// Look up an `AddressColumnKeys` by prefix.
    pub fn get(&self, prefix: &str) -> Option<&AddressColumnKeys<Key>> {
        self.address_columns_by_prefix.get(prefix)
    }

    /// What column should we remove from the input records in order
    /// to prevent duplicate columns?
    ///
    /// Returns the name and index of each column to remove, in order.
    pub fn duplicate_columns<'header>(
        &self,
        structure: &Structure,
        header: &'header StringRecord,
    ) -> Result<Vec<(&'header str, usize)>> {
        // Get all our column names for all prefixes, and insert them into a
        // hash table.
        let mut output_column_names = HashSet::new();
        for prefix in self.prefixes() {
            for name in structure.output_column_names(prefix)? {
                if !output_column_names.insert(name.clone()) {
                    return Err(format_err!("duplicate column name {:?}", name));
                }
            }
        }

        // Decide which columns of `header` need to be removed.
        let mut duplicate_columns = vec![];
        for (i, col) in header.iter().enumerate() {
            if output_column_names.contains(col) {
                duplicate_columns.push((col, i));
            }
        }
        Ok(duplicate_columns)
    }
}

#[test]
fn find_columns_to_remove() {
    use std::iter::FromIterator;

    let address_column_spec_json = r#"{
        "home": {
            "house_number_and_street": ["home_number", "home_street"],
            "city": "home_city",
            "state": "home_state",
            "postcode": "home_zip"
        },
        "work": {
            "address": "work_address"
        }
    }"#;
    let spec: AddressColumnSpec<String> =
        serde_json::from_str(address_column_spec_json).unwrap();

    let structure = Structure::complete().unwrap();
    let header =
        StringRecord::from_iter(&["existing", "home_addressee", "work_addressee"]);
    let indices = spec.duplicate_columns(&structure, &header).unwrap();
    assert_eq!(indices, vec![("home_addressee", 1), ("work_addressee", 2)]);
}

impl AddressColumnSpec<String> {
    /// Load an `AddressColumnSpec` from a file.
    pub fn from_path(path: &Path) -> Result<Self> {
        let f = File::open(path)
            .with_context(|_| format_err!("cannot open {}", path.display()))?;
        Ok(serde_json::from_reader(f)
            .with_context(|_| format_err!("error parsing {}", path.display()))?)
    }

    /// Given an `AddressColumnSpec` using strings, and the header row of a CSV
    /// file, convert it into a `AddressColumnSpec<usize>` containing the column
    /// indices.
    pub fn convert_to_indices_using_headers(
        &self,
        headers: &StringRecord,
    ) -> Result<AddressColumnSpec<usize>> {
        let mut header_columns = HashMap::new();
        for (idx, header) in headers.iter().enumerate() {
            if let Some(_existing) = header_columns.insert(header, idx) {
                return Err(format_err!("duplicate header column `{}`", header));
            }
        }
        self.convert_to_indices(&header_columns)
    }
}

#[test]
fn convert_address_column_spec_to_indices() {
    use std::iter::FromIterator;
    let headers = StringRecord::from_iter(&[
        "home_number",
        "home_street",
        "home_city",
        "home_state",
        "home_zip",
        "work_address",
    ]);
    let address_column_spec_json = r#"{
   "home": {
       "house_number_and_street": ["home_number", "home_street"],
       "city": "home_city",
       "state": "home_state",
       "postcode": "home_zip"
   },
   "work": {
       "address": "work_address"
   }
}"#;
    let address_column_spec: AddressColumnSpec<String> =
        serde_json::from_str(address_column_spec_json).unwrap();

    let mut expected = HashMap::new();
    expected.insert(
        "home".to_owned(),
        AddressColumnKeys {
            street: ColumnKeyOrKeys::Keys(vec![0, 1]),
            city: Some(2),
            state: Some(3),
            zipcode: Some(4),
        },
    );
    expected.insert(
        "work".to_owned(),
        AddressColumnKeys {
            street: ColumnKeyOrKeys::Key(5),
            city: None,
            state: None,
            zipcode: None,
        },
    );
    assert_eq!(
        address_column_spec
            .convert_to_indices_using_headers(&headers)
            .unwrap(),
        AddressColumnSpec::<usize> {
            address_columns_by_prefix: expected,
        },
    );
}

/// A value which can be converted from using string indices to numeric indices.
trait ConvertToIndices {
    type Output;

    /// Convert this value from using string indices to numeric indices.
    fn convert_to_indices(
        &self,
        header_columns: &HashMap<&str, usize>,
    ) -> Result<Self::Output>;
}

impl ConvertToIndices for String {
    type Output = usize;

    fn convert_to_indices(
        &self,
        header_columns: &HashMap<&str, usize>,
    ) -> Result<Self::Output> {
        header_columns
            .get(&self[..])
            .copied()
            .ok_or_else(|| format_err!("could not find column `{}` in header", self))
    }
}

impl ConvertToIndices for ColumnKeyOrKeys<String> {
    type Output = ColumnKeyOrKeys<usize>;

    fn convert_to_indices(
        &self,
        header_columns: &HashMap<&str, usize>,
    ) -> Result<Self::Output> {
        match self {
            ColumnKeyOrKeys::Key(key) => Ok(ColumnKeyOrKeys::Key(
                key.convert_to_indices(header_columns)?,
            )),
            ColumnKeyOrKeys::Keys(keys) => Ok(ColumnKeyOrKeys::Keys(
                keys.iter()
                    .map(|k| k.convert_to_indices(header_columns))
                    .collect::<Result<Vec<_>>>()?,
            )),
        }
    }
}

impl ConvertToIndices for AddressColumnKeys<String> {
    type Output = AddressColumnKeys<usize>;

    fn convert_to_indices(
        &self,
        header_columns: &HashMap<&str, usize>,
    ) -> Result<Self::Output> {
        Ok(AddressColumnKeys {
            street: self.street.convert_to_indices(header_columns)?,
            city: self
                .city
                .as_ref()
                .map(|c| c.convert_to_indices(header_columns))
                .transpose()?,
            state: self
                .state
                .as_ref()
                .map(|s| s.convert_to_indices(header_columns))
                .transpose()?,
            zipcode: self
                .zipcode
                .as_ref()
                .map(|z| z.convert_to_indices(header_columns))
                .transpose()?,
        })
    }
}

impl ConvertToIndices for AddressColumnSpec<String> {
    type Output = AddressColumnSpec<usize>;

    fn convert_to_indices(
        &self,
        header_columns: &HashMap<&str, usize>,
    ) -> Result<Self::Output> {
        let mut address_columns_by_prefix = HashMap::new();
        for (prefix, address_columns) in &self.address_columns_by_prefix {
            address_columns_by_prefix.insert(
                prefix.to_owned(),
                address_columns.convert_to_indices(header_columns)?,
            );
        }
        Ok(AddressColumnSpec {
            address_columns_by_prefix,
        })
    }
}