blib/lib/Geo/Coder/CA.pm

filter: Statementstmt 66.6% 66 / 99 Branchbran 50.0% 24 / 48 Conditioncond 64.7% 11 / 17 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 78.5% 11 / 14 Podpod 100% 5 / 5 totaltotal 63.4% 123 / 194 CC 29 SCAR 25.1
CC
29
Cov
62%
geocode
26.5
_get_params
11.0
ua
6.0
CRAP 12.3
↑ Summary
1 package Geo::Coder::CA;
2  
3 # See also https://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.html for the US for the future
4  
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Geo::Coder::Free::Validate holds the one definition of what counts as a
15 # location. Look for it when this module is loaded, so that a site with
16 # Geo::Coder::Free installed gets that definition and not a second copy of it.
17 # It is not a prerequisite: without it a reference is refused here, which is
18 # the case this module used to get wrong by testing the string the reference
19 # turns into instead of testing the value.
20 7 my $have_validate = eval {
21 7 require Geo::Coder::Free::Validate;
22 0 ▶ Geo::Coder::Free::Validate->can('_valid_location') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'Geo::Coder::Free::Validate'->can('_valid_location') ? : 0 0 0
23 };
24  
25 =head1 NAME
26  
27 Geo::Coder::CA - Provides a Geo-Coding functionality using L<https://geocoder.ca> for both Canada and the US.
28  
29 =head1 VERSION
30  
31 Version 0.16
32  
33 =cut
34  
35 7 our $VERSION = '0.16';
36  
37 =head1 SYNOPSIS
38  
39 use Geo::Coder::CA;
40  
41 my $geo_coder = Geo::Coder::CA->new();
42 my $location = $geo_coder->geocode(location => '9235 Main St, Richibucto, New Brunswick, Canada');
43  
44 =head1 DESCRIPTION
45  
46 Geo::Coder::CA provides an interface to geocoder.ca.
47 L<Geo::Coder::Canada> no longer seems to work.
48  
49 =head1 METHODS
50  
51 =head2 new
52  
53 $geo_coder = Geo::Coder::CA->new();
54 my $ua = LWP::UserAgent->new();
55 $ua->env_proxy(1);
56 $geo_coder = Geo::Coder::CA->new(ua => $ua);
57  
58 =cut
59  
60 14 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
61 14 my($class, %args) = @_;
62  
63 14 ▶ if(!defined($class)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $class) { } 1 13 14
elsif (ref $class) { } 0 13 13
64 # Geo::Coder::CA::new() used rather than Geo::Coder::CA->new()
65 1 $class = __PACKAGE__;
66 } elsif(ref($class)) {
67 # clone the given object
68 0 return bless { %{$class}, %args }, ref($class);
69 }
70  
71 14 my $ua = $args{ua};
72 14 ▶ if(!defined($ua)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ua) 3 11 14
73 3 $ua = LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
74 3 $ua->default_header(accept_encoding => 'gzip,deflate');
75 }
76 14 ▶ my $host = $args{host} || 'geocoder.ca';
Condition100%2/2
$args{'host'} || 'geocoder.ca'
l!l
113
MC/DC100%1/1
$args{'host'} || 'geocoder.ca'
$args{'host'}
Truth table100%2/2
A
A= $args{'host'}
Aresult
11
00
77  
78 14 return bless { ua => $ua, host => $host }, $class;
79 }
80  
81 =head2 geocode
82  
83 $location = $geo_coder->geocode(location => $location);
84 # @location = $geo_coder->geocode(location => $location);
85  
86 print 'Latitude: ', $location->{'latt'}, "\n";
87 print 'Longitude: ', $location->{'longt'}, "\n";
88  
89 =cut
90  
91 16 ▶sub geocode {
Subroutine geocode: called CC=13 SCAR=32.8
Pod documented
92 16 my $self = shift;
93 16 my $params = $self->_get_params('location', @_);
94  
95 16 my $location = $params->{location};
96 # A scalar reference is one way of passing the string it points at, and
97 # Params::Get dereferences it when it is the only argument. Do it here as
98 # well, in every calling form, so that it means the string however the
99 # method is called.
100 16 ▶ $location = ${$location} if ref($location) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $location eq "SCALAR" 2 14 16
101  
102 16 ▶ if((!defined($location)) || (length($location) == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $location or length $location == 0) 6 10 16
Condition100%3/3
not defined $location or length $location == 0
l!l&&r!l&&!r
3310
MC/DC100%2/2
not defined $location or length $location == 0
not defined $location length $location == 0
Truth table100%3/3
not(A) or B
A= not defined $locationB= length $location == 0
ABresult
1X1
011
000
103 6 Carp::croak('Usage: geocode(location => $location)');
104 5 return;
105 }
106  
107 # A reference is not a location, however it would print
108 10 ▶ if(ref($location) && !_could_be_a_location($location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $location and not _could_be_a_location($location)) 6 4 10
Condition66%2/3
ref $location and not _could_be_a_location($location)
!ll&&!rl&&r
406
MC/DC50%1/2
ref $location and not _could_be_a_location($location)
ref $location not _could_be_a_location($location)
Truth table66%2/3
A and not(B)
A= ref $locationB= not _could_be_a_location($location)
ABresult
0X0
100
111
109 6 Carp::croak('Usage: ', __PACKAGE__, ': invalid input to geocode(), a reference is not a location');
110 0 return;
111 }
112  
113 # Fail when the input is just a set of numbers
114 4 ▶ if($location !~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($location =~ /\D/) 1 3 4
115 1 Carp::croak('Usage: ', __PACKAGE__, ': invalid input to geocode(), ', $location);
116 0 return;
117 }
118  
119 3 ▶ if (Encode::is_utf8($location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($location)) 0 3 3
120 0 $location = Encode::encode_utf8($location);
121 }
122  
123 3 my $uri = URI->new("https://$self->{host}/some_location");
124 3 $location =~ s/\s/+/g;
125 3 my %query_parameters = ('locate' => $location, 'json' => 1, 'strictmode' => 1);
126 3 $uri->query_form(%query_parameters);
127 3 my $url = $uri->as_string();
128  
129 3 my $res = $self->{ua}->get($url);
130  
131 0 ▶ if($res->is_error()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
132 0 Carp::carp("$url API returned error: ", $res->status_line());
133 0 return;
134 }
135 # $res->content_type('text/plain'); # May be needed to decode correctly
136  
137 0 my $json = JSON::MaybeXS->new()->utf8();
138 0 ▶ if(my $rc = $json->decode($res->decoded_content())) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rc = $json->decode($res->decoded_content)) 0 0 0
139 0 ▶ if($rc->{'error'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc->{"error"}) 0 0 0
140 # Sorry - you lose the error code, but HTML::GoogleMaps::V3 relies on this
141 # TODO - send patch to the H:G:V3 author
142 0 return;
143 }
144 0 ▶ if(defined($rc->{'latt'}) && defined($rc->{'longt'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rc->{"latt"} and defined $rc->{"longt"}) 0 0 0
Condition0%0/3
defined $rc->{"latt"} and defined $rc->{"longt"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rc->{"latt"} and defined $rc->{"longt"}
defined $rc->{"latt"} defined $rc->{"longt"}
Truth table0%0/3
A and B
A= defined $rc->{"latt"}B= defined $rc->{"longt"}
ABresult
0X0
100
111
145 0 return $rc; # No support for list context, yet
146 }
147  
148 # if($location =~ /^(\w+),\+*(\w+),\+*(USA|US|United States)$/i) {
149 # $query_parameters{'locate'} = "$1 County, $2, $3";
150 # $uri->query_form(%query_parameters);
151 # $url = $uri->as_string();
152 #
153 # $res = $self->{ua}->get($url);
154 #
155 # if($res->is_error()) {
156 # Carp::croak("geocoder.ca API returned error: " . $res->status_line());
157 # return;
158 # }
159 # return $json->decode($res->content());
160 # }
161 }
162  
163 # my @results = @{ $data || [] };
164 # wantarray ? @results : $results[0];
165 }
166  
167 =head2 ua
168  
169 Accessor method to get and set UserAgent object used internally. You
170 can call I<env_proxy> for example, to get the proxy information from
171 environment variables:
172  
173 $geo_coder->ua()->env_proxy(1);
174  
175 You can also set your own User-Agent object:
176  
177 my $ua = LWP::UserAgent::Throttled->new();
178 $ua->throttle('geocoder.ca' => 1);
179 $geo_coder->ua($ua);
180  
181 =cut
182  
183 0 ▶sub ua {
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
184 0 my $self = shift;
185 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
186 0 $self->{ua} = shift;
187 }
188 0 $self->{ua};
189 }
190  
191 =head2 reverse_geocode
192  
193 $location = $geo_coder->reverse_geocode(latlng => '37.778907,-122.39732');
194  
195 Similar to geocode except it expects a latitude/longitude parameter.
196  
197 =cut
198  
199 0 ▶sub reverse_geocode {
Subroutine reverse_geocode: not called CC=2 SCAR=17.9
Pod documented
200 0 my $self = shift;
201 0 my $params = $self->_get_params('latlng', @_);
202  
203 my $latlng = $params->{latlng}
204 0 ▶ or Carp::croak('Usage: reverse_geocode(latlng => $latlng)');
Branch: 1 branch
BranchTrueFalseTotal
my $latlng = $params->{"latlng"} or Carp::croak("Usage: reverse_geocode(latlng => \$latlng)") 0 0 0
205  
206 0 return $self->geocode(location => $latlng, reverse => 1);
207 }
208  
209 =head2 run
210  
211 You can also run this module from the command line:
212  
213 perl CA.pm 1600 Pennsylvania Avenue NW, Washington DC
214  
215 =cut
216  
217 7 ▶__PACKAGE__->run(@ARGV) unless caller();
Branch: 1 branch
BranchTrueFalseTotal
unless &CORE::GLOBAL::caller() 0 7 7
218  
219 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
220 0 require Data::Dumper;
221  
222 0 my $class = shift;
223  
224 0 my $location = join(' ', @_);
225  
226 0 my @rc = $class->new()->geocode($location);
227  
228 0 ▶ if(scalar(@rc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rc) { } 0 0 0
229 0 print Data::Dumper->new([\@rc])->Dump();
230 } else {
231 0 die "$0: geo-coding failed";
232 }
233 }
234  
235 # Helper routine to parse the arguments given to a function.
236 # Processes arguments passed to methods and ensures they are in a usable format,
237 # allowing the caller to call the function in anyway that they want
238 # e.g. foo('bar'), foo(arg => 'bar'), foo({ arg => 'bar' }) all mean the same
239 # when called _get_params('arg', @_);
240 sub _get_params
241 16 ▶{
Subroutine _get_params: called CC=9 SCAR=24.0
242 16 shift; # Discard the first argument (typically $self)
243 16 my $default = shift;
244  
245 # Directly return hash reference if the first parameter is a hash reference
246 16 ▶ return $_[0] if(ref $_[0] eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 2 14 16
247  
248 14 my %rc;
249 14 my $num_args = scalar @_;
250  
251 # Populate %rc based on the number and type of arguments
252 14 ▶ if(($num_args == 1) && (defined $default)) {
Branch: 5 branches
BranchTrueFalseTotal
if ($num_args == 1 and defined $default) { } 2 12 14
elsif ($num_args == 1) { } 0 12 12
elsif ($num_args == 0 and defined $default) { } 1 11 12
elsif ($num_args % 2 == 0) { } 11 0 11
elsif ($num_args == 0) { } 0 0 0
Condition66%2/3
$num_args == 1 and defined $default
!ll&&!rl&&r
1202
MC/DC50%1/2
$num_args == 1 and defined $default
$num_args == 1 defined $default
Truth table66%2/3
A and B
A= $num_args == 1B= defined $default
ABresult
0X0
100
111
253 # %rc = ($default => shift);
254 2 return { $default => shift };
255 } elsif($num_args == 1) {
256 0 Carp::croak('Usage: ', __PACKAGE__, '->', (caller(1))[3], '()');
257 ▶ } elsif(($num_args == 0) && (defined($default))) {
Condition66%2/3
$num_args == 0 and defined $default
!ll&&!rl&&r
1101
MC/DC50%1/2
$num_args == 0 and defined $default
$num_args == 0 defined $default
Truth table66%2/3
A and B
A= $num_args == 0B= defined $default
ABresult
0X0
100
111
258 1 Carp::croak('Usage: ', __PACKAGE__, '->', (caller(1))[3], "($default => \$val)");
259 } elsif(($num_args % 2) == 0) {
260 11 %rc = @_;
261 } elsif($num_args == 0) {
262 0 return;
263 } else {
264 0 Carp::croak('Usage: ', __PACKAGE__, '->', (caller(1))[3], '()');
265 }
266  
267 12 return \%rc;
268 }
269  
270 # Could the value handed to it be a location? Only a reference gets asked here,
271 # and the answer is whatever Geo::Coder::Free::Validate says when it is
272 # installed. Without it, a reference is refused: only a string can name a
273 # place, and there is nothing in this module that could make sense of a
274 # reference that claims otherwise.
275 6 ▶sub _could_be_a_location {
Subroutine _could_be_a_location: called CC=2 SCAR=7.2
276 6 my $value = shift;
277  
278 6 ▶ return $have_validate ? Geo::Coder::Free::Validate::_valid_location($value) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$have_validate ? : 0 6 6
279 }
280  
281 =head1 AUTHOR
282  
283 Nigel Horne, C<< <njh@bandsman.co.uk> >>
284  
285 Based on L<Geo::Coder::GooglePlaces>.
286  
287 This library is free software; you can redistribute it and/or modify
288 it under the same terms as Perl itself.
289  
290 Lots of thanks to the folks at geocoder.ca.
291  
292 =head1 BUGS
293  
294 Please report any bugs or feature requests to the author.
295 This module is provided as-is without any warranty.
296  
297 Should be called Geo::Coder::NA for North America.
298  
299 =head1 SEE ALSO
300  
301 L<Geo::Coder::GooglePlaces>, L<HTML::GoogleMaps::V3>
302  
303 =head1 LICENSE AND COPYRIGHT
304  
305 Copyright 2017-2026 Nigel Horne.
306  
307 This program is released under the following licence: GPL2
308  
309 =cut
310  
311 7 1;
↑ Summary