| 1 | package Geo::Coder::All::OSM; | ||
| 2 | use Moose; | ||
| 3 | use namespace::autoclean; | ||
| 4 | use Carp; | ||
| 5 | use Geo::Coder::OSM; | ||
| 6 | use Data::Dumper; | ||
| 7 | use Locale::Codes::Country; | ||
| 8 | with 'Geo::Coder::Role::Geocode'; | ||
| 9 | has 'OSM' => ( | ||
| 10 | is => 'ro', | ||
| 11 | isa => 'Geo::Coder::OSM', | ||
| 12 | default => sub { Geo::Coder::OSM->new();}, | ||
| 13 | ); | ||
| 14 | |||
| 15 | sub geocode_local{ | ||
| 16 | my ($self,$rh_args) = @_; | ||
| 17 | croak "Location String needed" unless ($rh_args->{location}); | ||
| 18 | my $rh_response = $self->OSM->geocode(location => $rh_args->{location}); | ||
| 19 | print STDERR Dumper($rh_response) if($rh_args->{DEBUG}); | ||
| 20 | return $self->_process_response($rh_response); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub reverse_geocode_local { | ||
| 24 | my ($self,$rh_args) = @_; | ||
| 25 | croak 'latlng needed to reverse geocode' unless($rh_args->{latlng}); | ||
| 26 | my $rh_response = $self->OSM->reverse_geocode(latlng=>$rh_args->{latlng}); | ||
| 27 | print STDERR Dumper($rh_response) if($rh_args->{DEBUG}); | ||
| 28 | return $self->_process_response($rh_response); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub _process_response { | ||
| 32 | my ($self,$rh_response) = @_; | ||
| 33 | return undef unless($rh_response); | ||
| 34 | my $rh_data; | ||
| 35 | $rh_data->{geocoder} = 'OSM'; | ||
| 36 | $rh_data->{address} = $rh_response->{display_name}; | ||
| 37 | $rh_data->{coordinates}{lat}= $rh_response->{lat}; | ||
| 38 | $rh_data->{coordinates}{lon}= $rh_response->{lon}; | ||
| 39 | $rh_data->{country} = $rh_response->{address}{country}; | ||
| 40 | $rh_data->{country_code_alpha_3} = uc(country2code($rh_response->{address}{country},'alpha-3')); | ||
| 41 | $rh_data->{country_code} = uc($rh_response->{address}{country_code}); | ||
| 42 | return $rh_data; | ||
| 43 | } | ||
| 44 | __PACKAGE__->meta->make_immutable; | ||
| 45 | 1; |