geocoder 1.6.7 → 1.7.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ require 'geocoder/lookups/base'
2
+ require 'geocoder/results/photon'
3
+
4
+ module Geocoder::Lookup
5
+ class Photon < Base
6
+ def name
7
+ 'Photon'
8
+ end
9
+
10
+ private # ---------------------------------------------------------------
11
+
12
+ def supported_protocols
13
+ [:https]
14
+ end
15
+
16
+ def base_query_url(query)
17
+ host = configuration[:host] || 'photon.komoot.io'
18
+ method = query.reverse_geocode? ? 'reverse' : 'api'
19
+ "#{protocol}://#{host}/#{method}?"
20
+ end
21
+
22
+ def results(query)
23
+ return [] unless (doc = fetch_data(query))
24
+ return [] unless doc['type'] == 'FeatureCollection'
25
+ return [] unless doc['features'] || doc['features'].present?
26
+
27
+ doc['features']
28
+ end
29
+
30
+ def query_url_params(query)
31
+ lang = query.language || configuration.language
32
+ params = { lang: lang, limit: query.options[:limit] }
33
+
34
+ if query.reverse_geocode?
35
+ params.merge!(query_url_params_reverse(query))
36
+ else
37
+ params.merge!(query_url_params_coordinates(query))
38
+ end
39
+
40
+ params.merge!(super)
41
+ end
42
+
43
+ def query_url_params_coordinates(query)
44
+ params = { q: query.sanitized_text }
45
+
46
+ if (bias = query.options[:bias])
47
+ params.merge!(lat: bias[:latitude], lon: bias[:longitude], location_bias_scale: bias[:scale])
48
+ end
49
+
50
+ if (filter = query_url_params_coordinates_filter(query))
51
+ params.merge!(filter)
52
+ end
53
+
54
+ params
55
+ end
56
+
57
+ def query_url_params_coordinates_filter(query)
58
+ filter = query.options[:filter]
59
+ return unless filter
60
+
61
+ bbox = filter[:bbox]
62
+ {
63
+ bbox: bbox.is_a?(Array) ? bbox.join(',') : bbox,
64
+ osm_tag: filter[:osm_tag]
65
+ }
66
+ end
67
+
68
+ def query_url_params_reverse(query)
69
+ params = { lat: query.coordinates[0], lon: query.coordinates[1], radius: query.options[:radius] }
70
+
71
+ if (dsort = query.options[:distance_sort])
72
+ params[:distance_sort] = dsort ? 'true' : 'false'
73
+ end
74
+
75
+ if (filter = query_url_params_reverse_filter(query))
76
+ params.merge!(filter)
77
+ end
78
+
79
+ params
80
+ end
81
+
82
+ def query_url_params_reverse_filter(query)
83
+ filter = query.options[:filter]
84
+ return unless filter
85
+
86
+ { query_string_filter: filter[:string] }
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,57 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class AmazonLocationService < Base
5
+ def initialize(result)
6
+ @place = result
7
+ end
8
+
9
+ def coordinates
10
+ [@place.geometry.point[1], @place.geometry.point[0]]
11
+ end
12
+
13
+ def address
14
+ @place.label
15
+ end
16
+
17
+ def neighborhood
18
+ @place.neighborhood
19
+ end
20
+
21
+ def route
22
+ @place.street
23
+ end
24
+
25
+ def city
26
+ @place.municipality || @place.sub_region
27
+ end
28
+
29
+ def state
30
+ @place.region
31
+ end
32
+
33
+ def state_code
34
+ @place.region
35
+ end
36
+
37
+ def province
38
+ @place.region
39
+ end
40
+
41
+ def province_code
42
+ @place.region
43
+ end
44
+
45
+ def postal_code
46
+ @place.postal_code
47
+ end
48
+
49
+ def country
50
+ @place.country
51
+ end
52
+
53
+ def country_code
54
+ @place.country
55
+ end
56
+ end
57
+ end
@@ -16,11 +16,14 @@ module Geocoder::Result
16
16
  end
17
17
  end
18
18
 
19
- def state_code
19
+ def state
20
20
  attributes['Region']
21
21
  end
22
22
 
23
- alias_method :state, :state_code
23
+ def state_code
24
+ abbr = attributes['RegionAbbr']
25
+ abbr.to_s == "" ? state : abbr
26
+ end
24
27
 
