]> git.openstreetmap.org Git - rails.git/blob - app/controllers/geocoder_controller.rb
Create nominatim search query resource
[rails.git] / app / controllers / geocoder_controller.rb
1 class GeocoderController < ApplicationController
2   require "uri"
3   require "rexml/document"
4   include NominatimMethods
5
6   before_action :authorize_web
7   before_action :set_locale
8   before_action :require_oauth, :only => [:search]
9
10   authorize_resource :class => false
11
12   before_action :normalize_params, :only => [:search]
13
14   def search
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 => "osm_nominatim_reverse", :url => nominatim_reverse_query_url(:format => "html"),
21                     :fetch_url => url_for(params.permit(:lat, :lon, :zoom).merge(:action => "search_osm_nominatim_reverse")))
22     elsif params[:query]
23       @sources.push(:name => "osm_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   def search_osm_nominatim_reverse
35     # get query parameters
36     zoom = params[:zoom]
37
38     # create result array
39     @results = []
40
41     # ask nominatim
42     response = fetch_xml(nominatim_reverse_query_url(:format => "xml"))
43
44     # parse the response
45     response.elements.each("reversegeocode/result") do |result|
46       lat = result.attributes["lat"]
47       lon = result.attributes["lon"]
48       object_type = result.attributes["osm_type"]
49       object_id = result.attributes["osm_id"]
50       description = result.text
51
52       @results.push(:lat => lat, :lon => lon,
53                     :zoom => zoom,
54                     :name => description,
55                     :type => object_type, :id => object_id)
56     end
57
58     render "searches/queries/create"
59   rescue StandardError => e
60     host = URI(Settings.nominatim_url).host
61     @error = "Error contacting #{host}: #{e}"
62     render "searches/queries/error"
63   end
64
65   private
66
67   def fetch_text(url)
68     response = OSM.http_client.get(URI.parse(url))
69
70     if response.success?
71       response.body
72     else
73       raise response.status.to_s
74     end
75   end
76
77   def fetch_xml(url)
78     REXML::Document.new(fetch_text(url))
79   end
80
81   def normalize_params
82     if (query = params[:query])
83       query.strip!
84
85       if (latlon = query.match(/^(?<ns>[NS])\s*#{dms_regexp('ns')}\W*(?<ew>[EW])\s*#{dms_regexp('ew')}$/) ||
86                    query.match(/^#{dms_regexp('ns')}\s*(?<ns>[NS])\W*#{dms_regexp('ew')}\s*(?<ew>[EW])$/))
87         params.merge!(to_decdeg(latlon.named_captures.compact)).delete(:query)
88
89       elsif (latlon = query.match(%r{^(?<lat>[+-]?\d+(?:\.\d+)?)(?:\s+|\s*[,/]\s*)(?<lon>[+-]?\d+(?:\.\d+)?)$}))
90         params.merge!(latlon.named_captures).delete(:query)
91
92         params[:latlon_digits] = true
93       end
94     end
95   end
96
97   def dms_regexp(name_prefix)
98     /
99       (?: (?<#{name_prefix}d>\d{1,3}(?:\.\d+)?)°? ) |
100       (?: (?<#{name_prefix}d>\d{1,3})°?\s*(?<#{name_prefix}m>\d{1,2}(?:\.\d+)?)['′]? ) |
101       (?: (?<#{name_prefix}d>\d{1,3})°?\s*(?<#{name_prefix}m>\d{1,2})['′]?\s*(?<#{name_prefix}s>\d{1,2}(?:\.\d+)?)["″]? )
102     /x
103   end
104
105   def to_decdeg(captures)
106     ns = captures.fetch("ns").casecmp?("s") ? -1 : 1
107     nsd = BigDecimal(captures.fetch("nsd", "0"))
108     nsm = BigDecimal(captures.fetch("nsm", "0"))
109     nss = BigDecimal(captures.fetch("nss", "0"))
110
111     ew = captures.fetch("ew").casecmp?("w") ? -1 : 1
112     ewd = BigDecimal(captures.fetch("ewd", "0"))
113     ewm = BigDecimal(captures.fetch("ewm", "0"))
114     ews = BigDecimal(captures.fetch("ews", "0"))
115
116     lat = ns * (nsd + (nsm / 60) + (nss / 3600))
117     lon = ew * (ewd + (ewm / 60) + (ews / 3600))
118
119     { :lat => lat.round(6).to_s("F"), :lon => lon.round(6).to_s("F") }
120   end
121 end