Permalink
Browse files

Remove rubygems references and fix a bunch of other stuff.

  • Loading branch information...
1 parent b837723 commit fa3a0ffc988563c3f20a4deaeb401cec698a6240 @ieure ieure committed Aug 23, 2010
Showing with 16 additions and 182 deletions.
  1. +1 −0 Makefile
  2. +14 −0 bin/geocoder_api_server
  3. +1 −1 lib/geocoder/us/database.rb
  4. +0 −181 lib/geocoder/us/import.rb
View
@@ -10,4 +10,5 @@ install: all
clean:
make -C src clean
+ rm -f lib/geocoder/us/sqlite3.so
rm -f *.gem
View
@@ -0,0 +1,14 @@
+require 'sinatra'
+require 'geocoder/us/database'
+require 'json'
+
+set :port, 8080
+@@db = Geocoder::US::Database.new(ARGV[1])
+get '/geocode' do
+ if params[:q]
+ (@@db.geocode params[:q]).to_json
+ else
+ status 400
+ "parameter 'q' is missing"
+ end
+end
@@ -1,4 +1,4 @@
-require 'rubygems'
+# require 'rubygems'
require 'sqlite3'
# require 'text'
# require 'levenshtein'
View
@@ -1,181 +0,0 @@
-require 'tmpdir'
-require 'geocoder/us/database'
-
-require 'rubygems'
-require 'geo_ruby'
-require 'zip/zip'
-
-class Geocoder::US::Import < Geocoder::US::Database
- @tables = {}
-
- def self.tables
- @tables
- end
-
- def tables
- self.class.tables
- end
-
- def initialize (filename, options)
- options[:create] = true
- super(filename, options)
- @sqlpath = options[:sql]
- create_tables
- end
-
- def log (*args)
- $stderr.print *args
- end
-
- def spin
- @spin ||= 0
- log "|/-\\"[@spin/100..@spin/100]+"\010" if @spin % 100 == 0
- @spin += 1
- @spin %= 400
- end
-
- def execute_batch (*args)
- @db.execute_batch(*args)
- end
-
- def execute_script (file)
- if File.expand_path(file) != file
- file = File.join(@sqlpath, file)
- end
- execute_batch File.open(file).read
- end
-
- 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
-
- 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
-
- 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
-
- 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
-
- 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
-
- 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
-
- 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
-
- 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
-
- 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
-
- 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
-
- 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
-
- def post_create
- end
-end

0 comments on commit fa3a0ff

Please sign in to comment.