From 33f5c81279e1de9afd97adb1872c1b11caf7f6f9 Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Wed, 4 Mar 2015 13:29:07 -0800 Subject: [PATCH] add support for U.S. Census geocoding, #32250 --- FS/FS/Conf.pm | 1 + FS/FS/Misc/Geo.pm | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/FS/FS/Conf.pm b/FS/FS/Conf.pm index 479e9ab2b..d5e8960b2 100644 --- a/FS/FS/Conf.pm +++ b/FS/FS/Conf.pm @@ -4440,6 +4440,7 @@ and customer address. Include units.', 'section' => 'invoicing', 'description' => 'Instead of showing payments (and credits) applied to the invoice, show those received since the previous invoice date.', 'type' => 'checkbox', + 'uscensus' => 'U.S. Census Bureau', }, { diff --git a/FS/FS/Misc/Geo.pm b/FS/FS/Misc/Geo.pm index dbc383a14..1e17b5fcc 100644 --- a/FS/FS/Misc/Geo.pm +++ b/FS/FS/Misc/Geo.pm @@ -236,6 +236,8 @@ sub wa_sales { die "WA tax district lookup error: $error"; } +###### USPS Standardization ###### + sub standardize_usps { my $class = shift; @@ -292,6 +294,55 @@ sub standardize_usps { addr_clean=> 'Y' } } +###### U.S. Census Bureau ###### + +sub standardize_uscensus { + my $self = shift; + my $location = shift; + + eval "use Geo::USCensus::Geocoding"; + die $@ if $@; + + if ( $location->{country} ne 'US' ) { + # soft failure + warn "standardize_uscensus not for use in country ".$location->{country}."\n"; + $location->{addr_clean} = ''; + return $location; + } + + my $request = { + street => $location->{address1}, + city => $location->{city}, + state => $location->{state}, + zip => $location->{zip}, + debug => ($DEBUG || 0), + }; + + my $result = Geo::USCensus::Geocoding->query($request); + if ( $result->is_match ) { + # unfortunately we get the address back as a single line + if ($result->address =~ /^(.*), (.*), ([A-Z]{2}), (\d{5}.*)$/) { + return +{ + address1 => $1, + city => $2, + state => $3, + zip => $4, + address2 => uc($location->{address2}), + latitude => $result->latitude, + longitude => $result->longitude, + censustract => $result->censustract, + }; + } else { + die "can't parse address '".$result->address."'"; + } + } else { + warn Dumper($result) if $DEBUG; + die $result->error_message; + } +} + +####### EZLOCATE (obsolete) ####### + sub _tomtom_query { # helper method for the below my %args = @_; my $result = Geo::TomTom::Geocoding->query(%args); -- 2.11.0