+ if klass == 'boundary' && type == 'administrative'
+ rank = (place.attributes["place_rank"].to_i + 1) / 2
+ prefix_name = t "geocoder.search_osm_nominatim.admin_levels.level#{rank}", :default => prefix_name
+ end
+ prefix = t "geocoder.search_osm_nominatim.prefix_format", :name => prefix_name
+ object_type = place.attributes["osm_type"]
+ object_id = place.attributes["osm_id"]
+
+ @results.push(:lat => lat, :lon => lon,
+ :min_lat => min_lat, :max_lat => max_lat,
+ :min_lon => min_lon, :max_lon => max_lon,
+ :prefix => prefix, :name => name,
+ :type => object_type, :id => object_id)
+ end
+
+ render :action => "results"
+ # rescue StandardError => ex
+ # @error = "Error contacting nominatim.openstreetmap.org: #{ex.to_s}"
+ # render :action => "error"
+ end
+
+ def search_geonames
+ # get query parameters
+ query = params[:query]
+
+ # get preferred language
+ lang = I18n.locale.to_s.split("-").first
+
+ # 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 ws.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 ws.geonames.org: #{ex}"
+ render :action => "error"
+ end
+
+ private
+
+ def fetch_text(url)
+ Net::HTTP.get(URI.parse(url))
+ end
+
+ def fetch_xml(url)
+ REXML::Document.new(fetch_text(url))
+ end
+
+ def format_distance(distance)
+ t("geocoder.distance", :count => distance)
+ end
+
+ def format_direction(bearing)
+ return t("geocoder.direction.south_west") if bearing >= 22.5 && bearing < 67.5
+ return t("geocoder.direction.south") if bearing >= 67.5 && bearing < 112.5
+ return t("geocoder.direction.south_east") if bearing >= 112.5 && bearing < 157.5
+ return t("geocoder.direction.east") if bearing >= 157.5 && bearing < 202.5
+ return t("geocoder.direction.north_east") if bearing >= 202.5 && bearing < 247.5
+ return t("geocoder.direction.north") if bearing >= 247.5 && bearing < 292.5
+ return t("geocoder.direction.north_west") if bearing >= 292.5 && bearing < 337.5
+ t("geocoder.direction.west")
+ end
+
+ def format_name(name)
+ name.gsub(/( *\[[^\]]*\])*$/, "")
+ end
+
+ def count_results(results)
+ count = 0
+
+ results.each do |source|
+ count += source[:results].length if source[:results]
+ end
+
+ count
+ 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)