]> git.openstreetmap.org Git - rails.git/blob - lib/nominatim.rb
Merge remote-tracking branch 'upstream/pull/4964'
[rails.git] / lib / nominatim.rb
1 module Nominatim
2   require "timeout"
3
4   extend ActionView::Helpers::NumberHelper
5
6   def self.describe_location(lat, lon, zoom = nil, language = nil)
7     zoom ||= 14
8     language ||= http_accept_language.user_preferred_languages.join(",")
9
10     Rails.cache.fetch "/nominatim/location/#{lat}/#{lon}/#{zoom}/#{language}" do
11       url = "#{Settings.nominatim_url}reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
12
13       begin
14         response = OSM.http_client.get(URI.parse(url)) do |request|
15           request.options.timeout = 4
16         end
17
18         results = REXML::Document.new(response.body) if response.success?
19       rescue StandardError
20         results = nil
21       end
22
23       if results && result = results.get_text("reversegeocode/result")
24         result.value
25       else
26         "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
27       end
28     end
29   end
30 end