Skip to content

Instantly share code, notes, and snippets.

@hexatridecimal
Created January 28, 2012 16: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/1694885 to your computer and use it in GitHub Desktop.
Save hexatridecimal/1694885 to your computer and use it in GitHub Desktop.
Geocoder script to build Tiger/LINE 2011
#!/usr/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
cmds = ["echo '.load #{lib}'", "cat '#{sql}/setup.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'
cmds.push shp2+" -aS '#{full}.shp' 'tiger_#{dir.downcase}'"
else
cmds.push shp2+" -an '#{full}.dbf' 'tiger_#{dir.downcase}'"
end
end
end
end
cmds.push "cat '#{sql}/convert.sql'"
cmds.push "cat '#{tmp}/loading.sql'"
system(cmds.join(" | "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment