Created
March 18, 2013 11:51
-
-
Save codatory/5186669 to your computer and use it in GitHub Desktop.
This file contains 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
namespace :import do | |
desc 'Imports ZipCodes from geonames.org' | |
task :zip_codes do | |
require 'zip/zip' | |
require Rails.root.join('config/environment.rb') | |
ZIPFILE = Rails.root.join('tmp/zipcodes.zip') | |
CSV_OPTS = {col_sep: "\t", headers: [:country_code, :postal_code, :place_name, :state_name, :state_code, :county_name, :county_code, :community_name, :community_code, :lattitude, :longitude, :accuracy]} | |
if !File.exist?(ZIPFILE) | |
uri = URI('http://download.geonames.org/export/zip/US.zip') | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
request = Net::HTTP::Get.new(uri.request_uri) | |
http.request request do |response| | |
open ZIPFILE, 'w', encoding: Encoding.find('Binary') do |io| | |
response.read_body do |chunk| | |
io.write chunk | |
end | |
end | |
end | |
end | |
end | |
ZipCode.delete_all | |
ZipCode.transaction do | |
Zip::ZipFile.new(ZIPFILE).find_entry('US.txt').get_input_stream.each do |row| | |
CSV.parse(row, CSV_OPTS).each do |zip| | |
next if zip[:place_name] =~ /(FPO|APO)/ | |
ZipCode.create( | |
zip: zip[:postal_code], | |
city: zip[:place_name], | |
state: zip[:state_code], | |
lattitude: zip[:lattitude], | |
longitude: zip[:longitude] | |
) | |
end | |
end | |
end | |
File.delete(ZIPFILE) | |
ZipCode.connection.execute("ANALYZE zip_codes;") | |
ZipCode.connection.execute("SET vacuum_freeze_table_age = 0;") | |
ZipCode.connection.execute("VACUUM FULL zip_codes;") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment