blib/lib/Geo/Coder/Mapbox.pm

filter: Statementstmt 43.1% 38 / 88 Branchbran 15.6% 5 / 32 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 76.9% 10 / 13 Podpod 100% 4 / 4 totaltotal 38.4% 58 / 151 CC 20 SCAR 43.4
CC
20
Cov
34%
geocode
182.0
reverse_geocode
12.0
ua
6.0
CRAP 76.5
↑ 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::MaybeXS;
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.02
21  
22 =cut
23  
24 3 our $VERSION = '0.02';
25  
26 =head1 SYNOPSIS
27  
28 use Geo::Coder::Mapbox;
29  
30 my $geo_coder = Geo::Coder::Mapbox->new(access_token => $ENV{'MAPBOX_KEY'});
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 4 ▶sub new {
Subroutine new: called CC=5 SCAR=16.6
Pod documented
49 4 my $class = shift;
50 0 ▶ my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 4 4
51  
52 # Use Geo::Coder::Mapbox->new(), not Geo::Coder::Mapbox::new()
53 4 ▶ if(!defined($class)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $class) { } 1 3 4
elsif (ref $class) { } 1 2 3
54 # carp(__PACKAGE__, ' use ->new() not ::new() to instantiate');
55 # return;
56  
57 # FIXME: this only works when no arguments are given
58 1 $class = __PACKAGE__;
59 } elsif(ref($class)) {
60 # clone the given object
61 1 return bless { %{$class}, %args }, ref($class);
62 }
63  
64 3 ▶ my $ua = $args{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION");
Condition33%1/3
$args{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Mapbox' . "/$Geo::Coder::Mapbox::VERSION")
l!l&&r!l&&!r
030
MC/DC0%0/2
$args{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::Mapbox' . "/$Geo::Coder::Mapbox::VERSION")
$args{'ua'} 'LWP::UserAgent'->new('agent', 'Geo::Coder::Mapbox' . "/$Geo::Coder::Mapbox::VERSION")
Truth table33%1/3
A || B
A= $args{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::Mapbox' . "/$Geo::Coder::Mapbox::VERSION")
ABresult
1X1
011
000
65 # if(!defined($args{'host'})) {
66 # $ua->ssl_opts(verify_hostname => 0); # Yuck
67 # }
68 3 my %defaults = (
69 host => 'api.mapbox.com',
70 access_token => ''
71 );
72  
73 # Re-seen keys take precedence, so defaults come first
74 3 return bless { %defaults, %args, ua => $ua }, $class;
75 }
76  
77 =head2 geocode
78  
79 $location = $geo_coder->geocode(location => 'Toronto, Ontario, Canada');
80  
81 print 'Latitude: ', $location->{features}[0]->{center}[1], "\n"; # Latitude
82 print 'Longitude: ', $location->{features}[0]->{center}[0], "\n"; # Longitude
83  
84 @locations = $geo_coder->geocode('Portland, USA');
85 print 'There are Portlands in ', join (', ', map { $_->{'state'} } @locations), "\n";
86  
87 =cut
88  
89 0 ▶sub geocode {
Subroutine geocode: not called CC=13 SCAR=52.0
Pod documented
90 0 my $self = shift;
91 0 my %param;
92  
93 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
94 0 %param = %{$_[0]};
95 } elsif(ref($_[0])) {
96 0 Carp::croak('Usage: geocode(location => $location)');
97 0 return; # Not sure why this is needed, but t/carp.t fails without it
98 } elsif(@_ % 2 == 0) {
99 0 %param = @_;
100 } else {
101 0 $param{location} = shift;
102 }
103  
104 my $location = $param{location}
105 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
106  
107 0 ▶ if (Encode::is_utf8($location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($location)) 0 0 0
108 0 $location = Encode::encode_utf8($location);
109 }
110  
111 0 my $uri = URI->new("https://$self->{host}/geocoding/v5/mapbox.places/$location.json");
112 0 $location =~ s/\s/+/g;
113 0 my %query_parameters = ('access_token' => $self->{'access_token'});
114 0 $uri->query_form(%query_parameters);
115 0 my $url = $uri->as_string();
116  
117 # ::diag($url);
118  
119 0 my $res = $self->{ua}->get($url);
120  
121 0 ▶ if ($res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error) 0 0 0
122 0 Carp::carp("API returned error: on $url ", $res->status_line());
123 0 return { };
124 }
125  
126 0 my $json = JSON::MaybeXS->new()->utf8();
127 0 my $rc;
128 0 eval {
129 0 $rc = $json->decode($res->content());
130 };
131 0 ▶ if(!defined($rc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rc) 0 0 0
132 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
133 0 Carp::carp("$url: $@");
134 0 return { };
135 }
136 0 Carp::carp("$url: can't decode the JSON ", $res->content());
137 0 return { };
138 }
139  
140 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
141 (ref($rc->{'otherlocations'}->{'loc'}) eq 'ARRAY')) {
142 0 my @rc = @{$rc->{'otherlocations'}->{'loc'}};
143 0 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
144 0 return @rc;
145 }
146 0 return $rc[0];
147 }
148 0 return $rc;
149  
150 # my @results = @{ $data || [] };
151 # wantarray ? @results : $results[0];
152 }
153  
154 =head2 ua
155  
156 Accessor method to get and set UserAgent object used internally. You
157 can call I<env_proxy> for example, to get the proxy information from
158 environment variables:
159  
160 $geo_coder->ua()->env_proxy(1);
161  
162 You can also set your own User-Agent object:
163  
164 use LWP::UserAgent::Throttled;
165 my $ua = LWP::UserAgent::Throttled->new();
166 $ua->throttle({ 'api.mapbox.com' => 2 });
167 $geo_coder->ua($ua);
168  
169 =cut
170  
171 0 ▶sub ua {
Subroutine ua: not called CC=2 SCAR=17.9
Pod documented
172 0 my $self = shift;
173 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
174 0 $self->{ua} = shift;
175 }
176 0 $self->{ua};
177 }
178  
179 =head2 reverse_geocode
180  
181 $location = $geo_coder->reverse_geocode(lnglat => '-122.39732,37.778907');
182  
183 Similar to geocode except it expects a longitude/latitude (note the order) parameter.
184  
185 =cut
186  
187 0 ▶sub reverse_geocode {
Subroutine reverse_geocode: not called CC=3 SCAR=24.8
Pod documented
188 0 my $self = shift;
189  
190 0 my %param;
191 0 ▶ if (@_ % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 == 0) { } 0 0 0
192 0 %param = @_;
193 } else {
194 0 $param{lnglat} = shift;
195 }
196  
197 my $lnglat = $param{lnglat}
198 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
199  
200 # return $self->geocode(location => $lnglat, reverse => 1);
201 0 return $self->geocode(location => $lnglat);
202 }
203  
204 =head1 AUTHOR
205  
206 Nigel Horne, C<< <njh at bandsman.co.uk> >>
207  
208 Based on L<Geo::Coder::GooglePlaces>.
209  
210 This library is free software; you can redistribute it and/or modify
211 it under the same terms as Perl itself.
212  
213 Lots of thanks to the folks at mapbox.com.
214  
215 =head1 SEE ALSO
216  
217 L<Geo::Coder::GooglePlaces>, L<HTML::GoogleMaps::V3>, L<https://docs.mapbox.com/api/search/geocoding/>
218  
219 =head1 LICENSE AND COPYRIGHT
220  
221 Copyright 2021-2024 Nigel Horne.
222  
223 This program is released under the following licence: GPL2
224  
225 =cut
226  
227 3 1;
↑ Summary