class Terragona::GeoNames::Dump

Constants

HEADERS

Public Class Methods

new(args = {}) click to toggle source
Calls superclass method Terragona::GeoNames::Base::new
# File lib/terragona/geonames.rb, line 127
def initialize(args = {})
  super
  
  if not args[:dump]
    puts 'No dump file provided'
    return
  end
  @file = File.open(args[:dump])
  @admin_codes_cache = {:adminCode1=>{},
                        :adminCode2=>{},
                        :adminCode3=>{},
                        :adminCode4=>{}}
                        
  @max_points = args[:max_points]                    
end

Public Instance Methods

fetch_geonames(name, country, admin_code_type, admin_code) click to toggle source
# File lib/terragona/geonames.rb, line 143
def fetch_geonames(name, country, admin_code_type, admin_code)
  if admin_code_type and 
     @admin_codes_cache[admin_code_type] and 
     @admin_codes_cache[admin_code_type][admin_code]
  
    @admin_codes_cache[admin_code_type][admin_code]
     
  else
    dump_parser(name, country, admin_code_type, admin_code)
  end
end

Private Instance Methods

cache_hash(hash) click to toggle source
# File lib/terragona/geonames.rb, line 181
def cache_hash(hash)
  [:adminCode1,:adminCode2,:adminCode3,:adminCode4].each {|adm|
    if hash[adm] and not @admin_codes_cache[adm][hash[adm]]
      @admin_codes_cache[adm][hash[adm]]=[]
    end
    if hash[adm] and not @admin_codes_cache[adm][hash[adm]].include? hash
      @admin_codes_cache[adm][hash[adm]].push(hash)
    end
  }
end
dump_parser(name, country, admin_code_type, admin_code) click to toggle source
# File lib/terragona/geonames.rb, line 156
def dump_parser(name, country, admin_code_type, admin_code)
  @file.rewind
  records = @max_points ? @file.first(@max_points) : @file
  records.map {|l| 
    begin
      raw=CSV.parse_line(l,{:col_sep => "\t"})            
    rescue
      next
    end

    hash = {}
    HEADERS.each_with_index {|h,index| hash[h] = raw[index]}
    
    cache_hash(hash)
    
    next unless (name and name.similar(hash[:name]) > 30) or
                (name and hash[:alternatenames] and hash[:alternatenames].include? name) or                       
                (admin_code_type and admin_code and hash[admin_code_type] == admin_code)
    
    next if (country and country != hash[:countryCode]) 
    
    hash  
  }.compact
end