2   class NominatimQueriesController < QueriesController
 
   3     include NominatimMethods
 
   7       response = fetch_xml(nominatim_query_url(:format => "xml"))
 
   9       # extract the results from the response
 
  10       results = response.elements["searchresults"]
 
  15       # create parameter hash for "more results" link
 
  17                      .permit(:query, :minlon, :minlat, :maxlon, :maxlat, :exclude)
 
  18                      .merge(:exclude => results.attributes["exclude_place_ids"])
 
  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?
 
  31                         t "geocoder.search_osm_nominatim.prefix.#{klass}.#{type}", :default => type.tr("_", " ").capitalize
 
  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
 
  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"])
 
  43           prefix_name = place_type || border_type || prefix_name
 
  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"]
 
  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)
 
  55     rescue StandardError => e
 
  56       host = URI(Settings.nominatim_url).host
 
  57       @error = "Error contacting #{host}: #{e}"
 
  58       render :action => "error"