Class: OfflineGeocoder

Inherits:
Object
  • Object
show all
Defined in:
lib/offline_geocoder.rb,
lib/offline_geocoder/version.rb

Constant Summary collapse

CSV_PATH =
File.expand_path('../og_cities1000.csv', __dir__)
VERSION =
'0.3'

Instance Method Summary collapse

Constructor Details

#initializeOfflineGeocoder

Returns a new instance of OfflineGeocoder.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Method Details

#inspectObject

Hide internal variables



38
39
40
# File 'lib/offline_geocoder.rb', line 38

def inspect
  "#<#{self.class}:0x#{format('%<id>014x', id: (object_id << 1))}>"
end

#search(query, lon = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/offline_geocoder.rb', line 27

def search(query, lon = nil)
  lat, lon = lon.nil? ? [query[:lat], query[:lon]] : [query, lon]

  if lat && lon
    search_by_latlon(lat.to_f, lon.to_f)
  else
    search_by_attr(query)
  end
end