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
|
# 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 Digest::MD5 qw(md5_hex);
use Encode;
use JSON::MaybeXS;
use LWP::Simple qw($ua);
use Path::Tiny;
use URI::Escape;
use FixMyStreet::Geocode::Bing;
use FixMyStreet::Geocode::Google;
use FixMyStreet::Geocode::OSM;
use FixMyStreet::Geocode::Zurich;
use Utils;
# lookup STRING CONTEXT
# Given a user-inputted string, try and convert it into co-ordinates using either
# MaPit if it's a postcode, or some web API otherwise. Returns an array of
# data, including an error if there is one. The information in the query may be
# used by cobranded versions of the site to diambiguate locations.
sub lookup {
my ($s, $c) = @_;
my $data = $c->cobrand->geocode_postcode($s);
if (defined $data->{latitude}) {
( $data->{latitude}, $data->{longitude} ) =
map { Utils::truncate_coordinate($_) }
( $data->{latitude}, $data->{longitude} );
}
$data = string($s, $c)
unless $data->{error} || defined $data->{latitude};
$data->{error} = _('Sorry, we could not find that location.')
unless $data->{error} || defined $data->{latitude};
return ( $data->{latitude}, $data->{longitude}, $data->{error} );
}
# string STRING CONTEXT
# Passes the string to some external API to look stuff up.
sub string {
my ($s, $c) = @_;
my $service = $c->cobrand->get_geocoder($c);
$service = $service->{type} if ref $service;
$service = 'OSM' unless $service =~ /^(Bing|Google|OSM|Zurich)$/;
$service = 'OSM' if $service eq 'Bing' && !FixMyStreet->config('BING_MAPS_API_KEY');
$service = "FixMyStreet::Geocode::${service}::string";
no strict 'refs';
return &$service($s, $c);
}
# escape STRING CONTEXT
# Escapes string for putting in URL geocoding call
sub escape {
my ($s, $c) = @_;
$s = lc($s);
$s =~ s/[^-&\w ']/ /g;
$s =~ s/\s+/ /g;
$s = URI::Escape::uri_escape_utf8($s);
$s =~ s/%20/+/g;
return $s;
}
sub cache {
my ($type, $url, $args, $re) = @_;
my $cache_dir = path(FixMyStreet->config('GEO_CACHE'), $type)->absolute(FixMyStreet->path_to());
my $cache_file = $cache_dir->child(md5_hex($url));
my $js;
if (-s $cache_file && -M $cache_file <= 7 && !FixMyStreet->config('STAGING_SITE')) {
$js = $cache_file->slurp;
} else {
$url .= '&' . $args if $args;
$ua->timeout(15);
$js = LWP::Simple::get($url);
$js = encode_utf8($js) if utf8::is_utf8($js);
$cache_dir->mkpath;
if ($js && (!$re || $js !~ $re) && !FixMyStreet->config('STAGING_SITE')) {
$cache_file->spew($js);
}
}
$js = JSON->new->utf8->allow_nonref->decode($js) if $js;
return $js;
}
1;
|