]> git.openstreetmap.org Git - rails.git/blob - lib/nominatim.rb
5b1c7ac3203ca92ffdcda856ed3a47ba5a15cc01
[rails.git] / lib / nominatim.rb
1 module Nominatim
2   def self.describe_location(lat, lon, zoom = nil, language = nil)
3     zoom = zoom || 14
4     language = language || request.user_preferred_languages.join(',')
5
6     Rails.cache.fetch "/nominatim/location/#{lat}/#{lon}/#{zoom}/#{language}" do
7       url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
8
9       begin
10         response = OSM::Timer.timeout(4) do
11           REXML::Document.new(Net::HTTP.get(URI.parse(url)))
12         end
13       rescue Exception
14         response = nil
15       end
16
17       if response and result = response.get_text("reversegeocode/result")
18         result.to_s
19       else
20         "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
21       end
22     end
23   end
24 end