Skip to content

Commit

Permalink
De-Googleize.
Browse files Browse the repository at this point in the history
Make Nominatim the default lookup and map provider.

Closes #1026.
  • Loading branch information
alexreisner committed Jul 31, 2018
1 parent c1784ce commit 319a0e5
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 31 deletions.
18 changes: 9 additions & 9 deletions README.md
Expand Up @@ -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:

Expand All @@ -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)
Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -351,7 +351,7 @@ Supported parameters: `:lookup`, `:ip_lookup`, `:language`, and `:params`. You c
elsif country_code == "CN"
:baidu
else
:google
:nominatim
end
end

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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)
Expand Down
4 changes: 2 additions & 2 deletions lib/geocoder/cli.rb
Expand Up @@ -97,7 +97,7 @@ def self.run(args, out = STDOUT)
end

if (result = Geocoder.search(query).first)
google = Geocoder::Lookup.get(:google)
nominatim = Geocoder::Lookup.get(:nominatim)
lines = [
["Latitude", result.latitude],
["Longitude", result.longitude],
Expand All @@ -106,7 +106,7 @@ def self.run(args, out = STDOUT)
["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"
Expand Down
2 changes: 1 addition & 1 deletion lib/geocoder/configuration.rb
Expand Up @@ -97,7 +97,7 @@ def set_defaults

# 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
Expand Down
11 changes: 5 additions & 6 deletions test/unit/geocoder_test.rb
Expand Up @@ -27,9 +27,9 @@ def test_geographic_center_doesnt_overwrite_argument_value

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
Expand All @@ -40,9 +40,8 @@ def test_geocode_block_executed_when_no_results

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
Expand Down
4 changes: 4 additions & 0 deletions test/unit/lookups/google_test.rb
Expand Up @@ -3,6 +3,10 @@

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",
Expand Down
8 changes: 2 additions & 6 deletions test/unit/method_aliases_test.rb
Expand Up @@ -11,15 +11,11 @@ def test_distance_from_is_alias_for_distance_to

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
5 changes: 2 additions & 3 deletions test/unit/model_test.rb
Expand Up @@ -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
Expand All @@ -20,7 +19,7 @@ def test_geocode_with_block_doesnt_auto_assign_coordinates
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
Expand Down
6 changes: 3 additions & 3 deletions test/unit/mongoid_test.rb
Expand Up @@ -44,18 +44,18 @@ def test_geocoded_with_custom_handling
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

0 comments on commit 319a0e5

Please sign in to comment.