-
-
Save mikecmpbll/9afefdc179bdcfd90f07 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 | |
### 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