/usr/share/perl5/Geo/Coder/OSM.pm is in libgeo-coder-osm-perl 0.03-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | package Geo::Coder::OSM;
use strict;
use warnings;
use Carp qw(croak);
use Encode ();
use JSON;
use LWP::UserAgent;
use URI;
our $VERSION = '0.03';
$VERSION = eval $VERSION;
our %SOURCES = (
osm => 'http://nominatim.openstreetmap.org',
mapquest => 'http://open.mapquestapi.com/nominatim/v1',
);
sub new {
my ($class, @params) = @_;
my %params = (@params % 2) ? (key => @params) : @params;
my $self = bless \ %params, $class;
$self->ua(
$params{ua} || LWP::UserAgent->new(agent => "$class/$VERSION")
);
if (exists $self->{sources}) {
my $sources = $self->{sources};
$self->{sources} = $sources = [$sources] unless ref $sources;
for my $source (@$sources) {
croak qq(unknown source '$source')
unless exists $SOURCES{$source};
}
}
else {
$self->{sources} = ['osm'];
}
$self->{source_idx} = 0;
if ($self->{debug}) {
my $dump_sub = sub { $_[0]->dump(maxlength => 0); return };
$self->ua->set_my_handler(request_send => $dump_sub);
$self->ua->set_my_handler(response_done => $dump_sub);
}
elsif (exists $self->{compress} ? $self->{compress} : 1) {
$self->ua->default_header(accept_encoding => 'gzip,deflate');
}
return $self;
}
sub response { $_[0]->{response} }
sub ua {
my ($self, $ua) = @_;
if ($ua) {
croak q('ua' must be (or derived from) an LWP::UserAgent')
unless ref $ua and $ua->isa(q(LWP::UserAgent));
$self->{ua} = $ua;
}
return $self->{ua};
}
sub geocode {
my ($self, @params) = @_;
my %params = (@params % 2) ? (location => @params) : @params;
my $location = delete $params{location} or return;
$location = Encode::encode('utf-8', $location);
# Cycle throught the list of sources.
my $idx = ($self->{source_idx} %= @{ $self->{sources} })++;
my $uri = URI->new($SOURCES{ $self->{sources}[$idx] } . '/search');
$uri->query_form(
q => $location,
format => 'json',
addressdetails => 1,
'accept-language' => 'en',
%params,
);
return $self->_request($uri);
}
sub reverse_geocode {
my ($self, @params) = @_;
my %params = (@params % 2) ? (latlng => @params) : @params;
# Maintain api compatibility with other geocoders.
my ($lat, $lon);
if (my $latlon = delete $params{latlng}) {
($lat, $lon) = split '\s*,\s*', $latlon;
}
else {
$lat = delete $params{lat};
($lon) = grep defined, delete @params{qw(lon lng)};
}
return unless 2 == grep defined, $lat, $lon;
# Cycle throught the list of sources.
my $idx = ($self->{source_idx} %= @{ $self->{sources} })++;
my $uri = URI->new($SOURCES{ $self->{sources}[$idx] } . '/reverse');
$uri->query_form(
lat => $lat,
lon => $lon,
format => 'json',
addressdetails => 1,
'accept-language' => 'en',
%params,
);
return $self->_request($uri);
}
sub _request {
my ($self, $uri) = @_;
return unless $uri;
my $res = $self->{response} = $self->ua->get($uri);
return unless $res->is_success;
# Change the content type of the response (if necessary) so
# HTTP::Message will decode the character encoding.
$res->content_type('text/plain')
unless $res->content_type =~ /^text/;
my $content = $res->decoded_content;
return unless $content;
my $data = eval { from_json($content) };
return unless $data;
my @results = 'ARRAY' eq ref $data ? @$data : ($data);
return wantarray ? @results : $results[0];
}
1;
__END__
=head1 NAME
Geo::Coder::OSM - Geocode addresses with the OpenStreetMap Nominatim API
=head1 SYNOPSIS
use Geo::Coder::OSM;
my $geocoder = Geo::Coder::OSM->new;
my $location = $geocoder->geocode(
location => 'Hollywood and Highland, Los Angeles, CA'
);
=head1 DESCRIPTION
The C<Geo::Coder::OSM> module provides an interface to the OpenStreet
Nominatim geocoding service.
=head1 METHODS
=head2 new
$geocoder = Geo::Coder::OSM->new()
$geocoder = Geo::Coder::OSM->new(
ua => $ua,
sources => [ 'osm', 'mapquest' ],
debug => 1,
)
Creates a new geocoding object.
Accepts an optional B<ua> parameter for passing in a custom LWP::UserAgent
object.
Accepts an optional B<sources> parameter for specifying the data sources.
Current valid values are B<osm> and B<mapquest>. The default value is B<osm>.
To cycle between different sources, specify an array reference for the
B<sources> value. To define additional sources, see L</SOURCES> below.
=head2 geocode
$location = $geocoder->geocode(location => $location)
@locations = $geocoder->geocode(location => $location)
In scalar context, this method returns the first location result; and in
list context it returns all location results.
Each location result is a hashref; a typical example looks like:
{
address => {
city => "Los Angeles",
country => "United States of America",
country_code => "us",
hamlet => "Hollywood",
road => "Hollywood Boulevard",
station => "Hollywood/Highland",
suburb => "Little Armenia",
},
boundingbox => [
"34.101634979248", "34.1018371582031",
"-118.339317321777", "-118.33910369873",
],
class => "railway",
display_name =>
"Hollywood/Highland, Hollywood Boulevard, Little Armenia, Hollywood, Los Angeles, United States of America",
icon =>
"http://nominatim.openstreetmap.org/images/mapicons/transport_train_station2.p.20.png",
lat => "34.101736",
licence =>
"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.",
lon => "-118.33921",
osm_id => 472413621,
osm_type => "node",
place_id => 9071654,
type => "station",
}
=head2 reverse_geocode
$location = $geocoder->reverse_geocode(lat => $lat, lon => $lon)
$location = $geocoder->reverse_geocode(latlng => "$lat,$lon")
Returns a location result for the given lat/lon pair.
=head2 response
$response = $geocoder->response()
Returns an L<HTTP::Response> object for the last submitted request. Can be
used to determine the details of an error.
=head2 ua
$ua = $geocoder->ua()
$ua = $geocoder->ua($ua)
Accessor for the UserAgent object.
=head2 SOURCES
To define additional sources add them to the B<%SOURCES> package variable like
so:
$Geo::Coder::OSM::SOURCES{local} = 'http://127.0.0.1/api_base_path';
$Geo::Coder::OSM::SOURCES{internal} = 'http://internal.corp/api_base_path';
=head1 SEE ALSO
L<http://wiki.openstreetmap.org/wiki/Nominatim>
L<http://open.mapquestapi.com/nominatim/>
=head1 REQUESTS AND BUGS
Please report any bugs or feature requests to
L<http://rt.cpan.org/Public/Bug/Report.html?Queue=Geo-Coder-OSM>. I will be
notified, and then you'll automatically be notified of progress on your bug
as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Geo::Coder::OSM
You can also look for information at:
=over
=item * GitHub Source Repository
L<http://github.com/gray/geo-coder-osm>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Geo-Coder-OSM>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Geo-Coder-OSM>
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/Public/Dist/Display.html?Name=Geo-Coder-OSM>
=item * Search CPAN
L<http://search.cpan.org/dist/Geo-Coder-OSM/>
=back
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2010-2013 gray <gray at cpan.org>, all rights reserved.
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=head1 AUTHOR
gray, <gray at cpan.org>
=cut
|