| 1 | package Geo::Coder::Many::Ovi; | ||
| 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::Ovi - Ovi plugin Geo::Coder::Many | ||
| 12 | |||
| 13 | =head1 VERSION | ||
| 14 | |||
| 15 | Version 0.02 | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | our $VERSION = '0.02'; | ||
| 20 | |||
| 21 | # Requires Geo::Coder::Ovi 0.01 or above | ||
| 22 | sub _MIN_MODULE_VERSION { return '0.01'; } | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | This module adds Ovi support to Geo::Coder::Many. | ||
| 27 | |||
| 28 | Use as follows: | ||
| 29 | |||
| 30 | use Geo::Coder::Many; | ||
| 31 | use Geo::Coder::Ovi; | ||
| 32 | |||
| 33 | my $options = { }; | ||
| 34 | my $geocoder_many = Geo::Coder::Many->new( $options ); | ||
| 35 | my $GCO = Geo::Coder::Ovi->new(); | ||
| 36 | |||
| 37 | my $options = { | ||
| 38 | geocoder => $GCO, | ||
| 39 | }; | ||
| 40 | |||
| 41 | $geocoder_many->add_geocoder( $options ); | ||
| 42 | |||
| 43 | my $location = $geocoder_many->geocode( | ||
| 44 | { | ||
| 45 | location => 'London EC1M 5RF, United Kingdom' | ||
| 46 | } | ||
| 47 | ); | ||
| 48 | |||
| 49 | =head1 MORE INFO | ||
| 50 | |||
| 51 | please see http://search.cpan.org/dist/Geo-Coder-Ovi/ | ||
| 52 | |||
| 53 | =head1 SUBROUTINES/METHODS | ||
| 54 | |||
| 55 | =head2 geocode | ||
| 56 | |||
| 57 | This is called by Geo::Coder::Many - it sends the geocoding request (via | ||
| 58 | Geo::Coder::Ovi) and extracts the resulting location, returning it in a | ||
| 59 | standard Geo::Coder::Many::Response. | ||
| 60 | |||
| 61 | Note: the precision score is set based on the size of the bounding box returned. | ||
| 62 | Not all queries seem to return a bounding box. In that case precision in undef | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | sub geocode { | ||
| 67 | my $self = shift; | ||
| 68 | my $location = shift; | ||
| 69 | defined $location or croak "Geo::Coder::Many::Ovi::geocode | ||
| 70 | method must be given a location."; | ||
| 71 | |||
| 72 | my @raw_replies = $self->{GeoCoder}->geocode( location => $location ); | ||
| 73 | my $response = Geo::Coder::Many::Response->new( { location => $location } ); | ||
| 74 | |||
| 75 | foreach my $raw_reply ( @raw_replies ) { | ||
| 76 | |||
| 77 | my $lng = undef; | ||
| 78 | my $lat = undef; | ||
| 79 | if (defined($raw_reply->{properties})){ | ||
| 80 | $lat = $raw_reply->{properties}{geoLatitude}; | ||
| 81 | $lng = $raw_reply->{properties}{geoLongitude}; | ||
| 82 | } | ||
| 83 | |||
| 84 | if (defined($lng) && defined($lat)){ # did we get anything? | ||
| 85 | my $precision = $self->_determine_precision($raw_reply); | ||
| 86 | my $tmp = { | ||
| 87 | address => $raw_reply->{title}, | ||
| 88 | country => $raw_reply->{addrCountryName}, | ||
| 89 | longitude => $lng, | ||
| 90 | latitude => $lat, | ||
| 91 | precision => $precision, | ||
| 92 | }; | ||
| 93 | $response->add_response( $tmp, $self->get_name() ); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | my $http_response = $self->{GeoCoder}->response(); | ||
| 98 | $response->set_response_code($http_response->code()); | ||
| 99 | return $response; | ||
| 100 | } | ||
| 101 | |||
| 102 | # return number between 0 and 1 | ||
| 103 | sub _determine_precision { | ||
| 104 | my $self = shift; | ||
| 105 | my $raw = shift; | ||
| 106 | |||
| 107 | my $precision = undef; | ||
| 108 | # if we have a bounding box we can calculate a precision | ||
| 109 | if (defined($raw->{properties}) | ||
| 110 | && defined($raw->{properties}{geoBbxLatitude2}) | ||
| 111 | ){ | ||
| 112 | |||
| 113 | $precision = | ||
| 114 | Geo::Coder::Many::Util::determine_precision_from_bbox({ | ||
| 115 | 'lon1' => $raw->{properties}{geoBbxLongitude1}, | ||
| 116 | 'lat1' => $raw->{properties}{geoBbxLatitude1}, | ||
| 117 | 'lon2' => $raw->{properties}{geoBbxLongitude2}, | ||
| 118 | 'lat2' => $raw->{properties}{geoBbxLatitude2}, | ||
| 119 | }); | ||
| 120 | } | ||
| 121 | return $precision; | ||
| 122 | } | ||
| 123 | |||
| 124 | =head2 get_name | ||
| 125 | |||
| 126 | Returns the name of the geocoder type - used by Geo::Coder::Many | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub get_name { my $self = shift; return 'ovi ' . $self->{GeoCoder}->VERSION; } | ||
| 131 | |||
| 132 | 1; | ||
| 133 | |||
| 134 | __END__ |