Created
July 29, 2014 13:17
-
-
Save anonymous/76685639979b9d6d648c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| AB106XB #This here doesn't have a space | |
| CB2 1EG # This here has a space. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample data