Created
January 18, 2012 07:15
-
-
Save hexatridecimal/1631758 to your computer and use it in GitHub Desktop.
Ruby script to import tiger/line 2011 data into geocommons geocoder.db
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
#!/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