12 enrich_zips - Utility to add county information to city/state/ZIP data (and optionally use zips.txt format)
16 enrich_zips --db US.txt < citystatezip.tsv
17 enrich_zips --makezips --db US.txt < citystatezip.tsv > zips.txt
21 Geonames database can be downloaded from http://download.geonames.org/export/zip/US.zip
23 Add the --believegn flag if you want to believe the Geonames database when it conflicts with patron data.
27 my ($db, $makezips, %zips, $warn, $believegn);
28 my $result = GetOptions ("db=s" => \$db,
29 "makezips" => \$makezips,
31 "believegn" => \$believegn);
34 "Please specify the location of the Geonames database with --db US.txt\n" .
35 "HINT: You can download it at http://download.geonames.org/export/zip/US.zip" .
39 open DB, $db or die "Couldn't open Geonames database $db: $!\n";
41 # Slurp in the Geonames database
45 @{$zips{$f[1]}} = @f[4,2,5];
46 # @{$zips{"33166"}} == ("FL", "Miami Springs", "Miami-Dade")
52 (my $city, my $state, my $zip) = split(/\t/) or next;
54 my ($dbcity, $dbstate);
56 if (defined $zips{$zip}) {
57 ($dbstate, $dbcity, $county) = @{$zips{$zip}};
61 if (!defined $zips{$zip}) {
62 print STDERR "No county data found for ZIP code $zip ($city, $state)\n\n";
65 if ($city ne $dbcity || $state ne $dbstate) {
66 print STDERR "Patron data input says $zip is $city, $state\n".
67 "Geonames database says $zip is $dbcity, $dbstate\n\n";
72 if ($city ne $dbcity || $state ne $dbstate) {
78 print "|" . join("|", ($state, $city, $zip, "1", "", $county)) . "||\n";
80 print join("\t", ($city, $state, $zip, $county)) . "\n";