blib/lib/Geo/Coder/Postcodes.pm

filter: Statementstmt 33.3% 38 / 114 Branchbran 11.9% 5 / 42 Conditioncond 18.1% 2 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 76.9% 10 / 13 Podpod 100% 4 / 4 totaltotal 30.8% 59 / 191 CC 27 SCAR 47.8
CC
27
Cov
27%
geocode
272.0
reverse_geocode
42.0
new
6.5
CRAP 119.6
↑ Summary
1 package Geo::Coder::Postcodes;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Geo::Coder::Postcodes - Provides a geocoding functionality using L<https://postcodes.io>.
17  
18 =head1 VERSION
19  
20 Version 0.08
21  
22 =cut
23  
24 5 our $VERSION = '0.08';
25  
26 =head1 SYNOPSIS
27  
28 use Geo::Coder::Postcodes;
29  
30 my $geo_coder = Geo::Coder::Postcodes->new();
31 my $location = $geo_coder->geocode(location => 'Margate');
32  
33 =head1 DESCRIPTION
34  
35 Geo::Coder::Postcodes provides an interface to postcodes.io,
36 a free Geo-Coder database covering the towns in the UK.
37  
38 =head1 METHODS
39  
40 =head2 new
41  
42 $geo_coder = Geo::Coder::Postcodes->new();
43 my $ua = LWP::UserAgent->new();
44 $ua->env_proxy(1);
45 $geo_coder = Geo::Coder::Postcodes->new(ua => $ua);
46  
47 =cut
48  
49 4 ▶sub new {
Subroutine new: called CC=6 SCAR=18.7
Pod documented
50 4 my $class = shift;
51 4 ▶ my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 4 4
52  
53 4 ▶ if(!defined($class)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $class) { } 1 3 4
elsif (ref $class) { } 1 2 3
54 # Geo::Coder::Postcodes::new() used rather than Geo::Coder::Postcodes->new()
55 # FIXME: this only works when no arguments are given
56 1 $class = __PACKAGE__;
57 } elsif(ref($class)) {
58 # clone the given object
59 1 return bless { %{$class}, %args }, ref($class);
60 }
61  
62 3 ▶ my $ua = delete $args{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
Condition33%1/3
delete $args{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Postcodes' . "/$Geo::Coder::Postcodes::VERSION")
l!l&&r!l&&!r
030
MC/DC0%0/2
delete $args{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Postcodes' . "/$Geo::Coder::Postcodes::VERSION")
delete $args{'ua'} 'LWP::UserAgent'->new('agent', 'Geo::Coder::Postcodes' . "/$Geo::Coder::Postcodes::VERSION")
Truth table33%1/3
A || B
A= delete $args{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::Postcodes' . "/$Geo::Coder::Postcodes::VERSION")
ABresult
1X1
011
000
63 # if(!defined($args{'host'})) {
64 # $ua->ssl_opts(verify_hostname => 0); # Yuck
65 # }
66 3 ▶ my $host = delete $args{host} || 'api.postcodes.io';
Condition50%1/2
delete $args{'host'} || 'api.postcodes.io'
l!l
03
MC/DC0%0/1
delete $args{'host'} || 'api.postcodes.io'
delete $args{'host'}
Truth table50%1/2
A
A= delete $args{'host'}
Aresult
11
00
67  
68 3 return bless { ua => $ua, host => $host, %args }, $class;
69 }
70  
71 =head2 geocode
72  
73 $location = $geo_coder->geocode(location => $location);
74  
75 print 'Latitude: ', $location->{'latitude'}, "\n";
76 print 'Longitude: ', $location->{'logitude'}, "\n";
77  
78 =cut
79  
80 0 ▶sub geocode {
Subroutine geocode: not called CC=16 SCAR=56.1
Pod documented
81 0 my $self = shift;
82  
83 0 ▶ scalar(@_) > 0 or
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 0 or Carp::croak("Usage: geocode(location => \$location)") 0 0 0
84 Carp::croak('Usage: geocode(location => $location)');
85  
86 0 my %param;
87 0 ▶ if (@_ % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 0) { } 0 0 0
88 0 %param = @_;
89 } else {
90 0 $param{location} = shift;
91 }
92  
93 0 my $location = $param{location};
94 0 ▶ unless(defined($location)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $location) 0 0 0
95 0 Carp::croak('Usage: geocode(location => $location)');
96 0 return;
97 }
98  
99 0 my $county;
100 0 ▶ if($location =~ /,/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ /,/) 0 0 0
101 0 ▶ if($location =~ /^([\w\s\-]+?),([\w\s]+?),[\w\s]+?$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ /^([\w\s\-]+?),([\w\s]+?),[\w\s]+?$/i) { } 0 0 0
102 # Turn 'Ramsgate, Kent, UK' into 'Ramsgate'
103 0 $location = $1;
104 0 $county = $2;
105 0 $county =~ s/^\s//g;
106 0 $county =~ s/\s$//g;
107 } else {
108 0 Carp::croak('Postcodes.io only supports towns, not full addresses');
109 0 return;
110 }
111 }
112 0 $location =~ s/\s/+/g;
113  
114 0 ▶ if(Encode::is_utf8($location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($location)) 0 0 0
115 0 $location = Encode::encode_utf8($location);
116 }
117  
118 0 my $uri = URI->new("https://$self->{host}/places/");
119 0 my %query_parameters = ('q' => $location);
120 0 $uri->query_form(%query_parameters);
121 0 my $url = $uri->as_string();
122 0 $url =~ s/%2B/+/g;
123  
124 0 my $res = $self->{ua}->get($url);
125  
126 0 ▶ if($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
127 0 Carp::croak("postcodes.io API returned error: on $url " . $res->status_line());
128 0 return;
129 }
130  
131 0 my $json = JSON::MaybeXS->new()->utf8();
132  
133 # TODO: wantarray
134 0 my $rc = $json->decode($res->decoded_content());
135 0 my @results = @{$rc->{result}};
136 0 ▶ if($county) {
Branch: 1 branch
BranchTrueFalseTotal
if ($county) 0 0 0
137 # TODO: search through all results for the right one, e.g. Leeds in
138 # Kent or in West Yorkshire?
139 0 foreach my $result(@results) {
140 # if(defined($result->{'county_unitary'}) && ($result->{'county_unitary_type'} eq 'County')) {
141 0 ▶ if(my $unitary = $result->{'county_unitary'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $unitary = $result->{"county_unitary"}) 0 0 0
142 # $location =~ s/+/ /g;
143 0 ▶ if(($unitary =~ /$county/i) || ($unitary =~ /$location/i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unitary =~ /$county/i or $unitary =~ /$location/i) 0 0 0
Condition0%0/3
$unitary =~ /$county/i or $unitary =~ /$location/i
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
144 0 return $result;
145 }
146 }
147 0 ▶ if((my $region = $result->{'region'}) && ($county =~ /\s+(\w+)$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $region = $result->{"region"} and $county =~ /\s+(\w+)$/) 0 0 0
Condition0%0/3
my $region = $result->{"region"} and $county =~ /\s+(\w+)$/
!ll&&!rl&&r
000
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+)$/
ABresult
0X0
100
111
148 0 ▶ if($region =~ /$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($region =~ /$1/) 0 0 0
149 # e.g. looked for South Yorkshire, got Yorkshire and the Humber
150 0 return $result;
151 }
152 }
153 }
154 0 return;
155 }
156 0 return $results[0];
157 }
158  
159 =head2 ua
160  
161 Accessor method to get and set UserAgent object used internally. You
162 can call I<env_proxy> for example, to get the proxy information from
163 environment variables:
164  
165 $geo_coder->ua()->env_proxy(1);
166  
167 You can also set your own User-Agent object:
168  
169 use LWP::UserAgent::Throttled;
170 $geo_coder->ua(LWP::UserAgent::Throttled->new());
171  
172 =cut
173  
174 0 ▶sub ua {
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
175 0 my $self = shift;
176 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
177 0 $self->{ua} = shift;
178 }
179 0 $self->{ua};
180 }
181  
182 =head2 reverse_geocode
183  
184 $location = $geo_coder->reverse_geocode(latlng => '37.778907,-122.39732');
185  
186 Similar to geocode except it expects a latitude/longitude parameter.
187  
188 =cut
189  
190 0 ▶sub reverse_geocode {
Subroutine reverse_geocode: not called CC=6 SCAR=37.4
Pod documented
191 0 my $self = shift;
192  
193 0 ▶ scalar(@_) > 0 or
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 0 or Carp::croak("Usage: reverse_geocode(latlng => \$latlng)") 0 0 0
194 Carp::croak('Usage: reverse_geocode(latlng => $latlng)');
195  
196 0 my %param;
197 0 ▶ if (@_ % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 0) { } 0 0 0
198 0 %param = @_;
199 } else {
200 0 $param{latlng} = shift;
201 }
202  
203 0 my $latlng = $param{latlng};
204 0 ▶ unless(defined($latlng)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $latlng) 0 0 0
205 0 Carp::croak('Usage: reverse_geocode(latlng => $latlng)');
206 0 return;
207 }
208  
209 0 my $uri = URI->new("https://$self->{host}/postcodes/");
210 0 my ($lat, $lon) = split(/,/, $param{latlng});
211 0 my %query_parameters = ('lat' => $lat, 'lon' => $lon, radius => '1000');
212 0 $uri->query_form(%query_parameters);
213 0 my $url = $uri->as_string;
214  
215 0 my $res = $self->{ua}->get($url);
216  
217 0 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
218 0 Carp::croak("postcodes.io API returned error: on $url " . $res->status_line());
219 0 return;
220 }
221  
222 0 my $json = JSON::MaybeXS->new->utf8();
223  
224 0 my $rc = $json->decode($res->content);
225 0 ▶ if($rc->{'result'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc->{"result"}) 0 0 0
226 0 my @results = @{$rc->{'result'}};
227 0 return $results[0];
228 }
229 0 return;
230 }
231  
232 =head1 BUGS
233  
234 Note that this most only works on towns and cities, some searches such as "Margate, Kent, UK"
235 may work, but you're best to search only for "Margate".
236  
237 =head1 AUTHOR
238  
239 Nigel Horne C<< <njh@bandsman.co.uk> >>
240  
241 Based on L<Geo::Coder::GooglePlaces>.
242  
243 This library is free software; you can redistribute it and/or modify
244 it under the same terms as Perl itself.
245  
246 Lots of thanks to the folks at postcodes.io.
247  
248 =head1 SEE ALSO
249  
250 L<Geo::Coder::GooglePlaces>, L<HTML::GoogleMaps::V3>
251  
252 =head1 LICENSE AND COPYRIGHT
253  
254 Copyright 2017-2024 Nigel Horne.
255  
256 This program is released under the following licence: GPL2
257  
258 =cut
259  
260 5 1;
↑ Summary