File: nominatim.rb

package info (click to toggle)
ruby-geocoder 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 900 kB
  • sloc: ruby: 8,419; makefile: 3
file content (121 lines) | stat: -rw-r--r-- 2,100 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require 'geocoder/results/base'

module Geocoder::Result
  class Nominatim < Base

    def poi
      return address_data[place_type] if address_data.key?(place_type)
      return nil
    end

    def house_number
      address_data['house_number']
    end

    def address
      @data['display_name']
    end

    def street
      %w[road pedestrian highway].each do |key|
        return address_data[key] if address_data.key?(key)
      end
      return nil
    end

    def city
      %w[city town village hamlet].each do |key|
        return address_data[key] if address_data.key?(key)
      end
      return nil
    end

    def village
      address_data['village']
    end

    def town
      address_data['town']
    end

    def state
      address_data['state']
    end

    alias_method :state_code, :state

    def postal_code
      address_data['postcode']
    end

    def county
      address_data['county']
    end

    def country
      address_data['country']
    end

    def country_code
      address_data['country_code']
    end

    def suburb
      address_data['suburb']
    end

    def city_district
      address_data['city_district']
    end

    def state_district
      address_data['state_district']
    end

    def neighbourhood
      address_data['neighbourhood']
    end

    def municipality
      address_data['municipality']
    end

    def coordinates
      return [] unless @data['lat'] && @data['lon']

      [@data['lat'].to_f, @data['lon'].to_f]
    end

    def place_class
      @data['class']
    end

    def place_type
      @data['type']
    end

    def viewport
      south, north, west, east = @data['boundingbox'].map(&:to_f)
      [south, west, north, east]
    end

    def self.response_attributes
      %w[place_id osm_type osm_id boundingbox license
         polygonpoints display_name class type stadium]
    end

    response_attributes.each do |a|
      unless method_defined?(a)
        define_method a do
          @data[a]
        end
      end
    end

    private

    def address_data
      @data['address'] || {}
    end
  end
end