25
28
  def country
26
29
  country_key = reverse_geocode? ? "CountryCode" : "Country"
@@ -0,0 +1,179 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'geocoder/results/base'
4
+
5
+ module Geocoder
6
+ module Result
7
+ # https://apidocs.geoapify.com/docs/geocoding/api
8
+ class Geoapify < Base
9
+ def address(_format = :full)
10
+ properties['formatted']
11
+ end
12
+
13
+ def address_line1
14
+ properties['address_line1']
15
+ end
16
+
17
+ def address_line2
18
+ properties['address_line2']
19
+ end
20
+
21
+ def house_number
22
+ properties['housenumber']
23
+ end
24
+
25
+ def street
26
+ properties['street']
27
+ end
28
+
29
+ def postal_code
30
+ properties['postcode']
31
+ end
32
+
33
+ def district
34
+ properties['district']
35
+ end
36
+
37
+ def suburb
38
+ properties['suburb']
39
+ end
40
+
41
+ def city
42
+ properties['city']
43
+ end
44
+
45
+ def county
46
+ properties['county']
47
+ end
48
+
49
+ def state
50
+ properties['state']
51
+ end
52
+
53
+ # Not currently available in the API
54
+ def state_code
55
+ ''
56
+ end
57
+
58
+ def country
59
+ properties['country']
60
+ end
61
+
62
+ def country_code
63
+ return unless properties['country_code']
64
+
65
+ properties['country_code'].upcase
66
+ end
67
+
68
+ def coordinates
69
+ return unless properties['lat']
70
+ return unless properties['lon']
71
+
72
+ [properties['lat'], properties['lon']]
73
+ end
74
+
75
+ # See: https://tools.ietf.org/html/rfc7946#section-3.1
76
+ #
77
+ # Each feature has a "Point" type in the Geoapify API.
78
+ def geometry
79
+ return unless data['geometry']
80
+
81
+ symbol_hash data['geometry']
82
+ end
83
+
84
+ # See: https://tools.ietf.org/html/rfc7946#section-5
85
+ def bounds
86
+ data['bbox']
87
+ end
88
+
89
+ # Type of the result, one of:
90
+ #
91
+ # * :unknown
92
+ # * :amenity
93
+ # * :building
94
+ # * :street
95
+ # * :suburb
96
+ # * :district
97
+ # * :postcode
98
+ # * :city
99
+ # * :county
100
+ # * :state
101
+ # * :country
102
+ #
103
+ def type
104
+ return :unknown unless properties['result_type']
105
+
106
+ properties['result_type'].to_sym
107
+ end
108
+
109
+ # Distance in meters to given bias:proximity or to given coordinates for
110
+ # reverse geocoding
111
+ def distance
112
+ properties['distance']
113
+ end
114
+
115
+ # Calculated rank for the result, containing the following keys:
116
+ #
117
+ # * `popularity` - The popularity score of the result
118
+ # * `confidence` - The confidence value of the result (0-1)
119
+ # * `match_type` - The result's match type, one of following:
120
+ # * full_match
121
+ # * inner_part
122
+ # * match_by_building
123
+ # * match_by_street
124
+ # * match_by_postcode
125
+ # * match_by_city_or_disrict
126
+ # * match_by_country_or_state
127
+ #
128
+ # Example:
129
+ # {
130
+ # popularity: 8.615793062435909,
131
+ # confidence: 0.88,
132
+ # match_type: :full_match
133
+ # }
134
+ def rank
135
+ return unless properties['rank']
136
+
137
+ r = symbol_hash(properties['rank'])
138
+ r[:match_type] = r[:match_type].to_sym if r[:match_type]
139
+ r
140
+ end
141
+
142
+ # Examples:
143
+ #
144
+ # Open
145
+ # {
146
+ # sourcename: 'openstreetmap',
147
+ # wheelchair: 'limited',
148
+ # wikidata: 'Q186125',
149
+ # wikipedia: 'en:Madison Square Garden',
150
+ # website: 'http://www.thegarden.com/',
151
+ # phone: '12124656741',
152
+ # osm_type: 'W',
153
+ # osm_id: 138141251,
154
+ # continent: 'North America',
155
+ # }
156
+ def datasource
157
+ return unless properties['datasource']
158
+
159
+ symbol_hash properties['datasource']
160
+ end
161
+
162
+ private
163
+
164
+ def properties
165
+ @properties ||= data['properties'] || {}
166
+ end
167
+
168
+ def symbol_hash(orig_hash)
169
+ {}.tap do |result|
170
+ orig_hash.each_key do |key|
171
+ next unless orig_hash[key]
172
+
173
+ result[key.to_sym] = orig_hash[key]
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,54 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder
4
+ module Result
5
+ class Ipqualityscore < Base
6
+
7
+ def self.key_method_mappings
8
+ {
9
+ 'request_id' => :request_id,
10
+ 'success' => :success?,
11
+ 'message' => :message,
12
+ 'city' => :city,
13
+ 'region' => :state,
14
+ 'country_code' => :country_code,
15
+ 'mobile' => :mobile?,
16
+ 'fraud_score' => :fraud_score,
17
+ 'ISP' => :isp,
18
+ 'ASN' => :asn,
19
+ 'organization' => :organization,
20
+ 'is_crawler' => :crawler?,
21
+ 'host' => :host,
22
+ 'proxy' => :proxy?,
23
+ 'vpn' => :vpn?,
24
+ 'tor' => :tor?,
25
+ 'active_vpn' => :active_vpn?,
26
+ 'active_tor' => :active_tor?,
27
+ 'recent_abuse' => :recent_abuse?,
28
+ 'bot_status' => :bot?,
29
+ 'connection_type' => :connection_type,
30
+ 'abuse_velocity' => :abuse_velocity,
31
+ 'timezone' => :timezone,
32
+ }
33
+ end
34
+
35
+ key_method_mappings.each_pair do |key, meth|
36
+ define_method meth do
37
+ @data[key]
38
+ end
39
+ end
40
+
41
+ alias_method :state_code, :state
42
+ alias_method :country, :country_code
43
+
44
+ def postal_code
45
+ '' # No suitable fallback
46
+ end
47
+
48
+ def address
49
+ [city, state, country_code].compact.reject(&:empty?).join(', ')
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -23,7 +23,10 @@ module Geocoder::Result
23
23
  context_part('region')
