| 1 | package Geo::Coder::DataScienceToolkit; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use LWP::Protocol::http; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | Geo::Coder::DataScienceToolkit - Provides a geocoding functionality using | ||||||||||||||||||||||||||||||||||||||||||
| 17 | http://www.datasciencetoolkit.org/ | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | Version 0.02 | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 6 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | use Geo::Coder::DataScienceToolkit; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | my $geocoder = Geo::Coder::DataScienceToolkit->new(); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | my $location = $geocoder->geocode(location => '10 Downing St., London, UK'); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | Geo::Coder::DataScienceToolkit provides an interface to datasciencetoolkit, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | a free geocode database covering the US and UK. | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | $geocoder = Geo::Coder::DataScienceToolkit->new(); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | my $ua = LWP::UserAgent->new(); | ||||||||||||||||||||||||||||||||||||||||||
| 45 | $ua->env_proxy(1); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | $geocoder = Geo::Coder::DataScienceToolkit->new(ua => $ua); | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | my($class, %param) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | my $ua = delete $param{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION"); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
delete $param{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::DataScienceToolkit' . "/$Geo::Coder::DataScienceToolkit::VERSION")
MC/DC0%0/2
delete $param{'ua'} || 'LWP::UserAgent'->new('agent', 'Geo::Coder::DataScienceToolkit' . "/$Geo::Coder::DataScienceToolkit::VERSION")
delete $param{'ua'}
'LWP::UserAgent'->new('agent', 'Geo::Coder::DataScienceToolkit' . "/$Geo::Coder::DataScienceToolkit::VERSION")
Truth table33%1/3
A || B
A= delete $param{'ua'}B= 'LWP::UserAgent'->new('agent', 'Geo::Coder::DataScienceToolkit' . "/$Geo::Coder::DataScienceToolkit::VERSION")
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | # if(!defined($param{'host'})) { | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # $ua->ssl_opts(verify_hostname => 0); # Yuck | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | my $host = delete $param{host} || 'www.datasciencetoolkit.org'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $param{'host'} || 'www.datasciencetoolkit.org'
MC/DC0%0/1
delete $param{'host'} || 'www.datasciencetoolkit.org'
delete $param{'host'}
Truth table50%1/2
A
A= delete $param{'host'}
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | return bless { ua => $ua, host => $host }, $class; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | =head2 geocode | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | $location = $geocoder->geocode(location => $location); | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | print 'Latitude: ', $location->{'results'}[0]->{'geometry'}->{'location'}->{'lat'}, 38.90, 1e-2); "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 67 | print 'Longitude: ', delta_within($location->{'results'}[0]->{'geometry'}->{'location'}->{'lng'}, -77.04, 1e-2); "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | @locations = $geocoder->geocode('Portland, USA'); | ||||||||||||||||||||||||||||||||||||||||||
| 70 | diag 'There are Portlands in ', join (', ', map { $_->{'state'} } @locations); | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub geocode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
geocode: not called CC=17 SCAR=57.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my %params; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if(!ref($self)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if(scalar(@_)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | return(__PACKAGE__->new()->parse(@_)); | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | return(__PACKAGE__->new()->parse($self)); | |||||||||||||||||||||||||||||||||||||||||
| 83 | } elsif(ref($self) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | return(__PACKAGE__->new()->parse($self)); | |||||||||||||||||||||||||||||||||||||||||
| 85 | } elsif(ref($_[0]) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | %params = %{$_[0]}; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } elsif(ref($_[0])) { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | Carp::croak('Usage: ', __PACKAGE__, '::geocode(location => $location)'); | |||||||||||||||||||||||||||||||||||||||||
| 89 | ▶ | } elsif(scalar(@_) && (scalar(@_) % 2 == 0)) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
scalar @_ and scalar(@_) % 2 == 0
MC/DC0%0/2
scalar @_ and scalar(@_) % 2 == 0
scalar @_
scalar(@_) % 2 == 0
Truth table0%0/3
A and B
A= scalar @_B= scalar(@_) % 2 == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $params{'location'} = shift; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | my $location = $params{location} | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | or Carp::croak("Usage: geocode(location => \$location)"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (Encode::is_utf8($location)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $location = Encode::encode_utf8($location); | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $uri = URI->new("http://$self->{host}/maps/api/geocode/json"); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $location =~ s/\s/+/g; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my %query_parameters = ('address' => $location, 'sensor' => 'false'); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $uri->query_form(%query_parameters); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my $url = $uri->as_string(); | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my $res = $self->{ua}->get($url); | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($res->is_error) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | Carp::carp("API returned error: on $url ", $res->status_line()); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | return { }; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my $json = JSON->new()->utf8(); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my $rc; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $rc = $json->decode($res->content()); | |||||||||||||||||||||||||||||||||||||||||
| 119 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if(!defined($rc)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | Carp::carp("$url: $@"); | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | return { }; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | Carp::carp("$url: can't decode the JSON ", $res->content()); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | return { }; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if($rc->{'otherlocations'} && $rc->{'otherlocations'}->{'loc'} && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$rc->{"otherlocations"} and $rc->{"otherlocations"}{"loc"} and ref $rc->{"otherlocations"}{"loc"} eq "ARRAY"
$rc->{"otherlocations"} and $rc->{"otherlocations"}{"loc"}
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | (ref($rc->{'otherlocations'}->{'loc'}) eq 'ARRAY')) { | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my @rc = @{$rc->{'otherlocations'}->{'loc'}}; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if(wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | return @rc; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | return $rc[0]; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | return $rc; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | # my @results = @{ $data || [] }; | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # wantarray ? @results : $results[0]; | ||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =head2 ua | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | Accessor method to get and set UserAgent object used internally. You | ||||||||||||||||||||||||||||||||||||||||||
| 146 | can call I<env_proxy> for example, to get the proxy information from | ||||||||||||||||||||||||||||||||||||||||||
| 147 | environment variables: | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | $geocoder->ua()->env_proxy(1); | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | You can also set your own User-Agent object: | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | use LWP::UserAgent::Throttled; | ||||||||||||||||||||||||||||||||||||||||||
| 154 | $geocoder->ua(LWP::UserAgent::Throttled->new()); | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub ua { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ua: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | $self->{ua} = shift; | |||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | $self->{ua}; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | =head2 reverse_geocode | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | Reverse geocoding is not supported by datasciencetoolkit.org, so calls to | ||||||||||||||||||||||||||||||||||||||||||
| 169 | this will generate an error. | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | sub reverse_geocode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reverse_geocode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | Carp::carp('datasciencetoolkit.org does not support reverse encoding'); | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | Nigel Horne <njh@bandsman.co.uk> | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | Based on L<Geo::Coder::XYZ>. | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 184 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | Lots of thanks to the folks at DSTK. | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | L<Geo::Coder::GooglePlaces>, | ||||||||||||||||||||||||||||||||||||||||||
| 191 | L<HTML::GoogleMaps::V3>, | ||||||||||||||||||||||||||||||||||||||||||
| 192 | L<http://www.datasciencetoolkit.org/about>. | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | Copyright 2019 Nigel Horne. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | This program is released under the following licence: GPL2 | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||