aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Geocode.pm
diff options
context:
space:
mode:
authorEdmund von der Burg <evdb@mysociety.org>2011-04-04 12:24:03 +0100
committerEdmund von der Burg <evdb@mysociety.org>2011-04-04 12:24:03 +0100
commit8fa7e52025478515203b39f3e7afafb829b60731 (patch)
tree295fb14df87abc4577418cfd294931cc51f7c991 /perllib/FixMyStreet/Geocode.pm
parent689e4c3bd7c7c1cb24ecad3b35a57947bfb7945b (diff)
parente591ef105c5996e95f2e7738b324c25b3708f0be (diff)
Merge branch 'master' into migrate_to_catalyst
Conflicts: .gitignore commonlib conf/packages perllib/FixMyStreet/Map.pm templates/website/cobrands/barnet/footer web/confirm.cgi web/css/core.css web/import.cgi web/index.cgi
Diffstat (limited to 'perllib/FixMyStreet/Geocode.pm')
-rw-r--r--perllib/FixMyStreet/Geocode.pm55
1 files changed, 33 insertions, 22 deletions
diff --git a/perllib/FixMyStreet/Geocode.pm b/perllib/FixMyStreet/Geocode.pm
index 803e32a85..c06c3bb55 100644
--- a/perllib/FixMyStreet/Geocode.pm
+++ b/perllib/FixMyStreet/Geocode.pm
@@ -50,6 +50,14 @@ sub lookup {
$longitude = $location->{wgs84_lon};
}
}
+ } elsif (mySociety::Config::get('COUNTRY') eq 'NO') {
+ if ($s =~ /^\d{4}$/) {
+ my $location = mySociety::MaPit::call('postcode', $s);
+ unless ($error = Page::mapit_check_error($location)) {
+ $latitude = $location->{wgs84_lat};
+ $longitude = $location->{wgs84_lon};
+ }
+ }
}
unless ($error || defined $latitude) {
($latitude, $longitude, $error) = FixMyStreet::Geocode::string($s, $q);
@@ -80,15 +88,31 @@ sub geocoded_string_coordinates {
return ($latitude, $longitude, $error);
}
+sub results_check {
+ my $q = shift;
+ my ($error, @valid_locations);
+ foreach (@_) {
+ next unless /"address" *: *"(.*?)"/s;
+ my $address = $1;
+ next unless Cobrand::geocoded_string_check(Page::get_cobrand($q), $address, $q);
+ next if $address =~ /BT\d/;
+ push (@$error, $address);
+ push (@valid_locations, $_);
+ }
+ if (scalar @valid_locations == 1) {
+ return geocoded_string_coordinates($valid_locations[0], $q);
+ }
+ $error = _('Sorry, we could not find that location.') unless $error;
+ return (undef, undef, $error);
+}
+
# string STRING QUERY
# Canonicalises, looks up on Google Maps API, and caches, a user-inputted location.
-# Returns array of (TILE_X, TILE_Y, EASTING, NORTHING, ERROR), where ERROR is
-# either undef, a string, or an array of matches if there are more than one. The
-# information in the query may be used to disambiguate the location in cobranded versions
-# of the site.
+# Returns array of (LAT, LON, ERROR), where ERROR is either undef, a string, or
+# an array of matches if there are more than one. The information in the query
+# may be used to disambiguate the location in cobranded versions of the site.
sub string {
my ($s, $q) = @_;
- $s = decode_utf8($s);
$s = lc($s);
$s =~ s/[^-&\w ']/ /g;
$s =~ s/\s+/ /g;
@@ -98,7 +122,7 @@ sub string {
my $url = 'http://maps.google.com/maps/geo?' . $s;
my $cache_dir = mySociety::Config::get('GEO_CACHE');
my $cache_file = $cache_dir . md5_hex($url);
- my ($js, $error, $latitude, $longitude);
+ my ($js, $error);
if (-s $cache_file) {
$js = File::Slurp::read_file($cache_file);
} else {
@@ -115,27 +139,14 @@ sub string {
} elsif ($js !~ /"code" *: *200/) {
$error = _('Sorry, we could not find that location.');
} elsif ($js =~ /}, *{/) { # Multiple
- my @js = split /}, *{/, $js;
- my @valid_locations;
- foreach (@js) {
- next unless /"address" *: *"(.*?)"/s;
- my $address = $1;
- next unless Cobrand::geocoded_string_check(Page::get_cobrand($q), $address, $q);
- next if $address =~ /BT\d/;
- push (@valid_locations, $_);
- push (@$error, $address);
- }
- if (scalar @valid_locations == 1) {
- return geocoded_string_coordinates($valid_locations[0], $q);
- }
- $error = _('Sorry, we could not find that location.') unless $error;
+ return results_check($q, (split /}, *{/, $js));
} elsif ($js =~ /BT\d/) {
# Northern Ireland, hopefully
$error = _("We do not cover Northern Ireland, I'm afraid, as our licence doesn't include any maps for the region.");
} else {
- ($latitude, $longitude, $error) = geocoded_string_coordinates($js, $q);
+ return results_check($q, $js);
}
- return ($latitude, $longitude, $error);
+ return (undef, undef, $error);
}
# list_choices