blib/lib/Geo/Coder/XYZ.pm

filter: Statementstmt 72.8% 67 / 92 Branchbran 40.0% 12 / 30 Conditioncond 45.4% 5 / 11 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 85.7% 12 / 14 Podpod 100% 4 / 4 totaltotal 64.3% 101 / 157 CC 20 SCAR 28.0
CC
20
Cov
63%
geocode
36.2
new
6.1
ua
6.0
CRAP 16.4
↑ Summary
1 package Geo::Coder::XYZ;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Params::Get;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Geo::Coder::XYZ - Provides a Geo-Coding functionality using L<https://geocode.xyz>
18  
19 =head1 VERSION
20  
21 Version 0.10
22  
23 =cut
24  
25 7 our $VERSION = '0.10';
26  
27 =head1 SYNOPSIS
28  
29 use Geo::Coder::XYZ;
30  
31 my $geo_coder = Geo::Coder::XYZ->new();
32 my $location = $geo_coder->geocode(location => '10 Downing St., London, UK');
33  
34 =head1 DESCRIPTION
35  
36 Geo::Coder::XYZ provides an interface to geocode.xyz, a free Geo-Coding database covering many countries.
37  
38 =head1 METHODS
39  
40 =head2 new
41  
42 $geo_coder = Geo::Coder::XYZ->new();
43 my $ua = LWP::UserAgent->new();
44 $ua->env_proxy(1);
45 $geo_coder = Geo::Coder::XYZ->new(ua => $ua);
46  
47 =cut
48  
49 3 ▶sub new {
Subroutine new: called CC=6 SCAR=18.2
Pod documented
50 3 my $proto = shift;
51 3 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
021
MC/DC50%1/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
52  
53 # Use Geo::Coder::XYZ->new(), not Geo::Coder::XYZ::new()
54 3 ▶ if(!defined($class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class) 1 2 3
55 1 carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
56 1 return;
57 }
58  
59 2 my $params = Params::Get::get_params(undef, \@_);
60  
61 2 my $ua = $params->{ua};
62 2 ▶ if(!defined($ua)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ua) 2 0 2
63 2 $ua = LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
64 2 $ua->default_header(accept_encoding => 'gzip,deflate');
65 }
66 2 ▶ if(!defined($params->{'host'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params->{"host"}) 2 0 2
67 2 $ua->ssl_opts(verify_hostname => 0); # Yuck
68 }
69 2 ▶ my $host = $params->{host} || 'geocode.xyz';
Condition50%1/2
$params->{'host'} || 'geocode.xyz'
l!l
02
MC/DC0%0/1
$params->{'host'} || 'geocode.xyz'
$params->{'host'}
Truth table50%1/2
A
A= $params->{'host'}
Aresult
11
00
70  
71 2 return bless { ua => $ua, host => $host }, $class;
72 }
73  
74 =head2 geocode
75  
76 $location = $geo_coder->geocode(location => $location);
77  
78 print 'Latitude: ', $location->{'latt'}, "\n";
79 print 'Longitude: ', $location->{'longt'}, "\n";
80  
81 @locations = $geo_coder->geocode('Portland, USA');
82 print 'There are Portlands in ', join (', ', map { $_->{'state'} } @locations), "\n";
83  
84 =cut
85  
86 2 ▶sub geocode {
Subroutine geocode: called CC=13 SCAR=35.9
Pod documented
87 2 my $self = shift;
88 2 my $params = Params::Get::get_params('location', \@_);
89  
90 my $location = $params->{location}
91 1 ▶ or Carp::croak('Usage: geocode(location => $location)');
Branch: 1 branch
BranchTrueFalseTotal
my $location = $params->{"location"} or Carp::croak("Usage: geocode(location => \$location)") 1 0 1
92  
93 # Fail when the input is just a set of numbers
94 1 ▶ if($params->{'location'} !~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->{"location"} =~ /\D/) 0 1 1
95 0 Carp::croak('Usage: ', __PACKAGE__, ": invalid input to geocode(), $params->{location}");
96 0 return;
97 }
98  
99 1 ▶ if (Encode::is_utf8($location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($location)) 0 1 1
100 0 $location = Encode::encode_utf8($location);
101 }
102  
103 1 my $uri = URI->new("https://$self->{host}/");
104 1 ▶ if($location =~ /(.+),\s*England$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ /(.+),\s*England$/i) 0 1 1
105 0 $location = "$1, United Kingdom"; # geocode.xyz gets confused between England and New England
106 }
107 1 $location =~ s/\s/+/g;
108 1 my %query_parameters = ('locate' => $location, 'json' => 1);
109 1 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 1 1
110 # moreinfo is needed to find alternatives when the given location is ambiguous
111 0 $query_parameters{'moreinfo'} = 1;
112 }
113 1 $uri->query_form(%query_parameters);
114 1 my $url = $uri->as_string();
115  
116 1 my $res = $self->{ua}->get($url);
117  
118 1 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 1 1
119 0 Carp::carp("API returned error: on $url ", $res->status_line());
120 0 return { };
121 }
122  
123 1 my $json = JSON::MaybeXS->new()->utf8();
124 1 my $rc;
125 1 eval {
126 1 $rc = $json->decode($res->decoded_content());
127 };
128 1 ▶ if(!defined($rc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 0 1 1
129 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
130 0 Carp::carp("$url: $@");
131 0 return { };
132 }
133 0 Carp::carp("$url: can't decode the JSON ", $res->content());
134 0 return { };
135 }
136  
137 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
138 (ref($rc->{'otherlocations'}->{'loc'}) eq 'ARRAY')) {
139 0 my @rc = @{$rc->{'otherlocations'}->{'loc'}};
140 0 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
141 0 return @rc;
142 }
143 0 return $rc[0];
144 }
145 1 return $rc;
146  
147 # my @results = @{ $data || [] };
148 # wantarray ? @results : $results[0];
149 }
150  
151 =head2 ua
152  
153 Accessor method to get and set UserAgent object used internally. You
154 can call I<env_proxy> for example, to get the proxy information from
155 environment variables:
156  
157 $geo_coder->ua()->env_proxy(1);
158  
159 You can also set your own User-Agent object:
160  
161 use LWP::UserAgent::Throttled;
162 my $ua = LWP::UserAgent::Throttled->new();
163 $ua->throttle({ 'geocode.xyz' => 2 });
164 $geo_coder->ua($ua);
165  
166 =cut
167  
168 0 ▶sub ua {
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
169 0 my $self = shift;
170 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
171 0 $self->{ua} = shift;
172 }
173 0 $self->{ua};
174 }
175  
176 =head2 reverse_geocode
177  
178 $location = $geo_coder->reverse_geocode(latlng => '37.778907,-122.39732');
179  
180 Similar to geocode except it expects a latitude/longitude parameter.
181  
182 =cut
183  
184 sub reverse_geocode
185 0 ▶{
Subroutine reverse_geocode: not called CC=2 SCAR=17.9
Pod documented
186 0 my $self = shift;
187 0 my $params = Params::Get::get_params('latlng', \@_);
188  
189 0 ▶ my $latlng = $params->{'latlng'}
Branch: 1 branch
BranchTrueFalseTotal
my $latlng = $params->{"latlng"} or Carp::carp("Usage: reverse_geocode(latlng => \$latlng)") 0 0 0
190 or Carp::carp('Usage: reverse_geocode(latlng => $latlng)');
191  
192 0 return $self->geocode(location => $latlng, reverse => 1);
193 }
194  
195 =head1 SUPPORT
196  
197 This module is provided as-is without any warranty.
198  
199 =head1 AUTHOR
200  
201 Nigel Horne, C<< <njh at nigelhorne.com> >>
202  
203 Based on L<Geo::Coder::GooglePlaces>.
204  
205 This library is free software; you can redistribute it and/or modify
206 it under the same terms as Perl itself.
207  
208 Lots of thanks to the folks at geocode.xyz.
209  
210 =head1 SEE ALSO
211  
212 L<Geo::Coder::GooglePlaces>, L<HTML::GoogleMaps::V3>
213  
214 =head1 LICENSE AND COPYRIGHT
215  
216 Copyright 2017-2025 Nigel Horne.
217  
218 This program is released under the following licence: GPL2
219  
220 =cut
221  
222 7 1;
↑ Summary