diff options
author | Struan Donald <struan@exo.org.uk> | 2011-09-09 19:24:09 +0100 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2011-09-09 19:24:09 +0100 |
commit | d1020f6d455aa729ca52d18ccb39d531a866f4fc (patch) | |
tree | a9a02c62afc20b54d8f934766b9515050de02606 | |
parent | 63e03938cbc9401eccf36bdce8f312f171559c80 (diff) |
sort of working rss address stuff with db caching and populate script
-rw-r--r-- | bin/populate_bing_cache | 30 | ||||
-rw-r--r-- | db/schema_0009_add_gecode_column_to_problem.sql | 7 | ||||
-rwxr-xr-x | perllib/FixMyStreet/App/Controller/Rss.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 39 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 28 |
5 files changed, 95 insertions, 14 deletions
diff --git a/bin/populate_bing_cache b/bin/populate_bing_cache new file mode 100644 index 000000000..50f7fc673 --- /dev/null +++ b/bin/populate_bing_cache @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +use strict; +use warnings; +require 5.8.0; + +use Data::Dumper; + +use FixMyStreet::App; +use FixMyStreet::Geocode::Bing; + +my $reports = FixMyStreet::App->model('DB::Problem')->search( {geocode + => undef }, { select => [qw/id geocode confirmed latitude longitude/] } ); + +while ( my $report = $reports->next ) { + next unless $report->latitude && $report->longitude; + print $report->id . "\n"; + + my $j = FixMyStreet::Geocode::Bing::reverse( $report->latitude, $report->longitude ); + + # FIXME: something in this string causes RABX to read in 1 too many + # characters when pulling it back out the database and hence it explodes + # as it reads the ,. I have no idea why :( + $j->{copyright} = ''; + + $report->geocode( $j ); + $report->update; + + sleep 10; +} diff --git a/db/schema_0009_add_gecode_column_to_problem.sql b/db/schema_0009_add_gecode_column_to_problem.sql new file mode 100644 index 000000000..bda338ddd --- /dev/null +++ b/db/schema_0009_add_gecode_column_to_problem.sql @@ -0,0 +1,7 @@ + +begin; + +ALTER table problem + ADD column geocode TEXT; + +commit; diff --git a/perllib/FixMyStreet/App/Controller/Rss.pm b/perllib/FixMyStreet/App/Controller/Rss.pm index 767d38c21..c2a0eee7e 100755 --- a/perllib/FixMyStreet/App/Controller/Rss.pm +++ b/perllib/FixMyStreet/App/Controller/Rss.pm @@ -265,8 +265,9 @@ sub add_row : Private { } if ( $row->{used_map} ) { - #my $address = $c->cobrand->find_closest_address_for_rss( $row->{latitude}, $row->{longitude} ); - #$item{description} .= ent("\n<br>$address"); + # TODO: uncomment these when the populate script has been run + # my $address = $c->cobrand->find_closest_address_for_rss( $row->{latitude}, $row->{longitude}, $row ); + # $item{description} .= ent("\n<br>$address"); } my $recipient_name = $c->cobrand->contact_name; diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 1e87468ac..e1bf196bd 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -576,18 +576,37 @@ Used by rss feeds to provide a bit more context =cut sub find_closest_address_for_rss { - my ( $self, $latitude, $longitude ) = @_; + my ( $self, $latitude, $longitude, $problem ) = @_; 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 ) ); - } + my $j; + if ( $problem && ref($problem) =~ /FixMyStreet/ && $problem->can( 'geocode' ) ) { + $j = $problem->geocode; + } else { + $problem = FixMyStreet::App->model('DB::Problem')->find( { id => $problem->{id} } ); + $j = $problem->geocode; + } + + # if we've not cached it then we don't want to look it up in order to avoid + # hammering the bing api + # if ( !$j ) { + # $j = FixMyStreet::Geocode::Bing::reverse( $latitude, $longitude, 1 ); + + # # FIXME: work out why this does not work - see populate bing script for details + # # bad data here + # $j->{copyright} = ''; + + # $problem->geocode( $j ); + # $problem->update; + # } + + if ($j && $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; diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 987c92c64..8d8665258 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -78,8 +78,12 @@ __PACKAGE__->add_columns( { data_type => "timestamp", is_nullable => 1 }, "send_questionnaire", { data_type => "boolean", default_value => \"true", is_nullable => 0 }, + "extra", + { data_type => "text", is_nullable => 1 }, "flagged", { data_type => "boolean", default_value => \"false", is_nullable => 0 }, + "geocode", + { data_type => "text", is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many( @@ -102,8 +106,8 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-06-23 15:49:48 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:3sw/1dqxlTvcWEI/eJTm4w +# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-09-09 10:22:40 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:T+EtKT6/8cqMXmd/WzY4CA # Add fake relationship to stored procedure table __PACKAGE__->has_one( @@ -113,6 +117,26 @@ __PACKAGE__->has_one( { cascade_copy => 0, cascade_delete => 0 }, ); +__PACKAGE__->filter_column( + geocode => { + filter_from_storage => sub { + my $self = shift; + my $ser = shift; + return undef unless defined $ser; + my $h = new IO::String($ser); + return RABX::wire_rd($h); + }, + filter_to_storage => sub { + my $self = shift; + my $data = shift; + my $ser = ''; + my $h = new IO::String($ser); + RABX::wire_wr( $data, $h ); + return $ser; + }, + } +); + use DateTime::TimeZone; use Image::Size; use Moose; |