Created
August 1, 2014 08:52
-
-
Save anonymous/250107fda026c0e41a40 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
1.9.3p392 :002 > Postcode | |
=> Postcode(id: integer, postcode: string, long: decimal, lat: decimal, address: string, bias: string, data: text, tries: integer, updated_at: datetime, created_at: datetime) | |
1.9.3p392 :003 > |
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
desc 'Import PostCodes And Validate' | |
task :postcode_import => :environment do | |
puts "\n\n Importing PostCodes" | |
file = open('lib/data/postcode.gz') | |
gz = Zlib::GzipReader.new(file).read | |
arr = [] | |
gz.each_line do |line| | |
arr << line | |
end | |
new_arr = arr.map { |a| a.split(',') } | |
lat_long_res = [] | |
new_arr.each do |row| | |
if row[0].include? ' ' | |
postcode = row[0] | |
else | |
postcode = row[0][0..3] + " " + row[0][4..-1] | |
end | |
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 << [postcode, en_to_ll.lat.to_f, en_to_ll.long.to_f] | |
end | |
lat_long_res.each {|postcode, lat, long| Postcode.where(postcode: postcode, lat: lat, long: long).first_or_create} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment