diff options
author | Struan Donald <struan@exo.org.uk> | 2011-08-29 16:38:23 +0100 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2011-08-29 16:38:23 +0100 |
commit | c5de0e84387943901e89f8d327d4e97048e7c20c (patch) | |
tree | b1f8bc3644d92f51dc2d292074be418c850228ff | |
parent | 980d9f82fc76f8d2e73295f3f9fac7b81fdd5660 (diff) |
Add address information to description in RSS feeds
-rwxr-xr-x | perllib/FixMyStreet/App/Controller/Rss.pm | 6 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 40 | ||||
-rw-r--r-- | perllib/FixMyStreet/Geocode/Bing.pm | 32 |
3 files changed, 67 insertions, 11 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Rss.pm b/perllib/FixMyStreet/App/Controller/Rss.pm index 45a16a9dd..3a7ffa19a 100755 --- a/perllib/FixMyStreet/App/Controller/Rss.pm +++ b/perllib/FixMyStreet/App/Controller/Rss.pm @@ -263,6 +263,12 @@ sub add_row : Private { my $key = $alert_type->item_table eq 'comment' ? 'c' : 'id'; $item{description} .= ent("\n<br><img src=\"". $c->cobrand->base_url . "/photo?$key=$row->{id}\">"); } + + if ( $row->{used_map} ) { + my $address = $c->cobrand->find_closest_address_for_rss( $row->{latitude}, $row->{longitude} ); + $item{description} .= ent("\n<br>$address"); + } + my $recipient_name = $c->cobrand->contact_name; $item{description} .= ent("\n<br><a href='$url'>" . sprintf(_("Report on %s"), $recipient_name) . "</a>"); diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 69718f613..1e87468ac 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -4,6 +4,7 @@ use strict; use warnings; use FixMyStreet; use URI; +use Digest::MD5 qw(md5_hex); use Carp; use mySociety::MaPit; @@ -547,17 +548,10 @@ sub find_closest { my ( $self, $latitude, $longitude ) = @_; my $str = ''; - # Get nearest road-type thing from Bing - my $key = mySociety::Config::get('BING_MAPS_API_KEY', ''); - if ($key) { - my $url = "http://dev.virtualearth.net/REST/v1/Locations/$latitude,$longitude?c=en-GB&key=$key"; - my $j = LWP::Simple::get($url); - if ($j) { - $j = JSON->new->utf8->allow_nonref->decode($j); - if ($j->{resourceSets}[0]{resources}[0]{name}) { - $str .= sprintf(_("Nearest road to the pin placed on the map (automatically generated by Bing Maps): %s"), - $j->{resourceSets}[0]{resources}[0]{name}) . "\n\n"; - } + if ( my $j = FixMyStreet::Geocode::Bing::reverse( $latitude, $longitude ) ) { + if ($j->{resourceSets}[0]{resources}[0]{name}) { + $str .= sprintf(_("Nearest road to the pin placed on the map (automatically generated by Bing Maps): %s"), + $j->{resourceSets}[0]{resources}[0]{name}) . "\n\n"; } } @@ -575,6 +569,30 @@ sub find_closest { return $str; } +=head2 find_closest_address_for_rss + +Used by rss feeds to provide a bit more context + +=cut + +sub find_closest_address_for_rss { + my ( $self, $latitude, $longitude ) = @_; + my $str = ''; + + if ( my $j = FixMyStreet::Geocode::Bing::reverse( $latitude, $longitude, 1 ) ) { + if ($j->{resourceSets}[0]{resources}[0]{name}) { + my $address = $j->{resourceSets}[0]{resources}[0]{address}; + my @address; + push @address, $address->{addressLine} if $address->{addressLine} ne 'Street'; + push @address, $address->{locality}; + $str .= sprintf(_("Nearest road to the pin placed on the map (automatically generated by Bing Maps): %s"), + join( ', ', @address ) ); + } + } + + return $str; +} + =head2 council_check Paramters are COUNCILS, QUERY, CONTEXT. Return a boolean indicating whether diff --git a/perllib/FixMyStreet/Geocode/Bing.pm b/perllib/FixMyStreet/Geocode/Bing.pm index 90d7f98bd..4e12a7a7f 100644 --- a/perllib/FixMyStreet/Geocode/Bing.pm +++ b/perllib/FixMyStreet/Geocode/Bing.pm @@ -64,4 +64,36 @@ sub string { return { error => $error }; } +sub reverse { + my ( $latitude, $longitude, $cache ) = @_; + + # Get nearest road-type thing from Bing + my $key = mySociety::Config::get('BING_MAPS_API_KEY', ''); + if ($key) { + my $url = "http://dev.virtualearth.net/REST/v1/Locations/$latitude,$longitude?c=en-GB&key=$key"; + my $j; + if ( $cache ) { + my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'bing/'; + my $cache_file = $cache_dir . md5_hex($url); + + if (-s $cache_file) { + $j = File::Slurp::read_file($cache_file); + } else { + $j = LWP::Simple::get($url); + File::Path::mkpath($cache_dir); + File::Slurp::write_file($cache_file, $j) if $j; + } + } else { + $j = LWP::Simple::get($url); + } + + if ($j) { + $j = JSON->new->utf8->allow_nonref->decode($j); + return $j; + } + } + + return undef; +} + 1; |