add support for U.S. Census geocoding, #32250
authorMark Wells <mark@freeside.biz>
Wed, 4 Mar 2015 21:26:41 +0000 (13:26 -0800)
committerMark Wells <mark@freeside.biz>
Wed, 4 Mar 2015 21:28:25 +0000 (13:28 -0800)
FS/FS/Conf.pm
FS/FS/Misc/Geo.pm

index d7b0bb9..3b9a291 100644 (file)
@@ -4440,6 +4440,7 @@ and customer address. Include units.',
     'type'        => 'select',
     'select_hash' => [ '' => '', 
                        'usps'     => 'U.S. Postal Service',
+                       'uscensus' => 'U.S. Census Bureau',
                        'ezlocate' => 'EZLocate',
                        'tomtom'   => 'TomTom',
                        'melissa'  => 'Melissa WebSmart',
index e7b1fa8..009621b 100644 (file)
@@ -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) #######
+
 my %ezlocate_error = ( # USA_Geo_002 documentation
   10  => 'State not found',
   11  => 'City not found',