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 => search_nominatim_reverse_query_path(params.permit(:lat, :lon, :zoom)))
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
37 response = OSM.http_client.get(URI.parse(url))
42 raise response.status.to_s
47 REXML::Document.new(fetch_text(url))
51 if (query = params[:query])
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)
58 elsif (latlon = query.match(%r{^(?<lat>[+-]?\d+(?:\.\d+)?)(?:\s+|\s*[,/]\s*)(?<lon>[+-]?\d+(?:\.\d+)?)$}))
59 params.merge!(latlon.named_captures).delete(:query)
61 params[:latlon_digits] = true
66 def dms_regexp(name_prefix)
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+)?)["″]? )
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"))
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"))
85 lat = ns * (nsd + (nsm / 60) + (nss / 3600))
86 lon = ew * (ewd + (ewm / 60) + (ews / 3600))
88 { :lat => lat.round(6).to_s("F"), :lon => lon.round(6).to_s("F") }