| 1 | package Geo::Coder::Many::OSM; | ||
| 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::OSM - OpenStreetMap Nominatim plugin for 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::OSM 0.01 or above | ||
| 22 | sub _MIN_MODULE_VERSION { return '0.01'; } | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | This module adds OpenStreetMap Nominatim support to Geo::Coder::Many. | ||
| 27 | |||
| 28 | Use as follows: | ||
| 29 | |||
| 30 | use Geo::Coder::Many; | ||
| 31 | use Geo::Coder::OSM; | ||
| 32 | |||
| 33 | my $options = { }; | ||
| 34 | my $geocoder_many = Geo::Coder::Many->new( $options ); | ||
| 35 | my $osm = Geo::Coder::OSM->new; | ||
| 36 | |||
| 37 | my $osm_options = { | ||
| 38 | geocoder => $osm, | ||
| 39 | # This limit should not be taken as necessarily valid. | ||
| 40 | # Please check the Nominatim usage policy. | ||
| 41 | daily_limit => 5000, | ||
| 42 | }; | ||
| 43 | |||
| 44 | $geocoder_many->add_geocoder( $osm_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 | Be careful to limit the number of requests you send, or risk being blocked. | ||
| 55 | |||
| 56 | See http://wiki.openstreetmap.org/wiki/Nominatim#Usage_Policy for details. | ||
| 57 | |||
| 58 | =head1 SUBROUTINES/METHODS | ||
| 59 | |||
| 60 | =head2 geocode | ||
| 61 | |||
| 62 | This is called by Geo::Coder::Many - it sends the geocoding request (via | ||
| 63 | Geo::Coder::OSM) and extracts the resulting location, returning it in a | ||
| 64 | standard Geo::Coder::Many::Response. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub geocode { | ||
| 69 | my $self = shift; | ||
| 70 | my $location = shift; | ||
| 71 | defined $location or croak "Geo::Coder::Many::OSM::geocode | ||
| 72 | method must be given a location."; | ||
| 73 | |||
| 74 | my @raw_replies = $self->{GeoCoder}->geocode( location => $location ); | ||
| 75 | my $response = Geo::Coder::Many::Response->new( { location => $location } ); | ||
| 76 | |||
| 77 | my $location_data = []; | ||
| 78 | |||
| 79 | foreach my $raw_reply ( @raw_replies ) { | ||
| 80 | |||
| 81 | my $precision = 0; # unknown | ||
| 82 | if (defined($raw_reply->{boundingbox})){ | ||
| 83 | my $ra_bbox = $raw_reply->{boundingbox}; | ||
| 84 | |||
| 85 | $precision = | ||
| 86 | Geo::Coder::Many::Util::determine_precision_from_bbox({ | ||
| 87 | 'lon1' => $ra_bbox->[0], | ||
| 88 | 'lat1' => $ra_bbox->[2], | ||
| 89 | 'lon2' => $ra_bbox->[1], | ||
| 90 | 'lat2' => $ra_bbox->[3], | ||
| 91 | }); | ||
| 92 | } | ||
| 93 | my $tmp = { | ||
| 94 | address => $raw_reply->{display_name}, | ||
| 95 | country => $raw_reply->{address}{country}, | ||
| 96 | longitude => $raw_reply->{lon}, | ||
| 97 | latitude => $raw_reply->{lat}, | ||
| 98 | precision => $precision, | ||
| 99 | }; | ||
| 100 | |||
| 101 | $response->add_response( $tmp, $self->get_name() ); | ||
| 102 | }; | ||
| 103 | |||
| 104 | my $http_response = $self->{GeoCoder}->response(); | ||
| 105 | $response->set_response_code($http_response->code()); | ||
| 106 | |||
| 107 | return $response; | ||
| 108 | } | ||
| 109 | |||
| 110 | =head2 get_name | ||
| 111 | |||
| 112 | Returns the name of the geocoder type - used by Geo::Coder::Many | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub get_name { my $self = shift; return 'osm ' . $self->{GeoCoder}->VERSION; } | ||
| 117 | |||
| 118 | 1; # End of Geo::Coder::Many::OSM |