X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FMisc%2FGeo.pm;h=4ef10850e35b17c9517bbd9027aed57e8d6bcb61;hb=2d5acabf71d46aa469a6867f294706242c82db98;hp=f3263f6d873dcc44d66f8d105afd542286f1852d;hpb=1c36854ac3157fe15723d46398ea87d2d9adb484;p=freeside.git diff --git a/FS/FS/Misc/Geo.pm b/FS/FS/Misc/Geo.pm index f3263f6d8..4ef10850e 100644 --- a/FS/FS/Misc/Geo.pm +++ b/FS/FS/Misc/Geo.pm @@ -42,6 +42,10 @@ sub get_censustract_ffiec { my $location = shift; my $year = shift; + if ( length($location->{country}) and uc($location->{country}) ne 'US' ) { + return ''; + } + warn Dumper($location, $year) if $DEBUG; my $url = 'http://www.ffiec.gov/Geocode/default.aspx'; @@ -447,6 +451,12 @@ sub standardize_tomtom { my ($address1, $address2) = ($location->{address1}, $location->{address2}); my $subloc = ''; + # trim whitespace + $address1 =~ s/^\s+//; + $address1 =~ s/\s+$//; + $address2 =~ s/^\s+//; + $address2 =~ s/\s+$//; + # try to fix some cases of the address fields being switched if ( $address2 =~ /^\d/ and $address1 !~ /^\d/ ) { $address2 = $address1; @@ -487,8 +497,8 @@ sub standardize_tomtom { } } - if (!$match) { - die "Location not found.\n"; + if ( !$match or !$clean ) { # partial matches are not useful + die "Address not found\n"; } my $tract = ''; if ( defined $match->{censusTract} ) { @@ -642,9 +652,61 @@ sub subloc_address2 { warn "normalizing '$addr2' to '$result'\n" if $DEBUG > 1; $addr2 = $result; } + $addr2 = '' if $addr2 eq $subloc; # if it was entered redundantly ($subloc, $addr2); } +sub standardize_melissa { + my $class = shift; + my $location = shift; + + local $@; + eval "use Geo::Melissa::WebSmart"; + die $@ if $@; + + my $id = $conf->config('melissa-userid') + or die "no melissa-userid configured\n"; + my $geocode = $conf->exists('melissa-enable_geocoding') ? 1 : 0; + + my $request = { + id => $id, + a1 => $location->{address1}, + a2 => $location->{address2}, + city => $location->{city}, + state => $location->{state}, + ctry => $location->{country}, + zip => $location->{zip}, + geocode => $geocode, + }; + my $result = Geo::Melissa::WebSmart->query($request); + if ( $result->code =~ /AS01/ ) { # always present on success + my $addr = $result->address; + warn Dumper $addr if $DEBUG > 1; + my $out = { + address1 => $addr->{Address1}, + address2 => $addr->{Address2}, + city => $addr->{City}->{Name}, + state => $addr->{State}->{Abbreviation}, + country => $addr->{Country}->{Abbreviation}, + zip => $addr->{Zip}, + latitude => $addr->{Latitude}, + longitude => $addr->{Longitude}, + addr_clean => 'Y', + }; + if ( $addr->{Census}->{Tract} ) { + my $censustract = $addr->{County}->{Fips} . $addr->{Census}->{Tract}; + # insert decimal point two digits from the end + $censustract =~ s/(\d\d)$/\.$1/; + $out->{censustract} = $censustract; + $out->{censusyear} = $conf->config('census_year'); + } + # we could do a lot more nuanced reporting of the warning/status codes, + # but the UI doesn't support that yet. + return $out; + } else { + die $result->status_message; + } +} =back