24
24
  end
25
25
 
26
- alias_method :state_code, :state
26
+ def state_code
27
+ value = context_part('region', 'short_code')
28
+ value.split('-').last unless value.nil?
29
+ end
27
30
 
28
31
  def postal_code
29
32
  context_part('postcode')
@@ -33,7 +36,10 @@ module Geocoder::Result
33
36
  context_part('country')
34
37
  end
35
38
 
36
- alias_method :country_code, :country
39
+ def country_code
40
+ value = context_part('country', 'short_code')
41
+ value.upcase unless value.nil?
42
+ end
37
43
 
38
44
  def neighborhood
39
45
  context_part('neighborhood')
@@ -45,8 +51,8 @@ module Geocoder::Result
45
51
 
46
52
  private
47
53
 
48
- def context_part(name)
49
- context.map { |c| c['text'] if c['id'] =~ Regexp.new(name) }.compact.first
54
+ def context_part(name, key = 'text')
55
+ (context.detect { |c| c['id'] =~ Regexp.new(name) } || {})[key]
50
56
  end
51
57
 
52
58
  def context
@@ -0,0 +1,46 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class MelissaStreet < Base
5
+ def address(format = :full)
6
+ @data['FormattedAddress']
7
+ end
8
+
9
+ def street_address
10
+ @data['AddressLine1']
11
+ end
12
+
13
+ def suffix
14
+ @data['ThoroughfareTrailingType']
15
+ end
16
+
17
+ def number
18
+ @data['PremisesNumber']
19
+ end
20
+
21
+ def city
22
+ @data['Locality']
23
+ end
24
+
25
+ def state_code
26
+ @data['AdministrativeArea']
27
+ end
28
+ alias_method :state, :state_code
29
+
30
+ def country
31
+ @data['CountryName']
32
+ end
33
+
34
+ def country_code
35
+ @data['CountryISO3166_1_Alpha2']
36
+ end
37
+
38
+ def postal_code
39
+ @data['PostalCode']
40
+ end
41
+
42
+ def coordinates
43
+ [@data['Latitude'].to_f, @data['Longitude'].to_f]
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,119 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class Photon < Base
5
+ def name
6
+ properties['name']
7
+ end
8
+
9
+ def address(_format = :full)
10
+ parts = []
11
+ parts << name if name
12
+ parts << street_address if street_address
13
+ parts << city
14
+ parts << state if state
15
+ parts << postal_code
16
+ parts << country
17
+
18
+ parts.join(', ')
19
+ end
20
+
21
+ def street_address
22
+ return unless street
23
+ return street unless house_number
24
+
25
+ "#{house_number} #{street}"
26
+ end
27
+
28
+ def house_number
29
+ properties['housenumber']
30
+ end
31
+
32
+ def street
33
+ properties['street']
34
+ end
35
+
36
+ def postal_code
37
+ properties['postcode']
38
+ end
39
+
40
+ def city
41
+ properties['city']
42
+ end
43
+
44
+ def state
45
+ properties['state']
46
+ end
47
+
48
+ def state_code
49
+ ''
50
+ end
51
+
52
+ def country
53
+ properties['country']
54
+ end
55
+
56
+ def country_code
57
+ ''
58
+ end
59
+
60
+ def coordinates
61
+ return unless geometry
62
+ return unless geometry[:coordinates]
63
+
64
+ geometry[:coordinates].reverse
65
+ end
66
+
67
+ def geometry
68
+ return unless data['geometry']
69
+
70
+ symbol_hash data['geometry']
71
+ end
72
+
73
+ def bounds
74
+ properties['extent']
75
+ end
76
+
77
+ # Type of the result (OSM object type), one of:
78
+ #
79
+ # :node
80
+ # :way
81
+ # :relation
82
+ #
83
+ def type
84
+ {
85
+ 'N' => :node,
86
+ 'W' => :way,
87
+ 'R' => :relation
88
+ }[properties['osm_type']]
89
+ end
90
+
91
+ def osm_id
92
+ properties['osm_id']
93
+ end
94
+
95
+ # See: https://wiki.openstreetmap.org/wiki/Tags
96
+ def osm_tag
97
+ return unless properties['osm_key']
98
+ return properties['osm_key'] unless properties['osm_value']
99
+
100
+ "#{properties['osm_key']}=#{properties['osm_value']}"
101
+ end
102
+
103
+ private
104
+
105
+ def properties
106
+ @properties ||= data['properties'] || {}
107
+ end
108
+
109
+ def symbol_hash(orig_hash)
110
+ {}.tap do |result|
111
+ orig_hash.each_key do |key|
112
+ next unless orig_hash[key]
113
+
114
+ result[key.to_sym] = orig_hash[key]
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -1,3 +1,3 @@
1
1
  module Geocoder
