]> git.openstreetmap.org Git - rails.git/blob - app/controllers/searches_controller.rb
Merge pull request #6394 from openstreetmap/dependabot/github_actions/ruby/setup...
[rails.git] / app / controllers / searches_controller.rb
1 # frozen_string_literal: true
2
3 class SearchesController < ApplicationController
4   include NominatimMethods
5
6   before_action :authorize_web
7   before_action :set_locale
8   before_action :require_oauth
9
10   authorize_resource :class => false
11
12   before_action :normalize_params
13
14   def show
15     @sources = []
16
17     if params[:lat] && params[:lon]
18       @sources.push(:name => "latlon", :url => root_path,
19                     :fetch_url => search_latlon_query_path(params.permit(:lat, :lon, :latlon_digits, :zoom)))
20       @sources.push(:name => "nominatim_reverse", :url => nominatim_reverse_query_url(:format => "html"),
21                     :fetch_url => search_nominatim_reverse_query_path(params.permit(:lat, :lon, :zoom)))
22     elsif params[:query]
23       @sources.push(:name => "nominatim", :url => nominatim_query_url(:format => "html"),
24                     :fetch_url => search_nominatim_query_path(params.permit(:query, :minlat, :minlon, :maxlat, :maxlon)))
25     end
26
27     if @sources.empty?
28       head :bad_request
29     else
30       render :layout => map_layout
31     end
32   end
33
34   private
35
36   def normalize_params
37     if (query = params[:query])
38       query.strip!
39
40       if (latlon = query.match(/^(?<ns>[NS])\s*#{dms_regexp('ns')}\W*(?<ew>[EW])\s*#{dms_regexp('ew')}$/) ||
41                    query.match(/^#{dms_regexp('ns')}\s*(?<ns>[NS])\W*#{dms_regexp('ew')}\s*(?<ew>[EW])$/))
42         params.merge!(to_decdeg(latlon.named_captures.compact)).delete(:query)
43
44       elsif (latlon = query.match(%r{^(?<lat>[+-]?\d+(?:\.\d+)?)(?:\s+|\s*[,/]\s*)(?<lon>[+-]?\d+(?:\.\d+)?)$}))
45         params.merge!(latlon.named_captures).delete(:query)
46
47         params[:latlon_digits] = true
48       end
49     end
50   end
51
52   def dms_regexp(name_prefix)
53     /
54       (?: (?<#{name_prefix}d>\d{1,3}(?:\.\d+)?)°? ) |
55       (?: (?<#{name_prefix}d>\d{1,3})°?\s*(?<#{name_prefix}m>\d{1,2}(?:\.\d+)?)['′]? ) |
56       (?: (?<#{name_prefix}d>\d{1,3})°?\s*(?<#{name_prefix}m>\d{1,2})['′]?\s*(?<#{name_prefix}s>\d{1,2}(?:\.\d+)?)["″]? )
57     /x
58   end
59
60   def to_decdeg(captures)
61     ns = captures.fetch("ns").casecmp?("s") ? -1 : 1
62     nsd = BigDecimal(captures.fetch("nsd", "0"))
63     nsm = BigDecimal(captures.fetch("nsm", "0"))
64     nss = BigDecimal(captures.fetch("nss", "0"))
65
66     ew = captures.fetch("ew").casecmp?("w") ? -1 : 1
67     ewd = BigDecimal(captures.fetch("ewd", "0"))
68     ewm = BigDecimal(captures.fetch("ewm", "0"))
69     ews = BigDecimal(captures.fetch("ews", "0"))
70
71     lat = ns * (nsd + (nsm / 60) + (nss / 3600))
72     lon = ew * (ewd + (ewm / 60) + (ews / 3600))
73
74     { :lat => lat.round(6).to_s("F"), :lon => lon.round(6).to_s("F") }
75   end
76 end