From d4550c6873192d7a7c300808749ecc6562f87b8f Mon Sep 17 00:00:00 2001 From: Chris Myers <chris@cbmyers.com> Date: Mon, 3 Oct 2011 13:49:53 -0400 Subject: [PATCH] added nominatim --- lib/geocoder/lookups/nominatim.rb | 32 ++++ lib/geocoder/results/nominatim.rb | 68 ++++++++ .../nominatim_madison_square_garden.json | 150 ++++++++++++++++++ test/fixtures/nominatim_no_results.json | 1 + 4 files changed, 251 insertions(+) create mode 100644 lib/geocoder/lookups/nominatim.rb create mode 100644 lib/geocoder/results/nominatim.rb create mode 100644 test/fixtures/nominatim_madison_square_garden.json create mode 100644 test/fixtures/nominatim_no_results.json diff --git a/lib/geocoder/lookups/nominatim.rb b/lib/geocoder/lookups/nominatim.rb new file mode 100644 index 00000000..fc01ad62 --- /dev/null +++ b/lib/geocoder/lookups/nominatim.rb @@ -0,0 +1,32 @@ +require 'geocoder/lookups/base' +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.any? + return doc[0]['place_id'] != "" ? doc : [] + else + warn "Nominatim Geocoding Adress not founr or API error." + return [] + end + end + + def query_url(query, reverse = false) + params = { + :q => query, + :format => "json", + :polygon => "1", + :addressdetails => "1", +# :locale => "#{Geocoder::Configuration.language}_US", + } + "http://nominatim.openstreetmap.org/search?" + hash_to_query(params) + end + end +end \ No newline at end of file diff --git a/lib/geocoder/results/nominatim.rb b/lib/geocoder/results/nominatim.rb new file mode 100644 index 00000000..b5d0f9bf --- /dev/null +++ b/lib/geocoder/results/nominatim.rb @@ -0,0 +1,68 @@ +require 'geocoder/results/base' + +module Geocoder::Result + class Nominatim < Base + + def house_number + @data['address']['house_number'] + end + + def address + @data['display_name'] + end + + def street + @data['address']['road'] + end + + def city + @data['address']['city'] + end + + def village + @data['address']['villiage'] + end + + def town + @data['address']['town'] + end + + def state + @data['address']['state'] + end + + def postal_code + @data['address']['postcode'] + end + + alias_method :state, :state_code + + def county + @data['address']['county'] + end + + def country + @data['address']['country'] + end + + def country_code + @data['address']['country_code'] + end + + def coordinates + [@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 + + response_attributes.each do |a| + define_method a do + @data[a] + end + end + + end +end \ No newline at end of file diff --git a/test/fixtures/nominatim_madison_square_garden.json b/test/fixtures/nominatim_madison_square_garden.json new file mode 100644 index 00000000..a1d709ec --- /dev/null +++ b/test/fixtures/nominatim_madison_square_garden.json @@ -0,0 +1,150 @@ +[ + + { + "place_id": "30632629", + "licence": "Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.", + "osm_type": "way", + "osm_id": "24801588", + "boundingbox": [ + "40.749828338623", + "40.7511596679688", + "-73.9943389892578", + "-73.9926528930664" + ], + "polygonpoints": [ + [ + "-73.9943346", + "40.7503638" + ], + [ + "-73.9942745", + "40.7504158" + ], + [ + "-73.9942593", + "40.750629" + ], + [ + "-73.9941343", + "40.7508432" + ], + [ + "-73.9939794", + "40.7509703" + ], + [ + "-73.9938042", + "40.7510532" + ], + [ + "-73.9938025", + "40.7511311" + ], + [ + "-73.9936051", + "40.7511571" + ], + [ + "-73.9935673", + "40.751105" + ], + [ + "-73.9934095", + "40.7511089" + ], + [ + "-73.9931235", + "40.7510548" + ], + [ + "-73.9928863", + "40.7509311" + ], + [ + "-73.9928068", + "40.750949" + ], + [ + "-73.992721", + "40.7508515" + ], + [ + "-73.9927444", + "40.7507889" + ], + [ + "-73.9926693", + "40.7506457" + ], + [ + "-73.9926597", + "40.7503657" + ], + [ + "-73.9928305", + "40.7500953" + ], + [ + "-73.9929757", + "40.7499911" + ], + [ + "-73.9931281", + "40.7499238" + ], + [ + "-73.993133", + "40.7498631" + ], + [ + "-73.9932961", + "40.7498306" + ], + [ + "-73.9933664", + "40.7498742" + ], + [ + "-73.993471", + "40.7498701" + ], + [ + "-73.9938023", + "40.7499263" + ], + [ + "-73.9940703", + "40.7500756" + ], + [ + "-73.9941876", + "40.7502038" + ], + [ + "-73.9942831", + "40.7502142" + ], + [ + "-73.9943346", + "40.7503638" + ] + ], + "lat": "40.7504928941818", + "lon": "-73.993466492276", + "display_name": "Madison Square Garden, West 31st Street, Long Island City, New York City, New York, 10001, United States of America", + "class": "leisure", + "type": "stadium", + "address": { + "stadium": "Madison Square Garden", + "road": "West 31st Street", + "suburb": "Long Island City", + "city": "New York City", + "county": "New York", + "state": "New York", + "postcode": "10001", + "country": "United States of America", + "country_code": "us" + } + } + +] \ No newline at end of file diff --git a/test/fixtures/nominatim_no_results.json b/test/fixtures/nominatim_no_results.json new file mode 100644 index 00000000..1e3ec721 --- /dev/null +++ b/test/fixtures/nominatim_no_results.json @@ -0,0 +1 @@ +[ ] -- GitLab