| 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; | ||||||||||||||||||||||||||
|
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 https://postcodes.io. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 VERSION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Version 0.06 | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 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 | sub new { | ||||||||||||||||||||||||||||
| 50 | 1 | ▶ | my($class, %param) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | my $ua = delete $param{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION"); | ||||||||||||||||||||||||||
|
Condition33%1/3
delete $param{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Postcodes' . "/$Geo::Coder::Postcodes::VERSION")
MC/DC0%0/2
delete $param{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Postcodes' . "/$Geo::Coder::Postcodes::VERSION")
delete $param{'ua'}
'LWP::UserAgent'->new('agent', 'Geo::Coder::Postcodes' . "/$Geo::Coder::Postcodes::VERSION")
Truth table33%1/3
A || B
A= delete $param{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::Postcodes' . "/$Geo::Coder::Postcodes::VERSION")
| |||||||||||||||||||||||||||||
| 53 | # if(!defined($param{'host'})) { | ||||||||||||||||||||||||||||
| 54 | # $ua->ssl_opts(verify_hostname => 0); # Yuck | ||||||||||||||||||||||||||||
| 55 | # } | ||||||||||||||||||||||||||||
| 56 | 1 | ▶ | my $host = delete $param{host} || 'api.postcodes.io'; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $param{'host'} || 'api.postcodes.io'
MC/DC0%0/1
delete $param{'host'} || 'api.postcodes.io'
delete $param{'host'}
Truth table50%1/2
A
A= delete $param{'host'}
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | return bless { ua => $ua, host => $host }, $class; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head2 geocode | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | $location = $geo_coder->geocode(location => $location); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | print 'Latitude: ', $location->{'latitude'}, "\n"; | ||||||||||||||||||||||||||||
| 66 | print 'Longitude: ', $location->{'logitude'}, "\n"; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub geocode { | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
geocode: not called CC=16 SCAR=56.1
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | scalar(@_) > 0 or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | Carp::croak('Usage: geocode(location => $location)'); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | my %param; | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | if (@_ % 2 == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | %param = @_; | |||||||||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||||||||||
| 80 | 0 | $param{location} = shift; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my $location = $param{location}; | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | unless(defined($location)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | Carp::croak('Usage: geocode(location => $location)'); | |||||||||||||||||||||||||||
| 86 | 0 | return; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $county; | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | if($location =~ /,/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if($location =~ /^([\w\s\-]+?),([\w\s]+?),[\w\s]+?$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | # Turn 'Ramsgate, Kent, UK' into 'Ramsgate' | ||||||||||||||||||||||||||||
| 93 | 0 | $location = $1; | |||||||||||||||||||||||||||
| 94 | 0 | $county = $2; | |||||||||||||||||||||||||||
| 95 | 0 | $county =~ s/^\s//g; | |||||||||||||||||||||||||||
| 96 | 0 | $county =~ s/\s$//g; | |||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||
| 98 | 0 | Carp::croak('Postcodes.io only supports towns, not full addresses'); | |||||||||||||||||||||||||||
| 99 | 0 | return; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | $location =~ s/\s/+/g; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if(Encode::is_utf8($location)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | $location = Encode::encode_utf8($location); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my $uri = URI->new("https://$self->{host}/places/"); | |||||||||||||||||||||||||||
| 109 | 0 | my %query_parameters = ('q' => $location); | |||||||||||||||||||||||||||
| 110 | 0 | $uri->query_form(%query_parameters); | |||||||||||||||||||||||||||
| 111 | 0 | my $url = $uri->as_string(); | |||||||||||||||||||||||||||
| 112 | 0 | $url =~ s/%2B/+/g; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my $res = $self->{ua}->get($url); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if($res->is_error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | Carp::croak("postcodes.io API returned error: on $url " . $res->status_line()); | |||||||||||||||||||||||||||
| 118 | 0 | return; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | my $json = JSON->new()->utf8(); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # TODO: wantarray | ||||||||||||||||||||||||||||
| 124 | 0 | my $rc = $json->decode($res->content); | |||||||||||||||||||||||||||
| 125 | 0 | my @results = @{$rc->{result}}; | |||||||||||||||||||||||||||
| 126 | 0 | ▶ | if($county) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | # TODO: search through all results for the right one, e.g. Leeds in | ||||||||||||||||||||||||||||
| 128 | # Kent or in West Yorkshire? | ||||||||||||||||||||||||||||
| 129 | 0 | foreach my $result(@results) { | |||||||||||||||||||||||||||
| 130 | # if(defined($result->{'county_unitary'}) && ($result->{'county_unitary_type'} eq 'County')) { | ||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if(my $unitary = $result->{'county_unitary'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | # $location =~ s/+/ /g; | ||||||||||||||||||||||||||||
| 133 | 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
| |||||||||||||||||||||||||||||
| 134 | 0 | return $result; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 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+)$/
| |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if($region =~ /$1/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | # e.g. looked for South Yorkshire, got Yorkshire and the Humber | ||||||||||||||||||||||||||||
| 140 | 0 | return $result; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 0 | return; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 0 | return $results[0]; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head2 ua | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Accessor method to get and set UserAgent object used internally. You | ||||||||||||||||||||||||||||
| 152 | can call I<env_proxy> for example, to get the proxy information from | ||||||||||||||||||||||||||||
| 153 | environment variables: | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | $geo_coder->ua()->env_proxy(1); | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | You can also set your own User-Agent object: | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | use LWP::UserAgent::Throttled; | ||||||||||||||||||||||||||||
| 160 | $geo_coder->ua(LWP::UserAgent::Throttled->new()); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | sub ua { | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
ua: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | $self->{ua} = shift; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 0 | $self->{ua}; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head2 reverse_geocode | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | $location = $geo_coder->reverse_geocode(latlng => '37.778907,-122.39732'); | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Similar to geocode except it expects a latitude/longitude parameter. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =cut | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | sub reverse_geocode { | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
reverse_geocode: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | scalar(@_) > 0 or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | Carp::croak('Usage: reverse_geocode(latlng => $latlng)'); | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | my %param; | |||||||||||||||||||||||||||
| 187 | 0 | ▶ | if (@_ % 2 == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | %param = @_; | |||||||||||||||||||||||||||
| 189 | } else { | ||||||||||||||||||||||||||||
| 190 | 0 | $param{latlng} = shift; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | my $latlng = $param{latlng}; | |||||||||||||||||||||||||||
| 194 | 0 | ▶ | unless(defined($latlng)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | Carp::croak('Usage: reverse_geocode(latlng => $latlng)'); | |||||||||||||||||||||||||||
| 196 | 0 | return; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | my $uri = URI->new("https://$self->{host}/postcodes/"); | |||||||||||||||||||||||||||
| 200 | 0 | my ($lat, $lon) = split(/,/, $param{latlng}); | |||||||||||||||||||||||||||
| 201 | 0 | my %query_parameters = ('lat' => $lat, 'lon' => $lon, radius => '1000'); | |||||||||||||||||||||||||||
| 202 | 0 | $uri->query_form(%query_parameters); | |||||||||||||||||||||||||||
| 203 | 0 | my $url = $uri->as_string; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | my $res = $self->{ua}->get($url); | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | if ($res->is_error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | Carp::croak("postcodes.io API returned error: on $url " . $res->status_line()); | |||||||||||||||||||||||||||
| 209 | 0 | return; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 0 | my $json = JSON->new->utf8(); | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | my $rc = $json->decode($res->content); | |||||||||||||||||||||||||||
| 215 | 0 | ▶ | if($rc->{'result'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | my @results = @{$rc->{'result'}}; | |||||||||||||||||||||||||||
| 217 | 0 | return $results[0]; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | 0 | return; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head1 BUGS | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | Note that this most only works on towns and cities, some searches such as "Margate, Kent, UK" | ||||||||||||||||||||||||||||
| 225 | may work, but you're best to search only for "Margate". | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | Nigel Horne <njh@bandsman.co.uk> | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | Based on L<Geo::Coder::Coder::Googleplaces>. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 234 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | Lots of thanks to the folks at postcodes.io. | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | L<Geo::Coder::GooglePlaces>, L<HTML::GoogleMaps::V3> | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | Copyright 2017-2019 Nigel Horne. | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | This program is released under the following licence: GPL2 | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =cut | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 5 | 1; | |||||||||||||||||||||||||||