adding a simple counts dedupe report and fixing a typo
[migration-tools.git] / enrich_zips
1 #!/usr/bin/perl -w
2
3 # Copyright 2009-2012, Equinox Software, Inc.
4 #
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.
9 #
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.
14 #
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.
18
19 use strict;
20 use Getopt::Long;
21
22 my $VERSION = '1.01';
23
24 =pod
25
26 NAME
27
28         enrich_zips - Utility to add county information to city/state/ZIP data (and optionally use zips.txt format)
29
30 USAGE
31
32         enrich_zips --db US.txt < citystatezip.tsv
33         enrich_zips --makezips --db US.txt < citystatezip.tsv > zips.txt
34
35 NOTES
36
37         Geonames database can be downloaded from http://download.geonames.org/export/zip/US.zip
38
39         Add the --believegn flag if you want to believe the Geonames database when it conflicts with patron data.
40
41 =cut
42
43 my ($db, $makezips, %zips, $warn, $believegn);
44 my $result = GetOptions ("db=s"      => \$db,
45                          "makezips"  => \$makezips,
46                          "warn"      => \$warn,
47                          "believegn" => \$believegn);
48
49 die
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" .
52   "\n\nProgram halted"
53 unless defined($db);
54
55 open DB, $db or die "Couldn't open Geonames database $db: $!\n";
56
57 # Slurp in the Geonames database
58 while (<DB>) {
59         chomp;
60         my @f = split(/\t/);
61         @{$zips{$f[1]}} = @f[4,2,5];
62         # @{$zips{"33166"}} == ("FL", "Miami Springs", "Miami-Dade")
63 }
64
65 while (<>) {
66
67         chomp;
68         (my $city, my $state, my $zip) = split(/\t/) or next;
69         my $county = "";
70         my ($dbcity, $dbstate);
71
72         if (defined $zips{$zip}) {
73                 ($dbstate, $dbcity, $county) = @{$zips{$zip}};
74         } 
75
76         if ($warn) {
77                 if (!defined $zips{$zip}) {
78                         print STDERR "No county data found for ZIP code $zip ($city, $state)\n\n";
79                         next;
80                 }
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";
84                 }
85         }
86
87         if ($makezips) {
88                 if (defined $zips{$zip} && ($city ne $dbcity || $state ne $dbstate)) {
89                         if ($believegn) {
90                                 $city  = $dbcity;
91                                 $state = $dbstate;
92                         }
93                 }
94                 print "|" . join("|", ($state, $city, $zip, "1", "", $county)) . "||\n";
95         } else {
96                 print join("\t", ($city, $state, $zip, $county)) . "\n";
97         }
98 }