Added link to nominatim results in searching results
Fixes #3205. Added caching of nominatim URL query parameters in sources global variable (as parameters parameter) in GeocoderController#search for both direct and reverse geocoding. In app/views/geocoder/search.html.erb added displaying cached URL as forwarding link when clicked on "OpenStreetMap Nominatim" label. Updated GeocoderControllerTest to check only name (latlng, osm_nominatim, osm_nominatim_reverse) parameter of new sources variable.
This commit is contained in:
parent
d00a0667bc
commit
839d203d51
3 changed files with 38 additions and 27 deletions
|
@ -13,10 +13,10 @@ class GeocoderController < ApplicationController
|
||||||
@sources = []
|
@sources = []
|
||||||
|
|
||||||
if @params[:lat] && @params[:lon]
|
if @params[:lat] && @params[:lon]
|
||||||
@sources.push "latlon"
|
@sources.push({ :name => "latlon", :parameters => "" })
|
||||||
@sources.push "osm_nominatim_reverse"
|
@sources.push({ :name => "osm_nominatim_reverse", :parameters => "reverse?format=html&#{nominatim_reverse_url_parameters}" })
|
||||||
elsif @params[:query]
|
elsif @params[:query]
|
||||||
@sources.push "osm_nominatim"
|
@sources.push({ :name => "osm_nominatim", :parameters => "search?format=html&#{nominatim_url_parameters}" })
|
||||||
end
|
end
|
||||||
|
|
||||||
if @sources.empty?
|
if @sources.empty?
|
||||||
|
@ -71,21 +71,8 @@ class GeocoderController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_osm_nominatim
|
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
|
# 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
|
# extract the results from the response
|
||||||
results = response.elements["searchresults"]
|
results = response.elements["searchresults"]
|
||||||
|
@ -138,15 +125,13 @@ class GeocoderController < ApplicationController
|
||||||
|
|
||||||
def search_osm_nominatim_reverse
|
def search_osm_nominatim_reverse
|
||||||
# get query parameters
|
# get query parameters
|
||||||
lat = params[:lat]
|
|
||||||
lon = params[:lon]
|
|
||||||
zoom = params[:zoom]
|
zoom = params[:zoom]
|
||||||
|
|
||||||
# create result array
|
# create result array
|
||||||
@results = []
|
@results = []
|
||||||
|
|
||||||
# ask nominatim
|
# 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
|
# parse the response
|
||||||
response.elements.each("reversegeocode/result") do |result|
|
response.elements.each("reversegeocode/result") do |result|
|
||||||
|
@ -171,6 +156,32 @@ class GeocoderController < ApplicationController
|
||||||
|
|
||||||
private
|
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)
|
def fetch_text(url)
|
||||||
response = OSM.http_client.get(URI.parse(url))
|
response = OSM.http_client.get(URI.parse(url))
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,10 @@
|
||||||
|
|
||||||
<% @sources.each do |source| %>
|
<% @sources.each do |source| %>
|
||||||
<h4>
|
<h4>
|
||||||
<%= t(".title.results_from_html", :results_link => link_to(t(".title.#{source}"),
|
<%= t(".title.results_from_html", :results_link => link_to(t(".title.#{source[:name]}"),
|
||||||
t(".title.#{source}_url"))) %>
|
t(".title.#{source[:name]}_url").to_s + source[:parameters].to_s)) %>
|
||||||
</h4>
|
</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="text-center loader">
|
||||||
<div class="spinner-border" role="status">
|
<div class="spinner-border" role="status">
|
||||||
<span class="visually-hidden"><%= t("browse.start_rjs.loading") %></span>
|
<span class="visually-hidden"><%= t("browse.start_rjs.loading") %></span>
|
||||||
|
|
|
@ -364,7 +364,7 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest
|
||||||
assert_response :success
|
assert_response :success
|
||||||
assert_template :search
|
assert_template :search
|
||||||
assert_template :layout => "map"
|
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_nil @controller.params[:query]
|
||||||
assert_in_delta lat, @controller.params[:lat]
|
assert_in_delta lat, @controller.params[:lat]
|
||||||
assert_in_delta lon, @controller.params[:lon]
|
assert_in_delta lon, @controller.params[:lon]
|
||||||
|
@ -373,7 +373,7 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest
|
||||||
assert_response :success
|
assert_response :success
|
||||||
assert_template :search
|
assert_template :search
|
||||||
assert_template :layout => "xhr"
|
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_nil @controller.params[:query]
|
||||||
assert_in_delta lat, @controller.params[:lat]
|
assert_in_delta lat, @controller.params[:lat]
|
||||||
assert_in_delta lon, @controller.params[:lon]
|
assert_in_delta lon, @controller.params[:lon]
|
||||||
|
@ -384,13 +384,13 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest
|
||||||
assert_response :success
|
assert_response :success
|
||||||
assert_template :search
|
assert_template :search
|
||||||
assert_template :layout => "map"
|
assert_template :layout => "map"
|
||||||
assert_equal sources, assigns(:sources)
|
assert_equal sources, assigns(:sources).pluck(:name)
|
||||||
|
|
||||||
get search_path(:query => query), :xhr => true
|
get search_path(:query => query), :xhr => true
|
||||||
assert_response :success
|
assert_response :success
|
||||||
assert_template :search
|
assert_template :search
|
||||||
assert_template :layout => "xhr"
|
assert_template :layout => "xhr"
|
||||||
assert_equal sources, assigns(:sources)
|
assert_equal sources, assigns(:sources).pluck(:name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def results_check(*results)
|
def results_check(*results)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue