+ prefix = t ".prefix_format", :name => prefix_name
+ object_type = place.attributes["osm_type"]
+ object_id = place.attributes["osm_id"]
+
+ @results.push(:lat => lat, :lon => lon,
+ :min_lat => min_lat, :max_lat => max_lat,
+ :min_lon => min_lon, :max_lon => max_lon,
+ :prefix => prefix, :name => name,
+ :type => object_type, :id => object_id)
+ end
+
+ render :action => "results"
+ rescue StandardError => e
+ host = URI(Settings.nominatim_url).host
+ @error = "Error contacting #{host}: #{e}"
+ render :action => "error"
+ end
+
+ 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(',')}")
+
+ # parse the response
+ response.elements.each("reversegeocode/result") do |result|
+ lat = result.attributes["lat"]
+ lon = result.attributes["lon"]
+ object_type = result.attributes["osm_type"]
+ object_id = result.attributes["osm_id"]
+ description = result.text
+
+ @results.push(:lat => lat, :lon => lon,
+ :zoom => zoom,
+ :name => description,
+ :type => object_type, :id => object_id)
+ end
+
+ render :action => "results"
+ rescue StandardError => e
+ host = URI(Settings.nominatim_url).host
+ @error = "Error contacting #{host}: #{e}"
+ render :action => "error"
+ end
+
+ private
+
+ def fetch_text(url)
+ response = OSM.http_client.get(URI.parse(url))
+
+ if response.success?
+ response.body
+ else
+ raise response.status.to_s
+ end
+ end
+
+ def fetch_xml(url)
+ REXML::Document.new(fetch_text(url))
+ end
+
+ def escape_query(query)
+ CGI.escape(query)
+ end
+
+ def normalize_params
+ if query = params[:query]
+ query.strip!
+
+ if latlon = query.match(/^([NS])\s*(\d{1,3}(\.\d*)?)\W*([EW])\s*(\d{1,3}(\.\d*)?)$/).try(:captures) || # [NSEW] decimal degrees
+ query.match(/^(\d{1,3}(\.\d*)?)\s*([NS])\W*(\d{1,3}(\.\d*)?)\s*([EW])$/).try(:captures) # decimal degrees [NSEW]
+ params.merge!(nsew_to_decdeg(latlon)).delete(:query)
+
+ elsif latlon = query.match(/^([NS])\s*(\d{1,3})°?(?:\s*(\d{1,3}(\.\d*)?)?['′]?)?\W*([EW])\s*(\d{1,3})°?(?:\s*(\d{1,3}(\.\d*)?)?['′]?)?$/).try(:captures) || # [NSEW] degrees, decimal minutes
+ query.match(/^(\d{1,3})°?(?:\s*(\d{1,3}(\.\d*)?)?['′]?)?\s*([NS])\W*(\d{1,3})°?(?:\s*(\d{1,3}(\.\d*)?)?['′]?)?\s*([EW])$/).try(:captures) # degrees, decimal minutes [NSEW]
+ params.merge!(ddm_to_decdeg(latlon)).delete(:query)
+
+ elsif latlon = query.match(/^([NS])\s*(\d{1,3})°?\s*(\d{1,2})['′]?(?:\s*(\d{1,3}(\.\d*)?)?["″]?)?\W*([EW])\s*(\d{1,3})°?\s*(\d{1,2})['′]?(?:\s*(\d{1,3}(\.\d*)?)?["″]?)?$/).try(:captures) || # [NSEW] degrees, minutes, decimal seconds
+ 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*)?)$/)
+ params.merge!(:lat => latlon[1].to_f, :lon => latlon[3].to_f).delete(:query)
+
+ params[:latlon_digits] = true unless params[:whereami]
+ end