1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#!/usr/bin/perl
#
# FixMyStreet::Geocode
# The geocoding functions for FixMyStreet.
#
# Copyright (c) 2010 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org; WWW: http://www.mysociety.org/
package FixMyStreet::Geocode;
use strict;
use Encode;
use Error qw(:try);
use File::Slurp;
use File::Path ();
use LWP::Simple;
use Digest::MD5 qw(md5_hex);
use URI::Escape;
use Cobrand;
use Page;
use Utils;
use mySociety::Config;
use mySociety::Locale;
use mySociety::MaPit;
use mySociety::PostcodeUtil;
use mySociety::Web qw(NewURL);
BEGIN {
(my $dir = __FILE__) =~ s{/[^/]*?$}{};
mySociety::Config::set_file("$dir/../../conf/general");
}
# lookup STRING QUERY
# Given a user-inputted string, try and convert it into co-ordinates using either
# MaPit if it's a postcode, or Google Maps API otherwise. Returns an array of
# data, including an error if there is one (which includes a location being in
# Northern Ireland). The information in the query may be used by cobranded versions
# of the site to diambiguate locations.
sub lookup {
my ($s, $c) = @_;
my ($latitude, $longitude, $error);
if (mySociety::Config::get('COUNTRY') eq 'GB') {
if ($s =~ /^\d+$/) {
$error = 'FixMyStreet is a UK-based website that currently works in England, Scotland, and Wales. Please enter either a postcode, or a Great British street name and area.';
} elsif (mySociety::PostcodeUtil::is_valid_postcode($s)) {
my $location = mySociety::MaPit::call('postcode', $s);
unless ($error = mapit_check_error($location)) {
$latitude = $location->{wgs84_lat};
$longitude = $location->{wgs84_lon};
}
}
} elsif (mySociety::Config::get('COUNTRY') eq 'NO') {
if ($s =~ /^\d{4}$/) {
my $location = mySociety::MaPit::call('postcode', $s);
unless ($error = mapit_check_error($location)) {
$latitude = $location->{wgs84_lat};
$longitude = $location->{wgs84_lon};
}
}
}
unless ($error || defined $latitude) {
($latitude, $longitude, $error) = FixMyStreet::Geocode::string($s, $c);
}
return ($latitude, $longitude, $error);
}
sub geocoded_string_coordinates {
my ( $js ) = @_;
my ($latitude, $longitude, $error);
my ($accuracy) = $js =~ /"Accuracy" *: *(\d)/;
if ($accuracy < 4) {
$error = _('Sorry, that location appears to be too general; please be more specific.');
} elsif ( $js =~ /"coordinates" *: *\[ *(.*?), *(.*?),/ ) {
$longitude = $1;
$latitude = $2;
if (mySociety::Config::get('COUNTRY') eq 'GB') {
try {
my ($easting, $northing) = Utils::convert_latlon_to_en( $latitude, $longitude );
} catch Error::Simple with {
mySociety::Locale::pop(); # We threw exception, so it won't have happened.
$error = shift;
$error = _('That location does not appear to be in Britain; please try again.')
if $error =~ /out of the area covered/;
}
}
}
return ($latitude, $longitude, $error);
}
sub results_check {
my $c = shift;
my ($error, @valid_locations);
foreach (@_) {
next unless /"address" *: *"(.*?)"/s;
my $address = $1;
next unless $c->cobrand->geocoded_string_check( $address );
next if $address =~ /BT\d/;
push (@$error, $address);
push (@valid_locations, $_);
}
if (scalar @valid_locations == 1) {
return geocoded_string_coordinates( $valid_locations[0] );
}
$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 (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, $c) = @_;
$s = lc($s);
$s =~ s/[^-&\w ']/ /g;
$s =~ s/\s+/ /g;
$s = URI::Escape::uri_escape_utf8($s);
$s = $c->cobrand->disambiguate_location( "q=$s" );
$s =~ s/%20/+/g;
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);
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 ( mySociety::Config::get('COUNTRY') eq 'GB'
&& $url !~ /,\+UK/
&& $url !~ /united\++kingdom$/ )
{
if ( $url =~ /&/ ) {
$url =~ s/&/,+UK&/;
} else {
$url .= ',+UK';
}
}
$url .= '&sensor=false&key=' . mySociety::Config::get('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/;
}
if (!$js) {
$error = _('Sorry, we could not parse that location. Please try again.');
} elsif ($js !~ /"code" *: *200/) {
$error = _('Sorry, we could not find that location.');
} elsif ($js =~ /}, *{/) { # Multiple
return results_check($c, (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 {
return results_check($c, $js);
}
return (undef, undef, $error);
}
sub mapit_check_error {
my $location = shift;
if ($location->{error}) {
return _('That postcode was not recognised, sorry.') if $location->{code} =~ /^4/;
return $location->{error};
}
if (mySociety::Config::get('COUNTRY') eq 'GB') {
my $island = $location->{coordsyst};
if (!$island) {
return _("Sorry, that appears to be a Crown dependency postcode, which we don't cover.");
}
if ($island eq 'I') {
return _("We do not cover Northern Ireland, I'm afraid, as our licence doesn't include any maps for the region.");
}
}
return 0;
}
1;
|