]> git.openstreetmap.org Git - rails.git/blob - app/controllers/geocoder_controller.rb
Create nominatim reverse 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 => search_nominatim_reverse_query_path(params.permit(:lat, :lon, :zoom)))
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   private
35
36   def fetch_text(url)
37     response = OSM.http_client.get(URI.parse(url))
38
39     if response.success?
40       response.body
41     else
42       raise response.status.to_s
43     end
44   end
45
46   def fetch_xml(url)
47     REXML::Document.new(fetch_text(url))
48   end
49
50   def normalize_params
51     if (query = params[:query])
52       query.strip!
53
54       if (latlon = query.match(/^(?<ns>[NS])\s*#{dms_regexp('ns')}\W*(?<ew>[EW])\s*#{dms_regexp('ew')}$/) ||
55                    query.match(/^#{dms_regexp('ns')}\s*(?<ns>[NS])\W*#{dms_regexp('ew')}\s*(?<ew>[EW])$/))
56         params.merge!(to_decdeg(latlon.named_captures.compact)).delete(:query)
57
58       elsif (latlon = query.match(%r{^(?<lat>[+-]?\d+(?:\.\d+)?)(?:\s+|\s*[,/]\s*)(?<lon>[+-]?\d+(?:\.\d+)?)$}))
59         params.merge!(latlon.named_captures).delete(:query)
60
61         params[:latlon_digits] = true
62       end
63     end
64   end
65
66   def dms_regexp(name_prefix)
67     /
68       (?: (?<#{name_prefix}d>\d{1,3}(?:\.\d+)?)°? ) |
69       (?: (?<#{name_prefix}d>\d{1,3})°?\s*(?<#{name_prefix}m>\d{1,2}(?:\.\d+)?)['′]? ) |
70       (?: (?<#{name_prefix}d>\d{1,3})°?\s*(?<#{name_prefix}m>\d{1,2})['′]?\s*(?<#{name_prefix}s>\d{1,2}(?:\.\d+)?)["″]? )
71     /x
72   end
73
74   def to_decdeg(captures)
75     ns = captures.fetch("ns").casecmp?("s") ? -1 : 1
76     nsd = BigDecimal(captures.fetch("nsd", "0"))
77     nsm = BigDecimal(captures.fetch("nsm", "0"))
78     nss = BigDecimal(captures.fetch("nss", "0"))
79
80     ew = captures.fetch("ew").casecmp?("w") ? -1 : 1
81     ewd = BigDecimal(captures.fetch("ewd", "0"))
82     ewm = BigDecimal(captures.fetch("ewm", "0"))
83     ews = BigDecimal(captures.fetch("ews", "0"))
84
85     lat = ns * (nsd + (nsm / 60) + (nss / 3600))
86     lon = ew * (ewd + (ewm / 60) + (ews / 3600))
87
88     { :lat => lat.round(6).to_s("F"), :lon => lon.round(6).to_s("F") }
89   end
90 end