Merge remote-tracking branch 'upstream/pull/4895'
This commit is contained in:
commit
ddc252016a
3 changed files with 38 additions and 27 deletions
|
@ -13,10 +13,10 @@ class GeocoderController < ApplicationController
|
|||
@sources = []
|
||||
|
||||
if @params[:lat] && @params[:lon]
|
||||
@sources.push "latlon"
|
||||
@sources.push "osm_nominatim_reverse"
|
||||
@sources.push({ :name => "latlon", :parameters => "" })
|
||||
@sources.push({ :name => "osm_nominatim_reverse", :parameters => "reverse?format=html&#{nominatim_reverse_url_parameters}" })
|
||||
elsif @params[:query]
|
||||
@sources.push "osm_nominatim"
|
||||
@sources.push({ :name => "osm_nominatim", :parameters => "search?format=html&#{nominatim_url_parameters}" })
|
||||
end
|
||||
|
||||
if @sources.empty?
|
||||
|
@ -71,21 +71,8 @@ class GeocoderController < ApplicationController
|
|||
end
|
||||
|
||||
def search_osm_nominatim
|
||||
# get query parameters
|
||||
query = params[:query]
|
||||
minlon = params[:minlon]
|
||||
minlat = params[:minlat]
|
||||
maxlon = params[:maxlon]
|
||||
maxlat = params[:maxlat]
|
||||
|
||||
# get view box
|
||||
viewbox = "&viewbox=#{minlon},#{maxlat},#{maxlon},#{minlat}" if minlon && minlat && maxlon && maxlat
|
||||
|
||||
# get objects to excude
|
||||
exclude = "&exclude_place_ids=#{params[:exclude]}" if params[:exclude]
|
||||
|
||||
# ask nominatim
|
||||
response = fetch_xml("#{Settings.nominatim_url}search?format=xml&extratags=1&q=#{escape_query(query)}#{viewbox}#{exclude}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}")
|
||||
response = fetch_xml("#{Settings.nominatim_url}search?format=xml&" + nominatim_url_parameters)
|
||||
|
||||
# extract the results from the response
|
||||
results = response.elements["searchresults"]
|
||||
|
@ -138,15 +125,13 @@ class GeocoderController < ApplicationController
|
|||
|
||||
def search_osm_nominatim_reverse
|
||||
# get query parameters
|
||||
lat = params[:lat]
|
||||
lon = params[:lon]
|
||||
zoom = params[:zoom]
|
||||
|
||||
# create result array
|
||||
@results = []
|
||||
|
||||
# ask nominatim
|
||||
response = fetch_xml("#{Settings.nominatim_url}reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}")
|
||||
response = fetch_xml("#{Settings.nominatim_url}reverse?" + nominatim_reverse_url_parameters)
|
||||
|
||||
# parse the response
|
||||
response.elements.each("reversegeocode/result") do |result|
|
||||
|
@ -171,6 +156,32 @@ class GeocoderController < ApplicationController
|
|||
|
||||
private
|
||||
|
||||
def nominatim_url_parameters
|
||||
# get query parameters
|
||||
query = params[:query]
|
||||
minlon = params[:minlon]
|
||||
minlat = params[:minlat]
|
||||
maxlon = params[:maxlon]
|
||||
maxlat = params[:maxlat]
|
||||
|
||||
# get view box
|
||||
viewbox = "&viewbox=#{minlon},#{maxlat},#{maxlon},#{minlat}" if minlon && minlat && maxlon && maxlat
|
||||
|
||||
# get objects to excude
|
||||
exclude = "&exclude_place_ids=#{params[:exclude]}" if params[:exclude]
|
||||
|
||||
"extratags=1&q=#{escape_query(query)}#{viewbox}#{exclude}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}"
|
||||
end
|
||||
|
||||
def nominatim_reverse_url_parameters
|
||||
# get query parameters
|
||||
lat = params[:lat]
|
||||
lon = params[:lon]
|
||||
zoom = params[:zoom]
|
||||
|
||||
"lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}"
|
||||
end
|
||||
|
||||
def fetch_text(url)
|
||||
response = OSM.http_client.get(URI.parse(url))
|
||||
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
|
||||
<% @sources.each do |source| %>
|
||||
<h4>
|
||||
<%= t(".title.results_from_html", :results_link => link_to(t(".title.#{source}"),
|
||||
t(".title.#{source}_url"))) %>
|
||||
<%= t(".title.results_from_html", :results_link => link_to(t(".title.#{source[:name]}"),
|
||||
t(".title.#{source[:name]}_url").to_s + source[:parameters].to_s)) %>
|
||||
</h4>
|
||||
<div class="search_results_entry mx-n3" data-href="<%= url_for @params.merge(:action => "search_#{source}") %>">
|
||||
<div class="search_results_entry mx-n3" data-href="<%= url_for @params.merge(:action => "search_#{source[:name]}") %>">
|
||||
<div class="text-center loader">
|
||||
<div class="spinner-border" role="status">
|
||||
<span class="visually-hidden"><%= t("browse.start_rjs.loading") %></span>
|
||||
|
|
|
@ -364,7 +364,7 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest
|
|||
assert_response :success
|
||||
assert_template :search
|
||||
assert_template :layout => "map"
|
||||
assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources)
|
||||
assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
|
||||
assert_nil @controller.params[:query]
|
||||
assert_in_delta lat, @controller.params[:lat]
|
||||
assert_in_delta lon, @controller.params[:lon]
|
||||
|
@ -373,7 +373,7 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest
|
|||
assert_response :success
|
||||
assert_template :search
|
||||
assert_template :layout => "xhr"
|
||||
assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources)
|
||||
assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources).pluck(:name)
|
||||
assert_nil @controller.params[:query]
|
||||
assert_in_delta lat, @controller.params[:lat]
|
||||
assert_in_delta lon, @controller.params[:lon]
|
||||
|
@ -384,13 +384,13 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest
|
|||
assert_response :success
|
||||
assert_template :search
|
||||
assert_template :layout => "map"
|
||||
assert_equal sources, assigns(:sources)
|
||||
assert_equal sources, assigns(:sources).pluck(:name)
|
||||
|
||||
get search_path(:query => query), :xhr => true
|
||||
assert_response :success
|
||||
assert_template :search
|
||||
assert_template :layout => "xhr"
|
||||
assert_equal sources, assigns(:sources)
|
||||
assert_equal sources, assigns(:sources).pluck(:name)
|
||||
end
|
||||
|
||||
def results_check(*results)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue