1 class GeocoderController < ApplicationController
3 require "rexml/document"
4 include NominatimMethods
6 before_action :authorize_web
7 before_action :set_locale
8 before_action :require_oauth, :only => [:search]
10 authorize_resource :class => false
12 before_action :normalize_params, :only => [:search]
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")))
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)))
30 render :layout => map_layout
34 def search_osm_nominatim_reverse
35 # get query parameters
42 response = fetch_xml(nominatim_reverse_query_url(:format => "xml"))
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
52 @results.push(:lat => lat, :lon => lon,
55 :type => object_type, :id => object_id)
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"
68 response = OSM.http_client.get(URI.parse(url))
73 raise response.status.to_s
78 REXML::Document.new(fetch_text(url))
82 if (query = params[:query])
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)
89 elsif (latlon = query.match(%r{^(?<lat>[+-]?\d+(?:\.\d+)?)(?:\s+|\s*[,/]\s*)(?<lon>[+-]?\d+(?:\.\d+)?)$}))
90 params.merge!(latlon.named_captures).delete(:query)
92 params[:latlon_digits] = true
97 def dms_regexp(name_prefix)
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+)?)["″]? )
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"))
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"))
116 lat = ns * (nsd + (nsm / 60) + (nss / 3600))
117 lon = ew * (ewd + (ewm / 60) + (ews / 3600))
119 { :lat => lat.round(6).to_s("F"), :lon => lon.round(6).to_s("F") }