geocoder 1.6.3 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +61 -0
- data/LICENSE +1 -1
- data/README.md +329 -233
- data/examples/app_defined_lookup_services.rb +22 -0
- data/lib/generators/geocoder/config/templates/initializer.rb +6 -1
- data/lib/geocoder/cache.rb +16 -33
- data/lib/geocoder/cache_stores/base.rb +40 -0
- data/lib/geocoder/cache_stores/generic.rb +35 -0
- data/lib/geocoder/cache_stores/redis.rb +34 -0
- data/lib/geocoder/configuration.rb +19 -5
- data/lib/geocoder/configuration_hash.rb +4 -4
- data/lib/geocoder/ip_address.rb +6 -0
- data/lib/geocoder/lookup.rb +32 -4
- data/lib/geocoder/lookups/abstract_api.rb +46 -0
- data/lib/geocoder/lookups/amap.rb +2 -2
- data/lib/geocoder/lookups/amazon_location_service.rb +54 -0
- data/lib/geocoder/lookups/ban_data_gouv_fr.rb +1 -1
- data/lib/geocoder/lookups/base.rb +2 -1
- data/lib/geocoder/lookups/bing.rb +1 -1
- data/lib/geocoder/lookups/esri.rb +4 -0
- data/lib/geocoder/lookups/freegeoip.rb +8 -6
- data/lib/geocoder/lookups/geoapify.rb +78 -0
- data/lib/geocoder/lookups/geocodio.rb +1 -1
- data/lib/geocoder/lookups/geoip2.rb +4 -0
- data/lib/geocoder/lookups/geoportail_lu.rb +1 -1
- data/lib/geocoder/lookups/google.rb +7 -2
- data/lib/geocoder/lookups/google_places_details.rb +26 -12
- data/lib/geocoder/lookups/google_places_search.rb +45 -2
- data/lib/geocoder/lookups/google_premier.rb +4 -0
- data/lib/geocoder/lookups/here.rb +25 -20
- data/lib/geocoder/lookups/ip2location.rb +10 -6
- data/lib/geocoder/lookups/ipbase.rb +49 -0
- data/lib/geocoder/lookups/ipdata_co.rb +1 -1
- data/lib/geocoder/lookups/ipqualityscore.rb +50 -0
- data/lib/geocoder/lookups/location_iq.rb +5 -1
- data/lib/geocoder/lookups/maxmind_local.rb +7 -1
- data/lib/geocoder/lookups/melissa_street.rb +41 -0
- data/lib/geocoder/lookups/photon.rb +89 -0
- data/lib/geocoder/lookups/test.rb +5 -0
- data/lib/geocoder/lookups/twogis.rb +58 -0
- data/lib/geocoder/lookups/uk_ordnance_survey_names.rb +1 -1
- data/lib/geocoder/lookups/yandex.rb +3 -3
- data/lib/geocoder/results/abstract_api.rb +146 -0
- data/lib/geocoder/results/amazon_location_service.rb +57 -0
- data/lib/geocoder/results/ban_data_gouv_fr.rb +26 -1
- data/lib/geocoder/results/db_ip_com.rb +1 -1
- data/lib/geocoder/results/esri.rb +5 -2
- data/lib/geocoder/results/geoapify.rb +179 -0
- data/lib/geocoder/results/here.rb +20 -25
- data/lib/geocoder/results/ipbase.rb +40 -0
- data/lib/geocoder/results/ipqualityscore.rb +54 -0
- data/lib/geocoder/results/ipregistry.rb +4 -8
- data/lib/geocoder/results/mapbox.rb +10 -4
- data/lib/geocoder/results/melissa_street.rb +46 -0
- data/lib/geocoder/results/nationaal_georegister_nl.rb +1 -1
- data/lib/geocoder/results/nominatim.rb +27 -15
- data/lib/geocoder/results/photon.rb +119 -0
- data/lib/geocoder/results/twogis.rb +76 -0
- data/lib/geocoder/util.rb +29 -0
- data/lib/geocoder/version.rb +1 -1
- metadata +24 -6
- data/examples/autoexpire_cache_dalli.rb +0 -62
- data/examples/autoexpire_cache_redis.rb +0 -30
- data/lib/hash_recursive_merge.rb +0 -73
data/README.md
CHANGED
|
@@ -5,7 +5,7 @@ Geocoder
|
|
|
5
5
|
|
|
6
6
|
[](http://badge.fury.io/rb/geocoder)
|
|
7
7
|
[](https://codeclimate.com/github/alexreisner/geocoder)
|
|
8
|
-
[](https://travis-ci.com/alexreisner/geocoder)
|
|
9
9
|
|
|
10
10
|
Key features:
|
|
11
11
|
|
|
@@ -18,9 +18,9 @@ Key features:
|
|
|
18
18
|
|
|
19
19
|
Compatibility:
|
|
20
20
|
|
|
21
|
-
* Ruby versions: 2.
|
|
21
|
+
* Ruby versions: 2.1+, and JRuby.
|
|
22
22
|
* Databases: MySQL, PostgreSQL, SQLite, and MongoDB.
|
|
23
|
-
* Rails:
|
|
23
|
+
* Rails: 5.x, 6.x, and 7.x.
|
|
24
24
|
* Works outside of Rails with the `json` (for MRI) or `json_pure` (for JRuby) gem.
|
|
25
25
|
|
|
26
26
|
|
|
@@ -51,8 +51,8 @@ The Rest:
|
|
|
51
51
|
* [Technical Discussions](#technical-discussions)
|
|
52
52
|
* [Troubleshooting](#troubleshooting)
|
|
53
53
|
* [Known Issues](#known-issues)
|
|
54
|
-
* [Reporting Issues](#reporting-
|
|
55
|
-
* [Contributing](#
|
|
54
|
+
* [Reporting Issues](https://github.com/alexreisner/geocoder/blob/master/CONTRIBUTING.md#reporting-bugs)
|
|
55
|
+
* [Contributing](https://github.com/alexreisner/geocoder/blob/master/CONTRIBUTING.md#making-changes)
|
|
56
56
|
|
|
57
57
|
See Also:
|
|
58
58
|
|
|
@@ -64,23 +64,29 @@ Basic Search
|
|
|
64
64
|
|
|
65
65
|
In its simplest form, Geocoder takes an address and searches for its latitude/longitude coordinates:
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
```ruby
|
|
68
|
+
results = Geocoder.search("Paris")
|
|
69
|
+
results.first.coordinates
|
|
70
|
+
# => [48.856614, 2.3522219] # latitude and longitude
|
|
71
|
+
```
|
|
70
72
|
|
|
71
73
|
The reverse is possible too. Given coordinates, it finds an address:
|
|
72
74
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
75
|
+
```ruby
|
|
76
|
+
results = Geocoder.search([48.856614, 2.3522219])
|
|
77
|
+
results.first.address
|
|
78
|
+
# => "Hôtel de Ville, 75004 Paris, France"
|
|
79
|
+
```
|
|
76
80
|
|
|
77
|
-
You can also look up the location of an IP
|
|
81
|
+
You can also look up the location of an IP address:
|
|
78
82
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
```ruby
|
|
84
|
+
results = Geocoder.search("172.56.21.89")
|
|
85
|
+
results.first.coordinates
|
|
86
|
+
# => [30.267153, -97.7430608]
|
|
87
|
+
results.first.country
|
|
88
|
+
# => "United States"
|
|
89
|
+
```
|
|
84
90
|
|
|
85
91
|
**The success and accuracy of geocoding depends entirely on the API being used to do these lookups.** Most queries work fairly well with the default configuration, but every application has different needs and every API has its particular strengths and weaknesses. If you need better coverage for your application you'll want to get familiar with the large number of supported APIs, listed in the [API Guide](https://github.com/alexreisner/geocoder/blob/master/README_API_GUIDE.md).
|
|
86
92
|
|
|
@@ -92,30 +98,40 @@ To automatically geocode your objects:
|
|
|
92
98
|
|
|
93
99
|
**1.** Your model must provide a method that returns an address to geocode. This can be a single attribute, but it can also be a method that returns a string assembled from different attributes (eg: `city`, `state`, and `country`). For example, if your model has `street`, `city`, `state`, and `country` attributes you might do something like this:
|
|
94
100
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
101
|
+
```ruby
|
|
102
|
+
def address
|
|
103
|
+
[street, city, state, country].compact.join(', ')
|
|
104
|
+
end
|
|
105
|
+
```
|
|
98
106
|
|
|
99
107
|
**2.** Your model must have a way to store latitude/longitude coordinates. With ActiveRecord, add two attributes/columns (of type float or decimal) called `latitude` and `longitude`. For MongoDB, use a single field (of type Array) called `coordinates` (i.e., `field :coordinates, type: Array`). (See [Advanced Model Configuration](#advanced-model-configuration) for using different attribute names.)
|
|
100
108
|
|
|
101
109
|
**3.** In your model, tell geocoder where to find the object's address:
|
|
102
110
|
|
|
103
|
-
|
|
111
|
+
```ruby
|
|
112
|
+
geocoded_by :address
|
|
113
|
+
```
|
|
104
114
|
|
|
105
115
|
This adds a `geocode` method which you can invoke via callback:
|
|
106
116
|
|
|
107
|
-
|
|
117
|
+
```ruby
|
|
118
|
+
after_validation :geocode
|
|
119
|
+
```
|
|
108
120
|
|
|
109
121
|
Reverse geocoding (given lat/lon coordinates, find an address) is similar:
|
|
110
122
|
|
|
111
|
-
|
|
112
|
-
|
|
123
|
+
```ruby
|
|
124
|
+
reverse_geocoded_by :latitude, :longitude
|
|
125
|
+
after_validation :reverse_geocode
|
|
126
|
+
```
|
|
113
127
|
|
|
114
128
|
With any geocoded objects, you can do the following:
|
|
115
129
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
130
|
+
```ruby
|
|
131
|
+
obj.distance_to([43.9,-98.6]) # distance from obj to point
|
|
132
|
+
obj.bearing_to([43.9,-98.6]) # bearing from obj to point
|
|
133
|
+
obj.bearing_from(obj2) # bearing from obj2 to obj
|
|
134
|
+
```
|
|
119
135
|
|
|
120
136
|
The `bearing_from/to` methods take a single argument which can be: a `[lat,lon]` array, a geocoded object, or a geocodable address (string). The `distance_from/to` methods also take a units argument (`:mi`, `:km`, or `:nm` for nautical miles). See [Distance and Bearing](#distance-and-bearing) below for more info.
|
|
121
137
|
|
|
@@ -123,18 +139,24 @@ The `bearing_from/to` methods take a single argument which can be: a `[lat,lon]`
|
|
|
123
139
|
|
|
124
140
|
Before you can call `geocoded_by` you'll need to include the necessary module using one of the following:
|
|
125
141
|
|
|
126
|
-
|
|
127
|
-
|
|
142
|
+
```ruby
|
|
143
|
+
include Geocoder::Model::Mongoid
|
|
144
|
+
include Geocoder::Model::MongoMapper
|
|
145
|
+
```
|
|
128
146
|
|
|
129
147
|
### Latitude/Longitude Order in MongoDB
|
|
130
148
|
|
|
131
149
|
Everywhere coordinates are passed to methods as two-element arrays, Geocoder expects them to be in the order: `[lat, lon]`. However, as per [the GeoJSON spec](http://geojson.org/geojson-spec.html#positions), MongoDB requires that coordinates be stored longitude-first (`[lon, lat]`), so internally they are stored "backwards." Geocoder's methods attempt to hide this, so calling `obj.to_coordinates` (a method added to the object by Geocoder via `geocoded_by`) returns coordinates in the conventional order:
|
|
132
150
|
|
|
133
|
-
|
|
151
|
+
```ruby
|
|
152
|
+
obj.to_coordinates # => [37.7941013, -122.3951096] # [lat, lon]
|
|
153
|
+
```
|
|
134
154
|
|
|
135
155
|
whereas calling the object's coordinates attribute directly (`obj.coordinates` by default) returns the internal representation which is probably the reverse of what you want:
|
|
136
156
|
|
|
137
|
-
|
|
157
|
+
```ruby
|
|
158
|
+
obj.coordinates # => [-122.3951096, 37.7941013] # [lon, lat]
|
|
159
|
+
```
|
|
138
160
|
|
|
139
161
|
So, be careful.
|
|
140
162
|
|
|
@@ -142,7 +164,9 @@ So, be careful.
|
|
|
142
164
|
|
|
143
165
|
To use Geocoder with ActiveRecord and a framework other than Rails (like Sinatra or Padrino), you will need to add this in your model before calling Geocoder methods:
|
|
144
166
|
|
|
145
|
-
|
|
167
|
+
```ruby
|
|
168
|
+
extend Geocoder::Model::ActiveRecord
|
|
169
|
+
```
|
|
146
170
|
|
|
147
171
|
|
|
148
172
|
Geospatial Database Queries
|
|
@@ -152,19 +176,23 @@ Geospatial Database Queries
|
|
|
152
176
|
|
|
153
177
|
To find objects by location, use the following scopes:
|
|
154
178
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
179
|
+
```ruby
|
|
180
|
+
Venue.near('Omaha, NE, US') # venues within 20 miles of Omaha
|
|
181
|
+
Venue.near([40.71, -100.23], 50) # venues within 50 miles of a point
|
|
182
|
+
Venue.near([40.71, -100.23], 50, units: :km) # venues within 50 kilometres of a point
|
|
183
|
+
Venue.geocoded # venues with coordinates
|
|
184
|
+
Venue.not_geocoded # venues without coordinates
|
|
185
|
+
```
|
|
160
186
|
|
|
161
187
|
With geocoded objects you can do things like this:
|
|
162
188
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
189
|
+
```ruby
|
|
190
|
+
if obj.geocoded?
|
|
191
|
+
obj.nearbys(30) # other objects within 30 miles
|
|
192
|
+
obj.distance_from([40.714,-100.234]) # distance from arbitrary point to object
|
|
193
|
+
obj.bearing_to("Paris, France") # direction from object to arbitrary point
|
|
194
|
+
end
|
|
195
|
+
```
|
|
168
196
|
|
|
169
197
|
### For MongoDB-backed models:
|
|
170
198
|
|
|
@@ -176,8 +204,10 @@ Geocoding HTTP Requests
|
|
|
176
204
|
|
|
177
205
|
Geocoder adds `location` and `safe_location` methods to the standard `Rack::Request` object so you can easily look up the location of any HTTP request by IP address. For example, in a Rails controller or a Sinatra app:
|
|
178
206
|
|
|
179
|
-
|
|
180
|
-
|
|
207
|
+
```ruby
|
|
208
|
+
# returns Geocoder::Result object
|
|
209
|
+
result = request.location
|
|
210
|
+
```
|
|
181
211
|
|
|
182
212
|
**The `location` method is vulnerable to trivial IP address spoofing via HTTP headers.** If that's a problem for your application, use `safe_location` instead, but be aware that `safe_location` will *not* try to trace a request's originating IP through proxy headers; you will instead get the location of the last proxy the request passed through, if any (excepting any proxies you have explicitly whitelisted in your Rack config).
|
|
183
213
|
|
|
@@ -191,71 +221,82 @@ Geocoder supports a variety of street and IP address geocoding services. The def
|
|
|
191
221
|
|
|
192
222
|
To create a Rails initializer with sample configuration:
|
|
193
223
|
|
|
194
|
-
|
|
224
|
+
```sh
|
|
225
|
+
rails generate geocoder:config
|
|
226
|
+
```
|
|
195
227
|
|
|
196
228
|
Some common options are:
|
|
197
229
|
|
|
198
|
-
|
|
199
|
-
|
|
230
|
+
```ruby
|
|
231
|
+
# config/initializers/geocoder.rb
|
|
232
|
+
Geocoder.configure(
|
|
233
|
+
# street address geocoding service (default :nominatim)
|
|
234
|
+
lookup: :yandex,
|
|
200
235
|
|
|
201
|
-
|
|
202
|
-
|
|
236
|
+
# IP address geocoding service (default :ipinfo_io)
|
|
237
|
+
ip_lookup: :maxmind,
|
|
203
238
|
|
|
204
|
-
|
|
205
|
-
|
|
239
|
+
# to use an API key:
|
|
240
|
+
api_key: "...",
|
|
206
241
|
|
|
207
|
-
|
|
208
|
-
|
|
242
|
+
# geocoding service request timeout, in seconds (default 3):
|
|
243
|
+
timeout: 5,
|
|
209
244
|
|
|
210
|
-
|
|
211
|
-
|
|
245
|
+
# set default units to kilometers:
|
|
246
|
+
units: :km,
|
|
212
247
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
248
|
+
# caching (see Caching section below for details):
|
|
249
|
+
cache: Redis.new,
|
|
250
|
+
cache_options: {
|
|
251
|
+
expiration: 1.day, # Defaults to `nil`
|
|
252
|
+
prefix: "another_key:" # Defaults to `geocoder:`
|
|
253
|
+
}
|
|
254
|
+
)
|
|
255
|
+
```
|
|
221
256
|
|
|
222
257
|
Please see [`lib/geocoder/configuration.rb`](https://github.com/alexreisner/geocoder/blob/master/lib/geocoder/configuration.rb) for a complete list of configuration options. Additionally, some lookups have their own special configuration options which are directly supported by Geocoder. For example, to specify a value for Google's `bounds` parameter:
|
|
223
258
|
|
|
224
|
-
|
|
225
|
-
|
|
259
|
+
```ruby
|
|
260
|
+
# with Google:
|
|
261
|
+
Geocoder.search("Middletown", bounds: [[40.6,-77.9], [39.9,-75.9]])
|
|
262
|
+
```
|
|
226
263
|
|
|
227
264
|
Please see the [source code for each lookup](https://github.com/alexreisner/geocoder/tree/master/lib/geocoder/lookups) to learn about directly supported parameters. Parameters which are not directly supported can be specified using the `:params` option, which appends options to the query string of the geocoding request. For example:
|
|
228
265
|
|
|
229
|
-
|
|
230
|
-
|
|
266
|
+
```ruby
|
|
267
|
+
# Nominatim's `countrycodes` parameter:
|
|
268
|
+
Geocoder.search("Rome", params: {countrycodes: "us,ca"})
|
|
231
269
|
|
|
232
|
-
|
|
233
|
-
|
|
270
|
+
# Google's `region` parameter:
|
|
271
|
+
Geocoder.search("Rome", params: {region: "..."})
|
|
272
|
+
```
|
|
234
273
|
|
|
235
274
|
### Configuring Multiple Services
|
|
236
275
|
|
|
237
276
|
You can configure multiple geocoding services at once by using the service's name as a key for a sub-configuration hash, like this:
|
|
238
277
|
|
|
239
|
-
|
|
278
|
+
```ruby
|
|
279
|
+
Geocoder.configure(
|
|
240
280
|
|
|
241
|
-
|
|
242
|
-
|
|
281
|
+
timeout: 2,
|
|
282
|
+
cache: Redis.new,
|
|
243
283
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
284
|
+
yandex: {
|
|
285
|
+
api_key: "...",
|
|
286
|
+
timeout: 5
|
|
287
|
+
},
|
|
248
288
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
289
|
+
baidu: {
|
|
290
|
+
api_key: "..."
|
|
291
|
+
},
|
|
252
292
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
293
|
+
maxmind: {
|
|
294
|
+
api_key: "...",
|
|
295
|
+
service: :omni
|
|
296
|
+
}
|
|
257
297
|
|
|
258
|
-
|
|
298
|
+
)
|
|
299
|
+
```
|
|
259
300
|
|
|
260
301
|
Lookup-specific settings override global settings so, in this example, the timeout for all lookups is 2 seconds, except for Yandex which is 5.
|
|
261
302
|
|
|
@@ -267,12 +308,16 @@ Performance and Optimization
|
|
|
267
308
|
|
|
268
309
|
In MySQL and Postgres, queries use a bounding box to limit the number of points over which a more precise distance calculation needs to be done. To take advantage of this optimisation, you need to add a composite index on latitude and longitude. In your Rails migration:
|
|
269
310
|
|
|
270
|
-
|
|
311
|
+
```ruby
|
|
312
|
+
add_index :table, [:latitude, :longitude]
|
|
313
|
+
```
|
|
271
314
|
|
|
272
315
|
In MongoDB, by default, the methods `geocoded_by` and `reverse_geocoded_by` create a geospatial index. You can avoid index creation with the `:skip_index option`, for example:
|
|
273
316
|
|
|
274
|
-
|
|
275
|
-
|
|
317
|
+
```ruby
|
|
318
|
+
include Geocoder::Model::Mongoid
|
|
319
|
+
geocoded_by :address, skip_index: true
|
|
320
|
+
```
|
|
276
321
|
|
|
277
322
|
### Avoiding Unnecessary API Requests
|
|
278
323
|
|
|
@@ -283,13 +328,17 @@ Geocoding only needs to be performed under certain conditions. To avoid unnecess
|
|
|
283
328
|
|
|
284
329
|
The exact code will vary depending on the method you use for your geocodable string, but it would be something like this:
|
|
285
330
|
|
|
286
|
-
|
|
331
|
+
```ruby
|
|
332
|
+
after_validation :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }
|
|
333
|
+
```
|
|
287
334
|
|
|
288
335
|
### Caching
|
|
289
336
|
|
|
290
337
|
When relying on any external service, it's always a good idea to cache retrieved data. When implemented correctly, it improves your app's response time and stability. It's easy to cache geocoding results with Geocoder -- just configure a cache store:
|
|
291
338
|
|
|
292
|
-
|
|
339
|
+
```ruby
|
|
340
|
+
Geocoder.configure(cache: Redis.new)
|
|
341
|
+
```
|
|
293
342
|
|
|
294
343
|
This example uses Redis, but the cache store can be any object that supports these methods:
|
|
295
344
|
|
|
@@ -300,20 +349,30 @@ This example uses Redis, but the cache store can be any object that supports the
|
|
|
300
349
|
|
|
301
350
|
Even a plain Ruby hash will work, though it's not a great choice (cleared out when app is restarted, not shared between app instances, etc).
|
|
302
351
|
|
|
352
|
+
When using Rails use the Generic cache store as an adapter around `Rails.cache`:
|
|
353
|
+
|
|
354
|
+
```ruby
|
|
355
|
+
Geocoder.configure(cache: Geocoder::CacheStore::Generic.new(Rails.cache, {}))
|
|
356
|
+
```
|
|
357
|
+
|
|
303
358
|
You can also set a custom prefix to be used for cache keys:
|
|
304
359
|
|
|
305
|
-
|
|
360
|
+
```ruby
|
|
361
|
+
Geocoder.configure(cache_options: { prefix: "..." })
|
|
362
|
+
```
|
|
306
363
|
|
|
307
364
|
By default the prefix is `geocoder:`
|
|
308
365
|
|
|
309
366
|
If you need to expire cached content:
|
|
310
367
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
368
|
+
```ruby
|
|
369
|
+
Geocoder::Lookup.get(Geocoder.config[:lookup]).cache.expire(:all) # expire cached results for current Lookup
|
|
370
|
+
Geocoder::Lookup.get(:nominatim).cache.expire("http://...") # expire cached result for a specific URL
|
|
371
|
+
Geocoder::Lookup.get(:nominatim).cache.expire(:all) # expire cached results for Google Lookup
|
|
372
|
+
# expire all cached results for all Lookups.
|
|
373
|
+
# Be aware that this methods spawns a new Lookup object for each Service
|
|
374
|
+
Geocoder::Lookup.all_services.each{|service| Geocoder::Lookup.get(service).cache.expire(:all)}
|
|
375
|
+
```
|
|
317
376
|
|
|
318
377
|
Do *not* include the prefix when passing a URL to be expired. Expiring `:all` will only expire keys with the configured prefix -- it will *not* expire every entry in your key/value store.
|
|
319
378
|
|
|
@@ -327,44 +386,55 @@ Advanced Model Configuration
|
|
|
327
386
|
|
|
328
387
|
You are not stuck with the `latitude` and `longitude` database column names (with ActiveRecord) or the `coordinates` array (Mongo) for storing coordinates. For example:
|
|
329
388
|
|
|
330
|
-
|
|
331
|
-
|
|
389
|
+
```ruby
|
|
390
|
+
geocoded_by :address, latitude: :lat, longitude: :lon # ActiveRecord
|
|
391
|
+
geocoded_by :address, coordinates: :coords # MongoDB
|
|
392
|
+
```
|
|
332
393
|
|
|
333
394
|
For reverse geocoding, you can specify the attribute where the address will be stored. For example:
|
|
334
395
|
|
|
335
|
-
|
|
336
|
-
|
|
396
|
+
```ruby
|
|
397
|
+
reverse_geocoded_by :latitude, :longitude, address: :loc # ActiveRecord
|
|
398
|
+
reverse_geocoded_by :coordinates, address: :street_address # MongoDB
|
|
399
|
+
```
|
|
337
400
|
|
|
338
401
|
To specify geocoding parameters in your model:
|
|
339
402
|
|
|
340
|
-
|
|
403
|
+
```ruby
|
|
404
|
+
geocoded_by :address, params: {region: "..."}
|
|
405
|
+
```
|
|
341
406
|
|
|
342
407
|
Supported parameters: `:lookup`, `:ip_lookup`, `:language`, and `:params`. You can specify an anonymous function if you want to set these on a per-request basis. For example, to use different lookups for objects in different regions:
|
|
343
408
|
|
|
344
|
-
|
|
409
|
+
```ruby
|
|
410
|
+
geocoded_by :address, lookup: lambda{ |obj| obj.geocoder_lookup }
|
|
345
411
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
412
|
+
def geocoder_lookup
|
|
413
|
+
if country_code == "RU"
|
|
414
|
+
:yandex
|
|
415
|
+
elsif country_code == "CN"
|
|
416
|
+
:baidu
|
|
417
|
+
else
|
|
418
|
+
:nominatim
|
|
419
|
+
end
|
|
420
|
+
end
|
|
421
|
+
```
|
|
355
422
|
|
|
356
423
|
### Custom Result Handling
|
|
357
424
|
|
|
358
425
|
So far we have seen examples where geocoding results are assigned automatically to predefined object attributes. However, you can skip the auto-assignment by providing a block which handles the parsed geocoding results any way you like, for example:
|
|
359
426
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
427
|
+
```ruby
|
|
428
|
+
reverse_geocoded_by :latitude, :longitude do |obj,results|
|
|
429
|
+
if geo = results.first
|
|
430
|
+
obj.city = geo.city
|
|
431
|
+
obj.zipcode = geo.postal_code
|
|
432
|
+
obj.country = geo.country_code
|
|
433
|
+
end
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
after_validation :reverse_geocode
|
|
437
|
+
```
|
|
368
438
|
|
|
369
439
|
Every `Geocoder::Result` object, `result`, provides the following data:
|
|
370
440
|
|
|
@@ -390,23 +460,26 @@ You can apply both forward and reverse geocoding to the same model (i.e. users c
|
|
|
390
460
|
|
|
391
461
|
For example:
|
|
392
462
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
463
|
+
```ruby
|
|
464
|
+
class Venue
|
|
465
|
+
# build an address from street, city, and state attributes
|
|
466
|
+
geocoded_by :address_from_components
|
|
397
467
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
468
|
+
# store the fetched address in the full_address attribute
|
|
469
|
+
reverse_geocoded_by :latitude, :longitude, address: :full_address
|
|
470
|
+
end
|
|
471
|
+
```
|
|
401
472
|
|
|
402
473
|
The same goes for latitude/longitude. However, for purposes of querying the database, there can be only one authoritative set of latitude/longitude attributes for use in database queries. This is whichever you specify last. For example, here the attributes *without* the `fetched_` prefix will be authoritative:
|
|
403
474
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
475
|
+
```ruby
|
|
476
|
+
class Venue
|
|
477
|
+
geocoded_by :address,
|
|
478
|
+
latitude: :fetched_latitude,
|
|
479
|
+
longitude: :fetched_longitude
|
|
480
|
+
reverse_geocoded_by :latitude, :longitude
|
|
481
|
+
end
|
|
482
|
+
```
|
|
410
483
|
|
|
411
484
|
|
|
412
485
|
Advanced Database Queries
|
|
@@ -416,21 +489,29 @@ Advanced Database Queries
|
|
|
416
489
|
|
|
417
490
|
The default `near` search looks for objects within a circle. To search within a doughnut or ring use the `:min_radius` option:
|
|
418
491
|
|
|
419
|
-
|
|
492
|
+
```ruby
|
|
493
|
+
Venue.near("Austin, TX", 200, min_radius: 40)
|
|
494
|
+
```
|
|
420
495
|
|
|
421
496
|
To search within a rectangle (note that results will *not* include `distance` and `bearing` attributes):
|
|
422
497
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
498
|
+
```ruby
|
|
499
|
+
sw_corner = [40.71, 100.23]
|
|
500
|
+
ne_corner = [36.12, 88.65]
|
|
501
|
+
Venue.within_bounding_box(sw_corner, ne_corner)
|
|
502
|
+
```
|
|
426
503
|
|
|
427
504
|
To search for objects near a certain point where each object has a different distance requirement (which is defined in the database), you can pass a column name for the radius:
|
|
428
505
|
|
|
429
|
-
|
|
506
|
+
```ruby
|
|
507
|
+
Venue.near([40.71, 99.23], :effective_radius)
|
|
508
|
+
```
|
|
430
509
|
|
|
431
510
|
If you store multiple sets of coordinates for each object, you can specify latitude and longitude columns to use for a search:
|
|
432
511
|
|
|
433
|
-
|
|
512
|
+
```ruby
|
|
513
|
+
Venue.near("Paris", 50, latitude: :secondary_latitude, longitude: :secondary_longitude)
|
|
514
|
+
```
|
|
434
515
|
|
|
435
516
|
### Distance and Bearing
|
|
436
517
|
|
|
@@ -450,9 +531,11 @@ Results are automatically sorted by distance from the search point, closest to f
|
|
|
450
531
|
|
|
451
532
|
You can convert these to compass point names via provided method:
|
|
452
533
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
534
|
+
```ruby
|
|
535
|
+
Geocoder::Calculations.compass_point(355) # => "N"
|
|
536
|
+
Geocoder::Calculations.compass_point(45) # => "NE"
|
|
537
|
+
Geocoder::Calculations.compass_point(208) # => "SW"
|
|
538
|
+
```
|
|
456
539
|
|
|
457
540
|
_Note: when running queries on SQLite, `distance` and `bearing` are provided for consistency only. They are not very accurate._
|
|
458
541
|
|
|
@@ -464,13 +547,15 @@ Geospatial Calculations
|
|
|
464
547
|
|
|
465
548
|
The `Geocoder::Calculations` module contains some useful methods:
|
|
466
549
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
550
|
+
```ruby
|
|
551
|
+
# find the distance between two arbitrary points
|
|
552
|
+
Geocoder::Calculations.distance_between([47.858205,2.294359], [40.748433,-73.985655])
|
|
553
|
+
=> 3619.77359999382 # in configured units (default miles)
|
|
470
554
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
555
|
+
# find the geographic center (aka center of gravity) of objects or points
|
|
556
|
+
Geocoder::Calculations.geographic_center([city1, city2, [40.22,-73.99], city4])
|
|
557
|
+
=> [35.14968, -90.048929]
|
|
558
|
+
```
|
|
474
559
|
|
|
475
560
|
See [the code](https://github.com/alexreisner/geocoder/blob/master/lib/geocoder/calculations.rb) for more!
|
|
476
561
|
|
|
@@ -480,19 +565,27 @@ Batch Geocoding
|
|
|
480
565
|
|
|
481
566
|
If you have just added geocoding to an existing application with a lot of objects, you can use this Rake task to geocode them all:
|
|
482
567
|
|
|
483
|
-
|
|
568
|
+
```sh
|
|
569
|
+
rake geocode:all CLASS=YourModel
|
|
570
|
+
```
|
|
484
571
|
|
|
485
572
|
If you need reverse geocoding instead, call the task with REVERSE=true:
|
|
486
573
|
|
|
487
|
-
|
|
574
|
+
```sh
|
|
575
|
+
rake geocode:all CLASS=YourModel REVERSE=true
|
|
576
|
+
```
|
|
488
577
|
|
|
489
578
|
In either case, it won't try to geocode objects that are already geocoded. The task will print warnings if you exceed the rate limit for your geocoding service. Some services enforce a per-second limit in addition to a per-day limit. To avoid exceeding the per-second limit, you can add a `SLEEP` option to pause between requests for a given amount of time. You can also load objects in batches to save memory, for example:
|
|
490
579
|
|
|
491
|
-
|
|
580
|
+
```sh
|
|
581
|
+
rake geocode:all CLASS=YourModel SLEEP=0.25 BATCH=100
|
|
582
|
+
```
|
|
492
583
|
|
|
493
584
|
To avoid exceeding per-day limits you can add a `LIMIT` option. However, this will ignore the `BATCH` value, if provided.
|
|
494
585
|
|
|
495
|
-
|
|
586
|
+
```sh
|
|
587
|
+
rake geocode:all CLASS=YourModel LIMIT=1000
|
|
588
|
+
```
|
|
496
589
|
|
|
497
590
|
|
|
498
591
|
Testing
|
|
@@ -500,42 +593,54 @@ Testing
|
|
|
500
593
|
|
|
501
594
|
When writing tests for an app that uses Geocoder it may be useful to avoid network calls and have Geocoder return consistent, configurable results. To do this, configure the `:test` lookup and/or `:ip_lookup`
|
|
502
595
|
|
|
503
|
-
|
|
596
|
+
```ruby
|
|
597
|
+
Geocoder.configure(lookup: :test, ip_lookup: :test)
|
|
598
|
+
```
|
|
504
599
|
|
|
505
600
|
Add stubs to define the results that will be returned:
|
|
506
601
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
602
|
+
```ruby
|
|
603
|
+
Geocoder::Lookup::Test.add_stub(
|
|
604
|
+
"New York, NY", [
|
|
605
|
+
{
|
|
606
|
+
'coordinates' => [40.7143528, -74.0059731],
|
|
607
|
+
'address' => 'New York, NY, USA',
|
|
608
|
+
'state' => 'New York',
|
|
609
|
+
'state_code' => 'NY',
|
|
610
|
+
'country' => 'United States',
|
|
611
|
+
'country_code' => 'US'
|
|
612
|
+
}
|
|
613
|
+
]
|
|
614
|
+
)
|
|
615
|
+
```
|
|
519
616
|
|
|
520
617
|
With the above stub defined, any query for "New York, NY" will return the results array that follows. You can also set a default stub, to be returned when no other stub matches a given query:
|
|
521
618
|
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
619
|
+
```ruby
|
|
620
|
+
Geocoder::Lookup::Test.set_default_stub(
|
|
621
|
+
[
|
|
622
|
+
{
|
|
623
|
+
'coordinates' => [40.7143528, -74.0059731],
|
|
624
|
+
'address' => 'New York, NY, USA',
|
|
625
|
+
'state' => 'New York',
|
|
626
|
+
'state_code' => 'NY',
|
|
627
|
+
'country' => 'United States',
|
|
628
|
+
'country_code' => 'US'
|
|
629
|
+
}
|
|
630
|
+
]
|
|
631
|
+
)
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
You may also delete a single stub, or reset all stubs _including the default stub_:
|
|
635
|
+
|
|
636
|
+
```ruby
|
|
637
|
+
Geocoder::Lookup::Test.delete_stub('New York, NY')
|
|
638
|
+
Geocoder::Lookup::Test.reset
|
|
639
|
+
```
|
|
534
640
|
|
|
535
641
|
Notes:
|
|
536
642
|
|
|
537
643
|
- Keys must be strings (not symbols) when calling `add_stub` or `set_default_stub`. For example `'country' =>` not `:country =>`.
|
|
538
|
-
- To clear stubs (e.g. prior to another spec), use `Geocoder::Lookup::Test.reset`. This will clear all stubs _including the default stub_.
|
|
539
644
|
- The stubbed result objects returned by the Test lookup do not support all the methods real result objects do. If you need to test interaction with real results it may be better to use an external stubbing tool and something like WebMock or VCR to prevent network calls.
|
|
540
645
|
|
|
541
646
|
|
|
@@ -544,21 +649,27 @@ Error Handling
|
|
|
544
649
|
|
|
545
650
|
By default Geocoder will rescue any exceptions raised by calls to a geocoding service and return an empty array. You can override this on a per-exception basis, and also have Geocoder raise its own exceptions for certain events (eg: API quota exceeded) by using the `:always_raise` option:
|
|
546
651
|
|
|
547
|
-
|
|
652
|
+
```ruby
|
|
653
|
+
Geocoder.configure(always_raise: [SocketError, Timeout::Error])
|
|
654
|
+
```
|
|
548
655
|
|
|
549
656
|
You can also do this to raise all exceptions:
|
|
550
657
|
|
|
551
|
-
|
|
658
|
+
```ruby
|
|
659
|
+
Geocoder.configure(always_raise: :all)
|
|
660
|
+
```
|
|
552
661
|
|
|
553
662
|
The raise-able exceptions are:
|
|
554
663
|
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
664
|
+
```ruby
|
|
665
|
+
SocketError
|
|
666
|
+
Timeout::Error
|
|
667
|
+
Geocoder::OverQueryLimitError
|
|
668
|
+
Geocoder::RequestDenied
|
|
669
|
+
Geocoder::InvalidRequest
|
|
670
|
+
Geocoder::InvalidApiKey
|
|
671
|
+
Geocoder::ServiceUnavailable
|
|
672
|
+
```
|
|
562
673
|
|
|
563
674
|
Note that only a few of the above exceptions are raised by any given lookup, so there's no guarantee if you configure Geocoder to raise `ServiceUnavailable` that it will actually be raised under those conditions (because most APIs don't return 503 when they should; you may get a `Timeout::Error` instead). Please see the source code for your particular lookup for details.
|
|
564
675
|
|
|
@@ -568,15 +679,17 @@ Command Line Interface
|
|
|
568
679
|
|
|
569
680
|
When you install the Geocoder gem it adds a `geocode` command to your shell. You can search for a street address, IP address, postal code, coordinates, etc just like you can with the Geocoder.search method for example:
|
|
570
681
|
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
682
|
+
```sh
|
|
683
|
+
$ geocode 29.951,-90.081
|
|
684
|
+
Latitude: 29.952211
|
|
685
|
+
Longitude: -90.080563
|
|
686
|
+
Full address: 1500 Sugar Bowl Dr, New Orleans, LA 70112, USA
|
|
687
|
+
City: New Orleans
|
|
688
|
+
State/province: Louisiana
|
|
689
|
+
Postal code: 70112
|
|
690
|
+
Country: United States
|
|
691
|
+
Map: http://maps.google.com/maps?q=29.952211,-90.080563
|
|
692
|
+
```
|
|
580
693
|
|
|
581
694
|
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.
|
|
582
695
|
|
|
@@ -612,8 +725,10 @@ Troubleshooting
|
|
|
612
725
|
|
|
613
726
|
If you get one of these errors:
|
|
614
727
|
|
|
615
|
-
|
|
616
|
-
|
|
728
|
+
```ruby
|
|
729
|
+
uninitialized constant Geocoder::Model::Mongoid
|
|
730
|
+
uninitialized constant Geocoder::Model::Mongoid::Mongo
|
|
731
|
+
```
|
|
617
732
|
|
|
618
733
|
you should check your Gemfile to make sure the Mongoid gem is listed _before_ Geocoder. If Mongoid isn't loaded when Geocoder is initialized, Geocoder will not load support for Mongoid.
|
|
619
734
|
|
|
@@ -632,19 +747,23 @@ If your application requires quick geocoding responses you will probably need to
|
|
|
632
747
|
|
|
633
748
|
For IP address lookups in Rails applications, it is generally NOT a good idea to run `request.location` during a synchronous page load without understanding the speed/behavior of your configured lookup. If the lookup becomes slow, so will your website.
|
|
634
749
|
|
|
635
|
-
For the most part, the speed of geocoding requests has little to do with the Geocoder gem. Please take the time to learn about your configured lookup
|
|
750
|
+
For the most part, the speed of geocoding requests has little to do with the Geocoder gem. Please take the time to learn about your configured lookup before posting performance-related issues.
|
|
636
751
|
|
|
637
752
|
### Unexpected Responses from Geocoding Services
|
|
638
753
|
|
|
639
754
|
Take a look at the server's raw response. You can do this by getting the request URL in an app console:
|
|
640
755
|
|
|
641
|
-
|
|
756
|
+
```ruby
|
|
757
|
+
Geocoder::Lookup.get(:nominatim).query_url(Geocoder::Query.new("..."))
|
|
758
|
+
```
|
|
642
759
|
|
|
643
760
|
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.
|
|
644
761
|
|
|
645
762
|
You can also fetch the response in the console:
|
|
646
763
|
|
|
647
|
-
|
|
764
|
+
```ruby
|
|
765
|
+
Geocoder::Lookup.get(:nominatim).send(:fetch_raw_data, Geocoder::Query.new("..."))
|
|
766
|
+
```
|
|
648
767
|
|
|
649
768
|
|
|
650
769
|
Known Issues
|
|
@@ -660,14 +779,16 @@ You cannot use the `near` scope with another scope that provides an `includes` o
|
|
|
660
779
|
|
|
661
780
|
Instead of using `includes` to reduce the number of database queries, try using `joins` with either the `:select` option or a call to `preload`. For example:
|
|
662
781
|
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
782
|
+
```ruby
|
|
783
|
+
# Pass a :select option to the near scope to get the columns you want.
|
|
784
|
+
# Instead of City.near(...).includes(:venues), try:
|
|
785
|
+
City.near("Omaha, NE", 20, select: "cities.*, venues.*").joins(:venues)
|
|
666
786
|
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
787
|
+
# This preload call will normally trigger two queries regardless of the
|
|
788
|
+
# number of results; one query on hotels, and one query on administrators.
|
|
789
|
+
# Instead of Hotel.near(...).includes(:administrator), try:
|
|
790
|
+
Hotel.near("London, UK", 50).joins(:administrator).preload(:administrator)
|
|
791
|
+
```
|
|
671
792
|
|
|
672
793
|
If anyone has a more elegant solution to this problem I am very interested in seeing it.
|
|
673
794
|
|
|
@@ -676,29 +797,4 @@ If anyone has a more elegant solution to this problem I am very interested in se
|
|
|
676
797
|
The `near` method will not look across the 180th meridian to find objects close to a given point. In practice this is rarely an issue outside of New Zealand and certain surrounding islands. This problem does not exist with the zero-meridian. The problem is due to a shortcoming of the Haversine formula which Geocoder uses to calculate distances.
|
|
677
798
|
|
|
678
799
|
|
|
679
|
-
|
|
680
|
-
----------------
|
|
681
|
-
|
|
682
|
-
When reporting an issue, please list the version of Geocoder you are using and any relevant information about your application (Rails version, database type and version, etc). Please describe as specifically as you can what behavior you are seeing (eg: an error message? a nil return value?).
|
|
683
|
-
|
|
684
|
-
Please DO NOT use GitHub issues to ask questions about how to use Geocoder. Sites like [StackOverflow](http://www.stackoverflow.com/) are a better forum for such discussions.
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
Contributing
|
|
688
|
-
------------
|
|
689
|
-
|
|
690
|
-
Contributions are welcome via Github pull requests. If you are new to the project and looking for a way to get involved, try picking up an issue with a "beginner-task" label. Hints about what needs to be done are usually provided.
|
|
691
|
-
|
|
692
|
-
For all contributions, please respect the following guidelines:
|
|
693
|
-
|
|
694
|
-
* Each pull request should implement ONE feature or bugfix. If you want to add or fix more than one thing, submit more than one pull request.
|
|
695
|
-
* Do not commit changes to files that are irrelevant to your feature or bugfix (eg: `.gitignore`).
|
|
696
|
-
* Do not add dependencies on other gems.
|
|
697
|
-
* Do not add unnecessary `require` statements which could cause LoadErrors on certain systems.
|
|
698
|
-
* Remember: Geocoder needs to run outside of Rails. Don't assume things like ActiveSupport are available.
|
|
699
|
-
* Be willing to accept criticism and work on improving your code; Geocoder is used by thousands of developers and care must be taken not to introduce bugs.
|
|
700
|
-
* Be aware that the pull request review process is not immediate, and is generally proportional to the size of the pull request.
|
|
701
|
-
* If your pull request is merged, please do not ask for an immediate release of the gem. There are many factors contributing to when releases occur (remember that they affect thousands of apps with Geocoder in their Gemfiles). If necessary, please install from the Github source until the next official release.
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
Copyright :copyright: 2009-2020 Alex Reisner, released under the MIT license.
|
|
800
|
+
Copyright :copyright: 2009-2021 Alex Reisner, released under the MIT license.
|