class GeoBlacklightSchema::Gazetteer

Constants

CSV_FN

Public Class Methods

new() click to toggle source
# File lib/geoblacklight/gazetteer.rb, line 13
def initialize
  @registry = {}
  CSV.foreach(CSV_FN, :encoding => 'UTF-8', :headers => true) do |v|
    v = v.each { |k,v| v.to_s.strip }
    k = v[0]
    k = v[1] if k.nil? or k.empty?
    k.strip!
    @registry[k] = {
      :geonames_placename => v[1],
      :geonames_id => v[2].to_i,
      :loc_keyword => (v[3].nil? or v[3].empty?)? nil : v[3],
      :loc_id => (v[4].nil? or v[4].empty?)? nil : v[4]
    }
    if @registry[k][:geonames_placename].nil? && @registry[k][:loc_keyword].nil?
      @registry[k] = nil
    end
  end
end

Public Instance Methods

blank?(k) click to toggle source
# File lib/geoblacklight/gazetteer.rb, line 91
def blank?(k)
  @registry.include?(k) && @registry[k].nil?
end
each() { |k| ... } click to toggle source
# File lib/geoblacklight/gazetteer.rb, line 32
def each
  @registry.each_key.to_a.sort.each {|k| yield k }
end
find_id(k) click to toggle source

@return [Integer] geonames id

# File lib/geoblacklight/gazetteer.rb, line 42
def find_id(k)
  _get(k, :geonames_id)
end
find_keyword_by_id(id) click to toggle source

@return [String] The keyword

# File lib/geoblacklight/gazetteer.rb, line 84
def find_keyword_by_id(id)
  @registry.each do |k,v|
    return k if v[:geonames_id] == id
  end
  nil
end
find_loc_authority(k) click to toggle source

@return [String] authority name

# File lib/geoblacklight/gazetteer.rb, line 66
def find_loc_authority(k)
  lcid = _get(k, :loc_id)
  return $1 if lcid =~ /^(lcsh|lcnaf):/
  return 'lcsh' if lcid =~ /^sh\d+$/
  return 'lcnaf' if lcid =~ /^(n|no)\d+$/
  return 'lcsh' unless find_loc_keyword(k).nil? # default to lcsh if present
  nil
end
find_loc_keyword(k) click to toggle source

@return [String] library of congress name

# File lib/geoblacklight/gazetteer.rb, line 47
def find_loc_keyword(k)
  _get(k, :loc_keyword)
end
find_loc_uri(k) click to toggle source

@return [String] library of congress valueURI

# File lib/geoblacklight/gazetteer.rb, line 52
def find_loc_uri(k)
  lcid = _get(k, :loc_id)
  if lcid =~ /^lcsh:(\d+)$/ or lcid =~ /^sh(\d+)$/
    "http://id.loc.gov/authorities/subjects/sh#{$1}"
  elsif lcid =~ /^lcnaf:(\d+)$/ or lcid =~ /^n(\d+)$/
    "http://id.loc.gov/authorities/names/n#{$1}"
  elsif lcid =~ /^no(\d+)$/
    "http://id.loc.gov/authorities/names/no#{$1}"
  else
    nil
  end
end
find_placename(k) click to toggle source

@return [String] geonames name

# File lib/geoblacklight/gazetteer.rb, line 37
def find_placename(k)
  _get(k, :geonames_placename)
end
find_placename_uri(k) click to toggle source

@see www.geonames.org/ontology/documentation.html @return [String] geonames uri (includes trailing / as specified)

# File lib/geoblacklight/gazetteer.rb, line 78
def find_placename_uri(k)
  return nil if _get(k, :geonames_id).nil?
  "http://sws.geonames.org/#{_get(k, :geonames_id)}/"
end

Private Instance Methods

_get(k, i) click to toggle source
# File lib/geoblacklight/gazetteer.rb, line 96
def _get(k, i)
  return nil unless @registry.include?(k.strip)
  raise ArgumentError unless i.is_a? Symbol
  @registry[k.strip].nil?? nil : @registry[k.strip][i]
end