Skip to content

Commit

Permalink
Handle systems without libpostal
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Oct 8, 2020
1 parent 60bf65c commit 255f357
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions lib/Geo/Coder/Free/OpenAddresses.pm
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,15 @@ sub geocode {
}
$addr{'house_number'} = $c{'property_identifier'};
$addr{'city'} = $c{'suburb'};
if(my $rc = $self->_search(\%addr, ('house_number', 'road', 'city', 'state', 'country'))) {
return $rc;
}
# ::diag(Data::Dumper->new([\%addr])->Dump());
if($addr{'house_number'}) {
if(my $rc = $self->_search(\%addr, ('road', 'city', 'state', 'country'))) {
if(my $rc = $self->_search(\%addr, ('house_number', 'road', 'city', 'state', 'country'))) {
return $rc;
}
}
if(my $rc = $self->_search(\%addr, ('road', 'city', 'state', 'country'))) {
return $rc;
}
}
}

Expand All @@ -361,6 +362,7 @@ sub geocode {

# Work around for RT#122617
if(($location !~ /\sCounty,/i) && (my $href = (Geo::StreetAddress::US->parse_location($l) || Geo::StreetAddress::US->parse_address($l)))) {
# ::diag(Data::Dumper->new([$href])->Dump());
if($state = $href->{'state'}) {
if(length($state) > 2) {
if(my $twoletterstate = Locale::US->new()->{state2code}{uc($state)}) {
Expand Down Expand Up @@ -406,12 +408,30 @@ sub geocode {
}
}
if(length($state) == 2) {
$addr[1] = Geo::Coder::Free::_normalize($addr[1]);
# ::diag(Data::Dumper->new([\@addr])->Dump());
if(my $rc = $self->_get($addr[0], $addr[1], $addr[2], $state, 'US')) {
# ::diag(Data::Dumper->new([$rc])->Dump());
$rc->{'country'} = 'US';
return $rc;
}
}
# Hack to find "street, town, county, state, US"
if(length($state) == 2) {
$addr[0] = Geo::Coder::Free::_normalize($addr[0]);
$addr[2] =~ s/\s+COUNTY$//i;
# ::diag(Data::Dumper->new([\@addr])->Dump());
if(my $rc = $self->_get($addr[0], $addr[1], $addr[2], $state, 'US')) {
# ::diag(Data::Dumper->new([$rc])->Dump());
$rc->{'country'} = 'US';
return $rc;
}
if(my $rc = $self->_get($addr[0], $addr[1], $state, 'US')) {
# ::diag(Data::Dumper->new([$rc])->Dump());
$rc->{'country'} = 'US';
return $rc;
}
}
}
}

Expand Down Expand Up @@ -496,10 +516,12 @@ sub geocode {
return $rc;
}
}
# ::diag("$street$city$state", 'US');
if($rc = $self->_get("$street$city$state", 'US')) {
$rc->{'country'} = 'US';
return $rc;
}
# ::diag("$fullstreet$city$state", 'US');
if($rc = $self->_get("$fullstreet$city$state", 'US')) {
$rc->{'country'} = 'US';
return $rc;
Expand Down Expand Up @@ -769,6 +791,7 @@ sub geocode {

# Finally try libpostal,
# which is good but uses a lot of memory
# ::diag("try libpostal on $location");
if($libpostal_is_installed == LIBPOSTAL_UNKNOWN) {
if(eval { require Geo::libpostal; } ) {
Geo::libpostal->import();
Expand Down

0 comments on commit 255f357

Please sign in to comment.