| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Astro::App::Satpass2::Geocode::OSM; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 22 | use 5.008; | 
|  | 1 |  |  |  |  | 4 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 19 |  | 
| 6 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 9 | use parent qw{ Astro::App::Satpass2::Geocode }; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 1 |  |  | 1 |  | 47 | use Astro::App::Satpass2::Utils qw{ instance @CARP_NOT }; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 90 |  | 
| 11 | 1 |  |  | 1 |  | 7 | use List::Util (); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 31 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our $VERSION = '0.051'; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 1 |  |  | 1 |  | 6 | use constant GEOCODER_CLASS => 'Geo::Coder::OSM'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 81 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 1 |  |  | 1 |  | 6 | use constant GEOCODER_SITE => 'http://nominatim.openstreetmap.org/'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 609 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub geocode { | 
| 20 | 0 |  |  | 0 | 1 |  | my ( $self, $loc ) = @_; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 0 |  |  |  |  |  | my $geocoder = $self->geocoder(); | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 0 | 0 |  |  |  |  | if ( my @rslt = $geocoder->geocode( location => $loc ) ) { | 
| 25 |  |  |  |  |  |  | # Heuristic to prevent cruft being returned. | 
| 26 | 0 | 0 |  |  |  |  | if ( @rslt > 1 ) { | 
| 27 | 0 |  |  |  |  |  | my $cutoff = List::Util::max( map { $_->{importance} } | 
|  | 0 |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | @rslt ) - 0.3; | 
| 29 | 0 |  |  |  |  |  | @rslt = grep { $_->{importance} > $cutoff } @rslt; | 
|  | 0 |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  | return ( | 
| 32 |  |  |  |  |  |  | map { | 
| 33 | 0 |  |  |  |  |  | { | 
| 34 |  |  |  |  |  |  | ##		    country		=> uc $_->{address}{country_code}, | 
| 35 |  |  |  |  |  |  | description	=> _description( $_ ), | 
| 36 |  |  |  |  |  |  | latitude	=> $_->{lat}, | 
| 37 |  |  |  |  |  |  | longitude	=> $_->{lon}, | 
| 38 |  |  |  |  |  |  | } | 
| 39 | 0 |  |  |  |  |  | } @rslt ); | 
| 40 |  |  |  |  |  |  | } else { | 
| 41 | 0 |  |  |  |  |  | return $self->__geocode_failure(); | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | { | 
| 47 |  |  |  |  |  |  | my $desc = { | 
| 48 |  |  |  |  |  |  | us	=> sub { | 
| 49 |  |  |  |  |  |  | my ( $info ) = @_; | 
| 50 |  |  |  |  |  |  | my $addr = $info->{address}; | 
| 51 |  |  |  |  |  |  | my @field = ( | 
| 52 |  |  |  |  |  |  | _field( $addr, qw{ house_number pedestrian } ) || | 
| 53 |  |  |  |  |  |  | _field( $addr, qw{ house_number road } ) || | 
| 54 |  |  |  |  |  |  | _field( $addr, 'road' ), | 
| 55 |  |  |  |  |  |  | _field( $addr, 'city' ), | 
| 56 |  |  |  |  |  |  | _field( $addr, 'state' ), | 
| 57 |  |  |  |  |  |  | ) or return; | 
| 58 |  |  |  |  |  |  | $field[-1] .= ' USA'; | 
| 59 |  |  |  |  |  |  | return join ', ', @field; | 
| 60 |  |  |  |  |  |  | }, | 
| 61 |  |  |  |  |  |  | }; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub _field { | 
| 64 | 0 |  |  | 0 |  |  | my ( $addr, @items ) = @_; | 
| 65 | 0 |  |  |  |  |  | @items == grep { defined $addr->{$_} } @items | 
| 66 | 0 | 0 |  |  |  |  | and return join ' ', map { $addr->{$_} } @items; | 
|  | 0 |  |  |  |  |  |  | 
| 67 | 0 |  |  |  |  |  | return; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | sub _description { | 
| 71 | 0 |  |  | 0 |  |  | my ( $info ) = @_; | 
| 72 | 0 |  |  |  |  |  | my $country = lc $info->{address}{country_code}; | 
| 73 | 0 | 0 |  |  |  |  | if ( my $code = $desc->{$country} ) { | 
| 74 | 0 |  |  |  |  |  | return $code->( $info ); | 
| 75 |  |  |  |  |  |  | } else { | 
| 76 | 0 |  |  |  |  |  | my $desc = $info->{display_name}; | 
| 77 | 0 |  |  |  |  |  | $desc =~ s/ [^,]+ , \s* //smx; | 
| 78 | 0 |  |  |  |  |  | $desc =~ s/ \A ( [0-9]+ ) , /$1/smx;	# Oh, for 5.10 and \K | 
| 79 | 0 |  |  |  |  |  | return $desc; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | 1; | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | __END__ |