class Geonames::Dump

Constants

TMP

Work temporary files

URL

Geonames base URL

Attributes

data[R]

Public Class Methods

new(target, kind) click to toggle source
# File lib/geonames_local/data/dump.rb, line 10
def initialize(target, kind)
  @kind = kind
  @data = []

  target.each { |n| work(n) } if target.respond_to? :each
  nations if target == :all
end

Public Instance Methods

download(file) click to toggle source
# File lib/geonames_local/data/dump.rb, line 37
def download(file)
  Dir.mkdir(TMP) unless File.exist?(TMP)
  Dir.mkdir(TMP + @kind.to_s) unless File.exist?(TMP + @kind.to_s)
  fname = TMP + "#{@kind}/#{file}"
  return if File.exist?(fname)
  `curl #{URL}/#{@kind}/#{file} -o #{fname}`
end
get_file(nation) click to toggle source
# File lib/geonames_local/data/dump.rb, line 33
def get_file(nation)
  nation == 'nation' ? 'countryInfo.txt' : "#{nation.upcase}.zip"
end
nations() click to toggle source
# File lib/geonames_local/data/dump.rb, line 18
def nations
  info "\nDumping nation database"
  file = get_file('nation')
  download file
  parse file
end
parse(file) click to toggle source
# File lib/geonames_local/data/dump.rb, line 61
def parse(file)
  start = Time.now
  File.open("/tmp/geonames/#{@kind}/#{file.gsub('zip', 'txt')}") do |f|
    while line = f.gets
      if record = parse_line(line)
        @data << record
      end
    end
    total = Time.now - start
    info "#{@data.size} #{@kind} spots parsed #{total}s (#{(@data.size / total).to_i}/s)"
  end
rescue Errno::ENOENT => e
  info "Failed to download #{file}, skipping. #{e}"
end
parse_line(l) click to toggle source
# File lib/geonames_local/data/dump.rb, line 50
def parse_line(l)
  return if l =~ /^#|^iso/i
  if @kind == :dump
    return l if l =~ /^\D/
    if Opt[:level] != 'all'
      return unless l =~ /ADM\d/ # ADM2 => cities
    end
  end
  Spot.new(l, @kind)
end
uncompress(file) click to toggle source
# File lib/geonames_local/data/dump.rb, line 45
def uncompress(file)
  info "Uncompressing #{file}"
  `unzip -quo /tmp/geonames/#{@kind}/#{file} -d /tmp/geonames/#{@kind}`
end
work(nation) click to toggle source
# File lib/geonames_local/data/dump.rb, line 25
def work(nation)
  info "\nWorking on #{@kind} for #{nation}"
  file = get_file(nation)
  download file
  uncompress file
  parse file
end