blib/lib/Geo/Coder/DataScienceToolkit.pm

filter: Statementstmt 63.0% 63 / 100 Branchbran 42.5% 17 / 40 Conditioncond 30.0% 6 / 20 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 86.6% 13 / 15 Podpod 100% 4 / 4 totaltotal 54.4% 104 / 191 CC 27 SCAR 41.5
CC
27
Cov
54%
geocode
121.4
ua
6.0
new
5.6
CRAP 63.5
↑ 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 6 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use LWP::Protocol::http;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Object::Configure 0.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Geo::Coder::Free::Validate holds the one definition of what counts as a
16 # location. Look for it when this module is loaded, so that a site with
17 # Geo::Coder::Free installed gets that definition and not a second copy of it.
18 # It is not a prerequisite: without it a reference is refused here, which is
19 # the case this module used to get wrong by testing the string the reference
20 # turns into instead of testing the value.
21 6 my $have_validate = eval {
22 6 require Geo::Coder::Free::Validate;
23 0 ▶ Geo::Coder::Free::Validate->can('_valid_location') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'Geo::Coder::Free::Validate'->can('_valid_location') ? : 0 0 0
24 };
25  
26 =head1 NAME
27  
28 Geo::Coder::DataScienceToolkit - Provides a geocoding functionality using
29 http://www.datasciencetoolkit.org/
30  
31 =head1 VERSION
32  
33 Version 0.03
34  
35 =cut
36  
37 6 our $VERSION = '0.03';
38  
39 =head1 SYNOPSIS
40  
41 use Geo::Coder::DataScienceToolkit;
42  
43 my $geocoder = Geo::Coder::DataScienceToolkit->new();
44 my $location = $geocoder->geocode(location => '10 Downing St., London, UK');
45  
46 =head1 DESCRIPTION
47  
48 Geo::Coder::DataScienceToolkit provides an interface to datasciencetoolkit,
49 a free geocode database covering the US and UK.
50  
51 =head1 METHODS
52  
53 =head2 new
54  
55 $geocoder = Geo::Coder::DataScienceToolkit->new();
56 my $ua = LWP::UserAgent->new();
57 $ua->env_proxy(1);
58 $geocoder = Geo::Coder::DataScienceToolkit->new(ua => $ua);
59  
60 =cut
61  
62 23 ▶sub new {
Subroutine new: called CC=5 SCAR=17.3
Pod documented
63 23 my $proto = shift;
64 23 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0230
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
65 23 ▶ my $params = Object::Configure::configure($class, @_ ? \@_ : undef);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 11 12 23
66  
67 12 ▶ my $ua = delete $params->{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
Condition33%1/3
delete $params->{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::DataScienceToolkit' . "/$Geo::Coder::DataScienceToolkit::VERSION")
l!l&&r!l&&!r
0120
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 table33%1/3
A || B
A= delete $params->{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::DataScienceToolkit' . "/$Geo::Coder::DataScienceToolkit::VERSION")
ABresult
1X1
011
000
68 # if(!defined($params{'host'})) {
69 # $ua->ssl_opts(verify_hostname => 0); # Yuck
70 # }
71 12 ▶ my $host = $params->{host} || 'www.datasciencetoolkit.org';
Condition50%1/2
$params->{'host'} || 'www.datasciencetoolkit.org'
l!l
012
MC/DC0%0/1
$params->{'host'} || 'www.datasciencetoolkit.org'
$params->{'host'}
Truth table50%1/2
A
A= $params->{'host'}
Aresult
11
00
72  
73 12 return bless { %{$params}, ua => $ua, host => $host }, $class;
74 }
75  
76 =head2 geocode
77  
78 $location = $geocoder->geocode(location => $location);
79  
80 print 'Latitude: ', $location->{'results'}[0]->{'geometry'}->{'location'}->{'lat'}, 38.90, 1e-2); "\n";
81 print 'Longitude: ', delta_within($location->{'results'}[0]->{'geometry'}->{'location'}->{'lng'}, -77.04, 1e-2); "\n";
82  
83 @locations = $geocoder->geocode('Portland, USA');
84 diag 'There are Portlands in ', join (', ', map { $_->{'state'} } @locations);
85  
86 =cut
87  
88 10 ▶sub geocode {
Subroutine geocode: called CC=21 SCAR=48.0
Pod documented
89 10 my $self = shift;
90 10 my %params;
91  
92 10 ▶ if(!ref($self)) {
Branch: 5 branches
BranchTrueFalseTotal
if (not ref $self) { } 0 10 10
elsif (ref $self eq 'HASH') { } 0 10 10
elsif (ref $_[0] eq 'HASH') { } 0 10 10
elsif (scalar @_ == 1) { } 1 9 10
elsif (scalar @_ and scalar(@_) % 2 == 0) { } 9 0 9
93 0 ▶ if(scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 0 0 0
94 0 return(__PACKAGE__->new()->parse(@_));
95 }
96 0 return(__PACKAGE__->new()->parse($self));
97 } elsif(ref($self) eq 'HASH') {
98 0 return(__PACKAGE__->new()->parse($self));
99 } elsif(ref($_[0]) eq 'HASH') {
100 0 %params = %{$_[0]};
101 } elsif(scalar(@_) == 1) {
102 1 $params{'location'} = shift;
103 ▶ } elsif(scalar(@_) && (scalar(@_) % 2 == 0)) {
Condition33%1/3
scalar @_ and scalar(@_) % 2 == 0
!ll&&!rl&&r
009
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
104 9 %params = @_;
105 } else {
106 0 Carp::croak('Usage: ', __PACKAGE__, '::geocode(location => $location)');
107 }
108  
109 10 my $location = $params{location};
110 # A scalar reference is one way of passing the string it points at, and
111 # Params::Get dereferences it when it is the only argument. Do it here as
112 # well, in every calling form, so that it means the string however the
113 # method is called.
114 10 ▶ $location = ${$location} if ref($location) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $location eq "SCALAR" 2 8 10
115  
116 # A reference is not a location, however it would print. Checked before the
117 # "is there anything to look up?" test below, so that a reference is refused
118 # for being a reference rather than for being false.
119 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
120 6 Carp::croak('Usage: ', __PACKAGE__, ': invalid input to geocode(), a reference is not a location');
121 0 return;
122 }
123  
124 4 ▶ unless($location) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($location) 0 4 4
125 0 Carp::croak("Usage: geocode(location => \$location)");
126 }
127  
128 # Fail when the input is just a set of numbers
129 4 ▶ if($location !~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($location =~ /\D/) 1 3 4
130 1 Carp::croak('Usage: ', __PACKAGE__, ": invalid input to geocode(), $location");
131 0 return;
132 }
133  
134 3 ▶ if (Encode::is_utf8($location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($location)) 0 3 3
135 0 $location = Encode::encode_utf8($location);
136 }
137  
138 3 my $uri = URI->new("http://$self->{host}/maps/api/geocode/json");
139 3 $location =~ s/\s/+/g;
140 3 my %query_parameters = ('address' => $location, 'sensor' => 'false');
141 3 $uri->query_form(%query_parameters);
142 3 my $url = $uri->as_string();
143  
144 3 my $res = $self->{ua}->get($url);
145  
146 0 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
147 0 Carp::carp("API returned error: on $url ", $res->status_line());
148 0 return { };
149 }
150  
151 0 my $json = JSON::MaybeXS->new()->utf8();
152 0 my $rc;
153 0 eval {
154 0 $rc = $json->decode($res->content());
155 };
156 0 ▶ if(!defined($rc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 0 0 0
157 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
158 0 Carp::carp("$url: $@");
159 0 return { };
160 }
161 0 Carp::carp("$url: can't decode the JSON ", $res->content());
162 0 return { };
163 }
164  
165 0 ▶ 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 0 0
Condition0%0/6
$rc->{"otherlocations"} and $rc->{"otherlocations"}{"loc"} and ref $rc->{"otherlocations"}{"loc"} eq "ARRAY"
!ll&&!rl&&r
000
$rc->{"otherlocations"} and $rc->{"otherlocations"}{"loc"}
!ll&&!rl&&r
000
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 table0%0/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
166 (ref($rc->{'otherlocations'}->{'loc'}) eq 'ARRAY')) {
167 0 my @rc = @{$rc->{'otherlocations'}->{'loc'}};
168 0 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
169 0 return @rc;
170 }
171 0 return $rc[0];
172 }
173 0 return $rc;
174  
175 # my @results = @{ $data || [] };
176 # wantarray ? @results : $results[0];
177 }
178  
179 =head2 ua
180  
181 Accessor method to get and set UserAgent object used internally. You
182 can call I<env_proxy> for example, to get the proxy information from
183 environment variables:
184  
185 $geocoder->ua()->env_proxy(1);
186  
187 You can also set your own User-Agent object:
188  
189 use LWP::UserAgent::Throttled;
190 $geocoder->ua(LWP::UserAgent::Throttled->new());
191  
192 =cut
193  
194 0 ▶sub ua {
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
195 0 my $self = shift;
196 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
197 0 $self->{ua} = shift;
198 }
199 0 $self->{ua};
200 }
201  
202 =head2 reverse_geocode
203  
204 Reverse geocoding is not supported by datasciencetoolkit.org, so calls to
205 this will generate an error.
206  
207 =cut
208  
209 0 ▶sub reverse_geocode {
Subroutine reverse_geocode: not called CC=1 SCAR=6.9
Pod documented
210 0 Carp::carp('datasciencetoolkit.org does not support reverse encoding');
211 }
212  
213 # Could the value handed to it be a location? Only a reference gets asked here,
214 # and the answer is whatever Geo::Coder::Free::Validate says when it is
215 # installed. Without it, a reference is refused: only a string can name a
216 # place, and there is nothing in this module that could make sense of a
217 # reference that claims otherwise.
218 6 ▶sub _could_be_a_location {
Subroutine _could_be_a_location: called CC=2 SCAR=7.2
219 6 my $value = shift;
220  
221 6 ▶ return $have_validate ? Geo::Coder::Free::Validate::_valid_location($value) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$have_validate ? : 0 6 6
222 }
223  
224 =head1 AUTHOR
225  
226 Nigel Horne <njh@bandsman.co.uk>
227  
228 Based on L<Geo::Coder::XYZ>.
229  
230 This library is free software; you can redistribute it and/or modify
231 it under the same terms as Perl itself.
232  
233 Lots of thanks to the folks at DSTK.
234  
235 =head1 SEE ALSO
236  
237 L<Geo::Coder::GooglePlaces>,
238 L<HTML::GoogleMaps::V3>,
239 L<http://www.datasciencetoolkit.org/about>.
240  
241 =head1 SUPPORT
242  
243 You can find documentation for this module with the perldoc command.
244  
245 perldoc Geo::Coder::DataScienceToolKit
246  
247 You can also look for information at:
248  
249 =over 4
250  
251 =item * MetaCPAN
252  
253 L<https://metacpan.org/dist/Geo-Coder-DataScienceToolkit>
254  
255 =item * RT: CPAN's request tracker
256  
257 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Geo-Coder-DataScienceToolkit>
258  
259 =item * CPAN Testers' Matrix
260  
261 L<http://matrix.cpantesters.org/?dist=Geo-Coder-DataScienceToolkit>
262  
263 =item * CPAN Testers Dependencies
264  
265 L<http://deps.cpantesters.org/?module=Geo-Coder-DataScienceToolkit>
266  
267 =back
268  
269 =head1 LICENSE AND COPYRIGHT
270  
271 Copyright 2019-2026 Nigel Horne.
272  
273 This program is released under the following licence: GPL2
274  
275 =cut
276  
277 6 1;
↑ Summary