blib/lib/Geo/Coder/Mapbox.pm

filter: Statementstmt 43.1% 38 / 88 Branchbran 10.0% 3 / 30 Conditioncond 25.0% 3 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 76.9% 10 / 13 Podpod 100% 4 / 4 totaltotal 38.3% 59 / 154 SCAR 49.1
CC
20
Cov
34%
geocode
182.0
reverse_geocode
12.0
ua
6.0
CRAP 135.8
↑ Summary
1 package Geo::Coder::Mapbox;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Geo::Coder::Mapbox - Provides a Geo-Coding functionality using L<https://mapbox.com>
17  
18 =head1 VERSION
19  
20 Version 0.01
21  
22 =cut
23  
24 3 our $VERSION = '0.01';
25  
26 =head1 SYNOPSIS
27  
28 use Geo::Coder::Mapbox;
29  
30 my $geo_coder = Geo::Coder::Mapbox->new(access_token => $ENV{'MAPBOX'});
31 my $location = $geo_coder->geocode(location => 'Washington, DC');
32  
33 =head1 DESCRIPTION
34  
35 Geo::Coder::Mapbox provides an interface to mapbox.com, a Geo-Coding database covering many countries.
36  
37 =head1 METHODS
38  
39 =head2 new
40  
41 $geo_coder = Geo::Coder::Mapbox->new();
42 my $ua = LWP::UserAgent->new();
43 $ua->env_proxy(1);
44 $geo_coder = Geo::Coder::Mapbox->new(ua => $ua);
45  
46 =cut
47  
48 sub new {
49 2 ▶ my $proto = shift;
Subroutine new: called CC=5 SCAR=16.8
Pod documented
50 2 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
011
MC/DC50%1/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
51  
52 # Use Geo::Coder::Mapbox->new(), not Geo::Coder::Mapbox::new()
53 2 ▶ if(!defined($class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class) 1 1 2
54 1 carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
55 1 return;
56 }
57  
58 1 ▶ my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 1 1
59  
60 1 ▶ my $ua = delete $args{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
Condition33%1/3
delete $args{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Mapbox' . "/$Geo::Coder::Mapbox::VERSION")
l!l&&r!l&&!r
010
MC/DC0%0/2
delete $args{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Mapbox' . "/$Geo::Coder::Mapbox::VERSION")
delete $args{'ua'} 'LWP::UserAgent'->new('agent', 'Geo::Coder::Mapbox' . "/$Geo::Coder::Mapbox::VERSION")
Truth table33%1/3
A || B
A= delete $args{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::Mapbox' . "/$Geo::Coder::Mapbox::VERSION")
ABresult
1X1
011
000
61 # if(!defined($args{'host'})) {
62 # $ua->ssl_opts(verify_hostname => 0); # Yuck
63 # }
64 1 my %defaults = (
65 host => 'api.mapbox.com',
66 access_token => ''
67 );
68  
69 1 return bless { %defaults, %args, ua => $ua }, $class;
70 }
71  
72 =head2 geocode
73  
74 $location = $geo_coder->geocode(location => 'Toronto, Ontario, Canada');
75  
76 print 'Latitude: ', $location->{features}[0]->{center}[1], "\n"; # Latitude
77 print 'Longitude: ', $location->{features}[0]->{center}[0], "\n"; # Longitude
78  
79 @locations = $geo_coder->geocode('Portland, USA');
80 print 'There are Portlands in ', join (', ', map { $_->{'state'} } @locations), "\n";
81  
82 =cut
83  
84 sub geocode {
85 0 ▶ my $self = shift;
Subroutine geocode: not called CC=13 SCAR=52.0
Pod documented
86 0 my %param;
87  
88 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
89 0 %param = %{$_[0]};
90 } elsif(ref($_[0])) {
91 0 Carp::croak('Usage: geocode(location => $location)');
92 0 return; # Not sure why this is needed, but t/carp.t fails without it
93 } elsif(@_ % 2 == 0) {
94 0 %param = @_;
95 } else {
96 0 $param{location} = shift;
97 }
98  
99 my $location = $param{location}
100 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
101  
102 0 ▶ if (Encode::is_utf8($location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($location)) 0 0 0
103 0 $location = Encode::encode_utf8($location);
104 }
105  
106 0 my $uri = URI->new("https://$self->{host}/geocoding/v5/mapbox.places/$location.json");
107 0 $location =~ s/\s/+/g;
108 0 my %query_parameters = ('access_token' => $self->{'access_token'});
109 0 $uri->query_form(%query_parameters);
110 0 my $url = $uri->as_string();
111  
112 # ::diag($url);
113  
114 0 my $res = $self->{ua}->get($url);
115  
116 0 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
117 0 Carp::carp("API returned error: on $url ", $res->status_line());
118 0 return { };
119 }
120  
121 0 my $json = JSON->new()->utf8();
122 0 my $rc;
123 0 eval {
124 0 $rc = $json->decode($res->content());
125 };
126 0 ▶ if(!defined($rc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 0 0 0
127 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
128 0 Carp::carp("$url: $@");
129 0 return { };
130 }
131 0 Carp::carp("$url: can't decode the JSON ", $res->content());
132 0 return { };
133 }
134  
135 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
136 (ref($rc->{'otherlocations'}->{'loc'}) eq 'ARRAY')) {
137 0 my @rc = @{$rc->{'otherlocations'}->{'loc'}};
138 0 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
139 0 return @rc;
140 }
141 0 return $rc[0];
142 }
143 0 return $rc;
144  
145 # my @results = @{ $data || [] };
146 # wantarray ? @results : $results[0];
147 }
148  
149 =head2 ua
150  
151 Accessor method to get and set UserAgent object used internally. You
152 can call I<env_proxy> for example, to get the proxy information from
153 environment variables:
154  
155 $geo_coder->ua()->env_proxy(1);
156  
157 You can also set your own User-Agent object:
158  
159 use LWP::UserAgent::Throttled;
160 my $ua = LWP::UserAgent::Throttled->new();
161 $ua->throttle({ 'mapbox.com' => 2 });
162 $geo_coder->ua($ua);
163  
164 =cut
165  
166 sub ua {
167 0 ▶ my $self = shift;
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
168 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
169 0 $self->{ua} = shift;
170 }
171 0 $self->{ua};
172 }
173  
174 =head2 reverse_geocode
175  
176 $location = $geo_coder->reverse_geocode(lnglat => '-122.39732,37.778907');
177  
178 Similar to geocode except it expects a longitude/latitude (note the order) parameter.
179  
180 =cut
181  
182 sub reverse_geocode {
183 0 ▶ my $self = shift;
Subroutine reverse_geocode: not called CC=3 SCAR=24.8
Pod documented
184  
185 0 my %param;
186 0 ▶ if (@_ % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 0) { } 0 0 0
187 0 %param = @_;
188 } else {
189 0 $param{lnglat} = shift;
190 }
191  
192 my $lnglat = $param{lnglat}
193 0 ▶ or Carp::carp('Usage: reverse_geocode(location => $lnglat)');
Branch: 1 branch
BranchTrueFalseTotal
my $lnglat = $param{"lnglat"} or Carp::carp("Usage: reverse_geocode(location => \$lnglat)") 0 0 0
194  
195 # return $self->geocode(location => $lnglat, reverse => 1);
196 0 return $self->geocode(location => $lnglat);
197 }
198  
199 =head1 AUTHOR
200  
201 Nigel Horne, C<< <njh at bandsman.co.uk> >>
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 mapbox.com.
209  
210 =head1 SEE ALSO
211  
212 L<Geo::Coder::GooglePlaces>, L<HTML::GoogleMaps::V3>, L<https://docs.mapbox.com/api/search/geocoding/>
213  
214 =head1 LICENSE AND COPYRIGHT
215  
216 Copyright 2021 Nigel Horne.
217  
218 This program is released under the following licence: GPL2
219  
220 =cut
221  
222 3 1;
↑ Summary