blib/lib/Geo/Coder/XYZ.pm

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