3 # Copyright 2009-2012, Equinox Software, Inc.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 enrich_zips - Utility to add county information to city/state/ZIP data (and optionally use zips.txt format)
32 enrich_zips --db US.txt < citystatezip.tsv
33 enrich_zips --makezips --db US.txt < citystatezip.tsv > zips.txt
37 Geonames database can be downloaded from http://download.geonames.org/export/zip/US.zip
39 Add the --believegn flag if you want to believe the Geonames database when it conflicts with patron data.
43 my ($db, $makezips, %zips, $warn, $believegn);
44 my $result = GetOptions ("db=s" => \$db,
45 "makezips" => \$makezips,
47 "believegn" => \$believegn);
50 "Please specify the location of the Geonames database with --db US.txt\n" .
51 "HINT: You can download it at http://download.geonames.org/export/zip/US.zip" .
55 open DB, $db or die "Couldn't open Geonames database $db: $!\n";
57 # Slurp in the Geonames database
61 @{$zips{$f[1]}} = @f[4,2,5];
62 # @{$zips{"33166"}} == ("FL", "Miami Springs", "Miami-Dade")
68 (my $city, my $state, my $zip) = split(/\t/) or next;
70 my ($dbcity, $dbstate);
72 if (defined $zips{$zip}) {
73 ($dbstate, $dbcity, $county) = @{$zips{$zip}};
77 if (!defined $zips{$zip}) {
78 print STDERR "No county data found for ZIP code $zip ($city, $state)\n\n";
81 if ($city ne $dbcity || $state ne $dbstate) {
82 print STDERR "Patron data input says $zip is $city, $state\n".
83 "Geonames database says $zip is $dbcity, $dbstate\n\n";
88 if (defined $zips{$zip} && ($city ne $dbcity || $state ne $dbstate)) {
94 print "|" . join("|", ($state, $city, $zip, "1", "", $county)) . "||\n";
96 print join("\t", ($city, $state, $zip, $county)) . "\n";