| 1 | package Geo::Coder::Postcodes; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | # One definition of what counts as a location: Geo::Location::Predicate holds | ||||||||||||||||||||||||||||
| 9 | # the rule the facade and its backends apply, so what is refused here is refused | ||||||||||||||||||||||||||||
| 10 | # there for the same reason. It is called by name rather than imported - an | ||||||||||||||||||||||||||||
| 11 | # import copies the code reference into this package, and then a test could not | ||||||||||||||||||||||||||||
| 12 | # swap the rule for a spy to prove that it is the rule being consulted. | ||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||
| 14 | # The rule is an optional dependency, not a prerequisite: it is a distribution of | ||||||||||||||||||||||||||||
| 15 | # its own (Geo-Location-Predicate), declared in Makefile.PL under META_MERGE's | ||||||||||||||||||||||||||||
| 16 | # recommends, so an installer fetches it by default and a standalone install that | ||||||||||||||||||||||||||||
| 17 | # leaves it out still gets the one answer this module can give alone - a | ||||||||||||||||||||||||||||
| 18 | # reference is refused. It is looked for when it is first needed rather than | ||||||||||||||||||||||||||||
| 19 | # when this module is loaded, so importing this module reads no file it may not | ||||||||||||||||||||||||||||
| 20 | # need, and a rule installed after this module is loaded is still found. | ||||||||||||||||||||||||||||
| 21 | 7 | my $HAVE_RULE; # undef until the rule is first looked for | |||||||||||||||||||||||||||
| 22 | 7 | ▶ | use Params::Get 0.13; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 7 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 7 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 7 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 7 | ▶ | use LWP::Protocol::https; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 7 | ▶ | use Object::Configure 0.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 7 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 NAME | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Geo::Coder::Postcodes - Provides a geocoding functionality using L<https://postcodes.io>. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 VERSION | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | Version 0.09 | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 7 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | use Geo::Coder::Postcodes; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | my $geo_coder = Geo::Coder::Postcodes->new(); | ||||||||||||||||||||||||||||
| 47 | my $location = $geo_coder->geocode(location => 'Margate'); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Geo::Coder::Postcodes provides an interface to postcodes.io, | ||||||||||||||||||||||||||||
| 52 | a free Geo-Coder database covering the towns in the UK. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head1 METHODS | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head2 new | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | $geo_coder = Geo::Coder::Postcodes->new(); | ||||||||||||||||||||||||||||
| 59 | my $ua = LWP::UserAgent->new(); | ||||||||||||||||||||||||||||
| 60 | $ua->env_proxy(1); | ||||||||||||||||||||||||||||
| 61 | $geo_coder = Geo::Coder::Postcodes->new(ua => $ua); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 16 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 16 | my $class = shift; | |||||||||||||||||||||||||||
| 67 | 16 | ▶ | my $params = Params::Get::get_params(undef, @_) || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
Params::Get::get_params(undef, @_) || {}
MC/DC100%1/1
Params::Get::get_params(undef, @_) || {}
Params::Get::get_params(undef, @_)
Truth table100%2/2
A
A= Params::Get::get_params(undef, @_)
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 16 | ▶ | if(!defined($class)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 70 | # Geo::Coder::Postcodes::new() used rather than Geo::Coder::Postcodes->new() | ||||||||||||||||||||||||||||
| 71 | # FIXME: this only works when no arguments are given | ||||||||||||||||||||||||||||
| 72 | 1 | $class = __PACKAGE__; | |||||||||||||||||||||||||||
| 73 | } elsif(ref($class)) { | ||||||||||||||||||||||||||||
| 74 | # clone the given object | ||||||||||||||||||||||||||||
| 75 | 1 | return bless { %{$class}, %{$params} }, ref($class); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 15 | $params = Object::Configure::configure($class, $params); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 15 | ▶ | my $ua = $params->{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION"); | ||||||||||||||||||||||||||
|
Condition66%2/3
$params->{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Postcodes' . "/$Geo::Coder::Postcodes::VERSION")
MC/DC0%0/2
$params->{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Postcodes' . "/$Geo::Coder::Postcodes::VERSION")
$params->{'ua'}
'LWP::UserAgent'->new('agent', 'Geo::Coder::Postcodes' . "/$Geo::Coder::Postcodes::VERSION")
Truth table66%2/3
A || B
A= $params->{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::Postcodes' . "/$Geo::Coder::Postcodes::VERSION")
| |||||||||||||||||||||||||||||
| 81 | # if(!defined($args{'host'})) { | ||||||||||||||||||||||||||||
| 82 | # $ua->ssl_opts(verify_hostname => 0); # Yuck | ||||||||||||||||||||||||||||
| 83 | # } | ||||||||||||||||||||||||||||
| 84 | 15 | ▶ | my $host = $params->{host} || 'api.postcodes.io'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params->{'host'} || 'api.postcodes.io'
MC/DC0%0/1
$params->{'host'} || 'api.postcodes.io'
$params->{'host'}
Truth table50%1/2
A
A= $params->{'host'}
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 15 | return bless { %{$params}, ua => $ua, host => $host }, $class; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head2 geocode | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | $location = $geo_coder->geocode(location => $location); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | print 'Latitude: ', $location->{'latitude'}, "\n"; | ||||||||||||||||||||||||||||
| 94 | print 'Longitude: ', $location->{'logitude'}, "\n"; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # The rule when it is installed, and the one thing that can be said without it. | ||||||||||||||||||||||||||||
| 99 | # A reference is not a location however it would print: when the rule is there it | ||||||||||||||||||||||||||||
| 100 | # says so, and when it is not this refuses it - the hole this module used to have | ||||||||||||||||||||||||||||
| 101 | # was testing the string a reference turns into. Whether a string of digits is a | ||||||||||||||||||||||||||||
| 102 | # place is the rule's question, so without the rule it is not asked: the string | ||||||||||||||||||||||||||||
| 103 | # is looked up, as it was before the rule existed. | ||||||||||||||||||||||||||||
| 104 | 11 | ▶ | sub _could_be_a_location { | ||||||||||||||||||||||||||
|
Subroutine
_could_be_a_location: called CC=9 SCAR=29.5
| |||||||||||||||||||||||||||||
| 105 | 11 | my $location = shift; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 11 | ▶ | if(!defined($HAVE_RULE)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1 | ▶ | $HAVE_RULE = eval { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 1 | require Geo::Location::Predicate; | |||||||||||||||||||||||||||
| 110 | 1 | ▶ | Geo::Location::Predicate->can('_valid_location') ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | } ? 1 : 0; | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # When the rule is installed it decides everything, references included, so | ||||||||||||||||||||||||||||
| 115 | # that this module cannot be stricter or looser than the facade. Without it | ||||||||||||||||||||||||||||
| 116 | # there are two answers this module can give on its own. | ||||||||||||||||||||||||||||
| 117 | 11 | ▶ | return Geo::Location::Predicate::_valid_location($location) ? 1 : 0 if $HAVE_RULE; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return 0 if ref($location); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return 0 unless defined($location) && length($location); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $location and length $location
MC/DC0%0/2
defined $location and length $location
defined $location
length $location
Truth table0%0/3
A and B
A= defined $locationB= length $location
| |||||||||||||||||||||||||||||
| 121 | 0 | return 1; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 11 | ▶ | sub geocode { | ||||||||||||||||||||||||||
|
Subroutine
geocode: called CC=20 SCAR=48.7
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 11 | ▶ | my $params = Params::Get::get_params('location', @_) || | ||||||||||||||||||||||||||
|
Condition50%1/2
Params::Get::get_params('location', @_) || Carp::croak('Usage: geocode(location => $location)')
MC/DC0%0/1
Params::Get::get_params('location', @_) || Carp::croak('Usage: geocode(location => $location)')
Params::Get::get_params('location', @_)
Truth table50%1/2
A
A= Params::Get::get_params('location', @_)
| |||||||||||||||||||||||||||||
| 128 | Carp::croak('Usage: geocode(location => $location)'); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 11 | my $location = $params->{location}; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # A scalar reference is one way of passing the string it points at, and | ||||||||||||||||||||||||||||
| 133 | # Params::Get dereferences it when it is the only argument. Do it here as | ||||||||||||||||||||||||||||
| 134 | # well, in every calling form, so that it means the string however the | ||||||||||||||||||||||||||||
| 135 | # method is called. | ||||||||||||||||||||||||||||
| 136 | 11 | ▶ | $location = ${$location} if ref($location) eq 'SCALAR'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # What counts as a location is Geo::Location::Predicate's answer, not a | ||||||||||||||||||||||||||||
| 139 | # second copy of it here: undef and the empty string are nothing to look up, a | ||||||||||||||||||||||||||||
| 140 | # reference is not a location however it would print, and a string of digits is | ||||||||||||||||||||||||||||
| 141 | # what a numeric column holds when the wrong one has been read. | ||||||||||||||||||||||||||||
| 142 | 11 | ▶ | if(!_could_be_a_location($location)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 7 | ▶ | Carp::croak('Usage: geocode(location => $location)') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $location and length $location
MC/DC0%0/2
defined $location and length $location
defined $location
length $location
Truth table33%1/3
A and B
A= defined $locationB= length $location
| |||||||||||||||||||||||||||||
| 144 | unless(defined($location) && length($location)); | ||||||||||||||||||||||||||||
| 145 | 7 | ▶ | Carp::croak('Usage: ', __PACKAGE__, ': invalid input to geocode(), ', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | ref($location) ? 'a reference is not a location' : $location); | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 4 | my $county; | |||||||||||||||||||||||||||
| 150 | 4 | ▶ | if($location =~ /,/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if($location =~ /^([\w\s\-]+?),([\w\s]+?),[\w\s]+?$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | # Turn 'Ramsgate, Kent, UK' into 'Ramsgate' | ||||||||||||||||||||||||||||
| 153 | 0 | $location = $1; | |||||||||||||||||||||||||||
| 154 | 0 | $county = $2; | |||||||||||||||||||||||||||
| 155 | 0 | $county =~ s/^\s//g; | |||||||||||||||||||||||||||
| 156 | 0 | $county =~ s/\s$//g; | |||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||
| 158 | 0 | Carp::croak('Postcodes.io only supports towns, not full addresses'); | |||||||||||||||||||||||||||
| 159 | 0 | return; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 4 | $location =~ s/\s/+/g; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 4 | ▶ | if(Encode::is_utf8($location)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | $location = Encode::encode_utf8($location); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 4 | my $uri = URI->new("https://$self->{host}/places/"); | |||||||||||||||||||||||||||
| 169 | 4 | my %query_parameters = ('q' => $location); | |||||||||||||||||||||||||||
| 170 | 4 | $uri->query_form(%query_parameters); | |||||||||||||||||||||||||||
| 171 | 4 | my $url = $uri->as_string(); | |||||||||||||||||||||||||||
| 172 | 4 | $url =~ s/%2B/+/g; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 4 | my $res = $self->{ua}->get($url); | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | if($res->is_error()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 0 | Carp::croak("postcodes.io API returned error: on $url " . $res->status_line()); | |||||||||||||||||||||||||||
| 178 | 0 | return; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | my $json = JSON::MaybeXS->new()->utf8(); | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # TODO: wantarray | ||||||||||||||||||||||||||||
| 184 | 0 | my $rc = $json->decode($res->decoded_content()); | |||||||||||||||||||||||||||
| 185 | 0 | ▶ | if($rc->{'result'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 0 | my @results = @{$rc->{result}}; | |||||||||||||||||||||||||||
| 187 | 0 | ▶ | if($county) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | # TODO: search through all results for the right one, e.g. Leeds in | ||||||||||||||||||||||||||||
| 189 | # Kent or in West Yorkshire? | ||||||||||||||||||||||||||||
| 190 | 0 | foreach my $result(@results) { | |||||||||||||||||||||||||||
| 191 | # if(defined($result->{'county_unitary'}) && ($result->{'county_unitary_type'} eq 'County')) { | ||||||||||||||||||||||||||||
| 192 | 0 | ▶ | if(my $unitary = $result->{'county_unitary'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | # $location =~ s/+/ /g; | ||||||||||||||||||||||||||||
| 194 | 0 | ▶ | if(($unitary =~ /$county/i) || ($unitary =~ /$location/i)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$unitary =~ /$county/i or $unitary =~ /$location/i
MC/DC0%0/2
$unitary =~ /$county/i or $unitary =~ /$location/i
$unitary =~ /$county/i
$unitary =~ /$location/i
Truth table0%0/3
A or B
A= $unitary =~ /$county/iB= $unitary =~ /$location/i
| |||||||||||||||||||||||||||||
| 195 | 0 | return $result; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | 0 | ▶ | if((my $region = $result->{'region'}) && ($county =~ /\s+(\w+)$/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
my $region = $result->{"region"} and $county =~ /\s+(\w+)$/
MC/DC0%0/2
my $region = $result->{"region"} and $county =~ /\s+(\w+)$/
my $region = $result->{"region"}
$county =~ /\s+(\w+)$/
Truth table0%0/3
A and B
A= my $region = $result->{"region"}B= $county =~ /\s+(\w+)$/
| |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if($region =~ /$1/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | # e.g. looked for South Yorkshire, got Yorkshire and the Humber | ||||||||||||||||||||||||||||
| 201 | 0 | return $result; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 0 | return; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 0 | return $results[0]; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head2 ua | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Accessor method to get and set UserAgent object used internally. You | ||||||||||||||||||||||||||||
| 214 | can call I<env_proxy> for example, to get the proxy information from | ||||||||||||||||||||||||||||
| 215 | environment variables: | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | $geo_coder->ua()->env_proxy(1); | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | You can also set your own User-Agent object: | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | use LWP::UserAgent::Throttled; | ||||||||||||||||||||||||||||
| 222 | $geo_coder->ua(LWP::UserAgent::Throttled->new()); | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =cut | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | ▶ | sub ua { | ||||||||||||||||||||||||||
|
Subroutine
ua: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 227 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 228 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 0 | $self->{ua} = $_[0]; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | 0 | $self->{ua}; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =head2 reverse_geocode | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | $location = $geo_coder->reverse_geocode(latlng => '37.778907,-122.39732'); | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Similar to geocode except it expects a latitude/longitude parameter. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =cut | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 0 | ▶ | sub reverse_geocode { | ||||||||||||||||||||||||||
|
Subroutine
reverse_geocode: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 243 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | ▶ | scalar(@_) > 0 or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | Carp::croak('Usage: reverse_geocode(latlng => $latlng)'); | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 0 | ▶ | my $params = Params::Get::get_params('latlng', @_) || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
Params::Get::get_params('latlng', @_) || {}
MC/DC0%0/1
Params::Get::get_params('latlng', @_) || {}
Params::Get::get_params('latlng', @_)
Truth table0%0/2
A
A= Params::Get::get_params('latlng', @_)
| |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 0 | my $latlng = $params->{latlng}; | |||||||||||||||||||||||||||
| 251 | 0 | ▶ | unless(defined($latlng)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 0 | Carp::croak('Usage: reverse_geocode(latlng => $latlng)'); | |||||||||||||||||||||||||||
| 253 | 0 | return; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 0 | my $uri = URI->new("https://$self->{host}/postcodes/"); | |||||||||||||||||||||||||||
| 257 | 0 | my ($lat, $lon) = split(/,/, $params->{latlng}); | |||||||||||||||||||||||||||
| 258 | 0 | my %query_parameters = ('lat' => $lat, 'lon' => $lon, radius => '1000'); | |||||||||||||||||||||||||||
| 259 | 0 | $uri->query_form(%query_parameters); | |||||||||||||||||||||||||||
| 260 | 0 | my $url = $uri->as_string; | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 0 | my $res = $self->{ua}->get($url); | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | ▶ | if($res->is_error()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 0 | Carp::croak("postcodes.io API returned error: on $url ", $res->status_line()); | |||||||||||||||||||||||||||
| 266 | 0 | return; | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 0 | my $json = JSON::MaybeXS->new->utf8(); | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 0 | my $rc = $json->decode($res->content); | |||||||||||||||||||||||||||
| 272 | 0 | ▶ | if($rc->{'result'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 0 | my @results = @{$rc->{'result'}}; | |||||||||||||||||||||||||||
| 274 | 0 | return $results[0]; | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | 0 | return; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =head1 BUGS | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | Note that this most only works on towns and cities, some searches such as "Margate, Kent, UK" | ||||||||||||||||||||||||||||
| 282 | may work, but you're best to search only for "Margate". | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | Nigel Horne C<< <njh@nigelhorne.com> >> | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | Based on L<Geo::Coder::GooglePlaces>. | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 291 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | Lots of thanks to the folks at postcodes.io. | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | L<Geo::Coder::GooglePlaces>, L<HTML::GoogleMaps::V3> | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | Copyright 2017-2026 Nigel Horne. | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | This program is released under the following licence: GPL2 | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =cut | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 7 | 1; | |||||||||||||||||||||||||||