Skip to content

Commit

Permalink
Removed some false positives in Local.pm
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Nov 17, 2020
1 parent 2359170 commit e6d9c4e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Revision history for Geo-Coder-Free
Started work on importing data from OpenStreetMaps
using planet.osm.bz2 files in $OSM_HOME
More work to reduce the number of Maxmind results
Removed some false positives in Local.pm

0.28 Fri Oct 16 21:51:27 EDT 2020
Fixed the manifest
Expand Down
39 changes: 28 additions & 11 deletions lib/Geo/Coder/Free/Local.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use Text::xSV::Slurp;
Geo::Coder::Free::Local -
Provides an interface to locations that you know yourself.
I have found locations by using GPS apps on a smartphone and by
inspecting GeoTagged photographs.
inspecting GeoTagged photographs using
L<https://github.com/nigelhorne/NJH-Snippets/blob/master/bin/geotag>.
=head1 VERSION
Expand Down Expand Up @@ -64,7 +65,7 @@ sub new {
# Geo::Coder::Free::Local->new not Geo::Coder::Free::Local::new
return unless($class);

my $rc = {
bless {
data => xsv_slurp(
shape => 'aoh',
text_csv => {
Expand All @@ -76,9 +77,7 @@ sub new {
},
string => \join('', grep(!/^\s*(#|$)/, <DATA>))
)
};

return bless $rc, $class;
}, $class;
}

=head2 geocode
Expand Down Expand Up @@ -116,6 +115,7 @@ sub geocode {
my $rc = Geo::Location::Point->new($row);
my $str = lc($rc->as_string());

# ::diag("Compare $str->$lc");
if($str eq $lc) {
return $rc;
}
Expand All @@ -126,6 +126,8 @@ sub geocode {
}
}

# ::diag(__PACKAGE__, ': ', __LINE__, ': ', $location);

my $ap;
if(($location =~ /USA$/) || ($location =~ /United States$/)) {
$ap = $self->{'ap'}->{'us'} // Lingua::EN::AddressParse->new(country => 'US', auto_clean => 1, force_case => 1, force_post_code => 0);
Expand Down Expand Up @@ -187,6 +189,7 @@ sub geocode {
}
$addr{'number'} = $c{'property_identifier'};
$addr{'city'} = $c{'suburb'};
# ::diag(Data::Dumper->new([\%addr])->Dump());
if(my $rc = $self->_search(\%addr, ('number', 'road', 'city', 'state', 'country'))) {
return $rc;
}
Expand Down Expand Up @@ -276,7 +279,7 @@ sub geocode {
}

# Finally try libpostal,
# which is good but uses a lot of memory
# which is good but uses a lot of memory and can take a very long time to load
if($libpostal_is_installed == LIBPOSTAL_UNKNOWN) {
if(eval { require Geo::libpostal; } ) {
Geo::libpostal->import();
Expand All @@ -301,7 +304,7 @@ sub geocode {
delete $addr{'state'};
$addr{'country'} = 'GB';
}
# ::diag(__LINE__, ': ', Data::Dumper->new([\%addr])->Dump());
# ::diag(__PACKAGE__, ': ', __LINE__, ': ', Data::Dumper->new([\%addr])->Dump());
if($addr{'country'} && ($addr{'state'} || $addr{'state_district'})) {
if($addr{'country'} =~ /Canada/i) {
$addr{'country'} = 'Canada';
Expand All @@ -325,6 +328,7 @@ sub geocode {
}
}
if(my $rc = $self->_search(\%addr, ('number', 'road', 'city', 'state', 'country'))) {
# ::diag(__PACKAGE__, ': ', __LINE__, ': ', Data::Dumper->new([$rc])->Dump());
return $rc;
}
if($addr{'number'}) {
Expand Down Expand Up @@ -396,11 +400,12 @@ sub _search {

# FIXME: linear search is slow
# ::diag(__LINE__, ': ', Data::Dumper->new([\@columns, $data])->Dump());
# print Data::Dumper->new([\@columns, $data])->Dump();
# my @call_details = caller(0);
print Data::Dumper->new([\@columns, $data])->Dump();
my @call_details = caller(0);
# ::diag(__LINE__, ': called from ', $call_details[2]);
foreach my $row(@{$self->{'data'}}) {
my $match = 1;
my $number_of_columns_matched;

# ::diag(Data::Dumper->new([$self->{data}])->Dump());
# print Data::Dumper->new([$self->{data}])->Dump();
Expand All @@ -417,17 +422,29 @@ sub _search {
$match = 0;
last;
}
$number_of_columns_matched++;
}
}
# ::diag("match: $match");
if($match) {
if($match && ($number_of_columns_matched >= 3)) {
my $confidence;
if($number_of_columns_matched == scalar(@columns)) {
$confidence = 1.0;
} elsif($number_of_columns_matched >= 4) {
$confidence = 0.7;
} else {
$confidence = 0.5;
}
# ::diag("$number_of_columns_matched -> $confidence");
return Geo::Location::Point->new(
'lat' => $row->{'latitude'},
'long' => $row->{'longitude'},
'location' => $data->{'location'}
'location' => $data->{'location'},
'confidence' => $confidence,
);
}
}
return;
}

=head2 reverse_geocode
Expand Down

0 comments on commit e6d9c4e

Please sign in to comment.