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