class Geonames::SHP

Public Class Methods

import(file) click to toggle source
# File lib/geonames_local/data/shp.rb, line 70
def self.import(file)
  new(file)
end
new(file) click to toggle source
# File lib/geonames_local/data/shp.rb, line 7
def initialize(file)
  @file = file
  @fname = file.split('/')[-1] rescue nil
  @type = Object.module_eval("::#{Opt[:type].capitalize}", __FILE__, __LINE__)
  # @ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
  @sample = nil
  if file
    shp2pg; parse; write
  end
end

Public Instance Methods

parse() click to toggle source
# File lib/geonames_local/data/shp.rb, line 33
def parse
  info 'Parsing dump'
  start = Time.now
  red = 0
  File.open("/tmp/#{@fname}.dump") do |f|
    while line = f.gets
      if record = parse_line(line.chomp)
        @table ||= record.table
        Cache[@table] << record
        red += 1
      end
    end
  end
  info "#{red} parsed. #{Time.now - start}s"
end
parse_line(l) click to toggle source
# File lib/geonames_local/data/shp.rb, line 23
def parse_line(l)
  return if l =~ /^SET\s|^BEGIN;|^COPY\s|^END;|^\\/
  utf = l.encode('UTF-8')
  unless @sample
    info "Free sample\n" + utf.inspect
    @sample = true
  end
  @type.new(Opt[:map], utf, Opt[:nation].upcase, Opt[:city])
end
reduce!() click to toggle source
# File lib/geonames_local/data/shp.rb, line 49
def reduce!
  hsh = Cache[:roads].group_by(&:name)
  arr = []
  hsh.map do |_key, vals|
    first = vals.delete_at(0)
    #        p vals[0].geom.geometries.concat(vals[1].geom.geometries)
    vals.map(&:geom).each do |g|
      first.geom.geometries.concat g.geometries
    end
    # = GeoRuby::SimpleFeatures::MultiLineString.
    #  from_line_strings([*vals.map(&:geom).map(&:geometries)])
    first
  end
end
shp2pg() click to toggle source
# File lib/geonames_local/data/shp.rb, line 18
def shp2pg
  info 'Converting SRID'
  `shp2pgsql -D -as 4326 #{@file} nil > /tmp/#{@fname}.dump`
end
write() click to toggle source
# File lib/geonames_local/data/shp.rb, line 64
def write
  db = Postgis.new(Opt[:db])
  Geonames::CLI.do_write(db, Cache[:zones])
  Geonames::CLI.do_write(db, reduce!)
end