From 319a0e58a96c8d63e871f92d30517dbcf835c8be Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Sun, 17 Jun 2018 17:47:12 -0600 Subject: [PATCH] De-Googleize. Make Nominatim the default lookup and map provider. Closes #1026. --- README.md | 18 +++++++++--------- .../geocoder/config/templates/initializer.rb | 2 +- lib/geocoder/cli.rb | 4 ++-- lib/geocoder/configuration.rb | 2 +- test/unit/geocoder_test.rb | 11 +++++------ test/unit/lookups/google_test.rb | 4 ++++ test/unit/method_aliases_test.rb | 8 ++------ test/unit/model_test.rb | 5 ++--- test/unit/mongoid_test.rb | 6 +++--- 9 files changed, 29 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 993030d1..4f40f478 100644 --- a/README.md +++ b/README.md @@ -189,7 +189,7 @@ Note that these methods will usually return `nil` in test and development enviro Geocoding Service ("Lookup") Configuration ------------------------------------------ -Geocoder supports a variety of street and IP address geocoding services. The default lookups are `:google` for street addresses and `:ipinfo_io` for IP addresses. Please see the [API Guide](https://github.com/alexreisner/geocoder/blob/master/README_API_GUIDE.md) for details on specific geocoding services (not all settings are supported by all services). +Geocoder supports a variety of street and IP address geocoding services. The default lookups are `:nominatim` for street addresses and `:ipinfo_io` for IP addresses. Please see the [API Guide](https://github.com/alexreisner/geocoder/blob/master/README_API_GUIDE.md) for details on specific geocoding services (not all settings are supported by all services). To create a Rails initializer with sample configuration: @@ -200,7 +200,7 @@ Some common options are: # config/initializers/geocoder.rb Geocoder.configure( - # street address geocoding service (default :google) + # street address geocoding service (default :nominatim) lookup: :yandex, # IP address geocoding service (default :ipinfo_io) @@ -311,8 +311,8 @@ By default the prefix is `geocoder:` If you need to expire cached content: Geocoder::Lookup.get(Geocoder.config[:lookup]).cache.expire(:all) # expire cached results for current Lookup - Geocoder::Lookup.get(:google).cache.expire("http://...") # expire cached result for a specific URL - Geocoder::Lookup.get(:google).cache.expire(:all) # expire cached results for Google Lookup + Geocoder::Lookup.get(:nominatim).cache.expire("http://...") # expire cached result for a specific URL + Geocoder::Lookup.get(:nominatim).cache.expire(:all) # expire cached results for Google Lookup # expire all cached results for all Lookups. # Be aware that this methods spawns a new Lookup object for each Service Geocoder::Lookup.all_services.each{|service| Geocoder::Lookup.get(service).cache.expire(:all)} @@ -351,7 +351,7 @@ Supported parameters: `:lookup`, `:ip_lookup`, `:language`, and `:params`. You c elsif country_code == "CN" :baidu else - :google + :nominatim end end @@ -578,7 +578,7 @@ When you install the Geocoder gem it adds a `geocode` command to your shell. You State/province: Louisiana Postal code: 70112 Country: United States - Google map: http://maps.google.com/maps?q=29.952211,-90.080563 + Map: http://maps.google.com/maps?q=29.952211,-90.080563 There are also a number of options for setting the geocoding API, key, and language, viewing the raw JSON response, and more. Please run `geocode -h` for details. @@ -640,13 +640,13 @@ For the most part, the speed of geocoding requests has little to do with the Geo Take a look at the server's raw response. You can do this by getting the request URL in an app console: - Geocoder::Lookup.get(:google).query_url(Geocoder::Query.new("...")) + Geocoder::Lookup.get(:nominatim).query_url(Geocoder::Query.new("...")) -Replace `:google` with the lookup you are using and replace `...` with the address you are trying to geocode. Then visit the returned URL in your web browser. Often the API will return an error message that helps you resolve the problem. If, after reading the raw response, you believe there is a problem with Geocoder, please post an issue and include both the URL and raw response body. +Replace `:nominatim` with the lookup you are using and replace `...` with the address you are trying to geocode. Then visit the returned URL in your web browser. Often the API will return an error message that helps you resolve the problem. If, after reading the raw response, you believe there is a problem with Geocoder, please post an issue and include both the URL and raw response body. You can also fetch the response in the console: - Geocoder::Lookup.get(:google).send(:fetch_raw_data, Geocoder::Query.new("...")) + Geocoder::Lookup.get(:nominatim).send(:fetch_raw_data, Geocoder::Query.new("...")) Known Issues diff --git a/lib/generators/geocoder/config/templates/initializer.rb b/lib/generators/geocoder/config/templates/initializer.rb index 88c64890..0e641730 100644 --- a/lib/generators/geocoder/config/templates/initializer.rb +++ b/lib/generators/geocoder/config/templates/initializer.rb @@ -1,7 +1,7 @@ Geocoder.configure( # Geocoding options # timeout: 3, # geocoding service timeout (secs) - # lookup: :google, # name of geocoding service (symbol) + # lookup: :nominatim, # name of geocoding service (symbol) # ip_lookup: :ipinfo_io, # name of IP address geocoding service (symbol) # language: :en, # ISO-639 language code # use_https: false, # use HTTPS for lookup requests? (if supported) diff --git a/lib/geocoder/cli.rb b/lib/geocoder/cli.rb index f4832a15..2314aa18 100644 --- a/lib/geocoder/cli.rb +++ b/lib/geocoder/cli.rb @@ -97,7 +97,7 @@ module Geocoder end if (result = Geocoder.search(query).first) - google = Geocoder::Lookup.get(:google) + nominatim = Geocoder::Lookup.get(:nominatim) lines = [ ["Latitude", result.latitude], ["Longitude", result.longitude], @@ -106,7 +106,7 @@ module Geocoder ["State/province", result.state], ["Postal code", result.postal_code], ["Country", result.country], - ["Google map", google.map_link_url(result.coordinates)], + ["Map", nominatim.map_link_url(result.coordinates)], ] lines.each do |line| out << (line[0] + ": ").ljust(18) + line[1].to_s + "\n" diff --git a/lib/geocoder/configuration.rb b/lib/geocoder/configuration.rb index f2fbccf9..8e097a2d 100644 --- a/lib/geocoder/configuration.rb +++ b/lib/geocoder/configuration.rb @@ -97,7 +97,7 @@ module Geocoder # geocoding options @data[:timeout] = 3 # geocoding service timeout (secs) - @data[:lookup] = :google # name of street address geocoding service (symbol) + @data[:lookup] = :nominatim # name of street address geocoding service (symbol) @data[:ip_lookup] = :ipinfo_io # name of IP address geocoding service (symbol) @data[:language] = :en # ISO-639 language code @data[:http_headers] = {} # HTTP headers for lookup diff --git a/test/unit/geocoder_test.rb b/test/unit/geocoder_test.rb index 8dda2ba2..a73c9203 100644 --- a/test/unit/geocoder_test.rb +++ b/test/unit/geocoder_test.rb @@ -27,9 +27,9 @@ class GeocoderTest < GeocoderTestCase def test_geocode_assigns_and_returns_coordinates v = Place.new(*geocoded_object_params(:msg)) - coords = [40.750354, -73.993371] - assert_equal coords, v.geocode - assert_equal coords, [v.latitude, v.longitude] + assert_equal [Float, Float], v.geocode.map(&:class) + assert_kind_of Numeric, v.latitude + assert_kind_of Numeric, v.longitude end def test_geocode_block_executed_when_no_results @@ -40,9 +40,8 @@ class GeocoderTest < GeocoderTestCase def test_reverse_geocode_assigns_and_returns_address v = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg)) - address = "4 Penn Plaza, New York, NY 10001, USA" - assert_equal address, v.reverse_geocode - assert_equal address, v.address + assert_match /New York/, v.reverse_geocode + assert_match /New York/, v.address end def test_forward_and_reverse_geocoding_on_same_model_works diff --git a/test/unit/lookups/google_test.rb b/test/unit/lookups/google_test.rb index 7fe6a484..47a2b8dd 100644 --- a/test/unit/lookups/google_test.rb +++ b/test/unit/lookups/google_test.rb @@ -3,6 +3,10 @@ require 'test_helper' class GoogleTest < GeocoderTestCase + def setup + Geocoder.configure(lookup: :google) + end + def test_google_result_components result = Geocoder.search("Madison Square Garden, New York, NY").first assert_equal "Manhattan", diff --git a/test/unit/method_aliases_test.rb b/test/unit/method_aliases_test.rb index d33afc48..bd2f1c66 100644 --- a/test/unit/method_aliases_test.rb +++ b/test/unit/method_aliases_test.rb @@ -11,15 +11,11 @@ class MethodAliasesTest < GeocoderTestCase def test_fetch_coordinates_is_alias_for_geocode v = Place.new(*geocoded_object_params(:msg)) - coords = [40.750354, -73.993371] - assert_equal coords, v.fetch_coordinates - assert_equal coords, [v.latitude, v.longitude] + assert_equal [Float, Float], v.fetch_coordinates.map(&:class) end def test_fetch_address_is_alias_for_reverse_geocode v = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg)) - address = "4 Penn Plaza, New York, NY 10001, USA" - assert_equal address, v.fetch_address - assert_equal address, v.address + assert_match /New York/, v.fetch_address end end diff --git a/test/unit/model_test.rb b/test/unit/model_test.rb index b2fe0696..ed1dae04 100644 --- a/test/unit/model_test.rb +++ b/test/unit/model_test.rb @@ -5,9 +5,8 @@ class ModelTest < GeocoderTestCase def test_geocode_with_block_runs_block e = PlaceWithCustomResultsHandling.new(*geocoded_object_params(:msg)) - coords = [40.750354, -73.993371] e.geocode - assert_equal coords.map{ |c| c.to_s }.join(','), e.coords_string + assert_match /[0-9\.,\-]+/, e.coords_string end def test_geocode_with_block_doesnt_auto_assign_coordinates @@ -20,7 +19,7 @@ class ModelTest < GeocoderTestCase def test_reverse_geocode_with_block_runs_block e = PlaceReverseGeocodedWithCustomResultsHandling.new(*reverse_geocoded_object_params(:msg)) e.reverse_geocode - assert_equal "US", e.country + assert_equal "US", e.country.upcase end def test_reverse_geocode_with_block_doesnt_auto_assign_address diff --git a/test/unit/mongoid_test.rb b/test/unit/mongoid_test.rb index cea0fa73..d2a662ec 100644 --- a/test/unit/mongoid_test.rb +++ b/test/unit/mongoid_test.rb @@ -44,18 +44,18 @@ class MongoidTest < GeocoderTestCase p = PlaceUsingMongoidWithCustomResultsHandling.new(*geocoded_object_params(:msg)) p.location = [40.750354, -73.993371] p.geocode - assert p.coords_string == "40.750354,-73.993371" + assert_match /[0-9\.,\-]+/, p.coords_string end def test_reverse_geocoded p = PlaceUsingMongoidReverseGeocoded.new(*reverse_geocoded_object_params(:msg)) p.reverse_geocode - assert p.address == "4 Penn Plaza, New York, NY 10001, USA" + assert_match /New York/, p.address end def test_reverse_geocoded_with_custom_handling p = PlaceUsingMongoidReverseGeocodedWithCustomResultsHandling.new(*reverse_geocoded_object_params(:msg)) p.reverse_geocode - assert p.country == "US" + assert_equal "US", p.country.upcase end end -- GitLab