blib/lib/Geo/Coder/XYZ.pm

filter: Statementstmt 75.0% 78 / 104 Branchbran 52.0% 25 / 48 Conditioncond 35.2% 6 / 17 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 86.6% 13 / 15 Podpod 100% 4 / 4 totaltotal 64.1% 127 / 198 CC 31 SCAR 29.3
CC
31
Cov
64%
geocode
37.6
_could_be_a_location
19.1
new
6.1
CRAP 18.7
↑ Summary
1 package Geo::Coder::XYZ;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶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 8 my $HAVE_RULE; # undef until the rule is first looked for
22 8 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use Params::Get;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head1 NAME
30  
31 Geo::Coder::XYZ - Provides a Geo-Coding functionality using L<https://geocode.xyz>
32  
33 =head1 VERSION
34  
35 Version 0.11
36  
37 =cut
38  
39 8 our $VERSION = '0.11';
40  
41 =head1 SYNOPSIS
42  
43 use Geo::Coder::XYZ;
44  
45 my $geo_coder = Geo::Coder::XYZ->new();
46 my $location = $geo_coder->geocode(location => '10 Downing St., London, UK');
47  
48 =head1 DESCRIPTION
49  
50 Geo::Coder::XYZ provides an interface to geocode.xyz, a free Geo-Coding database covering many countries.
51  
52 =head1 METHODS
53  
54 =head2 new
55  
56 $geo_coder = Geo::Coder::XYZ->new();
57 my $ua = LWP::UserAgent->new();
58 $ua->env_proxy(1);
59 $geo_coder = Geo::Coder::XYZ->new(ua => $ua);
60  
61 =cut
62  
63 15 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
64 15 my $proto = shift;
65 15 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
0141
MC/DC50%1/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
66  
67 # Use Geo::Coder::XYZ->new(), not Geo::Coder::XYZ::new()
68 15 ▶ if(!defined($class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class) 1 14 15
69 1 carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
70 1 return;
71 }
72  
73 14 my $params = Params::Get::get_params(undef, \@_);
74  
75 14 my $ua = $params->{ua};
76 14 ▶ if(!defined($ua)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ua) 2 12 14
77 2 $ua = LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
78 2 $ua->default_header(accept_encoding => 'gzip,deflate');
79 }
80 14 ▶ if(!defined($params->{'host'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params->{"host"}) 14 0 14
81 14 $ua->ssl_opts(verify_hostname => 0); # Yuck
82 }
83 14 ▶ my $host = $params->{host} || 'geocode.xyz';
Condition50%1/2
$params->{'host'} || 'geocode.xyz'
l!l
014
MC/DC0%0/1
$params->{'host'} || 'geocode.xyz'
$params->{'host'}
Truth table50%1/2
A
A= $params->{'host'}
Aresult
11
00
84  
85 14 return bless { ua => $ua, host => $host }, $class;
86 }
87  
88 =head2 geocode
89  
90 $location = $geo_coder->geocode(location => $location);
91  
92 print 'Latitude: ', $location->{'latt'}, "\n";
93 print 'Longitude: ', $location->{'longt'}, "\n";
94  
95 @locations = $geo_coder->geocode('Portland, USA');
96 print 'There are Portlands in ', join (', ', map { $_->{'state'} } @locations), "\n";
97  
98 =cut
99  
100 # The rule when it is installed, and the one thing that can be said without it.
101 # A reference is not a location however it would print: when the rule is there it
102 # says so, and when it is not this refuses it - the hole this module used to have
103 # was testing the string a reference turns into. Whether a string of digits is a
104 # place is the rule's question, so without the rule it is not asked: the string
105 # is looked up, as it was before the rule existed.
106 12 ▶sub _could_be_a_location {
Subroutine _could_be_a_location: called CC=9 SCAR=29.5
107 12 my $location = shift;
108  
109 12 ▶ if(!defined($HAVE_RULE)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $HAVE_RULE) 2 10 12
110 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
111 2 require Geo::Location::Predicate;
112 2 ▶ Geo::Location::Predicate->can('_valid_location') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'Geo::Location::Predicate'->can('_valid_location') ? : 2 0 2
113 } ? 1 : 0;
114 }
115  
116 # When the rule is installed it decides everything, references included, so
117 # that this module cannot be stricter or looser than the facade. Without it
118 # there are two answers this module can give on its own.
119 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
120  
121 0 ▶ return 0 if ref($location);
Branch: 1 branch
BranchTrueFalseTotal
if ref $location 0 0 0
122 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
123 0 return 1;
124 }
125  
126 13 ▶sub geocode {
Subroutine geocode: called CC=16 SCAR=36.3
Pod documented
127 13 my $self = shift;
128 # @_ rather than \@_: Params::Get only unwraps that shorthand when the
129 # value is a plain string, so passing the reference to the argument list
130 # hid the value from the checks below and put the argument list itself in
131 # its place.
132 13 my $params = Params::Get::get_params('location', @_);
133  
134 12 my $location = $params->{location};
135  
136 # A scalar reference is one way of passing the string it points at, and
137 # Params::Get dereferences it when it is the only argument. Do it here as
138 # well, in every calling form, so that it means the string however the
139 # method is called.
140 12 ▶ $location = ${$location} if ref($location) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $location eq "SCALAR" 1 11 12
141  
142 # What counts as a location is Geo::Location::Predicate's answer, not a
143 # second copy of it here: undef and the empty string are nothing to look up, a
144 # reference is not a location however it would print, and a string of digits is
145 # what a numeric column holds when the wrong one has been read.
146 12 ▶ if(!_could_be_a_location($location)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_could_be_a_location($location)) 7 5 12
147 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
148 unless(defined($location) && length($location));
149 7 ▶ Carp::croak('Usage: ', __PACKAGE__, ': invalid input to geocode(), ',
Branch: 1 branch
BranchTrueFalseTotal
ref $location ? : 6 1 7
150 ref($location) ? 'a reference is not a location' : $location);
151 }
152  
153 5 ▶ if (Encode::is_utf8($location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($location)) 0 5 5
154 0 $location = Encode::encode_utf8($location);
155 }
156  
157 5 my $uri = URI->new("https://$self->{host}/");
158 5 ▶ if($location =~ /(.+),\s*England$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ /(.+),\s*England$/i) 0 5 5
159 0 $location = "$1, United Kingdom"; # geocode.xyz gets confused between England and New England
160 }
161 5 $location =~ s/\s/+/g;
162 5 my %query_parameters = ('locate' => $location, 'json' => 1);
163 5 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 5 5
164 # moreinfo is needed to find alternatives when the given location is ambiguous
165 0 $query_parameters{'moreinfo'} = 1;
166 }
167 5 $uri->query_form(%query_parameters);
168 5 my $url = $uri->as_string();
169  
170 5 my $res = $self->{ua}->get($url);
171  
172 1 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 1 1
173 0 Carp::carp("API returned error: on $url ", $res->status_line());
174 0 return { };
175 }
176  
177 1 my $json = JSON::MaybeXS->new()->utf8();
178 1 my $rc;
179 1 eval {
180 1 $rc = $json->decode($res->decoded_content());
181 };
182 1 ▶ if(!defined($rc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 0 1 1
183 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
184 0 Carp::carp("$url: $@");
185 0 return { };
186 }
187 0 Carp::carp("$url: can't decode the JSON ", $res->content());
188 0 return { };
189 }
190  
191 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
192 (ref($rc->{'otherlocations'}->{'loc'}) eq 'ARRAY')) {
193 0 my @rc = @{$rc->{'otherlocations'}->{'loc'}};
194 0 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
195 0 return @rc;
196 }
197 0 return $rc[0];
198 }
199 1 return $rc;
200  
201 # my @results = @{ $data || [] };
202 # wantarray ? @results : $results[0];
203 }
204  
205 =head2 ua
206  
207 Accessor method to get and set UserAgent object used internally. You
208 can call I<env_proxy> for example, to get the proxy information from
209 environment variables:
210  
211 $geo_coder->ua()->env_proxy(1);
212  
213 You can also set your own User-Agent object:
214  
215 use LWP::UserAgent::Throttled;
216 my $ua = LWP::UserAgent::Throttled->new();
217 $ua->throttle({ 'geocode.xyz' => 2 });
218 $geo_coder->ua($ua);
219  
220 =cut
221  
222 0 ▶sub ua {
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
223 0 my $self = shift;
224 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
225 0 $self->{ua} = shift;
226 }
227 0 $self->{ua};
228 }
229  
230 =head2 reverse_geocode
231  
232 $location = $geo_coder->reverse_geocode(latlng => '37.778907,-122.39732');
233  
234 Similar to geocode except it expects a latitude/longitude parameter.
235  
236 =cut
237  
238 sub reverse_geocode
239 0 ▶{
Subroutine reverse_geocode: not called CC=2 SCAR=17.9
Pod documented
240 0 my $self = shift;
241 0 my $params = Params::Get::get_params('latlng', @_);
242  
243 0 ▶ my $latlng = $params->{'latlng'}
Branch: 1 branch
BranchTrueFalseTotal
my $latlng = $params->{"latlng"} or Carp::carp("Usage: reverse_geocode(latlng => \$latlng)") 0 0 0
244 or Carp::carp('Usage: reverse_geocode(latlng => $latlng)');
245  
246 0 return $self->geocode(location => $latlng, reverse => 1);
247 }
248  
249 =head1 SUPPORT
250  
251 This module is provided as-is without any warranty.
252  
253 =head1 AUTHOR
254  
255 Nigel Horne, C<< <njh at nigelhorne.com> >>
256  
257 Based on L<Geo::Coder::GooglePlaces>.
258  
259 This library is free software; you can redistribute it and/or modify
260 it under the same terms as Perl itself.
261  
262 Lots of thanks to the folks at geocode.xyz.
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 8 1;
↑ Summary