]> git.openstreetmap.org Git - rails.git/blob - app/controllers/searches/nominatim_reverse_queries_controller.rb
Merge remote-tracking branch 'upstream/pull/5957'
[rails.git] / app / controllers / searches / nominatim_reverse_queries_controller.rb
1 module Searches
2   class NominatimReverseQueriesController < QueriesController
3     include NominatimMethods
4
5     def create
6       # get query parameters
7       zoom = params[:zoom]
8
9       # create result array
10       @results = []
11
12       # ask nominatim
13       response = fetch_xml(nominatim_reverse_query_url(:format => "xml"))
14
15       # parse the response
16       response.elements.each("reversegeocode/result") do |result|
17         lat = result.attributes["lat"]
18         lon = result.attributes["lon"]
19         object_type = result.attributes["osm_type"]
20         object_id = result.attributes["osm_id"]
21         description = result.text
22
23         @results.push(:lat => lat, :lon => lon,
24                       :zoom => zoom,
25                       :name => description,
26                       :type => object_type, :id => object_id)
27
28         respond_to do |format|
29           format.html
30           format.json { render :json => @results }
31         end
32       end
33     rescue StandardError => e
34       host = URI(Settings.nominatim_url).host
35       @error = "Error contacting #{host}: #{e}"
36       render :action => "error"
37     end
38   end
39 end