blib/lib/Geo/Coder/CA.pm

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