2
- VERSION = "1.6.7"
2
+ VERSION = "1.7.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geocoder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.7
4
+ version: 1.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Reisner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-17 00:00:00.000000000 Z
11
+ date: 2022-01-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Object geocoding (by street or IP address), reverse geocoding (coordinates
14
14
  to street address), distance queries for ActiveRecord and Mongoid, result caching,
@@ -39,6 +39,9 @@ files:
39
39
  - lib/generators/geocoder/migration_version.rb
40
40
  - lib/geocoder.rb
41
41
  - lib/geocoder/cache.rb
42
+ - lib/geocoder/cache_stores/base.rb
43
+ - lib/geocoder/cache_stores/generic.rb
44
+ - lib/geocoder/cache_stores/redis.rb
42
45
  - lib/geocoder/calculations.rb
43
46
  - lib/geocoder/cli.rb
44
47
  - lib/geocoder/configuration.rb
@@ -51,6 +54,7 @@ files:
51
54
  - lib/geocoder/lookup.rb
52
55
  - lib/geocoder/lookups/abstract_api.rb
53
56
  - lib/geocoder/lookups/amap.rb
57
+ - lib/geocoder/lookups/amazon_location_service.rb
54
58
  - lib/geocoder/lookups/baidu.rb
55
59
  - lib/geocoder/lookups/baidu_ip.rb
56
60
  - lib/geocoder/lookups/ban_data_gouv_fr.rb
@@ -60,6 +64,7 @@ files:
60
64
  - lib/geocoder/lookups/dstk.rb
