+ # create result array
+ @results = []
+
+ # ask geonames.org
+ response = fetch_xml("http://api.geonames.org/search?q=#{escape_query(query)}&lang=#{lang}&maxRows=20&username=#{GEONAMES_USERNAME}")
+
+ # parse the response
+ response.elements.each("geonames/geoname") do |geoname|
+ lat = geoname.get_text("lat").to_s
+ lon = geoname.get_text("lng").to_s
+ name = geoname.get_text("name").to_s
+ country = geoname.get_text("countryName").to_s
+ @results.push(:lat => lat, :lon => lon,
+ :zoom => GEONAMES_ZOOM,
+ :name => name,
+ :suffix => ", #{country}")
+ end
+
+ render :action => "results"
+ rescue StandardError => ex
+ @error = "Error contacting api.geonames.org: #{ex}"
+ render :action => "error"
+ end
+
+ def search_osm_nominatim_reverse
+ # get query parameters
+ lat = params[:lat]
+ lon = params[:lon]
+ zoom = params[:zoom]
+
+ # create result array
+ @results = []
+
+ # ask nominatim
+ response = fetch_xml("http:#{NOMINATIM_URL}reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}")
+
+ # parse the response
+ response.elements.each("reversegeocode/result") do |result|
+ lat = result.attributes["lat"].to_s
+ lon = result.attributes["lon"].to_s
+ object_type = result.attributes["osm_type"]
+ object_id = result.attributes["osm_id"]
+ description = result.get_text.to_s
+
+ @results.push(:lat => lat, :lon => lon,
+ :zoom => zoom,
+ :name => description,
+ :type => object_type, :id => object_id)
+ end
+
+ render :action => "results"
+ rescue StandardError => ex
+ @error = "Error contacting nominatim.openstreetmap.org: #{ex}"
+ render :action => "error"
+ end
+
+ def search_geonames_reverse
+ # get query parameters
+ lat = params[:lat]
+ lon = params[:lon]
+
+ # get preferred language
+ lang = I18n.locale.to_s.split("-").first
+
+ # create result array
+ @results = []
+
+ # ask geonames.org
+ response = fetch_xml("http://api.geonames.org/countrySubdivision?lat=#{lat}&lng=#{lon}&lang=#{lang}&username=#{GEONAMES_USERNAME}")
+
+ # parse the response
+ response.elements.each("geonames/countrySubdivision") do |geoname|
+ name = geoname.get_text("adminName1").to_s
+ country = geoname.get_text("countryName").to_s
+ @results.push(:lat => lat, :lon => lon,
+ :zoom => GEONAMES_ZOOM,
+ :name => name,
+ :suffix => ", #{country}")
+ end
+
+ render :action => "results"
+ rescue StandardError => ex
+ @error = "Error contacting api.geonames.org: #{ex}"
+ render :action => "error"
+ end
+
+ private
+
+ def fetch_text(url)
+ response = OSM.http_client.get(URI.parse(url))
+
+ if response.success?
+ response.body
+ else
+ fail response.status.to_s
+ end
+ end
+
+ def fetch_xml(url)
+ REXML::Document.new(fetch_text(url))
+ end
+
+ def escape_query(query)
+ URI.escape(query, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]", false, "N"))
+ end
+
+ def normalize_params
+ query = params[:query]
+ return unless query
+
+ query.strip!
+
+ if latlon = query.match(/^([NS])\s*(\d{1,3}(\.\d*)?)\W*([EW])\s*(\d{1,3}(\.\d*)?)$/).try(:captures) # [NSEW] decimal degrees
+ params.merge!(nsew_to_decdeg(latlon)).delete(:query)
+ elsif latlon = query.match(/^(\d{1,3}(\.\d*)?)\s*([NS])\W*(\d{1,3}(\.\d*)?)\s*([EW])$/).try(:captures) # decimal degrees [NSEW]
+ params.merge!(nsew_to_decdeg(latlon)).delete(:query)
+
+ elsif latlon = query.match(/^([NS])\s*(\d{1,3})°?\s*(\d{1,3}(\.\d*)?)?['′]?\W*([EW])\s*(\d{1,3})°?\s*(\d{1,3}(\.\d*)?)?['′]?$/).try(:captures) # [NSEW] degrees, decimal minutes
+ params.merge!(ddm_to_decdeg(latlon)).delete(:query)
+ elsif latlon = query.match(/^(\d{1,3})°?\s*(\d{1,3}(\.\d*)?)?['′]?\s*([NS])\W*(\d{1,3})°?\s*(\d{1,3}(\.\d*)?)?['′]?\s*([EW])$/).try(:captures) # degrees, decimal minutes [NSEW]
+ params.merge!(ddm_to_decdeg(latlon)).delete(:query)
+
+ elsif latlon = query.match(/^([NS])\s*(\d{1,3})°?\s*(\d{1,2})['′]?\s*(\d{1,3}(\.\d*)?)?["″]?\W*([EW])\s*(\d{1,3})°?\s*(\d{1,2})['′]?\s*(\d{1,3}(\.\d*)?)?["″]?$/).try(:captures) # [NSEW] degrees, minutes, decimal seconds
+ params.merge!(dms_to_decdeg(latlon)).delete(:query)
+ elsif latlon = query.match(/^(\d{1,3})°?\s*(\d{1,2})['′]?\s*(\d{1,3}(\.\d*)?)?["″]\s*([NS])\W*(\d{1,3})°?\s*(\d{1,2})['′]?\s*(\d{1,3}(\.\d*)?)?["″]?\s*([EW])$/).try(:captures) # degrees, minutes, decimal seconds [NSEW]
+ params.merge!(dms_to_decdeg(latlon)).delete(:query)
+
+ elsif latlon = query.match(/^\s*([+-]?\d+(\.\d*)?)\s*[\s,]\s*([+-]?\d+(\.\d*)?)\s*$/)
+ params.merge!(:lat => latlon[1].to_f, :lon => latlon[3].to_f).delete(:query)
+ end
+ end
+
+ def nsew_to_decdeg(captures)
+ begin
+ Float(captures[0])
+ captures[2].downcase != "s" ? lat = captures[0].to_f : lat = -(captures[0].to_f)
+ captures[5].downcase != "w" ? lon = captures[3].to_f : lon = -(captures[3].to_f)
+ rescue
+ captures[0].downcase != "s" ? lat = captures[1].to_f : lat = -(captures[1].to_f)
+ captures[3].downcase != "w" ? lon = captures[4].to_f : lon = -(captures[4].to_f)
+ end
+ { :lat => lat, :lon => lon }
+ end
+
+ def ddm_to_decdeg(captures)
+ begin
+ Float(captures[0])
+ captures[3].downcase != "s" ? lat = captures[0].to_f + captures[1].to_f / 60 : lat = -(captures[0].to_f + captures[1].to_f / 60)
+ captures[7].downcase != "w" ? lon = captures[4].to_f + captures[5].to_f / 60 : lon = -(captures[4].to_f + captures[5].to_f / 60)
+ rescue
+ captures[0].downcase != "s" ? lat = captures[1].to_f + captures[2].to_f / 60 : lat = -(captures[1].to_f + captures[2].to_f / 60)
+ captures[4].downcase != "w" ? lon = captures[5].to_f + captures[6].to_f / 60 : lon = -(captures[5].to_f + captures[6].to_f / 60)
+ end
+ { :lat => lat, :lon => lon }
+ end
+
+ def dms_to_decdeg(captures)
+ begin
+ Float(captures[0])
+ captures[4].downcase != "s" ? lat = captures[0].to_f + (captures[1].to_f + captures[2].to_f / 60) / 60 : lat = -(captures[0].to_f + (captures[1].to_f + captures[2].to_f / 60) / 60)
+ captures[9].downcase != "w" ? lon = captures[5].to_f + (captures[6].to_f + captures[7].to_f / 60) / 60 : lon = -(captures[5].to_f + (captures[6].to_f + captures[7].to_f / 60) / 60)
+ rescue
+ captures[0].downcase != "s" ? lat = captures[1].to_f + (captures[2].to_f + captures[3].to_f / 60) / 60 : lat = -(captures[1].to_f + (captures[2].to_f + captures[3].to_f / 60) / 60)
+ captures[5].downcase != "w" ? lon = captures[6].to_f + (captures[7].to_f + captures[8].to_f / 60) / 60 : lon = -(captures[6].to_f + (captures[7].to_f + captures[8].to_f / 60) / 60)
+ end
+ { :lat => lat, :lon => lon }