class OfflineGeocoder

Constants

CSV_PATH
VERSION

Public Class Methods

new() click to toggle source
# File lib/offline_geocoder.rb, line 10
def initialize
  return if defined? @@cities

  @@cities = []
  @@tree = Geokdtree::Tree.new(2)
  @@table = []
  index = 0
  CSV.foreach(CSV_PATH, headers: true, header_converters: :symbol) do |row|
    as_hash = row.to_h
    as_hash[:lat] = as_hash[:lat].to_f
    as_hash[:lon] = as_hash[:lon].to_f
    @@tree.insert([row[:lat], row[:lon]], index)
    @@table << as_hash
    index += 1
  end
end

Public Instance Methods

inspect() click to toggle source

Hide internal variables

# File lib/offline_geocoder.rb, line 38
def inspect
  "#<#{self.class}:0x#{format('%<id>014x', id: (object_id << 1))}>"
end

Private Instance Methods

search_by_attr(query = {}) click to toggle source
# File lib/offline_geocoder.rb, line 48
def search_by_attr(query = {})
  @@table.select { |object| object >= query }.first
end
search_by_latlon(lat, lon) click to toggle source
# File lib/offline_geocoder.rb, line 44
def search_by_latlon(lat, lon)
  @@table[@@tree.nearest([lat, lon]).data.to_i].to_h
end