diff --git a/lib/geocoder/lookups/nominatim.rb b/lib/geocoder/lookups/nominatim.rb index 7e4f85de895795e8db9b98e235a0c501ba7739ba..f0a0635012bacb1f36dc9eeffb854c5ed2967127 100644 --- a/lib/geocoder/lookups/nominatim.rb +++ b/lib/geocoder/lookups/nominatim.rb @@ -3,43 +3,42 @@ require "geocoder/results/nominatim" module Geocoder::Lookup class Nominatim < Base - + def map_link_url(coordinates) "http://nominatim.openstreetmap.org/reverse?format=html&lat=#{coordinates[0]}&lon=#{coordinates[1]}&zoom=18&addressdetails=1" end def results(query, reverse = false) return [] unless doc = fetch_data(query, reverse) - if doc.kind_of?(Array) - if doc.any? - return doc[0]['place_id'] != "" ? doc : [] - else - warn "Address not found or Nominatim Geocoding API error." - return [] - end - else - return doc['place_id'] != "" ? doc = [] << doc : [] - end + if doc.kind_of?(Array) + if doc.any? + return doc[0]['place_id'] != "" ? doc : [] + else + warn "Address not found or Nominatim Geocoding API error." + return [] + end + else + return doc['place_id'] != "" ? doc = [] << doc : [] + end end def query_url(query, reverse = false) params = { :format => "json", - :polygon => "1", + :polygon => "1", :addressdetails => "1", -# :locale => "#{Geocoder::Configuration.language}_US", + #:locale => "#{Geocoder::Configuration.language}_US", } - if (reverse) - method = 'reverse' - parts = query.split(/\s*,\s*/); - params[:lat] = parts[0] - params[:lon] = parts[1] - else - method = 'search' - params[:q] = query - end + if (reverse) + method = 'reverse' + parts = query.split(/\s*,\s*/); + params[:lat] = parts[0] + params[:lon] = parts[1] + else + method = 'search' + params[:q] = query + end "http://nominatim.openstreetmap.org/#{method}?" + hash_to_query(params) - end - + end end -end \ No newline at end of file +end diff --git a/lib/geocoder/results/nominatim.rb b/lib/geocoder/results/nominatim.rb index f81071e762cc81b6685b3da545ba02cd819fbcc0..cc9dce3ab56d8bc53ea0d97058996febb3fbfc74 100644 --- a/lib/geocoder/results/nominatim.rb +++ b/lib/geocoder/results/nominatim.rb @@ -2,30 +2,30 @@ require 'geocoder/results/base' module Geocoder::Result class Nominatim < Base - + def house_number - @data['address']['house_number'] - end - - def address - @data['display_name'] - end - + @data['address']['house_number'] + end + + def address + @data['display_name'] + end + def street - @data['address']['road'] - end - - def city - @data['address']['city'] + @data['address']['road'] + end + + def city + @data['address']['city'] + end + + def village + @data['address']['villiage'] + end + + def town + @data['address']['town'] end - - def village - @data['address']['villiage'] - end - - def town - @data['address']['town'] - end def state @data['address']['state'] @@ -33,13 +33,13 @@ module Geocoder::Result alias_method :state_code, :state - def postal_code - @data['address']['postcode'] - end - - def county - @data['address']['county'] - end + def postal_code + @data['address']['postcode'] + end + + def county + @data['address']['county'] + end def country @data['address']['country'] @@ -48,21 +48,20 @@ module Geocoder::Result def country_code @data['address']['country_code'] end - + def coordinates - [@data['lat'].to_f, @data['lon'].to_f] + [@data['lat'].to_f, @data['lon'].to_f] + end + + def self.response_attributes + %w[place_id, osm_type, osm_id, boundingbox, license, + polygonpoints, display_name, class, type, stadium, suburb] end - - def self.response_attributes - %w[place_id, osm_type, osm_id, boundingbox, license, - polygonpoints, display_name, class, type, stadium, suburb] - end response_attributes.each do |a| define_method a do @data[a] end - end - - end -end \ No newline at end of file + end + end +end diff --git a/test/services_test.rb b/test/services_test.rb index 479ce2e79ec7e0cf2020da392ec9e817fca8fb82..83fe571a0eaaa21a066339da4857ddf9cfedc32b 100644 --- a/test/services_test.rb +++ b/test/services_test.rb @@ -110,9 +110,9 @@ class ServicesTest < Test::Unit::TestCase results = Geocoder.search("no results") assert_equal 0, results.length end - + # --- Nominatim --- - + def test_nominatim_result_components Geocoder::Configuration.lookup = :nominatim result = Geocoder.search("Madison Square Garden, New York, NY").first @@ -125,7 +125,4 @@ class ServicesTest < Test::Unit::TestCase assert_equal "Madison Square Garden, West 31st Street, Long Island City, New York City, New York, 10001, United States of America", result.address end - - - end diff --git a/test/test_helper.rb b/test/test_helper.rb index 94d3ebf072e31f6dad5da7ba9ec4b40b927e5aaf..a6fe5ceabc11e5a1fe7fd188e01aae10e4265b47 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -150,7 +150,7 @@ module Geocoder end end - class Nominatim < Base + class Nominatim < Base private #----------------------------------------------------------------- def fetch_raw_data(query, reverse = false) raise TimeoutError if query == "timeout"