| 1 | package Geo::Coder::Many::OpenCage; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | use Geo::Coder::Many::Util; | ||
| 7 | use base 'Geo::Coder::Many::Generic'; | ||
| 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 | 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 | my $self = shift; | ||
| 59 | my $location = shift; | ||
| 60 | defined $location or croak "Geo::Coder::Many::OpenCage::geocode method must be given a location."; | ||
| 61 | |||
| 62 | my @raw_replies = $self->{GeoCoder}->geocode( location => $location ); | ||
| 63 | my $response = Geo::Coder::Many::Response->new( { location => $location } ); | ||
| 64 | |||
| 65 | my $location_data = []; | ||
| 66 | |||
| 67 | foreach my $raw_reply ( @raw_replies ) { | ||
| 68 | |||
| 69 | my $precision = 0; # unknown | ||
| 70 | if (defined($raw_reply->{boundingbox})){ | ||
| 71 | my $ra_bbox = $raw_reply->{boundingbox}; | ||
| 72 | |||
| 73 | $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 | 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 | $response->add_response( $tmp, $self->get_name() ); | ||
| 91 | }; | ||
| 92 | |||
| 93 | my $http_response = $self->{GeoCoder}->response(); | ||
| 94 | $response->set_response_code($http_response->code()); | ||
| 95 | |||
| 96 | 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 | sub get_name { my $self = shift; return 'opencage ' . $self->{GeoCoder}->VERSION; } | ||
| 106 | |||
| 107 | 1; # End of Geo::Coder::Many::OpenCage |