rename teleatlas to "EZLocate" and switch to our API wrapper
[freeside.git] / FS / FS / Misc / Geo.pm
index c8290e7..6bc71fc 100644 (file)
@@ -7,7 +7,7 @@ use LWP::UserAgent;
 use HTTP::Request;
 use HTTP::Request::Common qw( GET POST );
 use HTML::TokeParser;
-use URI::Escape;
+use URI::Escape 3.31;
 use Data::Dumper;
 
 FS::UID->install_callback( sub {
@@ -132,7 +132,7 @@ sub get_censustract_ffiec {
 
   } #unless ($res->code  eq '200')
 
-  die "FFIEC Geocoding error: $error" if $error;
+  die "FFIEC Geocoding error: $error\n" if $error;
 
   $return->{'statecode'} .  $return->{'countycode'} .  $return->{'tractcode'};
 }
@@ -291,7 +291,7 @@ sub standardize_usps {
       UserID => $userid,
       Password => $password,
       Testing => 0,
-  } ) or die "error starting USPS WebTools";
+  } ) or die "error starting USPS WebTools\n";
 
   my($zip5, $zip4) = split('-',$location->{'zip'});
 
@@ -312,7 +312,7 @@ sub standardize_usps {
   warn $verifier->response
     if $DEBUG > 1;
 
-  die "USPS WebTools error: ".$verifier->{error}{description} 
+  die "USPS WebTools error: ".$verifier->{error}{description} ."\n"
     if $verifier->is_error;
 
   my $zip = $hash->{Zip5};
@@ -328,7 +328,7 @@ sub standardize_usps {
     addr_clean=> 'Y' }
 }
 
-my %teleatlas_error = ( # USA_Geo_002 documentation
+my %ezlocate_error = ( # USA_Geo_002 documentation
   10  => 'State not found',
   11  => 'City not found',
   12  => 'Invalid street address',
@@ -338,36 +338,37 @@ my %teleatlas_error = ( # USA_Geo_002 documentation
   17  => 'Intersection not found', #unused?
 );
 
-sub standardize_teleatlas {
+sub standardize_ezlocate {
   my $self = shift;
   my $location = shift;
   my $class;
-  if ( $location->{country} eq 'US' ) {
-    $class = 'USA_Geo_004Tool';
-  }
-  elsif ( $location->{country} eq 'CA' ) {
-    $class = 'CAN_Geo_001Tool';
-  }
-  else { # shouldn't be a fatal error, just pass through unverified address
-    warn "standardize_teleatlas: address lookup in '".$location->{country}.
-         "' not available\n";
-    return $location;
-  }
+  #if ( $location->{country} eq 'US' ) {
+  #  $class = 'USA_Geo_004Tool';
+  #}
+  #elsif ( $location->{country} eq 'CA' ) {
+  #  $class = 'CAN_Geo_001Tool';
+  #}
+  #else { # shouldn't be a fatal error, just pass through unverified address
+  #  warn "standardize_teleatlas: address lookup in '".$location->{country}.
+  #       "' not available\n";
+  #  return $location;
+  #}
+  #my $path = $conf->config('teleatlas-path') || '';
+  #local @INC = (@INC, $path);
+  #eval "use $class;";
+  #if ( $@ ) {
+  #  die "Loading $class failed:\n$@".
+  #      "\nMake sure the TeleAtlas Perl SDK is installed correctly.\n";
+  #}
+
+  $class = 'Geo::EZLocate'; # use our own library
+  eval "use $class";
+  die $@ if $@;
 
-  if ( my $path = $conf->config('teleatlas-path') ) {
-    local @INC = (@INC, $path);
-  }
-  my $userid = $conf->config('teleatlas-userid')
-    or die "no teleatlas-userid configured";
-  my $password = $conf->config('teleatlas-password')
-    or die "no teleatlas-password configured";
-
-  local @INC = (@INC, $path);
-  eval "use $class;";
-  if ( $@ ) {
-    die "Loading $class failed:\n$@".
-        "\nMake sure the TeleAtlas Perl SDK is installed correctly.\n";
-  }
+  my $userid = $conf->config('ezlocate-userid')
+    or die "no ezlocate-userid configured\n";
+  my $password = $conf->config('ezlocate-password')
+    or die "no ezlocate-password configured\n";
   
   my $tool = $class->new($userid, $password);
   my $match = $tool->findAddress(
@@ -376,9 +377,9 @@ sub standardize_teleatlas {
     $location->{state},
     $location->{zip}, #12345-6789 format is allowed
   );
-  warn "teleatlas returned match:\n".Dumper($match) if $DEBUG > 1;
+  warn "ezlocate returned match:\n".Dumper($match) if $DEBUG > 1;
   # error handling - B codes indicate success
-  die $teleatlas_error{$match->{MAT_STAT}}."\n"
+  die $ezlocate_error{$match->{MAT_STAT}}."\n"
     unless $match->{MAT_STAT} =~ /^B\d$/;
 
   {
@@ -390,7 +391,8 @@ sub standardize_teleatlas {
     zip         => $match->{STD_ZIP}.'-'.$match->{STD_P4},
     latitude    => $match->{MAT_LAT},
     longitude   => $match->{MAT_LON},
-    censustract => $match->{FIPS_ST}.$match->{FIPS_CTY}.$match->{CEN_TRCT},
+    censustract => $match->{FIPS_ST}.$match->{FIPS_CTY}.
+                   sprintf('%04.2f',$match->{CEN_TRCT}),
     addr_clean  => 'Y',
   };
 }