Skip to content

Instantly share code, notes, and snippets.

@mikecmpbll
Forked from anonymous/console
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikecmpbll/9afefdc179bdcfd90f07 to your computer and use it in GitHub Desktop.
Save mikecmpbll/9afefdc179bdcfd90f07 to your computer and use it in GitHub Desktop.
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 >
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
### MIKES AWESUM CODE
sql_start = "INSERT INTO postcodes (postcode, lat, long) VALUES"
lat_long_res.in_groups_of(1000, false) do |g|
values = []
g.each do |pc, lat, long|
values << "(#{pc}, #{lat}, #{long})"
end
values.join(",")
sql = "#{sql_start} #{values};"
Postcode.connection.execute(sql)
end
### END OF MIKES AWESUM CODE
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment