aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--db/migrate_from_osgb36_to_wgs84.pl116
-rw-r--r--db/schema.sql10
2 files changed, 121 insertions, 5 deletions
diff --git a/db/migrate_from_osgb36_to_wgs84.pl b/db/migrate_from_osgb36_to_wgs84.pl
new file mode 100644
index 000000000..462061281
--- /dev/null
+++ b/db/migrate_from_osgb36_to_wgs84.pl
@@ -0,0 +1,116 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+=head1 DESCRIPTION
+
+This script will take a FMS database with eastings and northings in and migrate
+it to latitude and longitude. It touches the following tables and functions:
+
+=cut
+
+use FindBin;
+use lib "$FindBin::Bin/../perllib";
+use lib "$FindBin::Bin/../commonlib/perllib";
+
+use mySociety::Config;
+use mySociety::DBHandle qw(dbh);
+use mySociety::GeoUtil qw(national_grid_to_wgs84);
+
+BEGIN {
+ mySociety::Config::set_file("$FindBin::Bin/../conf/general");
+ mySociety::DBHandle::configure(
+ Name => mySociety::Config::get('BCI_DB_NAME'),
+ User => mySociety::Config::get('BCI_DB_USER'),
+ Password => mySociety::Config::get('BCI_DB_PASS'),
+ Host => mySociety::Config::get( 'BCI_DB_HOST', undef ),
+ Port => mySociety::Config::get( 'BCI_DB_PORT', undef )
+ );
+}
+
+migrate_problem_table();
+
+=head2 problem table
+
+Add columns 'latitude' and 'longitude'.
+Update all entries coverting from e,n to lon,lat.
+Make the lat, lon columns not null.
+Drop the 'problem_state_easting_northing_idx' index.
+Create new index 'problem_state_latitude_longitude_idx'.
+Drop the 'easting' and 'northing' columns.
+
+=cut
+
+sub migrate_problem_table {
+
+ my $dbh = dbh();
+
+ # add columns
+ print "add latitude, longitude columns\n";
+ $dbh->do("ALTER TABLE problem ADD $_ double precision")
+ for qw(latitude longitude);
+ $dbh->commit;
+
+ # create a query for rows that need converting
+ my $rows_to_convert_query = $dbh->prepare( #
+ "SELECT id, easting, northing FROM problem"
+ . " WHERE latitude is NULL limit 1" # FIXME
+ );
+
+ # update query
+ my $update_lat_lon_query = $dbh->prepare( #
+ "UPDATE problem SET latitude = ?, longitude = ? WHERE id = ?"
+ );
+
+ # loop through the entries in batches updating rows that need it. Do this in
+ # Perl rather than SQL for conveniance.
+ while (1) {
+ $rows_to_convert_query->execute;
+ last unless $rows_to_convert_query->rows;
+ while ( my $r = $rows_to_convert_query->fetchrow_hashref ) {
+ my ( $latitude, $longitude ) =
+ _e_n_to_lat_lon( $r->{easting}, $r->{northing} );
+ print "update problem $r->{id}: ( $latitude, $longitude )\n";
+ $update_lat_lon_query->execute( $latitude, $longitude, $r->{id} );
+ }
+ $dbh->commit; # every batch of updates
+ }
+
+ # drop old index, create new one
+ print "drop and create indexes\n";
+ $dbh->do("DROP INDEX problem_state_easting_northing_idx");
+ $dbh->do( "CREATE INDEX problem_state_latitude_longitude_idx "
+ . "ON problem(state, latitude, longitude)" );
+ $dbh->commit;
+
+ # drop columns
+ print "drop easting, northing columns\n";
+ $dbh->do("ALTER TABLE problem DROP $_") for qw(easting northing);
+ $dbh->commit;
+
+}
+
+=head2 problem_find_nearby function
+
+Convert to use lat and long.
+Also swap parameter order so that it is lat,lon rather than lon,lat to be consistent with pledgebank etc
+
+=head2 alert table
+
+NOTE: only for alert_types 'local_problems' or 'local_problems_state'
+
+parameter: convert easting to longitude
+parameter2: convert nothing to latitude
+
+=cut
+
+=head2 HELPERS
+
+=cut
+
+sub _e_n_to_lat_lon {
+ my ( $e, $n ) = @_;
+ my ( $lat, $lon ) = national_grid_to_wgs84( $e, $n, 'G' );
+ return ( $lat, $lon );
+}
diff --git a/db/schema.sql b/db/schema.sql
index 53d188244..59aacce6e 100644
--- a/db/schema.sql
+++ b/db/schema.sql
@@ -121,8 +121,8 @@ create table problem (
-- Problem details
postcode text not null,
- easting double precision not null,
- northing double precision not null,
+ latitude double precision not null,
+ longitude double precision not null,
council text, -- the council(s) we'll report this problem to
areas text not null, -- the voting areas this location is in
category text not null default 'Other',
@@ -155,7 +155,7 @@ create table problem (
whensent timestamp,
send_questionnaire boolean not null default 't'
);
-create index problem_state_easting_northing_idx on problem(state, easting, northing);
+create index problem_state_latitude_longitude_idx on problem(state, latitude, longitude);
create table questionnaire (
id serial not null primary key,
@@ -277,8 +277,8 @@ create table alert_type (
create table alert (
id serial not null primary key,
alert_type text not null references alert_type(ref),
- parameter text, -- e.g. Problem ID for new updates
- parameter2 text, -- e.g. Latitude for local problem alerts
+ parameter text, -- e.g. Problem ID for new updates, Easting for local problem alerts
+ parameter2 text, -- e.g. Northing for local problem alerts
email text not null,
confirmed integer not null default 0,
lang text not null default 'en-gb',