| 1 | package Geo::Coder::Many::PlaceFinder; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use Geo::Coder::Many::Generic; | ||
| 8 | use Geo::Coder::Many::Util; | ||
| 9 | use base 'Geo::Coder::Many::Generic'; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Geo::Coder::Many::PlaceFinder - Yahoo PlaceFinder plugin for Geo::Coder::Many | ||
| 14 | |||
| 15 | =head1 VERSION | ||
| 16 | |||
| 17 | Version 0.03 | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | our $VERSION = '0.03'; | ||
| 22 | |||
| 23 | # Requires Geo::Coder::PlaceFinder 0.02 or above | ||
| 24 | sub _MIN_MODULE_VERSION { return '0.02'; } | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | This module adds Yahoo PlaceFinder support to Geo::Coder::Many. | ||
| 29 | |||
| 30 | Use as follows: | ||
| 31 | |||
| 32 | use Geo::Coder::Many; | ||
| 33 | use Geo::Coder::PlaceFinder; | ||
| 34 | |||
| 35 | my $options = { }; | ||
| 36 | my $geocoder_many = Geo::Coder::Many->new( $options ); | ||
| 37 | my $place_finder = Geo::Coder::PlaceFinder->new( appid => 'YOUR_APP_ID' ); | ||
| 38 | |||
| 39 | my $place_finder_options = { | ||
| 40 | geocoder => $place_finder, | ||
| 41 | daily_limit => 50000, | ||
| 42 | }; | ||
| 43 | |||
| 44 | $geocoder_many->add_geocoder( $place_finder_options ); | ||
| 45 | |||
| 46 | my $location = $geocoder_many->geocode( | ||
| 47 | { | ||
| 48 | location => '82 Clerkenwell Road, London, EC1M 5RF' | ||
| 49 | } | ||
| 50 | ); | ||
| 51 | |||
| 52 | =head1 USAGE POLICY | ||
| 53 | |||
| 54 | As of writing, Yahoo PlaceFinder allows up to 50000 requests per day. This may | ||
| 55 | change, so you should check the latest documentation to make sure you aren't | ||
| 56 | going to get blocked. | ||
| 57 | |||
| 58 | http://developer.yahoo.com/geo/placefinder/ | ||
| 59 | |||
| 60 | =head1 SUBROUTINES/METHODS | ||
| 61 | |||
| 62 | =head2 geocode | ||
| 63 | |||
| 64 | This is called by Geo::Coder::Many - it sends the geocoding request (via | ||
| 65 | Geo::Coder::PlaceFinder) and extracts the resulting location, returning it in a | ||
| 66 | standard Geo::Coder::Many::Response. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub geocode { | ||
| 71 | my $self = shift; | ||
| 72 | my $location = shift; | ||
| 73 | defined $location or croak "geocode method must be given a location\n"; | ||
| 74 | |||
| 75 | my @raw_replies = $self->{GeoCoder}->geocode( location => $location ); | ||
| 76 | my $response = Geo::Coder::Many::Response->new( { location => $location } ); | ||
| 77 | |||
| 78 | my $http_response = $self->{GeoCoder}->response(); | ||
| 79 | my $location_data = []; | ||
| 80 | |||
| 81 | foreach my $raw_reply ( @raw_replies ) { | ||
| 82 | my @address_lines = @{$raw_reply}{'line1', 'line2', 'line3', 'line4'}; | ||
| 83 | |||
| 84 | @address_lines = grep {!/^\s*$/x} @address_lines; | ||
| 85 | my $address = (join ', ', @address_lines); | ||
| 86 | |||
| 87 | my $precision = 0; | ||
| 88 | if (defined($raw_reply->{boundingbox})){ | ||
| 89 | |||
| 90 | my $box = $raw_reply->{boundingbox}; | ||
| 91 | # lng and lat in decimal degree format | ||
| 92 | |||
| 93 | $precision = | ||
| 94 | Geo::Coder::Many::Util::determine_precision_from_bbox({ | ||
| 95 | 'lon1' => $box->{west}, | ||
| 96 | 'lat1' => $box->{south}, | ||
| 97 | 'lon2' => $box->{east}, | ||
| 98 | 'lat2' => $box->{north}, | ||
| 99 | }); | ||
| 100 | |||
| 101 | } | ||
| 102 | my $tmp = { | ||
| 103 | address => $address, | ||
| 104 | country => $raw_reply->{country}, | ||
| 105 | longitude => $raw_reply->{longitude}, | ||
| 106 | latitude => $raw_reply->{latitude}, | ||
| 107 | precision => $precision, | ||
| 108 | }; | ||
| 109 | |||
| 110 | $response->add_response( $tmp, $self->get_name() ); | ||
| 111 | } | ||
| 112 | |||
| 113 | $response->set_response_code($http_response->code()); | ||
| 114 | return $response; | ||
| 115 | } | ||
| 116 | |||
| 117 | =head2 get_name | ||
| 118 | |||
| 119 | Returns the name of the geocoder type - used by Geo::Coder::Many | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub get_name { my $self = shift; return 'place_finder ' . $self->{GeoCoder}->VERSION; }; | ||
| 124 | |||
| 125 | 1; # End of Geo::Coder::Many::PlaceFinder |