class Geocoder::US::Import
Public Class Methods
new(filename, options)
click to toggle source
Calls superclass method
Geocoder::US::Database::new
# File lib/geocoder/us/import.rb, line 19 def initialize (filename, options) options[:create] = true super(filename, options) @sqlpath = options[:sql] create_tables end
tables()
click to toggle source
# File lib/geocoder/us/import.rb, line 11 def self.tables @tables end
Public Instance Methods
create_tables()
click to toggle source
# File lib/geocoder/us/import.rb, line 165 def create_tables uninit = false begin @db.execute("SELECT 0 FROM place") rescue SQLite3::SQLException uninit = true end if uninit log "creating tables\n" execute_script "create.sql" post_create end end
execute_batch(*args)
click to toggle source
# File lib/geocoder/us/import.rb, line 37 def execute_batch (*args) @db.execute_batch(*args) end
execute_script(file)
click to toggle source
# File lib/geocoder/us/import.rb, line 41 def execute_script (file) if File.expand_path(file) != file file = File.join(@sqlpath, file) end execute_batch File.open(file).read end
import_path(path)
click to toggle source
# File lib/geocoder/us/import.rb, line 138 def import_path (path) log "\n#{path}: " execute_script "setup.sql" @db.transaction do tables.each do |table, glob| file = Dir[File.join(path, glob)][0] next unless file if file =~ /\.zip$/io import_zip file, table else import_shapefile file, table end end end execute_script "convert.sql" end
import_tree(root)
click to toggle source
# File lib/geocoder/us/import.rb, line 155 def import_tree (root) if !Dir[File.join(root, tables.values[0])].empty? import_path root else Dir[File.join(root, "*")].sort.each do |file| import_tree file if File.directory? file end end end
import_zip(zipfile, table)
click to toggle source
# File lib/geocoder/us/import.rb, line 120 def import_zip (zipfile, table) make_temp_dir do |tmpdir| unpack_zip zipfile, tmpdir basename = File.join(tmpdir, File.basename(zipfile))[0..-5] shpfile = basename + ".shp" dbffile = basename + ".dbf" if File.exists? shpfile log "#{table} " insert_shapefile shpfile, table elsif File.exists? dbffile log "#{table} " insert_dbf dbffile, table else log "\nNOT FOUND: #{shpfile}\n" end end end
insert_csv(file, table, delimiter="|")
click to toggle source
# File lib/geocoder/us/import.rb, line 89 def insert_csv (file, table, delimiter="|") st = nil File.open(file).readlines.each do |line| attrs = line.chomp.split(delimiter) insert_data st, table, attrs end end
insert_data(st, table, attrs)
click to toggle source
# File lib/geocoder/us/import.rb, line 62 def insert_data (st, table, attrs) unless st values = placeholders_for attrs st = @db.prepare("INSERT INTO #{table} VALUES (#{values});") end st.execute(attrs) end
insert_dbf(file, table)
click to toggle source
# File lib/geocoder/us/import.rb, line 78 def insert_dbf (file, table) st = nil GeoRuby::Shp4r::Dbf::Reader.open(file) do |dbf| fields = dbf.fields.map {|f| f.name} dbf.rows.each do |record| attrs = fields.map {|f| record[f]} insert_data st, table, attrs end end end
insert_shapefile(file, table)
click to toggle source
# File lib/geocoder/us/import.rb, line 70 def insert_shapefile (file, table) st = nil load_features(file) do |attrs, geom| attrs << SQLite3::Blob.new(geom) if geom insert_data st, table, attrs end end
load_features(file) { |attrs, coords| ... }
click to toggle source
# File lib/geocoder/us/import.rb, line 48 def load_features (file) dataset = GeoRuby::Shp4r::ShpFile.open(file) fields = dataset.fields.map {|f| f.name} dataset.each do |record| attrs = fields.map {|f| record.data[f]} geom = record.geometry geom = geom.geometries[0] \ if geom.kind_of? GeoRuby::SimpleFeatures::GeometryCollection points = geom.points.map {|pt| [pt.x, pt.y].map {|i| (i*1000000).to_i}} coords = points.flatten.pack("V*") yield attrs, coords end end
log(*args)
click to toggle source
# File lib/geocoder/us/import.rb, line 26 def log (*args) $stderr.print *args end
make_temp_dir(cleanup=true) { |path| ... }
click to toggle source
# File lib/geocoder/us/import.rb, line 97 def make_temp_dir (cleanup=true) path = File.join(Dir.tmpdir, "geocoder-#{$$}") FileUtils.mkdir_p path if block_given? begin yield path ensure FileUtils.rm_r(path) if cleanup end else path end end
post_create()
click to toggle source
# File lib/geocoder/us/import.rb, line 179 def post_create end
spin()
click to toggle source
# File lib/geocoder/us/import.rb, line 30 def spin @spin ||= 0 log "|/-\\"[@spin/100..@spin/100]+"\010" if @spin % 100 == 0 @spin += 1 @spin %= 400 end
tables()
click to toggle source
# File lib/geocoder/us/import.rb, line 15 def tables self.class.tables end
unpack_zip(file, path)
click to toggle source
# File lib/geocoder/us/import.rb, line 111 def unpack_zip (file, path) # log "- unpacking #{file}" Zip::ZipFile.open(file).each do |entry| target = File.join(path, entry.name) # log " - #{target}" entry.extract target end end