| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Geo::Coder::CA; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# See also https://geocoding.geo.census.gov/geocoder/Geocoding_Services_API.html for the US for the future |
|
4
|
|
|
|
|
|
|
|
|
5
|
5
|
|
|
5
|
|
708402
|
use strict; |
|
|
5
|
|
|
|
|
50
|
|
|
|
5
|
|
|
|
|
138
|
|
|
6
|
5
|
|
|
5
|
|
23
|
use warnings; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
132
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
5
|
|
|
5
|
|
20
|
use Carp; |
|
|
5
|
|
|
|
|
8
|
|
|
|
5
|
|
|
|
|
241
|
|
|
9
|
5
|
|
|
5
|
|
2430
|
use Encode; |
|
|
5
|
|
|
|
|
67944
|
|
|
|
5
|
|
|
|
|
439
|
|
|
10
|
5
|
|
|
5
|
|
2839
|
use JSON; |
|
|
5
|
|
|
|
|
46224
|
|
|
|
5
|
|
|
|
|
39
|
|
|
11
|
5
|
|
|
5
|
|
3338
|
use HTTP::Request; |
|
|
5
|
|
|
|
|
77737
|
|
|
|
5
|
|
|
|
|
184
|
|
|
12
|
5
|
|
|
5
|
|
3565
|
use LWP::UserAgent; |
|
|
5
|
|
|
|
|
129989
|
|
|
|
5
|
|
|
|
|
225
|
|
|
13
|
5
|
|
|
5
|
|
2683
|
use LWP::Protocol::https; |
|
|
5
|
|
|
|
|
459055
|
|
|
|
5
|
|
|
|
|
284
|
|
|
14
|
5
|
|
|
5
|
|
42
|
use URI; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
3395
|
|
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Geo::Coder::CA - Provides a Geo-Coding functionality using http:://geocoder.ca for both Canada and the US. |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 VERSION |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Version 0.12 |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=cut |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $VERSION = '0.12'; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use Geo::Coder::CA; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my $geo_coder = Geo::Coder::CA->new(); |
|
33
|
|
|
|
|
|
|
my $location = $geo_coder->geocode(location => '9235 Main St, Richibucto, New Brunswick, Canada'); |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Geo::Coder::CA provides an interface to geocoder.ca. Geo::Coder::Canada no longer seems to work. |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 METHODS |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head2 new |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$geo_coder = Geo::Coder::CA->new(); |
|
44
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
|
45
|
|
|
|
|
|
|
$ua->env_proxy(1); |
|
46
|
|
|
|
|
|
|
$geo_coder = Geo::Coder::CA->new(ua => $ua); |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=cut |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub new { |
|
51
|
3
|
|
|
3
|
1
|
2882
|
my($class, %param) = @_; |
|
52
|
|
|
|
|
|
|
|
|
53
|
3
|
100
|
|
|
|
13
|
if(!defined($class)) { |
|
54
|
|
|
|
|
|
|
# Geo::Coder::CA::new() used rather than Geo::Coder::CA->new() |
|
55
|
1
|
|
|
|
|
2
|
$class = __PACKAGE__; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
|
|
58
|
3
|
|
|
|
|
8
|
my $ua = delete $param{ua}; |
|
59
|
3
|
50
|
|
|
|
8
|
if(!defined($ua)) { |
|
60
|
3
|
|
|
|
|
33
|
$ua = LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION"); |
|
61
|
3
|
|
|
|
|
5667
|
$ua->default_header(accept_encoding => 'gzip,deflate'); |
|
62
|
|
|
|
|
|
|
} |
|
63
|
3
|
|
50
|
|
|
160
|
my $host = delete $param{host} || 'geocoder.ca'; |
|
64
|
|
|
|
|
|
|
|
|
65
|
3
|
|
|
|
|
39
|
return bless { ua => $ua, host => $host }, $class; |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head2 geocode |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
$location = $geo_coder->geocode(location => $location); |
|
71
|
|
|
|
|
|
|
# @location = $geo_coder->geocode(location => $location); |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
print 'Latitude: ', $location->{'latt'}, "\n"; |
|
74
|
|
|
|
|
|
|
print 'Longitude: ', $location->{'longt'}, "\n"; |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub geocode { |
|
79
|
3
|
|
|
3
|
1
|
1481
|
my $self = shift; |
|
80
|
3
|
|
|
|
|
4
|
my %param; |
|
81
|
|
|
|
|
|
|
|
|
82
|
3
|
100
|
|
|
|
18
|
if(ref($_[0]) eq 'HASH') { |
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
83
|
1
|
|
|
|
|
3
|
%param = %{$_[0]}; |
|
|
1
|
|
|
|
|
5
|
|
|
84
|
|
|
|
|
|
|
} elsif(ref($_[0])) { |
|
85
|
0
|
|
|
|
|
0
|
Carp::croak('Usage: geocode(location => $location)'); |
|
86
|
0
|
|
|
|
|
0
|
return; |
|
87
|
|
|
|
|
|
|
} elsif(@_ % 2 == 0) { |
|
88
|
2
|
|
|
|
|
6
|
%param = @_; |
|
89
|
|
|
|
|
|
|
} else { |
|
90
|
0
|
|
|
|
|
0
|
$param{location} = shift; |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
3
|
|
|
|
|
6
|
my $location = $param{location}; |
|
94
|
3
|
50
|
|
|
|
8
|
if(!defined($location)) { |
|
95
|
3
|
|
|
|
|
8
|
Carp::croak('Usage: geocode(location => $location)'); |
|
96
|
2
|
|
|
|
|
691
|
return; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
0
|
0
|
|
|
|
|
if (Encode::is_utf8($location)) { |
|
100
|
0
|
|
|
|
|
|
$location = Encode::encode_utf8($location); |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
my $uri = URI->new("https://$self->{host}/some_location"); |
|
104
|
0
|
|
|
|
|
|
$location =~ s/\s/+/g; |
|
105
|
0
|
|
|
|
|
|
my %query_parameters = ('locate' => $location, 'json' => 1, 'strictmode' => 1); |
|
106
|
0
|
|
|
|
|
|
$uri->query_form(%query_parameters); |
|
107
|
0
|
|
|
|
|
|
my $url = $uri->as_string(); |
|
108
|
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
my $res = $self->{ua}->get($url); |
|
110
|
|
|
|
|
|
|
|
|
111
|
0
|
0
|
|
|
|
|
if($res->is_error()) { |
|
112
|
0
|
|
|
|
|
|
Carp::croak("$url API returned error: " . $res->status_line()); |
|
113
|
0
|
|
|
|
|
|
return; |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
# $res->content_type('text/plain'); # May be needed to decode correctly |
|
116
|
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my $json = JSON->new->utf8(); |
|
118
|
0
|
0
|
|
|
|
|
if(my $rc = $json->decode($res->decoded_content())) { |
|
119
|
0
|
0
|
|
|
|
|
if($rc->{'error'}) { |
|
120
|
|
|
|
|
|
|
# Sorry - you lose the error code, but HTML::GoogleMaps::V3 relies on this |
|
121
|
|
|
|
|
|
|
# TODO - send patch to the H:G:V3 author |
|
122
|
0
|
|
|
|
|
|
return; |
|
123
|
|
|
|
|
|
|
} |
|
124
|
0
|
0
|
0
|
|
|
|
if(defined($rc->{'latt'}) && defined($rc->{'longt'})) { |
|
125
|
0
|
|
|
|
|
|
return $rc; # No support for list context, yet |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# if($location =~ /^(\w+),\+*(\w+),\+*(USA|US|United States)$/i) { |
|
129
|
|
|
|
|
|
|
# $query_parameters{'locate'} = "$1 County, $2, $3"; |
|
130
|
|
|
|
|
|
|
# $uri->query_form(%query_parameters); |
|
131
|
|
|
|
|
|
|
# $url = $uri->as_string(); |
|
132
|
|
|
|
|
|
|
# |
|
133
|
|
|
|
|
|
|
# $res = $self->{ua}->get($url); |
|
134
|
|
|
|
|
|
|
# |
|
135
|
|
|
|
|
|
|
# if($res->is_error()) { |
|
136
|
|
|
|
|
|
|
# Carp::croak("geocoder.ca API returned error: " . $res->status_line()); |
|
137
|
|
|
|
|
|
|
# return; |
|
138
|
|
|
|
|
|
|
# } |
|
139
|
|
|
|
|
|
|
# return $json->decode($res->content()); |
|
140
|
|
|
|
|
|
|
# } |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# my @results = @{ $data || [] }; |
|
144
|
|
|
|
|
|
|
# wantarray ? @results : $results[0]; |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 ua |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Accessor method to get and set UserAgent object used internally. You |
|
150
|
|
|
|
|
|
|
can call I for example, to get the proxy information from |
|
151
|
|
|
|
|
|
|
environment variables: |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
$geo_coder->ua()->env_proxy(1); |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
You can also set your own User-Agent object: |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
my $ua = LWP::UserAgent::Throttled->new(); |
|
158
|
|
|
|
|
|
|
$ua->throttle('geocoder.ca' => 1); |
|
159
|
|
|
|
|
|
|
$geo_coder->ua($ua); |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub ua { |
|
164
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
165
|
0
|
0
|
|
|
|
|
if (@_) { |
|
166
|
0
|
|
|
|
|
|
$self->{ua} = shift; |
|
167
|
|
|
|
|
|
|
} |
|
168
|
0
|
|
|
|
|
|
$self->{ua}; |
|
169
|
|
|
|
|
|
|
} |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 reverse_geocode |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
$location = $geo_coder->reverse_geocode(latlng => '37.778907,-122.39732'); |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Similar to geocode except it expects a latitude/longitude parameter. |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=cut |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub reverse_geocode { |
|
180
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
181
|
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
|
my %param; |
|
183
|
0
|
0
|
|
|
|
|
if (@_ % 2 == 0) { |
|
184
|
0
|
|
|
|
|
|
%param = @_; |
|
185
|
|
|
|
|
|
|
} else { |
|
186
|
0
|
|
|
|
|
|
$param{latlng} = shift; |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
my $latlng = $param{latlng} |
|
190
|
0
|
0
|
|
|
|
|
or Carp::croak('Usage: reverse_geocode(latlng => $latlng)'); |
|
191
|
|
|
|
|
|
|
|
|
192
|
0
|
|
|
|
|
|
return $self->geocode(location => $latlng, reverse => 1); |
|
193
|
|
|
|
|
|
|
} |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head2 run |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
You can also run this module from the command line: |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
perl CA.pm 1600 Pennsylvania Avenue NW, Washington DC |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=cut |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
__PACKAGE__->run(@ARGV) unless caller(); |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub run { |
|
206
|
0
|
|
|
0
|
1
|
|
require Data::Dumper; |
|
207
|
|
|
|
|
|
|
|
|
208
|
0
|
|
|
|
|
|
my $class = shift; |
|
209
|
|
|
|
|
|
|
|
|
210
|
0
|
|
|
|
|
|
my $location = join(' ', @_); |
|
211
|
|
|
|
|
|
|
|
|
212
|
0
|
|
|
|
|
|
my @rc = $class->new()->geocode($location); |
|
213
|
|
|
|
|
|
|
|
|
214
|
0
|
0
|
|
|
|
|
die "$0: geo-coding failed" unless(scalar(@rc)); |
|
215
|
|
|
|
|
|
|
|
|
216
|
0
|
|
|
|
|
|
print Data::Dumper->new([\@rc])->Dump(); |
|
217
|
|
|
|
|
|
|
} |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head1 AUTHOR |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Nigel Horne |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Based on L. |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
226
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
Lots of thanks to the folks at geocoder.ca. |
|
229
|
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head1 BUGS |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Should be called Geo::Coder::NA for North America. |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
L, L |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
Copyright 2017-2022 Nigel Horne. |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
This program is released under the following licence: GPL2 |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=cut |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
1; |