]> git.openstreetmap.org Git - rails.git/blob - app/controllers/searches/nominatim_queries_controller.rb
Limit number of directions endpoint geocoding results to 1
[rails.git] / app / controllers / searches / nominatim_queries_controller.rb
1 module Searches
2   class NominatimQueriesController < QueriesController
3     include NominatimMethods
4
5     def create
6       # ask nominatim
7       response = fetch_xml(nominatim_query_url(:format => "xml"))
8
9       # extract the results from the response
10       results = response.elements["searchresults"]
11
12       # create result array
13       @results = []
14
15       # create parameter hash for "more results" link
16       @more_params = params
17                      .permit(:query, :minlon, :minlat, :maxlon, :maxlat, :exclude)
18                      .merge(:exclude => results.attributes["exclude_place_ids"])
19
20       # parse the response
21       results.elements.each("place") do |place|
22         lat = place.attributes["lat"]
23         lon = place.attributes["lon"]
24         klass = place.attributes["class"]
25         type = place.attributes["type"]
26         name = place.attributes["display_name"]
27         min_lat, max_lat, min_lon, max_lon = place.attributes["boundingbox"].split(",")
28         prefix_name = if type.empty?
29                         ""
30                       else
31                         t "geocoder.search_osm_nominatim.prefix.#{klass}.#{type}", :default => type.tr("_", " ").capitalize
32                       end
33         if klass == "boundary" && type == "administrative"
34           rank = (place.attributes["address_rank"].to_i + 1) / 2
35           prefix_name = t "geocoder.search_osm_nominatim.admin_levels.level#{rank}", :default => prefix_name
36           border_type = nil
37           place_type = nil
38           place_tags = %w[linked_place place]
39           place.elements["extratags"].elements.each("tag") do |extratag|
40             border_type = t "geocoder.search_osm_nominatim.border_types.#{extratag.attributes['value']}", :default => border_type if extratag.attributes["key"] == "border_type"
41             place_type = t "geocoder.search_osm_nominatim.prefix.place.#{extratag.attributes['value']}", :default => place_type if place_tags.include?(extratag.attributes["key"])
42           end
43           prefix_name = place_type || border_type || prefix_name
44         end
45         prefix = t "geocoder.search_osm_nominatim.prefix_format", :name => prefix_name
46         object_type = place.attributes["osm_type"]
47         object_id = place.attributes["osm_id"]
48
49         @results.push(:lat => lat, :lon => lon,
50                       :min_lat => min_lat, :max_lat => max_lat,
51                       :min_lon => min_lon, :max_lon => max_lon,
52                       :prefix => prefix, :name => name,
53                       :type => object_type, :id => object_id)
54       end
55     rescue StandardError => e
56       host = URI(Settings.nominatim_url).host
57       @error = "Error contacting #{host}: #{e}"
58       render :action => "error"
59     end
60   end
61 end