Skip to content

Commit

Permalink
Look for more libpostal addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Mar 29, 2018
1 parent d9ac076 commit fda5c96
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
42 changes: 39 additions & 3 deletions lib/Geo/Coder/Free/OpenAddresses.pm
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ sub geocode {
if($libpostal_is_installed) {
if(my %addr = Geo::libpostal::parse_address($location)) {
# print Data::Dumper->new([\%addr])->Dump();
if($addr{'country'} && ($addr{'country'} =~ /^(Canada|United States|USA|US)$/i)) {
my $l;
if($addr{'country'} && $addr{'state'} && ($addr{'country'} =~ /^(Canada|United States|USA|US)$/i)) {
if(my $street = $addr{'road'}) {
$street = uc($street);
if($street =~ /(.+)\s+STREET$/) {
Expand Down Expand Up @@ -183,7 +182,43 @@ sub geocode {
}
}
}
foreach my $column('house_number', 'road', 'city', 'state', 'country') {
# TODO: paramerize and turn in to a function
my $l;
if($addr{'state_district'}) {
foreach my $column('house_number', 'road', 'city', 'state_district', 'state', 'country') {
if($addr{$column}) {
$l .= $addr{$column};
}
}
if(my $rc = $self->_get($l)) {
# print Data::Dumper->new([$rc])->Dump();
return $rc;
}
$l = undef;
foreach my $column('road', 'city', 'state_district', 'state', 'country') {
if($addr{$column}) {
$l .= $addr{$column};
}
}
if(my $rc = $self->_get($l)) {
# print Data::Dumper->new([$rc])->Dump();
return $rc;
}
}
if($addr{'house_number'}) {
$l = undef;
foreach my $column('house_number', 'road', 'city', 'state', 'country') {
if($addr{$column}) {
$l .= $addr{$column};
}
}
if(my $rc = $self->_get($l)) {
# print Data::Dumper->new([$rc])->Dump();
return $rc;
}
}
$l = undef;
foreach my $column('road', 'city', 'state', 'country') {
if($addr{$column}) {
$l .= $addr{$column};
}
Expand Down Expand Up @@ -898,6 +933,7 @@ sub _get {
cache => $self->{cache} || CHI->new(driver => 'Memory', datastore => {})
);
$self->{openaddr_db} = $openaddr_db;
# print "$location: $digest\n";
# ::diag("$location: $digest");
# my @call_details = caller(0);
# print "line " . $call_details[2], "\n";
Expand Down
17 changes: 2 additions & 15 deletions t/openaddr.t
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,8 @@ OPENADDR: {
ok(defined($location));
ok(ref($location) eq 'HASH');

my $libpostal_is_installed = 0;

if(eval { require Geo::libpostal; } ) {
Geo::libpostal->import();
$libpostal_is_installed = 1;
}

# FIXME: investigate why these results are different
if($libpostal_is_installed) {
delta_within($location->{latitude}, 40.07, 1e-2);
delta_within($location->{longitude}, -86.27, 1e-2);
} else {
delta_within($location->{latitude}, 39.05, 1e-2);
delta_within($location->{longitude}, -87.04, 1e-2);
}
delta_within($location->{latitude}, 39.05, 1e-2);
delta_within($location->{longitude}, -87.04, 1e-2);

$location = $ogeocoder->geocode('Boswell, Somerset, Pennsylvania, USA');
ok(defined($location));
Expand Down

0 comments on commit fda5c96

Please sign in to comment.