Class: Terragona::GeoNames::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/terragona/geonames.rb

Direct Known Subclasses

API, Dump

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Base

Returns a new instance of Base.



10
11
12
# File 'lib/terragona/geonames.rb', line 10

def initialize(args = {})
  @default_country = args[:default_country]
end

Instance Method Details

#search(options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/terragona/geonames.rb', line 14

def search(options)
  country = options[:country] || @default_country
  id = options[:id]
  name = options[:name]
  fcode = options[:fcode]

  children_fcode = options[:children_fcode] || case fcode
                       when 'PCLI' then 'ADM1'
                       when 'ADM1' then 'ADM2'
                       when 'ADM2' then 'ADM3'
                       when 'ADM3' then 'ADM4'
                       when 'ADM4' then 'ADM5'
                       when 'PPLC' then 'PPLX'
                     end

  field_to_compare = options[:field_to_compare] || calculate_field_to_compare(fcode)
  children_field_to_compare = calculate_field_to_compare(children_fcode)
  field_to_compare_value = options[:field_to_compare_value]

  if field_to_compare_value.nil?
    fetch_geonames(name,country,nil,nil).each{|g|
      if g[:fcode] == fcode
        name = g[:name]
        id = g[:geonameId]
        field_to_compare_value = g[field_to_compare]
        break
      end
    }
  end

  points = []
  children_places = []
                  
  fetch_geonames(nil,country,field_to_compare,field_to_compare_value).each{|g|
      points.push({:x=>g[:lng],:y=>g[:lat]})
      if g[:fcode] == children_fcode and children_field_to_compare
        child={:name=>g[:name],
               :id=>g[:geonameId],
               :fcode=>g[:fcode],
               :country=>g[:countryCode],
               :field_to_compare_value=>g[children_field_to_compare]}
        children_places.push(child)
      end
  }

  {:children_places=>children_places,:points=>points,:place_name=>name,:place_id=>id}
end