/
photon.rb
90 lines (69 loc) · 2.28 KB
/
photon.rb
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
# frozen_string_literal: true
require 'geocoder/lookups/base'
require 'geocoder/results/photon'
module Geocoder
module Lookup
# https://github.com/komoot/photon
class Photon < Base
def name
'Photon'
end
private
def base_query_url(query)
host = configuration[:host] || 'photon.komoot.de'
method = query.reverse_geocode? ? 'reverse' : 'api'
"#{protocol}://#{host}/#{method}?"
end
def results(query)
return [] unless (doc = fetch_data(query))
return [] unless doc['type'] == 'FeatureCollection'
return [] unless doc['features'] || doc['features'].present?
doc['features']
end
def query_url_params(query)
lang = query.language || configuration.language
params = { lang: lang, limit: query.options[:limit] }
if query.reverse_geocode?
params.merge!(query_url_params_reverse(query))
else
params.merge!(query_url_params_coordinates(query))
end
params.merge!(super)
end
def query_url_params_coordinates(query)
params = { q: query.sanitized_text }
if (bias = query.options[:bias])
params.merge!(lat: bias[:latitude], lon: bias[:longitude], location_bias_scale: bias[:scale])
end
if (filter = query_url_params_coordinates_filter(query))
params.merge!(filter)
end
params
end
def query_url_params_coordinates_filter(query)
filter = query.options[:filter]
return unless filter
bbox = filter[:bbox]
{
bbox: bbox.is_a?(Array) ? bbox.join(',') : bbox,
osm_tag: filter[:osm_tag]
}
end
def query_url_params_reverse(query)
params = { lat: query.coordinates[0], lon: query.coordinates[1], radius: query.options[:radius] }
if (dsort = query.options[:distance_sort])
params[:distance_sort] = dsort ? 'true' : 'false'
end
if (filter = query_url_params_reverse_filter(query))
params.merge!(filter)
end
params
end
def query_url_params_reverse_filter(query)
filter = query.options[:filter]
return unless filter
{ query_string_filter: filter[:string] }
end
end
end
end