aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Geocode
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/Geocode')
-rw-r--r--perllib/FixMyStreet/Geocode/Bing.pm64
-rw-r--r--perllib/FixMyStreet/Geocode/FixaMinGata.pm64
-rw-r--r--perllib/FixMyStreet/Geocode/Google.pm62
-rw-r--r--perllib/FixMyStreet/Geocode/OSM.pm41
-rw-r--r--perllib/FixMyStreet/Geocode/Zurich.pm20
5 files changed, 71 insertions, 180 deletions
diff --git a/perllib/FixMyStreet/Geocode/Bing.pm b/perllib/FixMyStreet/Geocode/Bing.pm
index 702e19814..d7db10ae6 100644
--- a/perllib/FixMyStreet/Geocode/Bing.pm
+++ b/perllib/FixMyStreet/Geocode/Bing.pm
@@ -7,13 +7,8 @@
package FixMyStreet::Geocode::Bing;
use strict;
-use Encode;
-use File::Slurp;
-use File::Path ();
-use LWP::Simple;
-use Digest::MD5 qw(md5_hex);
-use mySociety::Locale;
+use Utils;
# string STRING CONTEXT
# Looks up on Bing Maps API, and caches, a user-inputted location.
@@ -34,24 +29,10 @@ sub string {
$url .= '&userLocation=' . $params->{centre} if $params->{centre};
$url .= '&c=' . $params->{bing_culture} if $params->{bing_culture};
- my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'bing/';
- my $cache_file = $cache_dir . md5_hex($url);
- my $js;
- if (-s $cache_file) {
- $js = File::Slurp::read_file($cache_file);
- } else {
- $url .= '&key=' . FixMyStreet->config('BING_MAPS_API_KEY');
- $js = LWP::Simple::get($url);
- $js = encode_utf8($js) if utf8::is_utf8($js);
- File::Path::mkpath($cache_dir);
- File::Slurp::write_file($cache_file, $js) if $js;
- }
-
+ my $js = FixMyStreet::Geocode::cache('bing', $url, 'key=' . FixMyStreet->config('BING_MAPS_API_KEY'));
if (!$js) {
return { error => _('Sorry, we could not parse that location. Please try again.') };
}
-
- $js = JSON->new->utf8->allow_nonref->decode($js);
if ($js->{statusCode} ne '200') {
return { error => _('Sorry, we could not find that location.') };
}
@@ -71,15 +52,14 @@ sub string {
|| $valid_locations[-1]{address}{locality} eq $_->{address}{locality}
);
- ( $latitude, $longitude ) = @{ $_->{point}->{coordinates} };
- # These co-ordinates are output as query parameters in a URL, make sure they have a "."
- mySociety::Locale::in_gb_locale {
- push (@$error, {
- address => $address,
- latitude => sprintf('%0.6f', $latitude),
- longitude => sprintf('%0.6f', $longitude)
- });
- };
+ ( $latitude, $longitude ) =
+ map { Utils::truncate_coordinate($_) }
+ @{ $_->{point}->{coordinates} };
+ push (@$error, {
+ address => $address,
+ latitude => $latitude,
+ longitude => $longitude
+ });
push (@valid_locations, $_);
}
@@ -88,33 +68,15 @@ sub string {
}
sub reverse {
- my ( $latitude, $longitude, $bing_culture, $cache ) = @_;
+ my ( $latitude, $longitude, $bing_culture ) = @_;
# Get nearest road-type thing from Bing
my $key = mySociety::Config::get('BING_MAPS_API_KEY', '');
if ($key) {
my $url = "http://dev.virtualearth.net/REST/v1/Locations/$latitude,$longitude?key=$key";
$url .= '&c=' . $bing_culture if $bing_culture;
- my $j;
- if ( $cache ) {
- my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'bing/';
- my $cache_file = $cache_dir . md5_hex($url);
-
- if (-s $cache_file) {
- $j = File::Slurp::read_file($cache_file);
- } else {
- $j = LWP::Simple::get($url);
- File::Path::mkpath($cache_dir);
- File::Slurp::write_file($cache_file, $j) if $j;
- }
- } else {
- $j = LWP::Simple::get($url);
- }
-
- if ($j) {
- $j = JSON->new->utf8->allow_nonref->decode($j);
- return $j;
- }
+ my $j = FixMyStreet::Geocode::cache('bing', $url);
+ return $j if $j;
}
return undef;
diff --git a/perllib/FixMyStreet/Geocode/FixaMinGata.pm b/perllib/FixMyStreet/Geocode/FixaMinGata.pm
index 2ea92c422..3ad98b148 100644
--- a/perllib/FixMyStreet/Geocode/FixaMinGata.pm
+++ b/perllib/FixMyStreet/Geocode/FixaMinGata.pm
@@ -14,16 +14,11 @@ package FixMyStreet::Geocode::FixaMinGata;
use warnings;
use strict;
-use Data::Dumper;
-use Digest::MD5 qw(md5_hex);
-use Encode;
-use File::Slurp;
-use File::Path ();
-use LWP::Simple qw($ua);
+use LWP::Simple;
use Memcached;
use XML::Simple;
-use mySociety::Locale;
+use Utils;
my $osmapibase = "http://www.openstreetmap.org/api/";
my $nominatimbase = "http://nominatim.openstreetmap.org/";
@@ -45,8 +40,8 @@ sub string {
my %query_params = (
q => $s,
format => 'json',
- addressdetails => 1,
- limit => 20,
+ addressdetails => 1,
+ limit => 20,
#'accept-language' => '',
email => 'info' . chr(64) . 'morus.se',
);
@@ -56,53 +51,32 @@ sub string {
if $params->{country};
$url .= join('&', map { "$_=$query_params{$_}" } keys %query_params);
- my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'osm/';
- my $cache_file = $cache_dir . md5_hex($url);
- my $js;
- if (-s $cache_file) {
- $js = File::Slurp::read_file($cache_file);
- } else {
- $ua->timeout(15);
- $js = LWP::Simple::get($url);
- $js = encode_utf8($js) if utf8::is_utf8($js);
- File::Path::mkpath($cache_dir);
- File::Slurp::write_file($cache_file, $js) if $js;
- }
-
+ my $js = FixMyStreet::Geocode::cache('osm', $url);
if (!$js) {
return { error => _('Sorry, we could not find that location.') };
}
- $js = JSON->new->utf8->allow_nonref->decode($js);
-
my ( %locations, $error, @valid_locations, $latitude, $longitude );
foreach (@$js) {
- # These co-ordinates are output as query parameters in a URL, make sure they have a "."
- next if $_->{class} eq "boundary";
-
- my @s = split(/,/, $_->{display_name});
-
- my $address = join(",", @s[0,1,2]);
-
+ next if $_->{class} eq "boundary";
+ my @s = split(/,/, $_->{display_name});
+ my $address = join(",", @s[0,1,2]);
$locations{$address} = [$_->{lat}, $_->{lon}];
}
- my ($key) = keys %locations;
-
- return { latitude => $locations{$key}[0], longitude => $locations{$key}[1] } if scalar keys %locations == 1;
- return { error => _('Sorry, we could not find that location.') } if scalar keys %locations == 0;
-
- foreach $key (keys %locations) {
- ( $latitude, $longitude ) = ($locations{$key}[0], $locations{$key}[1]);
- mySociety::Locale::in_gb_locale {
- push (@$error, {
- address => $key,
- latitude => sprintf('%0.6f', $latitude),
- longitude => sprintf('%0.6f', $longitude)
- });
- };
+ foreach my $key (keys %locations) {
+ ( $latitude, $longitude ) =
+ map { Utils::truncate_coordinate($_) }
+ ($locations{$key}[0], $locations{$key}[1]);
+ push (@$error, {
+ address => $key,
+ latitude => $latitude,
+ longitude => $longitude
+ });
+ push (@valid_locations, $_);
}
+ return { latitude => $latitude, longitude => $longitude } if scalar @valid_locations == 1;
return { error => $error };
}
diff --git a/perllib/FixMyStreet/Geocode/Google.pm b/perllib/FixMyStreet/Geocode/Google.pm
index 11ff8ef80..35fcec36f 100644
--- a/perllib/FixMyStreet/Geocode/Google.pm
+++ b/perllib/FixMyStreet/Geocode/Google.pm
@@ -7,12 +7,7 @@
package FixMyStreet::Geocode::Google;
use strict;
-use Encode;
-use File::Slurp;
-use File::Path ();
-use LWP::Simple;
-use Digest::MD5 qw(md5_hex);
-use mySociety::Locale;
+use Utils;
# string STRING CONTEXT
# Looks up on Google Maps API, and caches, a user-inputted location.
@@ -24,6 +19,13 @@ sub string {
my $params = $c->cobrand->disambiguate_location($s);
+ # For some reason adding gl=uk is no longer sufficient to make google
+ # think we are in the UK for some locations so we explictly add UK to
+ # the address.
+ if ($c->cobrand->country eq 'GB' && $s !~ /, *UK/ && $s !~ /united *kingdom$/) {
+ $s .= ', UK';
+ }
+
$s = FixMyStreet::Geocode::escape($s);
my $url = 'http://maps.google.com/maps/geo?q=' . $s;
@@ -36,38 +38,11 @@ sub string {
}
$url .= '&hl=' . $params->{lang} if $params->{lang};
- my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'google/';
- my $cache_file = $cache_dir . md5_hex($url);
- my $js;
- if (-s $cache_file) {
- $js = File::Slurp::read_file($cache_file);
- } else {
- # For some reason adding gl=uk is no longer sufficient to make google
- # think we are in the UK for some locations so we explictly add UK to
- # the address. We do it here so as not to invalidate existing cache
- # entries
- if ( $c->cobrand->country eq 'GB'
- && $url !~ /,\+UK/
- && $url !~ /united\++kingdom$/ )
- {
- if ( $url =~ /&/ ) {
- $url =~ s/&/,+UK&/;
- } else {
- $url .= ',+UK';
- }
- }
- $url .= '&sensor=false&key=' . FixMyStreet->config('GOOGLE_MAPS_API_KEY');
- $js = LWP::Simple::get($url);
- $js = encode_utf8($js) if utf8::is_utf8($js);
- File::Path::mkpath($cache_dir);
- File::Slurp::write_file($cache_file, $js) if $js && $js !~ /"code":6[12]0/;
- }
-
+ my $args = 'sensor=false&key=' . FixMyStreet->config('GOOGLE_MAPS_API_KEY');
+ my $js = FixMyStreet::Geocode::cache('google', $url, $args, qr/"code":6[12]0/);
if (!$js) {
return { error => _('Sorry, we could not parse that location. Please try again.') };
}
-
- $js = JSON->new->utf8->allow_nonref->decode($js);
if ($js->{Status}->{code} ne '200') {
return { error => _('Sorry, we could not find that location.') };
}
@@ -78,15 +53,14 @@ sub string {
next unless $_->{AddressDetails}->{Accuracy} >= 4;
my $address = $_->{address};
next unless $c->cobrand->geocoded_string_check( $address );
- ( $longitude, $latitude ) = @{ $_->{Point}->{coordinates} };
- # These co-ordinates are output as query parameters in a URL, make sure they have a "."
- mySociety::Locale::in_gb_locale {
- push (@$error, {
- address => $address,
- latitude => sprintf('%0.6f', $latitude),
- longitude => sprintf('%0.6f', $longitude)
- });
- };
+ ( $longitude, $latitude ) =
+ map { Utils::truncate_coordinate($_) }
+ @{ $_->{Point}->{coordinates} };
+ push (@$error, {
+ address => $address,
+ latitude => $latitude,
+ longitude => $longitude
+ });
push (@valid_locations, $_);
}
return { latitude => $latitude, longitude => $longitude } if scalar @valid_locations == 1;
diff --git a/perllib/FixMyStreet/Geocode/OSM.pm b/perllib/FixMyStreet/Geocode/OSM.pm
index 919940f78..f165963d7 100644
--- a/perllib/FixMyStreet/Geocode/OSM.pm
+++ b/perllib/FixMyStreet/Geocode/OSM.pm
@@ -9,14 +9,10 @@ package FixMyStreet::Geocode::OSM;
use warnings;
use strict;
-use Digest::MD5 qw(md5_hex);
-use Encode;
-use File::Slurp;
-use File::Path ();
-use LWP::Simple qw($ua);
+use LWP::Simple;
use Memcached;
use XML::Simple;
-use mySociety::Locale;
+use Utils;
my $osmapibase = "http://www.openstreetmap.org/api/";
my $nominatimbase = "http://nominatim.openstreetmap.org/";
@@ -47,36 +43,21 @@ sub string {
if $params->{country};
$url .= join('&', map { "$_=$query_params{$_}" } keys %query_params);
- my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'osm/';
- my $cache_file = $cache_dir . md5_hex($url);
- my $js;
- if (-s $cache_file) {
- $js = File::Slurp::read_file($cache_file);
- } else {
- $ua->timeout(15);
- $js = LWP::Simple::get($url);
- $js = encode_utf8($js) if utf8::is_utf8($js);
- File::Path::mkpath($cache_dir);
- File::Slurp::write_file($cache_file, $js) if $js;
- }
-
+ my $js = FixMyStreet::Geocode::cache('osm', $url);
if (!$js) {
return { error => _('Sorry, we could not find that location.') };
}
- $js = JSON->new->utf8->allow_nonref->decode($js);
-
my ( $error, @valid_locations, $latitude, $longitude );
foreach (@$js) {
- # These co-ordinates are output as query parameters in a URL, make sure they have a "."
- ( $latitude, $longitude ) = ( $_->{lat}, $_->{lon} );
- mySociety::Locale::in_gb_locale {
- push (@$error, {
- address => $_->{display_name},
- latitude => sprintf('%0.6f', $latitude),
- longitude => sprintf('%0.6f', $longitude)
- });
- };
+ ( $latitude, $longitude ) =
+ map { Utils::truncate_coordinate($_) }
+ ( $_->{lat}, $_->{lon} );
+ push (@$error, {
+ address => $_->{display_name},
+ latitude => $latitude,
+ longitude => $longitude
+ });
push (@valid_locations, $_);
}
diff --git a/perllib/FixMyStreet/Geocode/Zurich.pm b/perllib/FixMyStreet/Geocode/Zurich.pm
index 1f0b4fc16..aad918b0e 100644
--- a/perllib/FixMyStreet/Geocode/Zurich.pm
+++ b/perllib/FixMyStreet/Geocode/Zurich.pm
@@ -15,7 +15,7 @@ use Digest::MD5 qw(md5_hex);
use File::Path ();
use Geo::Coordinates::CH1903;
use Storable;
-use mySociety::Locale;
+use Utils;
my ($soap, $method, $security);
@@ -66,7 +66,7 @@ sub string {
my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'zurich/';
my $cache_file = $cache_dir . md5_hex($s);
my $result;
- if (-s $cache_file) {
+ if (-s $cache_file && -M $cache_file <= 7) {
$result = retrieve($cache_file);
} else {
my $search = SOAP::Data->name('search' => $s)->type('');
@@ -92,14 +92,14 @@ sub string {
my ( $error, @valid_locations, $latitude, $longitude );
foreach (@$results) {
- ($latitude, $longitude) = Geo::Coordinates::CH1903::to_latlon($_->{easting}, $_->{northing});
- mySociety::Locale::in_gb_locale {
- push (@$error, {
- address => $_->{text},
- latitude => sprintf('%0.6f', $latitude),
- longitude => sprintf('%0.6f', $longitude)
- });
- };
+ ($latitude, $longitude) =
+ map { Utils::truncate_coordinate($_) }
+ Geo::Coordinates::CH1903::to_latlon($_->{easting}, $_->{northing});
+ push (@$error, {
+ address => $_->{text},
+ latitude => $latitude,
+ longitude => $longitude
+ });
push (@valid_locations, $_);
last if lc($_->{text}) eq lc($s);
}