Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/photon' of git://github.com/mainio/geocoder int…
…o mainio-feature/photon
- Loading branch information
Showing
10 changed files
with
479 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,8 @@ def street_services | |
:amap, | ||
:osmnames, | ||
:amazon_location_service, | ||
:geoapify | ||
:geoapify, | ||
:photon | ||
] | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'geocoder/results/base' | ||
|
||
module Geocoder | ||
module Result | ||
# https://github.com/komoot/photon | ||
class Photon < Base | ||
def name | ||
properties['name'] | ||
end | ||
|
||
def address(_format = :full) | ||
parts = [] | ||
parts << name if name | ||
parts << street_address if street_address | ||
parts << city | ||
parts << state if state | ||
parts << postal_code | ||
parts << country | ||
|
||
parts.join(', ') | ||
end | ||
|
||
def street_address | ||
return unless street | ||
return street unless house_number | ||
|
||
"#{house_number} #{street}" | ||
end | ||
|
||
def house_number | ||
properties['housenumber'] | ||
end | ||
|
||
def street | ||
properties['street'] | ||
end | ||
|
||
def postal_code | ||
properties['postcode'] | ||
end | ||
|
||
def city | ||
properties['city'] | ||
end | ||
|
||
def state | ||
properties['state'] | ||
end | ||
|
||
def state_code | ||
'' | ||
end | ||
|
||
def country | ||
properties['country'] | ||
end | ||
|
||
def country_code | ||
'' | ||
end | ||
|
||
def coordinates | ||
return unless geometry | ||
return unless geometry[:coordinates] | ||
|
||
geometry[:coordinates].reverse | ||
end | ||
|
||
def geometry | ||
return unless data['geometry'] | ||
|
||
symbol_hash data['geometry'] | ||
end | ||
|
||
def bounds | ||
properties['extent'] | ||
end | ||
|
||
# Type of the result (OSM object type), one of: | ||
# | ||
# :node | ||
# :way | ||
# :relation | ||
# | ||
def type | ||
{ | ||
'N' => :node, | ||
'W' => :way, | ||
'R' => :relation | ||
}[properties['osm_type']] | ||
end | ||
|
||
def osm_id | ||
properties['osm_id'] | ||
end | ||
|
||
# See: https://wiki.openstreetmap.org/wiki/Tags | ||
def osm_tag | ||
return unless properties['osm_key'] | ||
return properties['osm_key'] unless properties['osm_value'] | ||
|
||
"#{properties['osm_key']}=#{properties['osm_value']}" | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"message":"some error happened and the response code is always 400" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"features":[ | ||
{ | ||
"geometry":{ | ||
"coordinates":[ | ||
-73.99355027800776, | ||
40.7505247 | ||
], | ||
"type":"Point" | ||
}, | ||
"type":"Feature", | ||
"properties":{ | ||
"osm_id":138141251, | ||
"osm_type":"W", | ||
"extent":[ | ||
-73.9944446, | ||
40.751161, | ||
-73.9925924, | ||
40.7498531 | ||
], | ||
"country":"United States of America", | ||
"osm_key":"leisure", | ||
"housenumber":"4", | ||
"city":"New York", | ||
"street":"Pennsylvania Plaza", | ||
"osm_value":"stadium", | ||
"postcode":"10001", | ||
"name":"Madison Square Garden", | ||
"state":"New York" | ||
} | ||
} | ||
], | ||
"type":"FeatureCollection" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"type":"FeatureCollection", | ||
"features":[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"features":[ | ||
{ | ||
"geometry":{ | ||
"coordinates":[ | ||
-73.9935078, | ||
40.750499 | ||
], | ||
"type":"Point" | ||
}, | ||
"type":"Feature", | ||
"properties":{ | ||
"osm_id":6985936386, | ||
"osm_type":"N", | ||
"country":"United States of America", | ||
"osm_key":"tourism", | ||
"housenumber":"4", | ||
"city":"New York", | ||
"street":"Pennsylvania Plaza", | ||
"osm_value":"attraction", | ||
"postcode":"10121", | ||
"state":"New York" | ||
} | ||
} | ||
], | ||
"type":"FeatureCollection" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.