diff options
author | Matthew Somerville <matthew@mysociety.org> | 2020-06-23 14:32:42 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2020-06-25 09:35:02 +0100 |
commit | d1c38a2bdbe5891fa63bde40e398e3cc33881566 (patch) | |
tree | a3be291dccfde91d912e3ff5b4e3e3635e9aeb92 | |
parent | 2b462f20d2479649c14efead2680a49543ca32d0 (diff) |
Improve Bing geocoder results.
Add a couple of parameters to hopefully improve results, and make
sure the returned locality is included in the summary address.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | perllib/FixMyStreet/Geocode/Bing.pm | 10 | ||||
-rw-r--r-- | t/Mock/Bing.pm | 30 | ||||
-rw-r--r-- | t/geocode/bing.t | 24 |
4 files changed, 64 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 968d00efb..8f8fc5ef0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Add Open Location Codes support to search box. #3047 - Front end improvements: - Add lazy image loading on list items. + - Improve Bing geocoder results. - Changes: - Mark user as active when sent an email alert. - Bugfixes: diff --git a/perllib/FixMyStreet/Geocode/Bing.pm b/perllib/FixMyStreet/Geocode/Bing.pm index 1d39d911f..1b947abee 100644 --- a/perllib/FixMyStreet/Geocode/Bing.pm +++ b/perllib/FixMyStreet/Geocode/Bing.pm @@ -36,6 +36,8 @@ sub string { $url .= '&userMapView=' . join(',', @{$params->{bounds}}) if $params->{bounds}; $url .= '&userLocation=' . $params->{centre} if $params->{centre}; + $url .= '&userIp=127.0.0.1'; # So server location does not affect results + $url .= '&maxResults=10'; # Match what is said in the front end $url .= '&c=' . $params->{bing_culture} if $params->{bing_culture}; $c->stash->{geocoder_url} = $url; @@ -52,7 +54,13 @@ sub string { foreach (@$results) { my $address = $_->{name}; - next if $params->{bing_country} && $_->{address}->{countryRegion} ne $params->{bing_country}; + if ($params->{bing_country}) { + next if $_->{address}->{countryRegion} ne $params->{bing_country}; + $address =~ s/, $params->{bing_country}$//; + } + if ($address !~ /$_->{address}->{locality}/) { + $address .= ", $_->{address}->{locality}"; + } # Getting duplicate, yet different, results from Bing sometimes next if @valid_locations diff --git a/t/Mock/Bing.pm b/t/Mock/Bing.pm index 3dfb8fbe0..77045950f 100644 --- a/t/Mock/Bing.pm +++ b/t/Mock/Bing.pm @@ -14,6 +14,36 @@ has json => ( sub dispatch_request { my $self = shift; + sub (GET + /REST/v1/Locations + ?*) { + my ($self, $query) = @_; + my $results = [ { + point => { coordinates => [ 51, -1 ] }, + name => 'Constitution Hill, London, SW1A', + address => { + addressLine => 'Constitution Hill', + locality => 'London', + countryRegion => 'United Kingdom', + } + } ]; + if ($query->{q} =~ /two results/) { + push @$results, { + point => { coordinates => [ 51, -1 ] }, + name => 'Constitution Hill again, United Kingdom', + address => { + addressLine => 'Constitution Hill again', + locality => 'London', + countryRegion => 'United Kingdom', + } + }; + } + my $data = { + statusCode => 200, + resourceSets => [ { resources => $results } ], + }; + my $json = $self->json->encode($data); + return [ 200, [ 'Content-Type' => 'application/json' ], [ $json ] ]; + }, + sub (GET + /REST/v1/Locations/* + ?*) { my ($self, $location, $query) = @_; my $data = { diff --git a/t/geocode/bing.t b/t/geocode/bing.t new file mode 100644 index 000000000..32cffaa4e --- /dev/null +++ b/t/geocode/bing.t @@ -0,0 +1,24 @@ +use FixMyStreet::Test; +use FixMyStreet::Geocode::Bing; +use Catalyst::Test 'FixMyStreet::App'; +use t::Mock::Bing; + +my $c = ctx_request('/'); + +FixMyStreet::override_config { + GEOCODING_DISAMBIGUATION => { bing_culture => 'en-GB' } +}, sub { + my $r = FixMyStreet::Geocode::Bing->string('a result', $c); + ok $r->{latitude}; + ok $r->{longitude}; +}; + +FixMyStreet::override_config { + GEOCODING_DISAMBIGUATION => { bing_country => 'United Kingdom' } +}, sub { + my $r = FixMyStreet::Geocode::Bing->string('two results', $c); + is scalar @{$r->{error}}, 2; + is $r->{error}[1]{address}, 'Constitution Hill again, London'; +}; + +done_testing; |