]> git.openstreetmap.org Git - rails.git/blobdiff - app/controllers/geocoder_controller.rb
Simplify step mapping function
[rails.git] / app / controllers / geocoder_controller.rb
index 43f276efa020a35cfdbb1719155c4a3f5e61c1fe..81c179a677576d345515171feefa3f6101e5ecb3 100644 (file)
@@ -6,17 +6,22 @@ class GeocoderController < ApplicationController
   before_action :authorize_web
   before_action :set_locale
   before_action :require_oauth, :only => [:search]
+
   authorize_resource :class => false
 
+  before_action :normalize_params, :only => [:search]
+
   def search
-    @params = normalize_params
     @sources = []
 
-    if @params[:lat] && @params[:lon]
-      @sources.push(:name => "latlon", :url => root_path)
-      @sources.push(:name => "osm_nominatim_reverse", :url => nominatim_reverse_url(:format => "html"))
-    elsif @params[:query]
-      @sources.push(:name => "osm_nominatim", :url => nominatim_url(:format => "html"))
+    if params[:lat] && params[:lon]
+      @sources.push(:name => "latlon", :url => root_path,
+                    :fetch_url => url_for(params.permit(:lat, :lon, :latlon_digits, :zoom).merge(:action => "search_latlon")))
+      @sources.push(:name => "osm_nominatim_reverse", :url => nominatim_reverse_url(:format => "html"),
+                    :fetch_url => url_for(params.permit(:lat, :lon, :zoom).merge(:action => "search_osm_nominatim_reverse")))
+    elsif params[:query]
+      @sources.push(:name => "osm_nominatim", :url => nominatim_url(:format => "html"),
+                    :fetch_url => url_for(params.permit(:query, :minlat, :minlon, :maxlat, :maxlon).merge(:action => "search_osm_nominatim")))
     end
 
     if @sources.empty?
@@ -34,13 +39,13 @@ class GeocoderController < ApplicationController
       # We've got two nondescript numbers for a query, which can mean both "lat, lon" or "lon, lat".
       @results = []
 
-      if lat >= -90 && lat <= 90 && lon >= -180 && lon <= 180
+      if lat.between?(-90, 90) && lon.between?(-180, 180)
         @results.push(:lat => params[:lat], :lon => params[:lon],
                       :zoom => params[:zoom],
                       :name => "#{params[:lat]}, #{params[:lon]}")
       end
 
-      if lon >= -90 && lon <= 90 && lat >= -180 && lat <= 180
+      if lon.between?(-90, 90) && lat.between?(-180, 180)
         @results.push(:lat => params[:lon], :lon => params[:lat],
                       :zoom => params[:zoom],
                       :name => "#{params[:lon]}, #{params[:lat]}")
@@ -54,10 +59,10 @@ class GeocoderController < ApplicationController
       end
     else
       # Coordinates in a query have come with markers for latitude and longitude.
-      if lat < -90 || lat > 90
+      if !lat.between?(-90, 90)
         @error = "Latitude #{lat} out of range"
         render :action => "error"
-      elsif lon < -180 || lon > 180
+      elsif !lon.between?(-180, 180)
         @error = "Longitude #{lon} out of range"
         render :action => "error"
       else
@@ -101,9 +106,14 @@ class GeocoderController < ApplicationController
       if klass == "boundary" && type == "administrative"
         rank = (place.attributes["address_rank"].to_i + 1) / 2
         prefix_name = t "geocoder.search_osm_nominatim.admin_levels.level#{rank}", :default => prefix_name
+        border_type = nil
+        place_type = nil
+        place_tags = %w[linked_place place]
         place.elements["extratags"].elements.each("tag") do |extratag|
-          prefix_name = t "geocoder.search_osm_nominatim.prefix.place.#{extratag.attributes['value']}", :default => prefix_name if extratag.attributes["key"] == "linked_place" || extratag.attributes["key"] == "place"
+          border_type = t "geocoder.search_osm_nominatim.border_types.#{extratag.attributes['value']}", :default => border_type if extratag.attributes["key"] == "border_type"
+          place_type = t "geocoder.search_osm_nominatim.prefix.place.#{extratag.attributes['value']}", :default => place_type if place_tags.include?(extratag.attributes["key"])
         end
+        prefix_name = place_type || border_type || prefix_name
       end
       prefix = t ".prefix_format", :name => prefix_name
       object_type = place.attributes["osm_type"]
@@ -216,8 +226,6 @@ class GeocoderController < ApplicationController
         params[:latlon_digits] = true
       end
     end
-
-    params.permit(:query, :lat, :lon, :latlon_digits, :zoom, :minlat, :minlon, :maxlat, :maxlon)
   end
 
   def dms_regexp(name_prefix)