Created
July 23, 2014 00:14
-
-
Save digitalWestie/afda81610116aca60911 to your computer and use it in GitHub Desktop.
MoD open data spreadsheet to json enricher with latitudes and longitudes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
require 'json' | |
#take a csv of transactions e.g. http://data.gov.uk/dataset/financial-transactions-data-mod/resource/b8d720dc-d6b0-4875-b9fe-7d135cb1c14e | |
#compare with postcodes csv from: https://github.com/Gibbs/UK-Postcodes/raw/master/postcodes.csv | |
def extract_latlng(postcode) | |
unless postcode.nil? or postcode.size.eql?(0) | |
first_part = postcode.split.first | |
first_part = "M5" if first_part.eql?("M50") | |
first_part = "SN3" if first_part.eql?("SN38") | |
first_part = "WV1" if first_part.eql?("WW1") | |
first_part = "CR8" if first_part.eql?("CR9") | |
first_part = "S41" if first_part.eql?("S49") | |
if @postcodes.has_key?(first_part) | |
lat = @postcodes[first_part][3] | |
lng = @postcodes[first_part][4] | |
return [lat, lng] | |
else | |
puts "Did not recognise #{postcode}" | |
end | |
else | |
#puts "No postcode provided" | |
end | |
return ["", ""] | |
end | |
rows=CSV.read("transactions.csv") | |
pcodes=CSV.read("postcodes.csv") | |
ph = pcodes.delete_at(0) | |
@postcodes = {} | |
pcodes.each { |pc| @postcodes.merge!({ pc[0] => pc }) } | |
h = rows.delete_at(0) # ["Department", "Entity", "Date", "Expense Type", "Expense Area", "Supplier", "Post Code", "Transaction No", "Payment Description", "Total"] | |
suppliers = {} | |
for row in rows | |
supplier = row[5] | |
postcode = row[6] | |
total = row[9].to_f.round(2) | |
ll = extract_latlng(postcode) | |
if suppliers.has_key? supplier | |
current_total = suppliers[supplier]["total"].to_f | |
suppliers[supplier].merge!("total" => (current_total + total).round(2)) | |
if suppliers[supplier]["lat"].empty? or suppliers[supplier]["lng"].empty? | |
puts "Trying #{supplier} #{postcode} - #{ll[0]} #{ll[1]}" | |
suppliers[supplier]["lat"] = ll[0] | |
suppliers[supplier]["lng"] = ll[1] | |
end | |
else | |
suppliers.merge!({ supplier => { "total" => total, "postcode" => postcode, "lat" => ll[0], "lng" => ll[1] } }) | |
end | |
end | |
File.open("suppliers.json","w") do |f| | |
f.write(suppliers.to_json) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment