| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Geo::Coder::Many::OpenCage; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
7
|
use warnings; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
48
|
|
|
4
|
2
|
|
|
2
|
|
5
|
use strict; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
26
|
|
|
5
|
2
|
|
|
2
|
|
6
|
use Carp; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
74
|
|
|
6
|
2
|
|
|
2
|
|
7
|
use Data::Dumper; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
74
|
|
|
7
|
2
|
|
|
2
|
|
6
|
use Geo::Coder::Many::Util; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
48
|
|
|
8
|
2
|
|
|
2
|
|
8
|
use base 'Geo::Coder::Many::Generic'; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
537
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Geo::Coder::Many::OpenCage - OpenCage plugin for Geo::Coder::Many |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 VERSION |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Version 0.02 |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=cut |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Requires Geo::Coder::OpenCage 0.01 or above |
|
23
|
0
|
|
|
0
|
|
|
sub _MIN_MODULE_VERSION { return '0.02'; } |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
This module adds OpenCage Geocoder support to Geo::Coder::Many. |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Use as follows: |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use Geo::Coder::Many; |
|
32
|
|
|
|
|
|
|
use Geo::Coder::OpenCage; |
|
33
|
|
|
|
|
|
|
my $options = { }; |
|
34
|
|
|
|
|
|
|
my $geocoder_many = Geo::Coder::Many->new( $options ); |
|
35
|
|
|
|
|
|
|
my $OC = Geo::Coder::OpenCage->new( api_key => $my_OC_api_key ); |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $OC_options = { |
|
38
|
|
|
|
|
|
|
geocoder => $OC, |
|
39
|
|
|
|
|
|
|
daily_limit => 2500, |
|
40
|
|
|
|
|
|
|
}; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$geocoder_many->add_geocoder( $OC_options ); |
|
43
|
|
|
|
|
|
|
my $location = $geocoder_many->geocode({ |
|
44
|
|
|
|
|
|
|
location => '82 Clerkenwell Road, London, EC1M 5RF', |
|
45
|
|
|
|
|
|
|
}); |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 USAGE POLICY |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
See http://geocoder.opencagedata.com |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head2 geocode |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
This is called by Geo::Coder::Many - it sends the geocoding request |
|
56
|
|
|
|
|
|
|
(via L) and extracts the resulting location, returning it |
|
57
|
|
|
|
|
|
|
in a standard Geo::Coder::Many::Response. |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=cut |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub geocode { |
|
62
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
63
|
0
|
|
|
|
|
|
my $location = shift; |
|
64
|
0
|
0
|
|
|
|
|
defined $location |
|
65
|
|
|
|
|
|
|
or croak "Geo::Coder::Many::OpenCage::geocode must be given location."; |
|
66
|
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
my $rh_response = $self->{GeoCoder}->geocode( location => $location ); |
|
68
|
0
|
|
|
|
|
|
my $response = Geo::Coder::Many::Response->new({ location => $location }); |
|
69
|
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
my $location_data = []; |
|
71
|
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
foreach my $raw_reply ( @{ $rh_response->{results} } ){ |
|
|
0
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my $precision = 0; # unknown |
|
74
|
0
|
0
|
|
|
|
|
if (defined($raw_reply->{bounds})){ |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
$precision = |
|
77
|
|
|
|
|
|
|
Geo::Coder::Many::Util::determine_precision_from_bbox({ |
|
78
|
|
|
|
|
|
|
'lon1' => $raw_reply->{bounds}{northeast}{lng}, |
|
79
|
|
|
|
|
|
|
'lat1' => $raw_reply->{bounds}{northeast}{lat}, |
|
80
|
|
|
|
|
|
|
'lon2' => $raw_reply->{bounds}{southwest}{lng}, |
|
81
|
|
|
|
|
|
|
'lat2' => $raw_reply->{bounds}{southwest}{lng}, |
|
82
|
0
|
|
|
|
|
|
}); |
|
83
|
|
|
|
|
|
|
} else { |
|
84
|
0
|
|
|
|
|
|
$precision = $raw_reply->{confidence}; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my $tmp = { |
|
88
|
|
|
|
|
|
|
address => $raw_reply->{formatted}, |
|
89
|
|
|
|
|
|
|
country => $raw_reply->{components}{country}, |
|
90
|
|
|
|
|
|
|
longitude => $raw_reply->{geometry}{lng}, |
|
91
|
|
|
|
|
|
|
latitude => $raw_reply->{geometry}{lat}, |
|
92
|
0
|
|
|
|
|
|
precision => $precision, |
|
93
|
|
|
|
|
|
|
}; |
|
94
|
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
$response->add_response( $tmp, $self->get_name() ); |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
if (defined($rh_response)){ |
|
99
|
|
|
|
|
|
|
#print STDERR Dumper $rh_response; |
|
100
|
0
|
|
|
|
|
|
$response->set_response_code($rh_response->{status}->{code}); |
|
101
|
|
|
|
|
|
|
} else { |
|
102
|
0
|
|
|
|
|
|
$response->set_response_code(401); |
|
103
|
|
|
|
|
|
|
} |
|
104
|
0
|
|
|
|
|
|
return $response; |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 get_name |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Returns the name of the geocoder type - used by Geo::Coder::Many |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=cut |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub get_name { |
|
114
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
115
|
0
|
|
|
|
|
|
return 'opencage ' . $VERSION; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; # End of Geo::Coder::Many::OpenCage |