X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/ddc252016a42c076c4f8dd8d0efd1caf055eb518..d80fac7ad90bfd20d1ad66fba034c2a93e5713d2:/app/controllers/geocoder_controller.rb diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index 0419fb3b8..8ec0ab6b7 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -13,10 +13,10 @@ class GeocoderController < ApplicationController @sources = [] if @params[:lat] && @params[:lon] - @sources.push({ :name => "latlon", :parameters => "" }) - @sources.push({ :name => "osm_nominatim_reverse", :parameters => "reverse?format=html&#{nominatim_reverse_url_parameters}" }) + @sources.push(:name => "latlon", :url => root_path) + @sources.push(:name => "osm_nominatim_reverse", :url => nominatim_reverse_url(:format => "html")) elsif @params[:query] - @sources.push({ :name => "osm_nominatim", :parameters => "search?format=html&#{nominatim_url_parameters}" }) + @sources.push(:name => "osm_nominatim", :url => nominatim_url(:format => "html")) end if @sources.empty? @@ -72,7 +72,7 @@ class GeocoderController < ApplicationController def search_osm_nominatim # ask nominatim - response = fetch_xml("#{Settings.nominatim_url}search?format=xml&" + nominatim_url_parameters) + response = fetch_xml(nominatim_url(:format => "xml")) # extract the results from the response results = response.elements["searchresults"] @@ -131,7 +131,7 @@ class GeocoderController < ApplicationController @results = [] # ask nominatim - response = fetch_xml("#{Settings.nominatim_url}reverse?" + nominatim_reverse_url_parameters) + response = fetch_xml(nominatim_reverse_url(:format => "xml")) # parse the response response.elements.each("reversegeocode/result") do |result| @@ -156,7 +156,7 @@ class GeocoderController < ApplicationController private - def nominatim_url_parameters + def nominatim_url(format: nil) # get query parameters query = params[:query] minlon = params[:minlon] @@ -170,16 +170,18 @@ class GeocoderController < ApplicationController # 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(',')}" + # build url + "#{Settings.nominatim_url}search?format=#{format}&extratags=1&q=#{escape_query(query)}#{viewbox}#{exclude}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}" end - def nominatim_reverse_url_parameters + def nominatim_reverse_url(format: nil) # 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(',')}" + # build url + "#{Settings.nominatim_url}reverse?format=#{format}&lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}" end def fetch_text(url) @@ -216,10 +218,10 @@ class GeocoderController < ApplicationController query.match(/^(\d{1,3})°?\s*(\d{1,2})['′]?(?:\s*(\d{1,3}(\.\d*)?)?["″]?)?\s*([NS])\W*(\d{1,3})°?\s*(\d{1,2})['′]?(?:\s*(\d{1,3}(\.\d*)?)?["″]?)?\s*([EW])$/).try(:captures) # degrees, minutes, decimal seconds [NSEW] params.merge!(dms_to_decdeg(latlon)).delete(:query) - elsif latlon = query.match(/^([+-]?\d+(\.\d*)?)(?:\s+|\s*,\s*)([+-]?\d+(\.\d*)?)$/) + elsif latlon = query.match(%r{^([+-]?\d+(\.\d*)?)(?:\s+|\s*[,/]\s*)([+-]?\d+(\.\d*)?)$}) params.merge!(:lat => latlon[1].to_f, :lon => latlon[3].to_f).delete(:query) - params[:latlon_digits] = true unless params[:whereami] + params[:latlon_digits] = true end end