From 8a950b81740b0053e99b3a7489dac947da04e228 Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Sun, 29 Mar 2015 16:03:28 +0100 Subject: [PATCH] Add scripts to load polling station and polling district data --- scripts/load-polling-district-postcodes.rb | 13 +++++++++++ scripts/load-polling-districts.rb | 25 ++++++++++++++++++++++ scripts/load-polling-stations.rb | 24 +++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 scripts/load-polling-district-postcodes.rb create mode 100644 scripts/load-polling-districts.rb create mode 100644 scripts/load-polling-stations.rb diff --git a/scripts/load-polling-district-postcodes.rb b/scripts/load-polling-district-postcodes.rb new file mode 100644 index 0000000..41ee19a --- /dev/null +++ b/scripts/load-polling-district-postcodes.rb @@ -0,0 +1,13 @@ +require 'csv' +require './models' + +CSV.foreach(ARGV.shift) do |row| + pc = row[0].strip + puts pc + if @postcode = Postcode.get(pc) + @postcode.polling_station_id = row[1] + @postcode.save + else + puts "#{pc} not found" + end +end diff --git a/scripts/load-polling-districts.rb b/scripts/load-polling-districts.rb new file mode 100644 index 0000000..b0c5d06 --- /dev/null +++ b/scripts/load-polling-districts.rb @@ -0,0 +1,25 @@ +# load polling districts data + +require './models' +require 'csv' +require 'pp' + +DataMapper::Model.raise_on_save_failure = true +PollingStation.destroy + +CSV.foreach(ARGV.shift, :headers => true) do |row| + begin + @p = PollingStation.create( + :id => row['District'].strip, + :easting => row['Eastings'], + :northing => row['Northings'], + :lat => row['lat'], + :lng => row['lng'] + ) + + @p.save + rescue + pp @p + # puts p.errors + end +end diff --git a/scripts/load-polling-stations.rb b/scripts/load-polling-stations.rb new file mode 100644 index 0000000..e88a252 --- /dev/null +++ b/scripts/load-polling-stations.rb @@ -0,0 +1,24 @@ +# load polling stations data (polling-stations.csv) + +require './models' +require 'csv' +require 'pp' + +DataMapper::Model.raise_on_save_failure = true + +CSV.foreach(ARGV.shift, :headers => false) do |row| + begin + if @p = PollingStation.get(row[1]) + @p.name = row[2] + @p.address = row[3] + @p.postcode = row[4] + @p.save + else + puts "#{row[1]} not found" + end + rescue + puts @p.saved? + pp @p + @p.errors.each { |r| puts r } + end +end