61
65
  - lib/geocoder/lookups/esri.rb
62
66
  - lib/geocoder/lookups/freegeoip.rb
67
+ - lib/geocoder/lookups/geoapify.rb
63
68
  - lib/geocoder/lookups/geocoder_ca.rb
64
69
  - lib/geocoder/lookups/geocodio.rb
65
70
  - lib/geocoder/lookups/geoip2.rb
@@ -74,6 +79,7 @@ files:
74
79
  - lib/geocoder/lookups/ipdata_co.rb
75
80
  - lib/geocoder/lookups/ipgeolocation.rb
76
81
  - lib/geocoder/lookups/ipinfo_io.rb
82
+ - lib/geocoder/lookups/ipqualityscore.rb
77
83
  - lib/geocoder/lookups/ipregistry.rb
78
84
  - lib/geocoder/lookups/ipstack.rb
79
85
  - lib/geocoder/lookups/latlon.rb
@@ -83,11 +89,13 @@ files:
83
89
  - lib/geocoder/lookups/maxmind.rb
84
90
  - lib/geocoder/lookups/maxmind_geoip2.rb
85
91
  - lib/geocoder/lookups/maxmind_local.rb
92
+ - lib/geocoder/lookups/melissa_street.rb
86
93
  - lib/geocoder/lookups/nationaal_georegister_nl.rb
87
94
  - lib/geocoder/lookups/nominatim.rb
88
95
  - lib/geocoder/lookups/opencagedata.rb
89
96
  - lib/geocoder/lookups/osmnames.rb
90
97
  - lib/geocoder/lookups/pelias.rb
98
+ - lib/geocoder/lookups/photon.rb
91
99
  - lib/geocoder/lookups/pickpoint.rb
92
100
  - lib/geocoder/lookups/pointpin.rb
93
101
  - lib/geocoder/lookups/postcode_anywhere_uk.rb
@@ -108,6 +116,7 @@ files:
108
116
  - lib/geocoder/request.rb
109
117
  - lib/geocoder/results/abstract_api.rb
110
118
  - lib/geocoder/results/amap.rb
119
+ - lib/geocoder/results/amazon_location_service.rb
111
120
  - lib/geocoder/results/baidu.rb
112
121
  - lib/geocoder/results/baidu_ip.rb
113
122
  - lib/geocoder/results/ban_data_gouv_fr.rb
@@ -117,6 +126,7 @@ files:
117
126
  - lib/geocoder/results/dstk.rb
118
127
  - lib/geocoder/results/esri.rb
119
128
  - lib/geocoder/results/freegeoip.rb
129
+ - lib/geocoder/results/geoapify.rb
120
130
  - lib/geocoder/results/geocoder_ca.rb
121
131
  - lib/geocoder/results/geocodio.rb
122
132
  - lib/geocoder/results/geoip2.rb
@@ -131,6 +141,7 @@ files:
131
141
  - lib/geocoder/results/ipdata_co.rb
132
142
  - lib/geocoder/results/ipgeolocation.rb
133
143
  - lib/geocoder/results/ipinfo_io.rb
144
+ - lib/geocoder/results/ipqualityscore.rb
134
145
  - lib/geocoder/results/ipregistry.rb
135
146
  - lib/geocoder/results/ipstack.rb
136
147
  - lib/geocoder/results/latlon.rb
@@ -140,11 +151,13 @@ files:
140
151
  - lib/geocoder/results/maxmind.rb
141
152
  - lib/geocoder/results/maxmind_geoip2.rb
142
153
  - lib/geocoder/results/maxmind_local.rb
154
+ - lib/geocoder/results/melissa_street.rb
143
155
  - lib/geocoder/results/nationaal_georegister_nl.rb
144
156
  - lib/geocoder/results/nominatim.rb
145
157
  - lib/geocoder/results/opencagedata.rb
146
158
  - lib/geocoder/results/osmnames.rb
147
159
  - lib/geocoder/results/pelias.rb
160
+ - lib/geocoder/results/photon.rb
148
161
  - lib/geocoder/results/pickpoint.rb
149
162
  - lib/geocoder/results/pointpin.rb
150
163
  - lib/geocoder/results/postcode_anywhere_uk.rb