Skip to content

Instantly share code, notes, and snippets.

Created July 29, 2014 13:17
Show Gist options
  • Save anonymous/76685639979b9d6d648c to your computer and use it in GitHub Desktop.
Save anonymous/76685639979b9d6d648c to your computer and use it in GitHub Desktop.
task :postcode_import => :environment do
puts "\n\n Importing PostCodes"
file = open("lib/data/example.csv")
#gz = Zlib::GzipReader.new(file).read
gz = CSV.read(file)
arr = []
gz.each do |line|
arr << line
end
new_arr = arr.map { |a| a.split(',') }
lat_long_res = []
new_arr.each do |row|
postcode = row[0]
postcode.each {|x| x.scan(/.{4}|.+/).join(' ')} #Insert a space after the 4 character
easting = row[1].to_i
northing = row[2].to_i
en_to_ll = Silva::Location.from(:en, :easting => easting, :northing => northing).to(:wgs84)
lat_long_res << en_to_ll
#lat_long_res << postcode
end
lat_long_res.flatten
lat_long_res.each { |lat, long| Postcode.first_or_create(lat: lat, long: long)}
#lat_long_res.each {|postcode,lat, long| Postcode.find_or_create_by(postcode: postcode, lat: lat, long: long)}
end
AB106XB #This here doesn't have a space
CB2 1EG # This here has a space.
@davidpatters0n
Copy link

Sample data

CB114XJ,548574,239518
CB114XL,548294,240759
CB114XN,546017,237185
CB114XU,551586,239676
CB114YR,553692,238424
CB114ZH,553578,237976
CB114ZX,553692,238424
CB2 0AA,546031,254843
CB2 0AB,546317,255439
CB2 0AD,546284,255443
CB2 0AE,546251,255443
CB2 0AF,546218,255447

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment