blib/lib/Geo/Coder/DataScienceToolkit.pm

filter: Statementstmt 77.3% 82 / 106 Branchbran 57.6% 30 / 52 Conditioncond 38.4% 10 / 26 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 93.3% 14 / 15 Podpod 100% 4 / 4 totaltotal 64.3% 141 / 219 CC 36 SCAR 35.2
CC
36
Cov
66%
geocode
69.1
_could_be_a_location
19.1
new
7.3
CRAP 33.8
↑ Summary
1 package Geo::Coder::DataScienceToolkit;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶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 6 my $HAVE_RULE; # undef until the rule is first looked for
22 6 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use LWP::Protocol::http;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use Object::Configure 0.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head1 NAME
30  
31 Geo::Coder::DataScienceToolkit - Provides a geocoding functionality using
32 http://www.datasciencetoolkit.org/
33  
34 =head1 VERSION
35  
36 Version 0.04
37  
38 =cut
39  
40 6 our $VERSION = '0.04';
41  
42 =head1 SYNOPSIS
43  
44 use Geo::Coder::DataScienceToolkit;
45  
46 my $geocoder = Geo::Coder::DataScienceToolkit->new();
47 my $location = $geocoder->geocode(location => '10 Downing St., London, UK');
48  
49 =head1 DESCRIPTION
50  
51 Geo::Coder::DataScienceToolkit provides an interface to datasciencetoolkit,
52 a free geocode database covering the US and UK.
53  
54 =head1 METHODS
55  
56 =head2 new
57  
58 $geocoder = Geo::Coder::DataScienceToolkit->new();
59 my $ua = LWP::UserAgent->new();
60 $ua->env_proxy(1);
61 $geocoder = Geo::Coder::DataScienceToolkit->new(ua => $ua);
62  
63 A hashref of the same arguments is accepted as well:
64  
65 $geocoder = Geo::Coder::DataScienceToolkit->new({ ua => $ua });
66  
67 An odd number of arguments is refused rather than read as a key with no value.
68  
69 =cut
70  
71 18 ▶sub new {
Subroutine new: called CC=7 SCAR=19.8
Pod documented
72 18 my $proto = shift;
73 18 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0180
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
74  
75 # Object::Configure::configure() takes a hashref of the caller's values, not
76 # the argument list: handing it \@_ made every call that passed a value die
77 # with "Not a HASH reference", which is why new(ua => $ua) - the form the
78 # POD documents - did not work. A hashref is accepted as well as a list, so
79 # that neither spelling of the same arguments is a surprise.
80 18 my %args;
81 18 ▶ if(@_ == 1 && ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 1 17 18
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1701
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
82 1 %args = %{$_[0]};
83 } else {
84 17 ▶ Carp::croak('Usage: ', __PACKAGE__, '->new(ua => $ua)') if(@_ % 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 16 17
85 16 %args = @_;
86 }
87  
88 17 my $params = Object::Configure::configure($class, \%args);
89  
90 17 ▶ my $ua = delete $params->{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
Condition66%2/3
delete $params->{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::DataScienceToolkit' . "/$Geo::Coder::DataScienceToolkit::VERSION")
l!l&&r!l&&!r
1520
MC/DC0%0/2
delete $params->{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::DataScienceToolkit' . "/$Geo::Coder::DataScienceToolkit::VERSION")
delete $params->{'ua'} 'LWP::UserAgent'->new('agent', 'Geo::Coder::DataScienceToolkit' . "/$Geo::Coder::DataScienceToolkit::VERSION")
Truth table66%2/3
A || B
A= delete $params->{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::DataScienceToolkit' . "/$Geo::Coder::DataScienceToolkit::VERSION")
ABresult
1X1
011
000
91 # if(!defined($params{'host'})) {
92 # $ua->ssl_opts(verify_hostname => 0); # Yuck
93 # }
94 17 ▶ my $host = $params->{host} || 'www.datasciencetoolkit.org';
Condition50%1/2
$params->{'host'} || 'www.datasciencetoolkit.org'
l!l
017
MC/DC0%0/1
$params->{'host'} || 'www.datasciencetoolkit.org'
$params->{'host'}
Truth table50%1/2
A
A= $params->{'host'}
Aresult
11
00
95  
96 17 return bless { %{$params}, ua => $ua, host => $host }, $class;
97 }
98  
99 =head2 geocode
100  
101 $location = $geocoder->geocode(location => $location);
102  
103 print 'Latitude: ', $location->{'results'}[0]->{'geometry'}->{'location'}->{'lat'}, 38.90, 1e-2); "\n";
104 print 'Longitude: ', delta_within($location->{'results'}[0]->{'geometry'}->{'location'}->{'lng'}, -77.04, 1e-2); "\n";
105  
106 @locations = $geocoder->geocode('Portland, USA');
107 diag 'There are Portlands in ', join (', ', map { $_->{'state'} } @locations);
108  
109 =cut
110  
111 # The rule when it is installed, and the one thing that can be said without it.
112 # A reference is not a location however it would print: when the rule is there it
113 # says so, and when it is not this refuses it - the hole this module used to have
114 # was testing the string a reference turns into. Whether a string of digits is a
115 # place is the rule's question, so without the rule it is not asked: the string
116 # is looked up, as it was before the rule existed.
117 12 ▶sub _could_be_a_location {
Subroutine _could_be_a_location: called CC=9 SCAR=29.5
118 12 my $location = shift;
119  
120 12 ▶ if(!defined($HAVE_RULE)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $HAVE_RULE) 2 10 12
121 2 ▶ $HAVE_RULE = eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Geo::Location::Predicate; 'Geo::Location::Predicate'->can('_valid_location') ? 1 : 0 } } ? : 2 0 2
122 2 require Geo::Location::Predicate;
123 2 ▶ Geo::Location::Predicate->can('_valid_location') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'Geo::Location::Predicate'->can('_valid_location') ? : 2 0 2
124 } ? 1 : 0;
125 }
126  
127 # When the rule is installed it decides everything, references included, so
128 # that this module cannot be stricter or looser than the facade. Without it
129 # there are two answers this module can give on its own.
130 12 ▶ return Geo::Location::Predicate::_valid_location($location) ? 1 : 0 if $HAVE_RULE;
Branch: 2 branches
BranchTrueFalseTotal
if $HAVE_RULE 12 0 12
Geo::Location::Predicate::_valid_location($location) ? : 5 7 12
131  
132 0 ▶ return 0 if ref($location);
Branch: 1 branch
BranchTrueFalseTotal
if ref $location 0 0 0
133 0 ▶ return 0 unless defined($location) && length($location);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $location and length $location 0 0 0
Condition0%0/3
defined $location and length $location
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
134 0 return 1;
135 }
136  
137 12 ▶sub geocode {
Subroutine geocode: called CC=21 SCAR=42.4
Pod documented
138 12 my $self = shift;
139 12 my %params;
140  
141 12 ▶ if(!ref($self)) {
Branch: 5 branches
BranchTrueFalseTotal
if (not ref $self) { } 0 12 12
elsif (ref $self eq 'HASH') { } 0 12 12
elsif (ref $_[0] eq 'HASH') { } 0 12 12
elsif (scalar @_ == 1) { } 1 11 12
elsif (scalar @_ and scalar(@_) % 2 == 0) { } 11 0 11
142 0 ▶ if(scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 0 0 0
143 0 return(__PACKAGE__->new()->parse(@_));
144 }
145 0 return(__PACKAGE__->new()->parse($self));
146 } elsif(ref($self) eq 'HASH') {
147 0 return(__PACKAGE__->new()->parse($self));
148 } elsif(ref($_[0]) eq 'HASH') {
149 0 %params = %{$_[0]};
150 } elsif(scalar(@_) == 1) {
151 1 $params{'location'} = shift;
152 ▶ } elsif(scalar(@_) && (scalar(@_) % 2 == 0)) {
Condition33%1/3
scalar @_ and scalar(@_) % 2 == 0
!ll&&!rl&&r
0011
MC/DC0%0/2
scalar @_ and scalar(@_) % 2 == 0
scalar @_ scalar(@_) % 2 == 0
Truth table33%1/3
A and B
A= scalar @_B= scalar(@_) % 2 == 0
ABresult
0X0
100
111
153 11 %params = @_;
154 } else {
155 0 Carp::croak('Usage: ', __PACKAGE__, '::geocode(location => $location)');
156 }
157  
158 12 my $location = $params{location};
159 # A scalar reference is one way of passing the string it points at, and
160 # Params::Get dereferences it when it is the only argument. Do it here as
161 # well, in every calling form, so that it means the string however the
162 # method is called.
163 12 ▶ $location = ${$location} if ref($location) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $location eq "SCALAR" 2 10 12
164  
165 # What counts as a location is Geo::Location::Predicate's answer, not a
166 # second copy of it here: undef and the empty string are nothing to look up, a
167 # reference is not a location however it would print, and a string of digits is
168 # what a numeric column holds when the wrong one has been read.
169 12 ▶ if(!_could_be_a_location($location)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_could_be_a_location($location)) 7 5 12
170 7 ▶ Carp::croak('Usage: geocode(location => $location)')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $location and length $location 0 7 7
Condition33%1/3
defined $location and length $location
!ll&&!rl&&r
007
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
ABresult
0X0
100
111
171 unless(defined($location) && length($location));
172 7 ▶ Carp::croak('Usage: ', __PACKAGE__, ': invalid input to geocode(), ',
Branch: 1 branch
BranchTrueFalseTotal
ref $location ? : 6 1 7
173 ref($location) ? 'a reference is not a location' : $location);
174 }
175  
176 5 ▶ if (Encode::is_utf8($location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($location)) 0 5 5
177 0 $location = Encode::encode_utf8($location);
178 }
179  
180 5 my $uri = URI->new("http://$self->{host}/maps/api/geocode/json");
181 5 $location =~ s/\s/+/g;
182 5 my %query_parameters = ('address' => $location, 'sensor' => 'false');
183 5 $uri->query_form(%query_parameters);
184 5 my $url = $uri->as_string();
185  
186 5 my $res = $self->{ua}->get($url);
187  
188 1 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 1 1
189 0 Carp::carp("API returned error: on $url ", $res->status_line());
190 0 return { };
191 }
192  
193 1 my $json = JSON::MaybeXS->new()->utf8();
194 1 my $rc;
195 1 eval {
196 1 $rc = $json->decode($res->content());
197 };
198 1 ▶ if(!defined($rc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 0 1 1
199 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
200 0 Carp::carp("$url: $@");
201 0 return { };
202 }
203 0 Carp::carp("$url: can't decode the JSON ", $res->content());
204 0 return { };
205 }
206  
207 1 ▶ if($rc->{'otherlocations'} && $rc->{'otherlocations'}->{'loc'} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($rc->{"otherlocations"} and $rc->{"otherlocations"}{"loc"} and ref $rc->{"otherlocations"}{"loc"} eq "ARRAY") 0 1 1
Condition33%2/6
$rc->{"otherlocations"} and $rc->{"otherlocations"}{"loc"} and ref $rc->{"otherlocations"}{"loc"} eq "ARRAY"
!ll&&!rl&&r
100
$rc->{"otherlocations"} and $rc->{"otherlocations"}{"loc"}
!ll&&!rl&&r
100
MC/DC0%0/3
$rc->{"otherlocations"} and $rc->{"otherlocations"}{"loc"} and ref $rc->{"otherlocations"}{"loc"} eq "ARRAY"
$rc->{"otherlocations"} $rc->{"otherlocations"}{"loc"} ref $rc->{"otherlocations"}{"loc"} eq "ARRAY"
Truth table25%1/4
A and B and C
A= $rc->{"otherlocations"}B= $rc->{"otherlocations"}{"loc"}C= ref $rc->{"otherlocations"}{"loc"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
208 (ref($rc->{'otherlocations'}->{'loc'}) eq 'ARRAY')) {
209 0 my @rc = @{$rc->{'otherlocations'}->{'loc'}};
210 0 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
211 0 return @rc;
212 }
213 0 return $rc[0];
214 }
215 1 return $rc;
216  
217 # my @results = @{ $data || [] };
218 # wantarray ? @results : $results[0];
219 }
220  
221 =head2 ua
222  
223 Accessor method to get and set UserAgent object used internally. You
224 can call I<env_proxy> for example, to get the proxy information from
225 environment variables:
226  
227 $geocoder->ua()->env_proxy(1);
228  
229 You can also set your own User-Agent object:
230  
231 use LWP::UserAgent::Throttled;
232 $geocoder->ua(LWP::UserAgent::Throttled->new());
233  
234 =cut
235  
236 6 ▶sub ua {
Subroutine ua: called CC=2 SCAR=6.9
Pod documented
237 6 my $self = shift;
238 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 5 6
239 1 $self->{ua} = shift;
240 }
241 6 $self->{ua};
242 }
243  
244 =head2 reverse_geocode
245  
246 Reverse geocoding is not supported by datasciencetoolkit.org, so calls to
247 this will generate an error.
248  
249 =cut
250  
251 0 ▶sub reverse_geocode {
Subroutine reverse_geocode: not called CC=1 SCAR=6.9
Pod documented
252 0 Carp::carp('datasciencetoolkit.org does not support reverse encoding');
253 }
254  
255 =head1 AUTHOR
256  
257 Nigel Horne <njh@bandsman.co.uk>
258  
259 Based on L<Geo::Coder::XYZ>.
260  
261 This library is free software; you can redistribute it and/or modify
262 it under the same terms as Perl itself.
263  
264 Lots of thanks to the folks at DSTK.
265  
266 =head1 SEE ALSO
267  
268 L<Geo::Coder::GooglePlaces>,
269 L<HTML::GoogleMaps::V3>,
270 L<http://www.datasciencetoolkit.org/about>.
271  
272 =head1 SUPPORT
273  
274 You can find documentation for this module with the perldoc command.
275  
276 perldoc Geo::Coder::DataScienceToolKit
277  
278 You can also look for information at:
279  
280 =over 4
281  
282 =item * MetaCPAN
283  
284 L<https://metacpan.org/dist/Geo-Coder-DataScienceToolkit>
285  
286 =item * RT: CPAN's request tracker
287  
288 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Geo-Coder-DataScienceToolkit>
289  
290 =item * CPAN Testers' Matrix
291  
292 L<http://matrix.cpantesters.org/?dist=Geo-Coder-DataScienceToolkit>
293  
294 =item * CPAN Testers Dependencies
295  
296 L<http://deps.cpantesters.org/?module=Geo-Coder-DataScienceToolkit>
297  
298 =back
299  
300 =head1 LICENSE AND COPYRIGHT
301  
302 Copyright 2019-2026 Nigel Horne.
303  
304 This program is released under the following licence: GPL2
305  
306 =cut
307  
308 6 1;
↑ Summary