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