File: geoapify.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 (179 lines) | stat: -rw-r--r-- 3,934 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# frozen_string_literal: true

require 'geocoder/results/base'

module Geocoder
  module Result
    # https://apidocs.geoapify.com/docs/geocoding/api
    class Geoapify < Base
      def address(_format = :full)
        properties['formatted']
      end

      def address_line1
        properties['address_line1']
      end

      def address_line2
        properties['address_line2']
      end

      def house_number
        properties['housenumber']
      end

      def street
        properties['street']
      end

      def postal_code
        properties['postcode']
      end

      def district
        properties['district']
      end

      def suburb
        properties['suburb']
      end

      def city
        properties['city']
      end

      def county
        properties['county']
      end

      def state
        properties['state']
      end

      # Not currently available in the API
      def state_code
        ''
      end

      def country
        properties['country']
      end

      def country_code
        return unless properties['country_code']

        properties['country_code'].upcase
      end

      def coordinates
        return unless properties['lat']
        return unless properties['lon']

        [properties['lat'], properties['lon']]
      end

      # See: https://tools.ietf.org/html/rfc7946#section-3.1
      #
      # Each feature has a "Point" type in the Geoapify API.
      def geometry
        return unless data['geometry']

        symbol_hash data['geometry']
      end

      # See: https://tools.ietf.org/html/rfc7946#section-5
      def bounds
        data['bbox']
      end

      # Type of the result, one of:
      #
      #   * :unknown
      #   * :amenity
      #   * :building
      #   * :street
      #   * :suburb
      #   * :district
      #   * :postcode
      #   * :city
      #   * :county
      #   * :state
      #   * :country
      #
      def type
        return :unknown unless properties['result_type']

        properties['result_type'].to_sym
      end

      # Distance in meters to given bias:proximity or to given coordinates for
      # reverse geocoding
      def distance
        properties['distance']
      end

      # Calculated rank for the result, containing the following keys:
      #
      #   * `popularity` - The popularity score of the result
      #   * `confidence` - The confidence value of the result (0-1)
      #   * `match_type` - The result's match type, one of following:
      #      * full_match
      #      * inner_part
      #      * match_by_building
      #      * match_by_street
      #      * match_by_postcode
      #      * match_by_city_or_disrict
      #      * match_by_country_or_state
      #
      # Example:
      #   {
      #     popularity: 8.615793062435909,
      #     confidence: 0.88,
      #     match_type: :full_match
      #   }
      def rank
        return unless properties['rank']

        r = symbol_hash(properties['rank'])
        r[:match_type] = r[:match_type].to_sym if r[:match_type]
        r
      end

      # Examples:
      #
      # Open
      #   {
      #     sourcename: 'openstreetmap',
      #     wheelchair: 'limited',
      #     wikidata: 'Q186125',
      #     wikipedia: 'en:Madison Square Garden',
      #     website: 'http://www.thegarden.com/',
      #     phone: '12124656741',
      #     osm_type: 'W',
      #     osm_id: 138141251,
      #     continent: 'North America',
      #   }
      def datasource
        return unless properties['datasource']

        symbol_hash properties['datasource']
      end

      private

      def properties
        @properties ||= data['properties'] || {}
      end

      def symbol_hash(orig_hash)
        {}.tap do |result|
          orig_hash.each_key do |key|
            next unless orig_hash[key]

            result[key.to_sym] = orig_hash[key]
          end
        end
      end
    end
  end
end