Skip to content

Instantly share code, notes, and snippets.

@hexatridecimal
Created January 18, 2012 07:15
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 hexatridecimal/1631758 to your computer and use it in GitHub Desktop.
Save hexatridecimal/1631758 to your computer and use it in GitHub Desktop.
Ruby script to import tiger/line 2011 data into geocommons geocoder.db
#!/bin/env ruby
require 'tmpdir'
tmp = Dir.tmpdir + "/TIGER2011"
base = Dir.pwd + "/build"
shp2 = base + "/shp2sqlite"
sql = base + "/sql"
lib = Dir.pwd + "/lib/geocoder/us/sqlite3.so"
database = ARGV.shift
tiger = ARGV.shift
STDERR.puts "Building in #{tmp}..."
system("mkdir -p '#{tmp}'")
system("cat '#{sql}/create.sql' '#{sql}/place.sql' | sqlite3 '#{database}'") unless File.exists? database
system("echo '.load #{lib}' > '#{tmp}/loading.sql'")
system("cat '#{sql}/setup.sql' >> '#{tmp}/loading.sql'")
['EDGES', 'FEATNAMES', 'ADDR'].each do |dir|
path = tiger + '/' + dir
Dir.open(path).each do |zip|
if zip =~ /^(.*?)\.zip$/i
STDERR.puts "Unzipping: "+path + "/" + zip+"..."
# Skip unzipping the same zip twice
full = tmp + "/" + $1
system("/usr/bin/unzip", "-q", path + "/" + zip, "-d", tmp)
if dir == 'EDGES'
STDERR.puts "Importing: #{full}.shp..."
system(shp2+" -aS '#{full}.shp' 'tiger_#{dir.downcase}' >> '#{tmp}/loading.sql'")
else
STDERR.puts "Importing: #{full}.dbf..."
system(shp2+" -an '#{full}.dbf' 'tiger_#{dir.downcase}' >> '#{tmp}/loading.sql'")
end
end
end
end
STDERR.puts "Adding conversion..."
system("cat '#{sql}/convert.sql' >> '#{tmp}/loading.sql'")
STDERR.puts "Doing the import..."
system("cat '#{tmp}/loading.sql' | sqlite3 '#{database}'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment