geocoder 1.4.9 → 1.6.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +60 -0
- data/LICENSE +1 -1
- data/README.md +350 -979
- data/bin/console +13 -0
- data/examples/autoexpire_cache_redis.rb +2 -0
- data/lib/easting_northing.rb +171 -0
- data/lib/generators/geocoder/config/templates/initializer.rb +1 -1
- data/lib/geocoder/cache.rb +4 -0
- data/lib/geocoder/calculations.rb +1 -1
- data/lib/geocoder/cli.rb +2 -2
- data/lib/geocoder/configuration.rb +3 -2
- data/lib/geocoder/configuration_hash.rb +4 -4
- data/lib/geocoder/exceptions.rb +1 -1
- data/lib/geocoder/ip_address.rb +16 -2
- data/lib/geocoder/lookup.rb +9 -5
- data/lib/geocoder/lookups/abstract_api.rb +46 -0
- data/lib/geocoder/lookups/amap.rb +7 -3
- data/lib/geocoder/lookups/baidu.rb +14 -10
- data/lib/geocoder/lookups/baidu_ip.rb +7 -36
- data/lib/geocoder/lookups/ban_data_gouv_fr.rb +18 -5
- data/lib/geocoder/lookups/base.rb +27 -4
- data/lib/geocoder/lookups/bing.rb +10 -13
- data/lib/geocoder/lookups/db_ip_com.rb +9 -6
- data/lib/geocoder/lookups/dstk.rb +4 -2
- data/lib/geocoder/lookups/esri.rb +44 -28
- data/lib/geocoder/lookups/freegeoip.rb +11 -7
- data/lib/geocoder/lookups/geocoder_ca.rb +4 -4
- data/lib/geocoder/lookups/geocodio.rb +5 -5
- data/lib/geocoder/lookups/geoportail_lu.rb +7 -7
- data/lib/geocoder/lookups/google.rb +15 -10
- data/lib/geocoder/lookups/google_places_details.rb +11 -17
- data/lib/geocoder/lookups/google_places_search.rb +30 -4
- data/lib/geocoder/lookups/google_premier.rb +14 -0
- data/lib/geocoder/lookups/here.rb +29 -23
- data/lib/geocoder/lookups/ip2location.rb +67 -0
- data/lib/geocoder/lookups/ipapi_com.rb +9 -13
- data/lib/geocoder/lookups/ipdata_co.rb +9 -4
- data/lib/geocoder/lookups/ipgeolocation.rb +51 -0
- data/lib/geocoder/lookups/ipinfo_io.rb +11 -29
- data/lib/geocoder/lookups/ipregistry.rb +68 -0
- data/lib/geocoder/lookups/ipstack.rb +11 -12
- data/lib/geocoder/lookups/latlon.rb +5 -6
- data/lib/geocoder/lookups/location_iq.rb +10 -4
- data/lib/geocoder/lookups/mapbox.rb +7 -6
- data/lib/geocoder/lookups/mapquest.rb +4 -5
- data/lib/geocoder/lookups/maxmind.rb +6 -6
- data/lib/geocoder/lookups/maxmind_geoip2.rb +8 -7
- data/lib/geocoder/lookups/maxmind_local.rb +7 -1
- data/lib/geocoder/lookups/nationaal_georegister_nl.rb +38 -0
- data/lib/geocoder/lookups/nominatim.rb +4 -4
- data/lib/geocoder/lookups/opencagedata.rb +6 -5
- data/lib/geocoder/lookups/osmnames.rb +57 -0
- data/lib/geocoder/lookups/pelias.rb +8 -9
- data/lib/geocoder/lookups/pickpoint.rb +9 -3
- data/lib/geocoder/lookups/pointpin.rb +10 -9
- data/lib/geocoder/lookups/postcode_anywhere_uk.rb +4 -5
- data/lib/geocoder/lookups/postcodes_io.rb +6 -3
- data/lib/geocoder/lookups/smarty_streets.rb +26 -11
- data/lib/geocoder/lookups/telize.rb +24 -4
- data/lib/geocoder/lookups/tencent.rb +59 -0
- data/lib/geocoder/lookups/test.rb +4 -0
- data/lib/geocoder/lookups/uk_ordnance_survey_names.rb +59 -0
- data/lib/geocoder/lookups/yandex.rb +7 -8
- data/lib/geocoder/query.rb +14 -0
- data/lib/geocoder/railtie.rb +1 -1
- data/lib/geocoder/results/abstract_api.rb +146 -0
- data/lib/geocoder/results/baidu.rb +10 -14
- data/lib/geocoder/results/ban_data_gouv_fr.rb +27 -2
- data/lib/geocoder/results/base.rb +13 -1
- data/lib/geocoder/results/bing.rb +1 -1
- data/lib/geocoder/results/db_ip_com.rb +1 -6
- data/lib/geocoder/results/freegeoip.rb +0 -5
- data/lib/geocoder/results/geocoder_ca.rb +3 -3
- data/lib/geocoder/results/geoip2.rb +0 -4
- data/lib/geocoder/results/geoportail_lu.rb +5 -3
- data/lib/geocoder/results/here.rb +4 -1
- data/lib/geocoder/results/ip2location.rb +22 -0
- data/lib/geocoder/results/ipdata_co.rb +0 -5
- data/lib/geocoder/results/ipgeolocation.rb +59 -0
- data/lib/geocoder/results/ipregistry.rb +304 -0
- data/lib/geocoder/results/maxmind.rb +0 -5
- data/lib/geocoder/results/maxmind_local.rb +0 -5
- data/lib/geocoder/results/nationaal_georegister_nl.rb +62 -0
- data/lib/geocoder/results/nominatim.rb +4 -0
- data/lib/geocoder/results/osmnames.rb +56 -0
- data/lib/geocoder/results/smarty_streets.rb +48 -18
- data/lib/geocoder/results/telize.rb +0 -5
- data/lib/geocoder/results/tencent.rb +72 -0
- data/lib/geocoder/results/test.rb +1 -1
- data/lib/geocoder/results/uk_ordnance_survey_names.rb +59 -0
- data/lib/geocoder/results/yandex.rb +217 -59
- data/lib/geocoder/sql.rb +4 -4
- data/lib/geocoder/stores/active_record.rb +1 -3
- data/lib/geocoder/util.rb +29 -0
- data/lib/geocoder/version.rb +1 -1
- data/lib/maxmind_database.rb +3 -3
- metadata +27 -23
- data/lib/geocoder/lookups/geocoder_us.rb +0 -43
- data/lib/geocoder/lookups/mapzen.rb +0 -15
- data/lib/geocoder/lookups/okf.rb +0 -44
- data/lib/geocoder/lookups/ovi.rb +0 -62
- data/lib/geocoder/results/geocoder_us.rb +0 -39
- data/lib/geocoder/results/mapzen.rb +0 -5
- data/lib/geocoder/results/okf.rb +0 -106
- data/lib/geocoder/results/ovi.rb +0 -71
- data/lib/hash_recursive_merge.rb +0 -74
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 17d8f44da8b584b65c5546df089f0c16e6d20045676d4a1fe3fef38cdf3c3c59
|
4
|
+
data.tar.gz: c527329b8d1f28a326092e3b3f6d2c9c143dacc2da2397fab5b4109ae7387f09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 918ed290277a1aa7e57a102ad06b0cf733e1908e9f31ead6cd59c5d08e5cc5b7d2f581a9958961781e013b535763c92701ad02f2bd8268374e339a36fd75eb25
|
7
|
+
data.tar.gz: 14416b59a9f34890d9a466aad87a883651f13f58a1f0a972c7d48370daed0f70923b9534b13e489673dbbc19543c82fb9017dd274529ce3a587bebe1f230649d
|
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,66 @@ Changelog
|
|
3
3
|
|
4
4
|
Major changes to Geocoder for each release. Please see the Git log for complete list of changes.
|
5
5
|
|
6
|
+
1.6.7 (2021 Apr 17)
|
7
|
+
-------------------
|
8
|
+
* Add support for Abstract API lookup (thanks github.com/randoum).
|
9
|
+
|
10
|
+
1.6.6 (2021 Mar 4)
|
11
|
+
-------------------
|
12
|
+
* Rescue from exception on cache read/write error. Issue warning instead.
|
13
|
+
|
14
|
+
1.6.5 (2021 Feb 10)
|
15
|
+
-------------------
|
16
|
+
* Fix backward coordinates bug in NationaalregisterNl lookup (thanks github.com/Marthyn).
|
17
|
+
* Allow removal of single stubs in test mode (thanks github.com/jmmastey).
|
18
|
+
* Improve results for :ban_data_gouv_fr lookup (thanks github.com/Intrepidd).
|
19
|
+
|
20
|
+
1.6.4 (2020 Oct 6)
|
21
|
+
-------------------
|
22
|
+
* Various updates in response to geocoding API changes.
|
23
|
+
* Refactor of Google Places Search lookup (thanks github.com/maximilientyc).
|
24
|
+
|
25
|
+
1.6.3 (2020 Apr 30)
|
26
|
+
-------------------
|
27
|
+
* Update URL for :telize lookup (thanks github.com/alexwalling).
|
28
|
+
* Fix bug parsing IPv6 with port (thanks github.com/gdomingu).
|
29
|
+
|
30
|
+
1.6.2 (2020 Mar 16)
|
31
|
+
-------------------
|
32
|
+
* Add support for :nationaal_georegister_nl lookup (thanks github.com/opensourceame).
|
33
|
+
* Add support for :uk_ordnance_survey_names lookup (thanks github.com/pezholio).
|
34
|
+
* Refactor and fix bugs in Yandex lookup (thanks github.com/iarie and stereodenis).
|
35
|
+
|
36
|
+
1.6.1 (2020 Jan 23)
|
37
|
+
-------------------
|
38
|
+
* Sanitize lat/lon values passed to within_bounding_box to prevent SQL injection.
|
39
|
+
|
40
|
+
1.6.0 (2020 Jan 6)
|
41
|
+
-------------------
|
42
|
+
* Drop support for Rails 3.x.
|
43
|
+
* Add support for :osmnames lookup (thanks github.com/zacviandier).
|
44
|
+
* Add support for :ipgeolocation IP lookup (thanks github.com/ahsannawaz111).
|
45
|
+
|
46
|
+
1.5.2 (2019 Oct 3)
|
47
|
+
-------------------
|
48
|
+
* Add support for :ipregistry lookup (thanks github.com/ipregistry).
|
49
|
+
* Various fixes for Yandex lookup.
|
50
|
+
|
51
|
+
1.5.1 (2019 Jan 23)
|
52
|
+
-------------------
|
53
|
+
* Add support for :tencent lookup (thanks github.com/Anders-E).
|
54
|
+
* Add support for :smarty_streets international API (thanks github.com/ankane).
|
55
|
+
* Remove :mapzen lookup.
|
56
|
+
|
57
|
+
1.5.0 (2018 Jul 31)
|
58
|
+
-------------------
|
59
|
+
* Drop support for Ruby <2.0.
|
60
|
+
* Change default street address lookup from :google to :nominatim.
|
61
|
+
* Cache keys no longer include API credentials. This means many entries in existing cache implementations will be invalidated.
|
62
|
+
* Test lookup fixtures should now return `coordinates` and NOT `latitude`/`longitude` attributes (see #1258). This may break some people's tests.
|
63
|
+
* Add support for :ip2location lookup (thanks github.com/ip2location).
|
64
|
+
* Remove :ovi and :okf lookups.
|
65
|
+
|
6
66
|
1.4.9 (2018 May 27)
|
7
67
|
-------------------
|
8
68
|
* Fix regression in :geoip2 lookup.
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,137 +1,178 @@
|
|
1
1
|
Geocoder
|
2
2
|
========
|
3
3
|
|
4
|
-
|
4
|
+
**Complete geocoding solution for Ruby.**
|
5
5
|
|
6
|
-
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/geocoder.svg)](http://badge.fury.io/rb/geocoder)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/alexreisner/geocoder/badges/gpa.svg)](https://codeclimate.com/github/alexreisner/geocoder)
|
8
|
+
[![Build Status](https://travis-ci.org/alexreisner/geocoder.svg?branch=master)](https://travis-ci.org/alexreisner/geocoder)
|
7
9
|
|
10
|
+
Key features:
|
8
11
|
|
9
|
-
|
10
|
-
|
12
|
+
* Forward and reverse geocoding.
|
13
|
+
* IP address geocoding.
|
14
|
+
* Connects to more than 40 APIs worldwide.
|
15
|
+
* Performance-enhancing features like caching.
|
16
|
+
* Integrates with ActiveRecord and Mongoid.
|
17
|
+
* Basic geospatial queries: search within radius (or rectangle, or ring).
|
11
18
|
|
12
|
-
|
13
|
-
* Supports multiple databases: MySQL, PostgreSQL, SQLite, and MongoDB (1.7.0 and higher).
|
14
|
-
* Supports Rails 3, 4, and 5. If you need to use it with Rails 2 please see the `rails2` branch (no longer maintained, limited feature set).
|
15
|
-
* Works very well outside of Rails, you just need to install either the `json` (for MRI) or `json_pure` (for JRuby) gem.
|
19
|
+
Compatibility:
|
16
20
|
|
21
|
+
* Ruby versions: 2.x, and JRuby.
|
22
|
+
* Databases: MySQL, PostgreSQL, SQLite, and MongoDB.
|
23
|
+
* Rails: 4, 5, and 6.
|
24
|
+
* Works outside of Rails with the `json` (for MRI) or `json_pure` (for JRuby) gem.
|
17
25
|
|
18
|
-
Note on Rails 4.1 and Greater
|
19
|
-
-----------------------------
|
20
|
-
|
21
|
-
Due to [a change in ActiveRecord's `count` method](https://github.com/rails/rails/pull/10710) you will need to use `count(:all)` to explicitly count all columns ("*") when using a `near` scope. Using `near` and calling `count` with no argument will cause exceptions in many cases.
|
22
26
|
|
27
|
+
Table of Contents
|
28
|
+
-----------------
|
23
29
|
|
24
|
-
|
25
|
-
------------
|
30
|
+
Basic Features:
|
26
31
|
|
27
|
-
|
32
|
+
* [Basic Search](#basic-search)
|
33
|
+
* [Geocoding Objects](#geocoding-objects)
|
34
|
+
* [Geospatial Database Queries](#geospatial-database-queries)
|
35
|
+
* [Geocoding HTTP Requests](#geocoding-http-requests)
|
36
|
+
* [Geocoding Service ("Lookup") Configuration](#geocoding-service-lookup-configuration)
|
28
37
|
|
29
|
-
|
38
|
+
Advanced Features:
|
30
39
|
|
31
|
-
|
40
|
+
* [Performance and Optimization](#performance-and-optimization)
|
41
|
+
* [Advanced Model Configuration](#advanced-model-configuration)
|
42
|
+
* [Advanced Database Queries](#advanced-database-queries)
|
43
|
+
* [Geospatial Calculations](#geospatial-calculations)
|
44
|
+
* [Batch Geocoding](#batch-geocoding)
|
45
|
+
* [Testing](#testing)
|
46
|
+
* [Error Handling](#error-handling)
|
47
|
+
* [Command Line Interface](#command-line-interface)
|
32
48
|
|
33
|
-
|
49
|
+
The Rest:
|
34
50
|
|
35
|
-
|
51
|
+
* [Technical Discussions](#technical-discussions)
|
52
|
+
* [Troubleshooting](#troubleshooting)
|
53
|
+
* [Known Issues](#known-issues)
|
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)
|
36
56
|
|
37
|
-
|
57
|
+
See Also:
|
38
58
|
|
59
|
+
* [Guide to Geocoding APIs](https://github.com/alexreisner/geocoder/blob/master/README_API_GUIDE.md) (formerly part of this README)
|
39
60
|
|
40
|
-
Object Geocoding
|
41
|
-
----------------
|
42
61
|
|
43
|
-
|
62
|
+
Basic Search
|
63
|
+
------------
|
44
64
|
|
45
|
-
|
65
|
+
In its simplest form, Geocoder takes an address and searches for its latitude/longitude coordinates:
|
46
66
|
|
47
|
-
|
48
|
-
|
67
|
+
results = Geocoder.search("Paris")
|
68
|
+
results.first.coordinates
|
69
|
+
=> [48.856614, 2.3522219] # latitude and longitude
|
49
70
|
|
50
|
-
|
71
|
+
The reverse is possible too. Given coordinates, it finds an address:
|
51
72
|
|
52
|
-
|
73
|
+
results = Geocoder.search([48.856614, 2.3522219])
|
74
|
+
results.first.address
|
75
|
+
=> "Hôtel de Ville, 75004 Paris, France"
|
53
76
|
|
54
|
-
|
55
|
-
after_validation :geocode # auto-fetch coordinates
|
77
|
+
You can also look up the location of an IP addresses:
|
56
78
|
|
57
|
-
|
79
|
+
results = Geocoder.search("172.56.21.89")
|
80
|
+
results.first.coordinates
|
81
|
+
=> [30.267153, -97.7430608]
|
82
|
+
results.first.country
|
83
|
+
=> "United States"
|
58
84
|
|
59
|
-
|
60
|
-
after_validation :reverse_geocode # auto-fetch address
|
85
|
+
**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).
|
61
86
|
|
62
|
-
### Mongoid
|
63
87
|
|
64
|
-
|
88
|
+
Geocoding Objects
|
89
|
+
-----------------
|
65
90
|
|
66
|
-
|
91
|
+
To automatically geocode your objects:
|
67
92
|
|
68
|
-
|
93
|
+
**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:
|
69
94
|
|
70
|
-
|
95
|
+
def address
|
96
|
+
[street, city, state, country].compact.join(', ')
|
97
|
+
end
|
71
98
|
|
72
|
-
|
99
|
+
**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.)
|
73
100
|
|
74
|
-
|
101
|
+
**3.** In your model, tell geocoder where to find the object's address:
|
75
102
|
|
76
|
-
|
77
|
-
geocoded_by :address # can also be an IP address
|
78
|
-
after_validation :geocode # auto-fetch coordinates
|
103
|
+
geocoded_by :address
|
79
104
|
|
80
|
-
|
105
|
+
This adds a `geocode` method which you can invoke via callback:
|
81
106
|
|
82
|
-
|
83
|
-
reverse_geocoded_by :coordinates
|
84
|
-
after_validation :reverse_geocode # auto-fetch address
|
107
|
+
after_validation :geocode
|
85
108
|
|
86
|
-
|
109
|
+
Reverse geocoding (given lat/lon coordinates, find an address) is similar:
|
87
110
|
|
88
|
-
|
111
|
+
reverse_geocoded_by :latitude, :longitude
|
112
|
+
after_validation :reverse_geocode
|
89
113
|
|
90
|
-
|
114
|
+
With any geocoded objects, you can do the following:
|
91
115
|
|
92
|
-
|
116
|
+
obj.distance_to([43.9,-98.6]) # distance from obj to point
|
117
|
+
obj.bearing_to([43.9,-98.6]) # bearing from obj to point
|
118
|
+
obj.bearing_from(obj2) # bearing from obj2 to obj
|
93
119
|
|
94
|
-
|
120
|
+
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.
|
95
121
|
|
96
|
-
###
|
122
|
+
### One More Thing for MongoDB!
|
97
123
|
|
98
|
-
|
124
|
+
Before you can call `geocoded_by` you'll need to include the necessary module using one of the following:
|
99
125
|
|
100
126
|
include Geocoder::Model::Mongoid
|
101
|
-
|
127
|
+
include Geocoder::Model::MongoMapper
|
102
128
|
|
103
|
-
###
|
129
|
+
### Latitude/Longitude Order in MongoDB
|
104
130
|
|
105
|
-
|
131
|
+
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:
|
106
132
|
|
107
|
-
|
133
|
+
obj.to_coordinates # => [37.7941013, -122.3951096] # [lat, lon]
|
108
134
|
|
109
|
-
|
135
|
+
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:
|
110
136
|
|
111
|
-
|
137
|
+
obj.coordinates # => [-122.3951096, 37.7941013] # [lon, lat]
|
112
138
|
|
113
|
-
|
139
|
+
So, be careful.
|
114
140
|
|
115
|
-
|
141
|
+
### Use Outside of Rails
|
116
142
|
|
117
|
-
To
|
143
|
+
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:
|
118
144
|
|
119
|
-
|
145
|
+
extend Geocoder::Model::ActiveRecord
|
120
146
|
|
121
|
-
### Avoiding Unnecessary API Requests
|
122
147
|
|
123
|
-
|
148
|
+
Geospatial Database Queries
|
149
|
+
---------------------------
|
124
150
|
|
125
|
-
|
126
|
-
* the address has been changed since last save (or it has never been saved)
|
151
|
+
### For ActiveRecord models:
|
127
152
|
|
128
|
-
|
153
|
+
To find objects by location, use the following scopes:
|
129
154
|
|
130
|
-
|
155
|
+
Venue.near('Omaha, NE, US') # venues within 20 miles of Omaha
|
156
|
+
Venue.near([40.71, -100.23], 50) # venues within 50 miles of a point
|
157
|
+
Venue.near([40.71, -100.23], 50, units: :km) # venues within 50 kilometres of a point
|
158
|
+
Venue.geocoded # venues with coordinates
|
159
|
+
Venue.not_geocoded # venues without coordinates
|
131
160
|
|
161
|
+
With geocoded objects you can do things like this:
|
132
162
|
|
133
|
-
|
134
|
-
|
163
|
+
if obj.geocoded?
|
164
|
+
obj.nearbys(30) # other objects within 30 miles
|
165
|
+
obj.distance_from([40.714,-100.234]) # distance from arbitrary point to object
|
166
|
+
obj.bearing_to("Paris, France") # direction from object to arbitrary point
|
167
|
+
end
|
168
|
+
|
169
|
+
### For MongoDB-backed models:
|
170
|
+
|
171
|
+
Please do not use Geocoder's `near` method. Instead use MongoDB's built-in [geospatial query language](https://docs.mongodb.org/manual/reference/command/geoNear/), which is faster. Mongoid also provides [a DSL](http://mongoid.github.io/en/mongoid/docs/querying.html#geo_near) for geospatial queries.
|
172
|
+
|
173
|
+
|
174
|
+
Geocoding HTTP Requests
|
175
|
+
-----------------------
|
135
176
|
|
136
177
|
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:
|
137
178
|
|
@@ -140,164 +181,181 @@ Geocoder adds `location` and `safe_location` methods to the standard `Rack::Requ
|
|
140
181
|
|
141
182
|
**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).
|
142
183
|
|
143
|
-
Note that these methods will usually return `nil` in
|
184
|
+
Note that these methods will usually return `nil` in test and development environments because things like "localhost" and "0.0.0.0" are not geocodable IP addresses.
|
144
185
|
|
145
|
-
See _Advanced Geocoding_ below for more information about `Geocoder::Result` objects.
|
146
186
|
|
187
|
+
Geocoding Service ("Lookup") Configuration
|
188
|
+
------------------------------------------
|
147
189
|
|
148
|
-
|
149
|
-
-------------------------------
|
190
|
+
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).
|
150
191
|
|
151
|
-
|
192
|
+
To create a Rails initializer with sample configuration:
|
152
193
|
|
153
|
-
|
194
|
+
rails generate geocoder:config
|
154
195
|
|
155
|
-
|
196
|
+
Some common options are:
|
156
197
|
|
157
|
-
|
198
|
+
# config/initializers/geocoder.rb
|
199
|
+
Geocoder.configure(
|
158
200
|
|
159
|
-
|
160
|
-
|
161
|
-
Venue.near([40.71, -100.23], 50, :units => :km)
|
162
|
-
# venues within 50 kilometres of a point
|
163
|
-
Venue.geocoded # venues with coordinates
|
164
|
-
Venue.not_geocoded # venues without coordinates
|
201
|
+
# street address geocoding service (default :nominatim)
|
202
|
+
lookup: :yandex,
|
165
203
|
|
166
|
-
|
204
|
+
# IP address geocoding service (default :ipinfo_io)
|
205
|
+
ip_lookup: :maxmind,
|
167
206
|
|
168
|
-
|
207
|
+
# to use an API key:
|
208
|
+
api_key: "...",
|
169
209
|
|
170
|
-
|
210
|
+
# geocoding service request timeout, in seconds (default 3):
|
211
|
+
timeout: 5,
|
171
212
|
|
172
|
-
|
173
|
-
|
174
|
-
obj.distance_from([40.714,-100.234]) # distance from arbitrary point to object
|
175
|
-
obj.bearing_to("Paris, France") # direction from object to arbitrary point
|
176
|
-
end
|
213
|
+
# set default units to kilometers:
|
214
|
+
units: :km,
|
177
215
|
|
178
|
-
|
216
|
+
# caching (see Caching section below for details):
|
217
|
+
cache: Redis.new,
|
218
|
+
cache_prefix: "..."
|
179
219
|
|
180
|
-
|
181
|
-
Geocoder.coordinates("25 Main St, Cooperstown, NY")
|
182
|
-
=> [42.700149, -74.922767]
|
220
|
+
)
|
183
221
|
|
184
|
-
|
185
|
-
Geocoder::Calculations.distance_between([47.858205,2.294359], [40.748433,-73.985655])
|
186
|
-
=> 3619.77359999382 # in configured units (default miles)
|
222
|
+
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:
|
187
223
|
|
188
|
-
#
|
189
|
-
Geocoder
|
190
|
-
=> [35.14968, -90.048929]
|
224
|
+
# with Google:
|
225
|
+
Geocoder.search("Middletown", bounds: [[40.6,-77.9], [39.9,-75.9]])
|
191
226
|
|
192
|
-
Please see the code for
|
227
|
+
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:
|
193
228
|
|
229
|
+
# Nominatim's `countrycodes` parameter:
|
230
|
+
Geocoder.search("Rome", params: {countrycodes: "us,ca"})
|
194
231
|
|
195
|
-
|
196
|
-
|
232
|
+
# Google's `region` parameter:
|
233
|
+
Geocoder.search("Rome", params: {region: "..."})
|
197
234
|
|
198
|
-
|
235
|
+
### Configuring Multiple Services
|
199
236
|
|
200
|
-
|
201
|
-
* `obj.bearing` - direction from the search point to this object
|
237
|
+
You can configure multiple geocoding services at once by using the service's name as a key for a sub-configuration hash, like this:
|
202
238
|
|
203
|
-
|
239
|
+
Geocoder.configure(
|
204
240
|
|
205
|
-
|
206
|
-
|
207
|
-
* `90` - due east
|
208
|
-
* `270` - due west
|
209
|
-
* `230.1` - southwest
|
210
|
-
* `359.9` - almost due north
|
241
|
+
timeout: 2,
|
242
|
+
cache: Redis.new,
|
211
243
|
|
212
|
-
|
244
|
+
yandex: {
|
245
|
+
api_key: "...",
|
246
|
+
timeout: 5
|
247
|
+
},
|
213
248
|
|
214
|
-
|
215
|
-
|
216
|
-
|
249
|
+
baidu: {
|
250
|
+
api_key: "..."
|
251
|
+
},
|
217
252
|
|
218
|
-
|
253
|
+
maxmind: {
|
254
|
+
api_key: "...",
|
255
|
+
service: :omni
|
256
|
+
}
|
219
257
|
|
220
|
-
|
258
|
+
)
|
221
259
|
|
222
|
-
|
223
|
-
obj.bearing_to([43.9,-98.6]) # bearing from obj to point
|
224
|
-
obj.bearing_from(obj2) # bearing from obj2 to obj
|
260
|
+
Lookup-specific settings override global settings so, in this example, the timeout for all lookups is 2 seconds, except for Yandex which is 5.
|
225
261
|
|
226
|
-
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).
|
227
262
|
|
263
|
+
Performance and Optimization
|
264
|
+
----------------------------
|
228
265
|
|
229
|
-
|
230
|
-
-------------------
|
266
|
+
### Database Indices
|
231
267
|
|
232
|
-
|
268
|
+
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:
|
233
269
|
|
234
|
-
|
235
|
-
geocoded_by :address, :coordinates => :coords # MongoDB
|
270
|
+
add_index :table, [:latitude, :longitude]
|
236
271
|
|
237
|
-
|
272
|
+
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:
|
238
273
|
|
239
|
-
|
240
|
-
|
241
|
-
* "Paris, TX, US"
|
274
|
+
include Geocoder::Model::Mongoid
|
275
|
+
geocoded_by :address, skip_index: true
|
242
276
|
|
243
|
-
|
277
|
+
### Avoiding Unnecessary API Requests
|
244
278
|
|
245
|
-
|
279
|
+
Geocoding only needs to be performed under certain conditions. To avoid unnecessary work (and quota usage) you will probably want to geocode an object only when:
|
246
280
|
|
247
|
-
|
248
|
-
|
249
|
-
end
|
281
|
+
* an address is present
|
282
|
+
* the address has been changed since last save (or it has never been saved)
|
250
283
|
|
251
|
-
|
284
|
+
The exact code will vary depending on the method you use for your geocodable string, but it would be something like this:
|
252
285
|
|
253
|
-
|
254
|
-
reverse_geocoded_by :coordinates, :address => :loc # MongoDB
|
286
|
+
after_validation :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }
|
255
287
|
|
256
|
-
|
288
|
+
### Caching
|
257
289
|
|
258
|
-
|
290
|
+
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:
|
259
291
|
|
260
|
-
|
292
|
+
Geocoder.configure(cache: Redis.new)
|
261
293
|
|
262
|
-
|
294
|
+
This example uses Redis, but the cache store can be any object that supports these methods:
|
263
295
|
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
:baidu
|
269
|
-
else
|
270
|
-
:google
|
271
|
-
end
|
272
|
-
end
|
296
|
+
* `store#[](key)` or `#get` or `#read` - retrieves a value
|
297
|
+
* `store#[]=(key, value)` or `#set` or `#write` - stores a value
|
298
|
+
* `store#del(url)` - deletes a value
|
299
|
+
* `store#keys` - (Optional) Returns array of keys. Used if you wish to expire the entire cache (see below).
|
273
300
|
|
301
|
+
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).
|
274
302
|
|
275
|
-
|
276
|
-
-----------------
|
303
|
+
You can also set a custom prefix to be used for cache keys:
|
277
304
|
|
278
|
-
|
305
|
+
Geocoder.configure(cache_prefix: "...")
|
279
306
|
|
280
|
-
|
281
|
-
center_point = [40.71, 100.23]
|
282
|
-
box = Geocoder::Calculations.bounding_box(center_point, distance)
|
283
|
-
Venue.within_bounding_box(box)
|
307
|
+
By default the prefix is `geocoder:`
|
284
308
|
|
285
|
-
|
309
|
+
If you need to expire cached content:
|
286
310
|
|
287
|
-
|
288
|
-
|
311
|
+
Geocoder::Lookup.get(Geocoder.config[:lookup]).cache.expire(:all) # expire cached results for current Lookup
|
312
|
+
Geocoder::Lookup.get(:nominatim).cache.expire("http://...") # expire cached result for a specific URL
|
313
|
+
Geocoder::Lookup.get(:nominatim).cache.expire(:all) # expire cached results for Google Lookup
|
314
|
+
# expire all cached results for all Lookups.
|
315
|
+
# Be aware that this methods spawns a new Lookup object for each Service
|
316
|
+
Geocoder::Lookup.all_services.each{|service| Geocoder::Lookup.get(service).cache.expire(:all)}
|
289
317
|
|
290
|
-
|
318
|
+
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.
|
291
319
|
|
292
|
-
|
320
|
+
For an example of a cache store with URL expiry, please see examples/autoexpire_cache.rb
|
293
321
|
|
294
|
-
|
322
|
+
_Before you implement caching in your app please be sure that doing so does not violate the Terms of Service for your geocoding service._
|
323
|
+
|
324
|
+
|
325
|
+
Advanced Model Configuration
|
326
|
+
----------------------------
|
295
327
|
|
328
|
+
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
|
+
|
330
|
+
geocoded_by :address, latitude: :lat, longitude: :lon # ActiveRecord
|
331
|
+
geocoded_by :address, coordinates: :coords # MongoDB
|
332
|
+
|
333
|
+
For reverse geocoding, you can specify the attribute where the address will be stored. For example:
|
334
|
+
|
335
|
+
reverse_geocoded_by :latitude, :longitude, address: :loc # ActiveRecord
|
336
|
+
reverse_geocoded_by :coordinates, address: :street_address # MongoDB
|
337
|
+
|
338
|
+
To specify geocoding parameters in your model:
|
339
|
+
|
340
|
+
geocoded_by :address, params: {region: "..."}
|
341
|
+
|
342
|
+
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
|
+
|
344
|
+
geocoded_by :address, lookup: lambda{ |obj| obj.geocoder_lookup }
|
345
|
+
|
346
|
+
def geocoder_lookup
|
347
|
+
if country_code == "RU"
|
348
|
+
:yandex
|
349
|
+
elsif country_code == "CN"
|
350
|
+
:baidu
|
351
|
+
else
|
352
|
+
:nominatim
|
353
|
+
end
|
354
|
+
end
|
296
355
|
|
297
|
-
|
298
|
-
------------------
|
356
|
+
### Custom Result Handling
|
299
357
|
|
300
|
-
So far we have
|
358
|
+
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:
|
301
359
|
|
302
360
|
reverse_geocoded_by :latitude, :longitude do |obj,results|
|
303
361
|
if geo = results.first
|
@@ -321,752 +379,130 @@ Every `Geocoder::Result` object, `result`, provides the following data:
|
|
321
379
|
* `result.country` - string
|
322
380
|
* `result.country_code` - string
|
323
381
|
|
324
|
-
|
325
|
-
|
382
|
+
Most APIs return other data in addition to these globally-supported attributes. To directly access the full response, call the `#data` method of any Geocoder::Result object. See the [API Guide](https://github.com/alexreisner/geocoder/blob/master/README_API_GUIDE.md) for links to documentation for all geocoding services.
|
326
383
|
|
327
|
-
Geocoding
|
328
|
-
------------------------------------------
|
329
|
-
|
330
|
-
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 listing and comparison below for details on specific geocoding services (not all settings are supported by all services).
|
331
|
-
|
332
|
-
To create a Rails initializer with an example configuration:
|
333
|
-
|
334
|
-
rails generate geocoder:config
|
384
|
+
### Forward and Reverse Geocoding in the Same Model
|
335
385
|
|
336
|
-
|
337
|
-
|
338
|
-
# config/initializers/geocoder.rb
|
339
|
-
Geocoder.configure(
|
340
|
-
|
341
|
-
# geocoding service (see below for supported options):
|
342
|
-
:lookup => :yandex,
|
343
|
-
|
344
|
-
# IP address geocoding service (see below for supported options):
|
345
|
-
:ip_lookup => :maxmind,
|
346
|
-
|
347
|
-
# to use an API key:
|
348
|
-
:api_key => "...",
|
349
|
-
|
350
|
-
# geocoding service request timeout, in seconds (default 3):
|
351
|
-
:timeout => 5,
|
352
|
-
|
353
|
-
# set default units to kilometers:
|
354
|
-
:units => :km,
|
386
|
+
You can apply both forward and reverse geocoding to the same model (i.e. users can supply an address or coordinates and Geocoder fills in whatever's missing) but you'll need to provide two different address methods:
|
355
387
|
|
356
|
-
|
357
|
-
|
358
|
-
:cache_prefix => "..."
|
359
|
-
|
360
|
-
)
|
361
|
-
|
362
|
-
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 configuration options, some of which are directly supported by Geocoder. For example, to specify a value for Google's `bounds` parameter:
|
363
|
-
|
364
|
-
# with Google:
|
365
|
-
Geocoder.search("Paris", :bounds => [[32.1,-95.9], [33.9,-94.3]])
|
366
|
-
|
367
|
-
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, by which you can pass arbitrary parameters to any geocoding service. For example, to use Nominatim's `countrycodes` parameter:
|
368
|
-
|
369
|
-
# with Nominatim:
|
370
|
-
Geocoder.search("Paris", :params => {:countrycodes => "gb,de,fr,es,us"})
|
371
|
-
|
372
|
-
Or, to search within a particular region with Google:
|
373
|
-
|
374
|
-
Geocoder.search("...", :params => {:region => "..."})
|
388
|
+
* one for storing the fetched address (when reverse geocoding)
|
389
|
+
* one for providing an address to use when fetching coordinates (forward geocoding)
|
375
390
|
|
376
|
-
|
391
|
+
For example:
|
377
392
|
|
378
393
|
class Venue
|
379
394
|
|
380
395
|
# build an address from street, city, and state attributes
|
381
|
-
geocoded_by :address_from_components
|
396
|
+
geocoded_by :address_from_components
|
382
397
|
|
383
398
|
# store the fetched address in the full_address attribute
|
384
|
-
reverse_geocoded_by :latitude, :longitude, :
|
399
|
+
reverse_geocoded_by :latitude, :longitude, address: :full_address
|
385
400
|
end
|
386
401
|
|
402
|
+
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:
|
387
403
|
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
:timeout => 2,
|
395
|
-
:cache => Redis.new,
|
396
|
-
|
397
|
-
:yandex => {
|
398
|
-
:api_key => "...",
|
399
|
-
:timeout => 5
|
400
|
-
},
|
401
|
-
|
402
|
-
:baidu => {
|
403
|
-
:api_key => "..."
|
404
|
-
},
|
405
|
-
|
406
|
-
:maxmind => {
|
407
|
-
:api_key => "...",
|
408
|
-
:service => :omni
|
409
|
-
}
|
410
|
-
|
411
|
-
)
|
412
|
-
|
413
|
-
The above combines global and service-specific options and could be useful if you specify different geocoding services for different models or under different conditions. Lookup-specific settings override global settings. In the above example, the timeout for all lookups would be 2 seconds, except for Yandex which would be 5.
|
414
|
-
|
415
|
-
|
416
|
-
### Street Address Services
|
417
|
-
|
418
|
-
The following is a comparison of the supported geocoding APIs. The "Limitations" listed for each are a very brief and incomplete summary of some special limitations beyond basic data source attribution. Please read the official Terms of Service for a service before using it.
|
419
|
-
|
420
|
-
#### Google (`:google`)
|
421
|
-
|
422
|
-
* **API key**: optional, but quota is higher if key is used (use of key requires HTTPS so be sure to set: `:use_https => true` in `Geocoder.configure`)
|
423
|
-
* **Key signup**: https://console.developers.google.com/flows/enableapi?apiid=geocoding_backend&keyType=SERVER_SIDE
|
424
|
-
* **Quota**: 2,500 requests/24 hrs, 5 requests/second
|
425
|
-
* **Region**: world
|
426
|
-
* **SSL support**: yes (required if key is used)
|
427
|
-
* **Languages**: see https://developers.google.com/maps/faq#languagesupport
|
428
|
-
* **Extra params**:
|
429
|
-
* `:bounds` - pass SW and NE coordinates as an array of two arrays to bias results towards a viewport
|
430
|
-
* `:google_place_id` - pass `true` if search query is a Google Place ID
|
431
|
-
* **Documentation**: https://developers.google.com/maps/documentation/geocoding/intro
|
432
|
-
* **Terms of Service**: http://code.google.com/apis/maps/terms.html#section_10_12
|
433
|
-
* **Limitations**: "You must not use or display the Content without a corresponding Google map, unless you are explicitly permitted to do so in the Maps APIs Documentation, or through written permission from Google." "You must not pre-fetch, cache, or store any Content, except that you may store: (i) limited amounts of Content for the purpose of improving the performance of your Maps API Implementation..."
|
434
|
-
|
435
|
-
#### Google Maps API for Work (`:google_premier`)
|
436
|
-
|
437
|
-
Similar to `:google`, with the following differences:
|
438
|
-
|
439
|
-
* **API key**: required, plus client and channel (set `Geocoder.configure(:lookup => :google_premier, :api_key => [key, client, channel])`)
|
440
|
-
* **Key signup**: https://developers.google.com/maps/documentation/business/
|
441
|
-
* **Quota**: 100,000 requests/24 hrs, 10 requests/second
|
442
|
-
|
443
|
-
#### Google Places Details (`:google_places_details`)
|
444
|
-
|
445
|
-
The [Google Places Details API](https://developers.google.com/places/documentation/details) is not, strictly speaking, a geocoding service. It accepts a Google `place_id` and returns address information, ratings and reviews. A `place_id` can be obtained from the Google Places Search lookup (`:google_places_search`) and should be passed to Geocoder as the first search argument: `Geocoder.search("ChIJhRwB-yFawokR5Phil-QQ3zM", lookup: :google_places_details)`.
|
446
|
-
|
447
|
-
* **API key**: required
|
448
|
-
* **Key signup**: https://code.google.com/apis/console/
|
449
|
-
* **Quota**: 1,000 request/day, 100,000 after credit card authentication
|
450
|
-
* **Region**: world
|
451
|
-
* **SSL support**: yes
|
452
|
-
* **Languages**: ar, eu, bg, bn, ca, cs, da, de, el, en, en-AU, en-GB, es, eu, fa, fi, fil, fr, gl, gu, hi, hr, hu, id, it, iw, ja, kn, ko, lt, lv, ml, mr, nl, no, pl, pt, pt-BR, pt-PT, ro, ru, sk, sl, sr, sv, tl, ta, te, th, tr, uk, vi, zh-CN, zh-TW (see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1)
|
453
|
-
* **Documentation**: https://developers.google.com/places/documentation/details
|
454
|
-
* **Terms of Service**: https://developers.google.com/places/policies
|
455
|
-
* **Limitations**: "If your application displays Places API data on a page or view that does not also display a Google Map, you must show a "Powered by Google" logo with that data."
|
456
|
-
|
457
|
-
#### Google Places Search (`:google_places_search`)
|
458
|
-
|
459
|
-
The [Google Places Search API](https://developers.google.com/places/web-service/search) is the geocoding service of Google Places API. It returns very limited location data, but it also returns a `place_id` which can be used with Google Place Details to get more detailed information. For a comparison between this and the regular Google Geocoding API, see https://maps-apis.googleblog.com/2016/11/address-geocoding-in-google-maps-apis.html
|
460
|
-
|
461
|
-
* Same specifications as Google Places Details (see above).
|
462
|
-
|
463
|
-
#### Bing (`:bing`)
|
464
|
-
|
465
|
-
* **API key**: required (set `Geocoder.configure(:lookup => :bing, :api_key => key)`)
|
466
|
-
* **Key signup**: https://www.microsoft.com/maps/create-a-bing-maps-key.aspx
|
467
|
-
* **Quota**: 50,0000 requests/day (Windows app), 125,000 requests/year (non-Windows app)
|
468
|
-
* **Region**: world
|
469
|
-
* **SSL support**: no
|
470
|
-
* **Languages**: ?
|
471
|
-
* **Documentation**: http://msdn.microsoft.com/en-us/library/ff701715.aspx
|
472
|
-
* **Terms of Service**: http://www.microsoft.com/maps/product/terms.html
|
473
|
-
* **Limitations**: No country codes or state names. Must be used on "public-facing, non-password protected web sites," "in conjunction with Bing Maps or an application that integrates Bing Maps."
|
474
|
-
|
475
|
-
#### Nominatim (`:nominatim`)
|
476
|
-
|
477
|
-
* **API key**: none
|
478
|
-
* **Quota**: 1 request/second
|
479
|
-
* **Region**: world
|
480
|
-
* **SSL support**: yes
|
481
|
-
* **Languages**: ?
|
482
|
-
* **Documentation**: http://wiki.openstreetmap.org/wiki/Nominatim
|
483
|
-
* **Terms of Service**: http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy
|
484
|
-
* **Limitations**: Please limit request rate to 1 per second and include your contact information in User-Agent headers (eg: `Geocoder.configure(:http_headers => { "User-Agent" => "your contact info" })`). [Data licensed under Open Database License (ODbL) (you must provide attribution).](http://www.openstreetmap.org/copyright)
|
485
|
-
|
486
|
-
#### PickPoint (`:pickpoint`)
|
487
|
-
|
488
|
-
* **API key**: required
|
489
|
-
* **Key signup**: [https://pickpoint.io](https://pickpoint.io)
|
490
|
-
* **Quota**: 2500 requests / day for free non-commercial usage, commercial plans are [available](https://pickpoint.io/#pricing). No rate limit.
|
491
|
-
* **Region**: world
|
492
|
-
* **SSL support**: required
|
493
|
-
* **Languages**: worldwide
|
494
|
-
* **Documentation**: [https://pickpoint.io/api-reference](https://pickpoint.io/api-reference)
|
495
|
-
* **Limitations**: [Data licensed under Open Database License (ODbL) (you must provide attribution).](http://www.openstreetmap.org/copyright)
|
496
|
-
|
497
|
-
|
498
|
-
#### LocationIQ (`:location_iq`)
|
499
|
-
|
500
|
-
* **API key**: required
|
501
|
-
* **Quota**: 60 requests/minute (2 req/sec, 10k req/day), then [ability to purchase more](http://locationiq.org/#pricing)
|
502
|
-
* **Region**: world
|
503
|
-
* **SSL support**: yes
|
504
|
-
* **Languages**: ?
|
505
|
-
* **Documentation**: https://locationiq.org/#docs
|
506
|
-
* **Terms of Service**: https://unwiredlabs.com/tos
|
507
|
-
* **Limitations**: [Data licensed under Open Database License (ODbL) (you must provide attribution).](https://www.openstreetmap.org/copyright)
|
508
|
-
|
509
|
-
#### OpenCageData (`:opencagedata`)
|
510
|
-
|
511
|
-
* **API key**: required
|
512
|
-
* **Key signup**: http://geocoder.opencagedata.com
|
513
|
-
* **Quota**: 2500 requests / day, then [ability to purchase more](https://geocoder.opencagedata.com/pricing)
|
514
|
-
* **Region**: world
|
515
|
-
* **SSL support**: yes
|
516
|
-
* **Languages**: worldwide
|
517
|
-
* **Documentation**: http://geocoder.opencagedata.com/api.html
|
518
|
-
* **Limitations**: [Data licensed under Open Database License (ODbL) (you must provide attribution).](http://www.openstreetmap.org/copyright)
|
519
|
-
|
520
|
-
#### Yandex (`:yandex`)
|
521
|
-
|
522
|
-
* **API key**: optional, but without it lookup is territorially limited
|
523
|
-
* **Quota**: 25000 requests / day
|
524
|
-
* **Region**: world with API key. Otherwise restricted to Russia, Ukraine, Belarus, Kazakhstan, Georgia, Abkhazia, South Ossetia, Armenia, Azerbaijan, Moldova, Turkmenistan, Tajikistan, Uzbekistan, Kyrgyzstan and Turkey
|
525
|
-
* **SSL support**: HTTPS only
|
526
|
-
* **Languages**: Russian, Belarusian, Ukrainian, English, Turkish (only for maps of Turkey)
|
527
|
-
* **Documentation**: http://api.yandex.com.tr/maps/doc/intro/concepts/intro.xml
|
528
|
-
* **Terms of Service**: http://api.yandex.com.tr/maps/doc/intro/concepts/intro.xml#rules
|
529
|
-
* **Limitations**: ?
|
530
|
-
|
531
|
-
#### Geocoder.ca (`:geocoder_ca`)
|
532
|
-
|
533
|
-
* **API key**: none
|
534
|
-
* **Quota**: ?
|
535
|
-
* **Region**: US and Canada
|
536
|
-
* **SSL support**: no
|
537
|
-
* **Languages**: English
|
538
|
-
* **Documentation**: ?
|
539
|
-
* **Terms of Service**: http://geocoder.ca/?terms=1
|
540
|
-
* **Limitations**: "Under no circumstances can our data be re-distributed or re-sold by anyone to other parties without our written permission."
|
541
|
-
|
542
|
-
#### Mapbox (`:mapbox`)
|
543
|
-
|
544
|
-
* **API key**: required
|
545
|
-
* **Dataset**: Uses `mapbox.places` dataset by default. Specify the `mapbox.places-permanent` dataset by setting: `Geocoder.configure(:mapbox => {:dataset => "mapbox.places-permanent"})`
|
546
|
-
* **Key signup**: https://www.mapbox.com/pricing/
|
547
|
-
* **Quota**: depends on plan
|
548
|
-
* **Region**: complete coverage of US and Canada, partial coverage elsewhere (see for details: https://www.mapbox.com/developers/api/geocoding/#coverage)
|
549
|
-
* **SSL support**: yes
|
550
|
-
* **Languages**: English
|
551
|
-
* **Extra params** (see Mapbox docs for more):
|
552
|
-
* `:country` - restrict results to a specific country, e.g., `us` or `ca`
|
553
|
-
* `:types` - restrict results to categories such as `address`,
|
554
|
-
`neighborhood`, `postcode`
|
555
|
-
* `:proximity` - bias results toward a `lng,lat`, e.g.,
|
556
|
-
`params: { proximity: "-84.0,42.5" }`
|
557
|
-
* **Documentation**: https://www.mapbox.com/developers/api/geocoding/
|
558
|
-
* **Terms of Service**: https://www.mapbox.com/tos/
|
559
|
-
* **Limitations**: For `mapbox.places` dataset, must be displayed on a Mapbox map; Cache results for up to 30 days. For `mapbox.places-permanent` dataset, depends on plan.
|
560
|
-
* **Notes**: Currently in public beta.
|
561
|
-
|
562
|
-
#### Mapquest (`:mapquest`)
|
563
|
-
|
564
|
-
* **API key**: required
|
565
|
-
* **Key signup**: https://developer.mapquest.com/plans
|
566
|
-
* **Quota**: ?
|
567
|
-
* **HTTP Headers**: when using the licensed API you can specify a referer like so:
|
568
|
-
`Geocoder.configure(:http_headers => { "Referer" => "http://foo.com" })`
|
569
|
-
* **Region**: world
|
570
|
-
* **SSL support**: no
|
571
|
-
* **Languages**: English
|
572
|
-
* **Documentation**: http://www.mapquestapi.com/geocoding/
|
573
|
-
* **Terms of Service**: http://info.mapquest.com/terms-of-use/
|
574
|
-
* **Limitations**: ?
|
575
|
-
* **Notes**: You can use the open (non-licensed) API by setting: `Geocoder.configure(:mapquest => {:open => true})` (defaults to licensed version)
|
576
|
-
|
577
|
-
#### Ovi/Nokia (`:ovi`)
|
578
|
-
|
579
|
-
* **API key**: not required, but performance restricted without it
|
580
|
-
* **Quota**: ?
|
581
|
-
* **Region**: world
|
582
|
-
* **SSL support**: no
|
583
|
-
* **Languages**: English
|
584
|
-
* **Documentation**: http://api.maps.ovi.com/devguide/overview.html
|
585
|
-
* **Terms of Service**: http://www.developer.nokia.com/Develop/Maps/TC.html
|
586
|
-
* **Limitations**: ?
|
587
|
-
|
588
|
-
#### Here/Nokia (`:here`)
|
589
|
-
|
590
|
-
* **API key**: required (set `Geocoder.configure(:api_key => [app_id, app_code])`)
|
591
|
-
* **Quota**: Depending on the API key
|
592
|
-
* **Region**: world
|
593
|
-
* **SSL support**: yes
|
594
|
-
* **Languages**: The preferred language of address elements in the result. Language code must be provided according to RFC 4647 standard.
|
595
|
-
* **Documentation**: http://developer.here.com/rest-apis/documentation/geocoder
|
596
|
-
* **Terms of Service**: http://developer.here.com/faqs#l&t
|
597
|
-
* **Limitations**: ?
|
598
|
-
|
599
|
-
#### ESRI (`:esri`)
|
600
|
-
|
601
|
-
* **API key**: optional (set `Geocoder.configure(:esri => {:api_key => ["client_id", "client_secret"]})`)
|
602
|
-
* **Quota**: Required for some scenarios (see Terms of Service)
|
603
|
-
* **Region**: world
|
604
|
-
* **SSL support**: yes
|
605
|
-
* **Languages**: English
|
606
|
-
* **Documentation**: https://developers.arcgis.com/rest/geocode/api-reference/overview-world-geocoding-service.htm
|
607
|
-
* **Terms of Service**: http://www.esri.com/legal/software-license
|
608
|
-
* **Limitations**: Requires API key if results will be stored. Using API key will also remove rate limit.
|
609
|
-
* **Notes**: You can specify which projection you want to use by setting, for example: `Geocoder.configure(:esri => {:outSR => 102100})`. If you will store results, set the flag and provide API key: `Geocoder.configure(:esri => {:api_key => ["client_id", "client_secret"], :for_storage => true})`. If you want to, you can also supply an ESRI token directly: `Geocoder.configure(:esri => {:token => Geocoder::EsriToken.new('TOKEN', Time.now + 1.day})`
|
610
|
-
|
611
|
-
#### Mapzen (`:mapzen`)
|
612
|
-
|
613
|
-
* **API key**: required
|
614
|
-
* **Quota**: 25,000 free requests/month and [ability to purchase more](https://mapzen.com/pricing/)
|
615
|
-
* **Region**: world
|
616
|
-
* **SSL support**: yes
|
617
|
-
* **Languages**: en; see https://mapzen.com/documentation/search/language-codes/
|
618
|
-
* **Documentation**: https://mapzen.com/documentation/search/search/
|
619
|
-
* **Terms of Service**: http://mapzen.com/terms
|
620
|
-
* **Limitations**: [You must provide attribution](https://mapzen.com/rights/)
|
621
|
-
* **Notes**: Mapzen is the primary author of Pelias and offers Pelias-as-a-service in free and paid versions https://mapzen.com/pelias.
|
622
|
-
|
623
|
-
#### Pelias (`:pelias`)
|
624
|
-
|
625
|
-
* **API key**: configurable (self-hosted service)
|
626
|
-
* **Quota**: none (self-hosted service)
|
627
|
-
* **Region**: world
|
628
|
-
* **SSL support**: yes
|
629
|
-
* **Languages**: en; see https://mapzen.com/documentation/search/language-codes/
|
630
|
-
* **Documentation**: http://pelias.io/
|
631
|
-
* **Terms of Service**: http://pelias.io/data_licenses.html
|
632
|
-
* **Limitations**: See terms
|
633
|
-
* **Notes**: Configure your self-hosted pelias with the `endpoint` option: `Geocoder.configure(:lookup => :pelias, :api_key => 'your_api_key', :pelias => {:endpoint => 'self.hosted/pelias'})`. Defaults to `localhost`.
|
634
|
-
|
635
|
-
#### Data Science Toolkit (`:dstk`)
|
636
|
-
|
637
|
-
Data Science Toolkit provides an API whose response format is like Google's but which can be set up as a privately hosted service.
|
638
|
-
|
639
|
-
* **API key**: none
|
640
|
-
* **Quota**: No quota if you are self-hosting the service.
|
641
|
-
* **Region**: world
|
642
|
-
* **SSL support**: ?
|
643
|
-
* **Languages**: en
|
644
|
-
* **Documentation**: http://www.datasciencetoolkit.org/developerdocs
|
645
|
-
* **Terms of Service**: http://www.datasciencetoolkit.org/developerdocs#googlestylegeocoder
|
646
|
-
* **Limitations**: No reverse geocoding.
|
647
|
-
* **Notes**: If you are hosting your own DSTK server you will need to configure the host name, eg: `Geocoder.configure(:lookup => :dstk, :dstk => {:host => "localhost:4567"})`.
|
648
|
-
|
649
|
-
#### Baidu (`:baidu`)
|
650
|
-
|
651
|
-
* **API key**: required
|
652
|
-
* **Quota**: No quota limits for geocoding
|
653
|
-
* **Region**: China
|
654
|
-
* **SSL support**: no
|
655
|
-
* **Languages**: Chinese (Simplified)
|
656
|
-
* **Documentation**: http://developer.baidu.com/map/webservice-geocoding.htm
|
657
|
-
* **Terms of Service**: http://developer.baidu.com/map/law.htm
|
658
|
-
* **Limitations**: Only good for non-commercial use. For commercial usage please check http://developer.baidu.com/map/question.htm#qa0013
|
659
|
-
* **Notes**: To use Baidu set `Geocoder.configure(:lookup => :baidu, :api_key => "your_api_key")`.
|
660
|
-
|
661
|
-
#### Geocodio (`:geocodio`)
|
662
|
-
|
663
|
-
* **API key**: required
|
664
|
-
* **Quota**: 2,500 free requests/day then purchase $0.0005 for each, also has volume pricing and plans.
|
665
|
-
* **Region**: USA & Canada
|
666
|
-
* **SSL support**: yes
|
667
|
-
* **Languages**: en
|
668
|
-
* **Documentation**: https://geocod.io/docs/
|
669
|
-
* **Terms of Service**: https://geocod.io/terms-of-use/
|
670
|
-
* **Limitations**: No restrictions on use
|
671
|
-
|
672
|
-
#### SmartyStreets (`:smarty_streets`)
|
673
|
-
|
674
|
-
* **API key**: requires auth_id and auth_token (set `Geocoder.configure(:api_key => [id, token])`)
|
675
|
-
* **Quota**: 250/month then purchase at sliding scale.
|
676
|
-
* **Region**: US
|
677
|
-
* **SSL support**: yes (required)
|
678
|
-
* **Languages**: en
|
679
|
-
* **Documentation**: http://smartystreets.com/kb/liveaddress-api/rest-endpoint
|
680
|
-
* **Terms of Service**: http://smartystreets.com/legal/terms-of-service
|
681
|
-
* **Limitations**: No reverse geocoding.
|
682
|
-
|
683
|
-
|
684
|
-
#### OKF Geocoder (`:okf`)
|
685
|
-
|
686
|
-
* **API key**: none
|
687
|
-
* **Quota**: none
|
688
|
-
* **Region**: FI
|
689
|
-
* **SSL support**: no
|
690
|
-
* **Languages**: fi
|
691
|
-
* **Documentation**: http://books.okf.fi/geocoder/_full/
|
692
|
-
* **Terms of Service**: http://www.itella.fi/liitteet/palvelutjatuotteet/yhteystietopalvelut/Postinumeropalvelut-Palvelukuvausjakayttoehdot.pdf
|
693
|
-
* **Limitations**: ?
|
694
|
-
|
695
|
-
#### Geoportail.lu (`:geoportail_lu`)
|
696
|
-
|
697
|
-
* **API key**: none
|
698
|
-
* **Quota**: none
|
699
|
-
* **Region**: LU
|
700
|
-
* **SSL support**: yes
|
701
|
-
* **Languages**: en
|
702
|
-
* **Documentation**: http://wiki.geoportail.lu/doku.php?id=en:api
|
703
|
-
* **Terms of Service**: http://wiki.geoportail.lu/doku.php?id=en:mcg_1
|
704
|
-
* **Limitations**: ?
|
705
|
-
|
706
|
-
#### Postcodes.io (`:postcodes_io`)
|
707
|
-
|
708
|
-
* **API key**: none
|
709
|
-
* **Quota**: ?
|
710
|
-
* **Region**: UK
|
711
|
-
* **SSL support**: yes
|
712
|
-
* **Languages**: English
|
713
|
-
* **Documentation**: http://postcodes.io/docs
|
714
|
-
* **Terms of Service**: ?
|
715
|
-
* **Limitations**: UK postcodes only
|
716
|
-
|
717
|
-
#### PostcodeAnywhere UK (`:postcode_anywhere_uk`)
|
718
|
-
|
719
|
-
This uses the PostcodeAnywhere UK Geocode service, this will geocode any string from UK postcode, placename, point of interest or location.
|
720
|
-
|
721
|
-
* **API key**: required
|
722
|
-
* **Quota**: Dependant on service plan?
|
723
|
-
* **Region**: UK
|
724
|
-
* **SSL support**: yes
|
725
|
-
* **Languages**: English
|
726
|
-
* **Documentation**: http://www.postcodeanywhere.co.uk/Support/WebService/Geocoding/UK/Geocode/2/
|
727
|
-
* **Terms of Service**: ?
|
728
|
-
* **Limitations**: ?
|
729
|
-
* **Notes**: To use PostcodeAnywhere you must include an API key: `Geocoder.configure(:lookup => :postcode_anywhere_uk, :api_key => 'your_api_key')`.
|
730
|
-
|
731
|
-
#### LatLon.io (`:latlon`)
|
732
|
-
|
733
|
-
* **API key**: required
|
734
|
-
* **Quota**: Depends on the user's plan (free and paid plans available)
|
735
|
-
* **Region**: US
|
736
|
-
* **SSL support**: yes
|
737
|
-
* **Languages**: en
|
738
|
-
* **Documentation**: https://latlon.io/documentation
|
739
|
-
* **Terms of Service**: ?
|
740
|
-
* **Limitations**: No restrictions on use
|
741
|
-
|
742
|
-
#### Base Adresse Nationale FR (`:ban_data_gouv_fr`)
|
743
|
-
|
744
|
-
* **API key**: none
|
745
|
-
* **Quota**: none
|
746
|
-
* **Region**: FR
|
747
|
-
* **SSL support**: yes
|
748
|
-
* **Languages**: en / fr
|
749
|
-
* **Documentation**: https://adresse.data.gouv.fr/api/ (in french)
|
750
|
-
* **Terms of Service**: https://adresse.data.gouv.fr/faq/ (in french)
|
751
|
-
* **Limitations**: [Data licensed under Open Database License (ODbL) (you must provide attribution).](http://openstreetmap.fr/ban)
|
752
|
-
|
753
|
-
#### AMap (`:amap`)
|
754
|
-
|
755
|
-
- **API key**: required
|
756
|
-
- **Quota**: 2000/day and 2000/minute for personal developer, 4000000/day and 60000/minute for enterprise developer, for geocoding requests
|
757
|
-
- **Region**: China
|
758
|
-
- **SSL support**: yes
|
759
|
-
- **Languages**: Chinese (Simplified)
|
760
|
-
- **Documentation**: http://lbs.amap.com/api/webservice/guide/api/georegeo
|
761
|
-
- **Terms of Service**: http://lbs.amap.com/home/terms/
|
762
|
-
- **Limitations**: Only good for non-commercial use. For commercial usage please check http://lbs.amap.com/home/terms/
|
763
|
-
- **Notes**: To use AMap set `Geocoder.configure(:lookup => :amap, :api_key => "your_api_key")`.
|
764
|
-
|
765
|
-
### IP Address Services
|
766
|
-
|
767
|
-
#### IPInfo.io (`:ipinfo_io`)
|
768
|
-
|
769
|
-
* **API key**: optional - see http://ipinfo.io/pricing
|
770
|
-
* **Quota**: 1,000/day - more with api key
|
771
|
-
* **Region**: world
|
772
|
-
* **SSL support**: no (not without access key - see http://ipinfo.io/pricing)
|
773
|
-
* **Languages**: English
|
774
|
-
* **Documentation**: http://ipinfo.io/developers
|
775
|
-
* **Terms of Service**: http://ipinfo.io/developers
|
776
|
-
|
777
|
-
#### FreeGeoIP (`:freegeoip`) - [DISCONTINUED](https://github.com/alexreisner/geocoder/wiki/Freegeoip-Discontinuation)
|
778
|
-
|
779
|
-
* **API key**: none
|
780
|
-
* **Quota**: 15,000 requests per hour. After reaching the hourly quota, all of your requests will result in HTTP 403 (Forbidden) until it clears up on the next roll over.
|
781
|
-
* **Region**: world
|
782
|
-
* **SSL support**: no
|
783
|
-
* **Languages**: English
|
784
|
-
* **Documentation**: http://github.com/fiorix/freegeoip/blob/master/README.md
|
785
|
-
* **Terms of Service**: ?
|
786
|
-
* **Limitations**: ?
|
787
|
-
* **Notes**: If you are [running your own local instance of the FreeGeoIP service](https://github.com/fiorix/freegeoip) you can configure the host like this: `Geocoder.configure(freegeoip: {host: "..."})`.
|
788
|
-
|
789
|
-
#### Pointpin (`:pointpin`)
|
790
|
-
|
791
|
-
* **API key**: required
|
792
|
-
* **Quota**: 50,000/mo for €9 through 1m/mo for €49
|
793
|
-
* **Region**: world
|
794
|
-
* **SSL support**: yes
|
795
|
-
* **Languages**: English
|
796
|
-
* **Documentation**: https://pointp.in/docs/get-started
|
797
|
-
* **Terms of Service**: https://pointp.in/terms
|
798
|
-
* **Limitations**: ?
|
799
|
-
* **Notes**: To use Pointpin set `Geocoder.configure(:ip_lookup => :pointpin, :api_key => "your_pointpin_api_key")`.
|
800
|
-
|
801
|
-
#### Telize (`:telize`)
|
802
|
-
|
803
|
-
* **API key**: required
|
804
|
-
* **Quota**: 1,000/day for $7/mo through 100,000/day for $100/mo
|
805
|
-
* **Region**: world
|
806
|
-
* **SSL support**: yes
|
807
|
-
* **Languages**: English
|
808
|
-
* **Documentation**: https://market.mashape.com/fcambus/telize
|
809
|
-
* **Terms of Service**: ?
|
810
|
-
* **Limitations**: ?
|
811
|
-
* **Notes**: To use Telize set `Geocoder.configure(:ip_lookup => :telize, :api_key => "your_api_key")`. Or configure your self-hosted telize with the `host` option: `Geocoder.configure(:ip_lookup => :telize, :telize => {:host => "localhost"})`.
|
812
|
-
|
813
|
-
|
814
|
-
#### MaxMind Legacy Web Services (`:maxmind`)
|
815
|
-
|
816
|
-
* **API key**: required
|
817
|
-
* **Quota**: Request Packs can be purchased
|
818
|
-
* **Region**: world
|
819
|
-
* **SSL support**: yes
|
820
|
-
* **Languages**: English
|
821
|
-
* **Documentation**: http://dev.maxmind.com/geoip/legacy/web-services/
|
822
|
-
* **Terms of Service**: ?
|
823
|
-
* **Limitations**: ?
|
824
|
-
* **Notes**: You must specify which MaxMind service you are using in your configuration. For example: `Geocoder.configure(:maxmind => {:service => :omni})`.
|
825
|
-
|
826
|
-
#### Baidu IP (`:baidu_ip`)
|
827
|
-
|
828
|
-
* **API key**: required
|
829
|
-
* **Quota**: No quota limits for geocoding
|
830
|
-
* **Region**: China
|
831
|
-
* **SSL support**: no
|
832
|
-
* **Languages**: Chinese (Simplified)
|
833
|
-
* **Documentation**: http://developer.baidu.com/map/webservice-geocoding.htm
|
834
|
-
* **Terms of Service**: http://developer.baidu.com/map/law.htm
|
835
|
-
* **Limitations**: Only good for non-commercial use. For commercial usage please check http://developer.baidu.com/map/question.htm#qa0013
|
836
|
-
* **Notes**: To use Baidu set `Geocoder.configure(:lookup => :baidu_ip, :api_key => "your_api_key")`.
|
837
|
-
|
838
|
-
#### MaxMind GeoIP2 Precision Web Services (`:maxmind_geoip2`)
|
839
|
-
|
840
|
-
* **API key**: required
|
841
|
-
* **Quota**: Request Packs can be purchased
|
842
|
-
* **Region**: world
|
843
|
-
* **SSL support**: yes
|
844
|
-
* **Languages**: English
|
845
|
-
* **Documentation**: http://dev.maxmind.com/geoip/geoip2/web-services/
|
846
|
-
* **Terms of Service**: ?
|
847
|
-
* **Limitations**: ?
|
848
|
-
* **Notes**: You must specify which MaxMind service you are using in your configuration, and also basic authentication. For example: `Geocoder.configure(:maxmind_geoip2 => {:service => :country, :basic_auth => {:user => '', :password => ''}})`.
|
849
|
-
|
850
|
-
#### Ipstack (`:ipstack`)
|
851
|
-
|
852
|
-
* **API key**: required (see https://ipstack.com/product)
|
853
|
-
* **Quota**: 10,000 requests per month (with free API Key, 50,000/day and up for paid plans)
|
854
|
-
* **Region**: world
|
855
|
-
* **SSL support**: yes ( only with paid plan )
|
856
|
-
* **Languages**: English, German, Spanish, French, Japanese, Portugues (Brazil), Russian, Chinese
|
857
|
-
* **Documentation**: https://ipstack.com/documentation
|
858
|
-
* **Terms of Service**: ?
|
859
|
-
* **Limitations**: ?
|
860
|
-
* **Notes**: To use Ipstack set `Geocoder.configure(:ip_lookup => :ipstack, :api_key => "your_ipstack_api_key")`. Supports the optional params: `:hostname`, `:security`, `:fields`, `:language` (see API documentation for details).
|
861
|
-
|
862
|
-
#### IP-API.com (`:ipapi_com`)
|
863
|
-
|
864
|
-
* **API key**: optional - see http://ip-api.com/docs/#usage_limits
|
865
|
-
* **Quota**: 150/minute - unlimited with api key
|
866
|
-
* **Region**: world
|
867
|
-
* **SSL support**: no (not without access key - see https://signup.ip-api.com/)
|
868
|
-
* **Languages**: English
|
869
|
-
* **Documentation**: http://ip-api.com/docs/
|
870
|
-
* **Terms of Service**: https://signup.ip-api.com/terms
|
871
|
-
|
872
|
-
#### DB-IP.com (`:db_ip_com`)
|
873
|
-
|
874
|
-
* **API key**: required
|
875
|
-
* **Quota**: 2,500/day (with free API Key, 50,000/day and up for paid API keys)
|
876
|
-
* **Region**: world
|
877
|
-
* **SSL support**: yes (with paid API keys - see https://db-ip.com/api/)
|
878
|
-
* **Languages**: English (English with free API key, multiple languages with paid API keys)
|
879
|
-
* **Documentation**: https://db-ip.com/api/doc.php
|
880
|
-
* **Terms of Service**: https://db-ip.com/tos.php
|
881
|
-
|
882
|
-
#### Ipdata.co (`:ipdata_co`)
|
883
|
-
|
884
|
-
* **API key**: optional, see: https://ipdata.co/pricing.html
|
885
|
-
* **Quota**: 1500/day (up to 600k with paid API keys)
|
886
|
-
* **Region**: world
|
887
|
-
* **SSL support**: yes
|
888
|
-
* **Languages**: English
|
889
|
-
* **Documentation**: https://ipdata.co/docs.html
|
890
|
-
* **Terms of Service**: https://ipdata.co/terms.html
|
891
|
-
* **Limitations**: ?
|
892
|
-
|
893
|
-
|
894
|
-
### IP Address Local Database Services
|
895
|
-
|
896
|
-
#### MaxMind Local (`:maxmind_local`) - EXPERIMENTAL
|
897
|
-
|
898
|
-
This lookup provides methods for geocoding IP addresses without making a call to a remote API (improves speed and availability). It works, but support is new and should not be considered production-ready. Please [report any bugs](https://github.com/alexreisner/geocoder/issues) you encounter.
|
899
|
-
|
900
|
-
* **API key**: none (requires the GeoLite City database which can be downloaded from [MaxMind](http://dev.maxmind.com/geoip/legacy/geolite/))
|
901
|
-
* **Quota**: none
|
902
|
-
* **Region**: world
|
903
|
-
* **SSL support**: N/A
|
904
|
-
* **Languages**: English
|
905
|
-
* **Documentation**: http://www.maxmind.com/en/city
|
906
|
-
* **Terms of Service**: ?
|
907
|
-
* **Limitations**: ?
|
908
|
-
* **Notes**: There are two supported formats for MaxMind local data: binary file, and CSV file imported into an SQL database. **You must download a database from MaxMind and set either the `:file` or `:package` configuration option for local lookups to work.**
|
909
|
-
|
910
|
-
**To use a binary file** you must add the *geoip* (or *jgeoip* for JRuby) gem to your Gemfile or have it installed in your system, and specify the path of the MaxMind database in your configuration. For example:
|
911
|
-
|
912
|
-
Geocoder.configure(ip_lookup: :maxmind_local, maxmind_local: {file: File.join('folder', 'GeoLiteCity.dat')})
|
913
|
-
|
914
|
-
**To use a CSV file** you must import it into an SQL database. The GeoLite *City* and *Country* packages are supported. Configure like so:
|
915
|
-
|
916
|
-
Geocoder.configure(ip_lookup: :maxmind_local, maxmind_local: {package: :city})
|
917
|
-
|
918
|
-
You can generate ActiveRecord migrations and download and import data via provided rake tasks:
|
919
|
-
|
920
|
-
# generate migration to create tables
|
921
|
-
rails generate geocoder:maxmind:geolite_city
|
922
|
-
|
923
|
-
# download, unpack, and import data
|
924
|
-
rake geocoder:maxmind:geolite:load PACKAGE=city
|
925
|
-
|
926
|
-
You can replace `city` with `country` in any of the above tasks, generators, and configurations.
|
927
|
-
|
928
|
-
#### GeoLite2 (`:geoip2`)
|
929
|
-
|
930
|
-
This lookup provides methods for geocoding IP addresses without making a call to a remote API (improves speed and availability). It works, but support is new and should not be considered production-ready. Please [report any bugs](https://github.com/alexreisner/geocoder/issues) you encounter.
|
931
|
-
|
932
|
-
* **API key**: none (requires a GeoIP2 or free GeoLite2 City or Country binary database which can be downloaded from [MaxMind](http://dev.maxmind.com/geoip/geoip2/))
|
933
|
-
* **Quota**: none
|
934
|
-
* **Region**: world
|
935
|
-
* **SSL support**: N/A
|
936
|
-
* **Languages**: English
|
937
|
-
* **Documentation**: http://www.maxmind.com/en/city
|
938
|
-
* **Terms of Service**: ?
|
939
|
-
* **Limitations**: ?
|
940
|
-
* **Notes**: **You must download a binary database file from MaxMind and set the `:file` configuration option.** The CSV format databases are not yet supported since they are still in alpha stage. Set the path to the database file in your configuration:
|
941
|
-
|
942
|
-
Geocoder.configure(
|
943
|
-
ip_lookup: :geoip2,
|
944
|
-
geoip2: {
|
945
|
-
file: File.join('folder', 'GeoLite2-City.mmdb')
|
946
|
-
}
|
947
|
-
)
|
948
|
-
|
949
|
-
You must add either the *[hive_geoip2](https://rubygems.org/gems/hive_geoip2)* gem (native extension that relies on libmaxminddb) or the *[maxminddb](http://rubygems.org/gems/maxminddb)* gem (pure Ruby implementation) to your Gemfile or have it installed in your system. The pure Ruby gem (maxminddb) will be used by default. To use `hive_geoip2`:
|
950
|
-
|
951
|
-
Geocoder.configure(
|
952
|
-
ip_lookup: :geoip2,
|
953
|
-
geoip2: {
|
954
|
-
lib: 'hive_geoip2',
|
955
|
-
file: File.join('folder', 'GeoLite2-City.mmdb')
|
956
|
-
}
|
957
|
-
)
|
958
|
-
|
959
|
-
Caching
|
960
|
-
-------
|
961
|
-
|
962
|
-
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:
|
963
|
-
|
964
|
-
Geocoder.configure(:cache => Redis.new)
|
965
|
-
|
966
|
-
This example uses Redis, but the cache store can be any object that supports these methods:
|
967
|
-
|
968
|
-
* `store#[](key)` or `#get` or `#read` - retrieves a value
|
969
|
-
* `store#[]=(key, value)` or `#set` or `#write` - stores a value
|
970
|
-
* `store#del(url)` - deletes a value
|
971
|
-
* `store#keys` - (Optional) Returns array of keys. Used if you wish to expire the entire cache (see below).
|
404
|
+
class Venue
|
405
|
+
geocoded_by :address,
|
406
|
+
latitude: :fetched_latitude,
|
407
|
+
longitude: :fetched_longitude
|
408
|
+
reverse_geocoded_by :latitude, :longitude
|
409
|
+
end
|
972
410
|
|
973
|
-
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).
|
974
411
|
|
975
|
-
|
412
|
+
Advanced Database Queries
|
413
|
+
-------------------------
|
976
414
|
|
977
|
-
|
415
|
+
*The following apply to ActiveRecord only. For MongoDB, please use the built-in geospatial features.*
|
978
416
|
|
979
|
-
|
980
|
-
|
981
|
-
If you need to expire cached content:
|
417
|
+
The default `near` search looks for objects within a circle. To search within a doughnut or ring use the `:min_radius` option:
|
982
418
|
|
983
|
-
|
984
|
-
Geocoder::Lookup.get(:google).cache.expire("http://...") # expire cached result for a specific URL
|
985
|
-
Geocoder::Lookup.get(:google).cache.expire(:all) # expire cached results for Google Lookup
|
986
|
-
# expire all cached results for all Lookups.
|
987
|
-
# Be aware that this methods spawns a new Lookup object for each Service
|
988
|
-
Geocoder::Lookup.all_services.each{|service| Geocoder::Lookup.get(service).cache.expire(:all)}
|
419
|
+
Venue.near("Austin, TX", 200, min_radius: 40)
|
989
420
|
|
990
|
-
|
421
|
+
To search within a rectangle (note that results will *not* include `distance` and `bearing` attributes):
|
991
422
|
|
992
|
-
|
423
|
+
sw_corner = [40.71, 100.23]
|
424
|
+
ne_corner = [36.12, 88.65]
|
425
|
+
Venue.within_bounding_box(sw_corner, ne_corner)
|
993
426
|
|
994
|
-
|
427
|
+
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:
|
995
428
|
|
429
|
+
Venue.near([40.71, 99.23], :effective_radius)
|
996
430
|
|
997
|
-
|
998
|
-
-----------------------------------------------
|
431
|
+
If you store multiple sets of coordinates for each object, you can specify latitude and longitude columns to use for a search:
|
999
432
|
|
1000
|
-
|
433
|
+
Venue.near("Paris", 50, latitude: :secondary_latitude, longitude: :secondary_longitude)
|
1001
434
|
|
1002
|
-
|
1003
|
-
* one for providing an address to use when fetching coordinates (forward geocoding)
|
435
|
+
### Distance and Bearing
|
1004
436
|
|
1005
|
-
|
437
|
+
When you run a geospatial query, the returned objects have two attributes added:
|
1006
438
|
|
1007
|
-
|
439
|
+
* `obj.distance` - number of miles from the search point to this object
|
440
|
+
* `obj.bearing` - direction from the search point to this object
|
1008
441
|
|
1009
|
-
|
1010
|
-
geocoded_by :address_from_components
|
442
|
+
Results are automatically sorted by distance from the search point, closest to farthest. Bearing is given as a number of degrees clockwise from due north, for example:
|
1011
443
|
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
444
|
+
* `0` - due north
|
445
|
+
* `180` - due south
|
446
|
+
* `90` - due east
|
447
|
+
* `270` - due west
|
448
|
+
* `230.1` - southwest
|
449
|
+
* `359.9` - almost due north
|
1015
450
|
|
1016
|
-
|
451
|
+
You can convert these to compass point names via provided method:
|
1017
452
|
|
1018
|
-
|
453
|
+
Geocoder::Calculations.compass_point(355) # => "N"
|
454
|
+
Geocoder::Calculations.compass_point(45) # => "NE"
|
455
|
+
Geocoder::Calculations.compass_point(208) # => "SW"
|
1019
456
|
|
1020
|
-
|
1021
|
-
:latitude => :fetched_latitude, # this will be overridden by the below
|
1022
|
-
:longitude => :fetched_longitude # same here
|
457
|
+
_Note: when running queries on SQLite, `distance` and `bearing` are provided for consistency only. They are not very accurate._
|
1023
458
|
|
1024
|
-
|
1025
|
-
end
|
459
|
+
For more advanced geospatial querying, please see the [rgeo gem](https://github.com/rgeo/rgeo).
|
1026
460
|
|
1027
|
-
We don't want ambiguity when doing distance calculations -- we need a single, authoritative source for coordinates!
|
1028
461
|
|
1029
|
-
|
462
|
+
Geospatial Calculations
|
463
|
+
-----------------------
|
1030
464
|
|
1031
|
-
|
465
|
+
The `Geocoder::Calculations` module contains some useful methods:
|
1032
466
|
|
1033
|
-
|
467
|
+
# find the distance between two arbitrary points
|
468
|
+
Geocoder::Calculations.distance_between([47.858205,2.294359], [40.748433,-73.985655])
|
469
|
+
=> 3619.77359999382 # in configured units (default miles)
|
1034
470
|
|
1035
|
-
|
471
|
+
# find the geographic center (aka center of gravity) of objects or points
|
472
|
+
Geocoder::Calculations.geographic_center([city1, city2, [40.22,-73.99], city4])
|
473
|
+
=> [35.14968, -90.048929]
|
1036
474
|
|
1037
|
-
|
475
|
+
See [the code](https://github.com/alexreisner/geocoder/blob/master/lib/geocoder/calculations.rb) for more!
|
1038
476
|
|
1039
|
-
For certain geolocation services such as Google's geolocation API, this may cause issues during subsequent updates to database records if the longitude and latitude coordinates cannot be associated with a known location address (on a large body of water for example). On subsequent callbacks the following call:
|
1040
477
|
|
1041
|
-
|
478
|
+
Batch Geocoding
|
479
|
+
---------------
|
1042
480
|
|
1043
|
-
|
481
|
+
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:
|
1044
482
|
|
1045
|
-
|
483
|
+
rake geocode:all CLASS=YourModel
|
1046
484
|
|
1047
|
-
|
485
|
+
If you need reverse geocoding instead, call the task with REVERSE=true:
|
1048
486
|
|
1049
|
-
|
1050
|
-
after_validation :geocode, :if => :has_location, :unless => :has_coordinates
|
487
|
+
rake geocode:all CLASS=YourModel REVERSE=true
|
1051
488
|
|
1052
|
-
|
489
|
+
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:
|
1053
490
|
|
1054
|
-
|
1055
|
-
--------------------
|
491
|
+
rake geocode:all CLASS=YourModel SLEEP=0.25 BATCH=100
|
1056
492
|
|
1057
|
-
|
493
|
+
To avoid exceeding per-day limits you can add a `LIMIT` option. However, this will ignore the `BATCH` value, if provided.
|
1058
494
|
|
1059
|
-
|
495
|
+
rake geocode:all CLASS=YourModel LIMIT=1000
|
1060
496
|
|
1061
|
-
This returns an array of `Geocoder::Result` objects with all data provided by the geocoding service.
|
1062
497
|
|
498
|
+
Testing
|
499
|
+
-------
|
1063
500
|
|
1064
|
-
|
1065
|
-
------------------------------
|
501
|
+
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`
|
1066
502
|
|
1067
|
-
|
503
|
+
Geocoder.configure(lookup: :test, ip_lookup: :test)
|
1068
504
|
|
1069
|
-
|
505
|
+
Add stubs to define the results that will be returned:
|
1070
506
|
|
1071
507
|
Geocoder::Lookup::Test.add_stub(
|
1072
508
|
"New York, NY", [
|
@@ -1081,9 +517,7 @@ When writing tests for an app that uses Geocoder it may be useful to avoid netwo
|
|
1081
517
|
]
|
1082
518
|
)
|
1083
519
|
|
1084
|
-
|
1085
|
-
|
1086
|
-
Geocoder.configure(:lookup => :test)
|
520
|
+
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:
|
1087
521
|
|
1088
522
|
Geocoder::Lookup::Test.set_default_stub(
|
1089
523
|
[
|
@@ -1098,13 +532,41 @@ Now, any time Geocoder looks up "New York, NY" its results array will contain on
|
|
1098
532
|
]
|
1099
533
|
)
|
1100
534
|
|
535
|
+
You may also delete a single stub, or reset all stubs _including the default stub_:
|
536
|
+
|
537
|
+
Geocoder::Lookup::Test.delete_stub('New York, NY')
|
538
|
+
Geocoder::Lookup::Test.reset
|
539
|
+
|
1101
540
|
Notes:
|
1102
541
|
|
1103
|
-
- Keys must be strings not symbols when calling `add_stub` or `set_default_stub`. For example `'
|
1104
|
-
- To clear stubs (e.g. prior to another spec), use `Geocoder::Lookup::Test.reset`. This will clear all stubs _including the default stub_.
|
542
|
+
- Keys must be strings (not symbols) when calling `add_stub` or `set_default_stub`. For example `'country' =>` not `:country =>`.
|
1105
543
|
- 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.
|
1106
544
|
|
1107
545
|
|
546
|
+
Error Handling
|
547
|
+
--------------
|
548
|
+
|
549
|
+
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:
|
550
|
+
|
551
|
+
Geocoder.configure(always_raise: [SocketError, Timeout::Error])
|
552
|
+
|
553
|
+
You can also do this to raise all exceptions:
|
554
|
+
|
555
|
+
Geocoder.configure(always_raise: :all)
|
556
|
+
|
557
|
+
The raise-able exceptions are:
|
558
|
+
|
559
|
+
SocketError
|
560
|
+
Timeout::Error
|
561
|
+
Geocoder::OverQueryLimitError
|
562
|
+
Geocoder::RequestDenied
|
563
|
+
Geocoder::InvalidRequest
|
564
|
+
Geocoder::InvalidApiKey
|
565
|
+
Geocoder::ServiceUnavailable
|
566
|
+
|
567
|
+
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.
|
568
|
+
|
569
|
+
|
1108
570
|
Command Line Interface
|
1109
571
|
----------------------
|
1110
572
|
|
@@ -1118,63 +580,18 @@ When you install the Geocoder gem it adds a `geocode` command to your shell. You
|
|
1118
580
|
State/province: Louisiana
|
1119
581
|
Postal code: 70112
|
1120
582
|
Country: United States
|
1121
|
-
|
583
|
+
Map: http://maps.google.com/maps?q=29.952211,-90.080563
|
1122
584
|
|
1123
585
|
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.
|
1124
586
|
|
1125
|
-
Numeric Data Types and Precision
|
1126
|
-
--------------------------------
|
1127
|
-
|
1128
|
-
Geocoder works with any numeric data type (e.g. float, double, decimal) on which trig (and other mathematical) functions can be performed.
|
1129
|
-
|
1130
|
-
A summary of the relationship between geographic precision and the number of decimal places in latitude and longitude degree values is available on [Wikipedia](http://en.wikipedia.org/wiki/Decimal_degrees#Accuracy). As an example: at the equator, latitude/longitude values with 4 decimal places give about 11 metres precision, whereas 5 decimal places gives roughly 1 metre precision.
|
1131
|
-
|
1132
|
-
Notes on MongoDB
|
1133
|
-
----------------
|
1134
|
-
|
1135
|
-
### The Near Method
|
1136
|
-
|
1137
|
-
Mongo document classes (Mongoid and MongoMapper) have a built-in `near` scope, but since it only works two-dimensions Geocoder overrides it with its own spherical `near` method in geocoded classes.
|
1138
587
|
|
1139
|
-
|
588
|
+
Technical Discussions
|
589
|
+
---------------------
|
1140
590
|
|
1141
|
-
|
1142
|
-
|
1143
|
-
To access an object's coordinates in the conventional order, use the `to_coordinates` instance method provided by Geocoder. For example:
|
1144
|
-
|
1145
|
-
obj.to_coordinates # => [37.7941013, -122.3951096] # [lat, lon]
|
1146
|
-
|
1147
|
-
Calling `obj.coordinates` directly returns the internal representation of the coordinates which, in the case of MongoDB, is probably the reverse of what you want:
|
1148
|
-
|
1149
|
-
obj.coordinates # => [-122.3951096, 37.7941013] # [lon, lat]
|
1150
|
-
|
1151
|
-
For consistency with the rest of Geocoder, always use the `to_coordinates` method instead.
|
1152
|
-
|
1153
|
-
Notes on Non-Rails Frameworks
|
1154
|
-
-----------------------------
|
1155
|
-
|
1156
|
-
If you are using 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:
|
1157
|
-
|
1158
|
-
extend Geocoder::Model::ActiveRecord
|
1159
|
-
|
1160
|
-
Optimisation of Distance Queries
|
1161
|
-
--------------------------------
|
1162
|
-
|
1163
|
-
In MySQL and Postgres, the finding of objects near a given point is sped up by using a bounding box to limit the number of points over which a full distance calculation needs to be done.
|
1164
|
-
|
1165
|
-
To take advantage of this optimisation, you need to add a composite index on latitude and longitude. In your Rails migration:
|
1166
|
-
|
1167
|
-
add_index :table, [:latitude, :longitude]
|
1168
|
-
|
1169
|
-
|
1170
|
-
Distance Queries in SQLite
|
1171
|
-
--------------------------
|
591
|
+
### Distance Queries in SQLite
|
1172
592
|
|
1173
593
|
SQLite's lack of trigonometric functions requires an alternate implementation of the `near` scope. When using SQLite, Geocoder will automatically use a less accurate algorithm for finding objects near a given point. Results of this algorithm should not be trusted too much as it will return objects that are outside the given radius, along with inaccurate distance and bearing calculations.
|
1174
594
|
|
1175
|
-
|
1176
|
-
### Discussion
|
1177
|
-
|
1178
595
|
There are few options for finding objects near a given point in SQLite without installing extensions:
|
1179
596
|
|
1180
597
|
1. Use a square instead of a circle for finding nearby points. For example, if you want to find points near 40.71, 100.23, search for objects with latitude between 39.71 and 41.71 and longitude between 99.23 and 101.23. One degree of latitude or longitude is at most 69 miles so divide your radius (in miles) by 69.0 to get the amount to add and subtract from your center coordinates to get the upper and lower bounds. The results will not be very accurate (you'll get points outside the desired radius), but you will get all the points within the required radius.
|
@@ -1185,37 +602,11 @@ There are few options for finding objects near a given point in SQLite without i
|
|
1185
602
|
|
1186
603
|
Because Geocoder needs to provide this functionality as a scope, we must go with option #1, but feel free to implement #2 or #3 if you need more accuracy.
|
1187
604
|
|
605
|
+
### Numeric Data Types and Precision
|
1188
606
|
|
1189
|
-
|
1190
|
-
-----
|
1191
|
-
|
1192
|
-
Geocoder comes with a test suite (just run `rake test`) that mocks ActiveRecord and is focused on testing the aspects of Geocoder that do not involve executing database queries. Geocoder uses many database engine-specific queries which must be tested against all supported databases (SQLite, MySQL, etc). Ideally this involves creating a full, working Rails application, and that seems beyond the scope of the included test suite. As such, I have created a separate repository which includes a full-blown Rails application and some utilities for easily running tests against multiple environments:
|
1193
|
-
|
1194
|
-
http://github.com/alexreisner/geocoder_test
|
1195
|
-
|
1196
|
-
|
1197
|
-
Error Handling
|
1198
|
-
--------------
|
1199
|
-
|
1200
|
-
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:
|
1201
|
-
|
1202
|
-
Geocoder.configure(:always_raise => [SocketError, Timeout::Error])
|
1203
|
-
|
1204
|
-
You can also do this to raise all exceptions:
|
1205
|
-
|
1206
|
-
Geocoder.configure(:always_raise => :all)
|
1207
|
-
|
1208
|
-
The raise-able exceptions are:
|
1209
|
-
|
1210
|
-
SocketError
|
1211
|
-
Timeout::Error
|
1212
|
-
Geocoder::OverQueryLimitError
|
1213
|
-
Geocoder::RequestDenied
|
1214
|
-
Geocoder::InvalidRequest
|
1215
|
-
Geocoder::InvalidApiKey
|
1216
|
-
Geocoder::ServiceUnavailable
|
607
|
+
Geocoder works with any numeric data type (e.g. float, double, decimal) on which trig (and other mathematical) functions can be performed.
|
1217
608
|
|
1218
|
-
|
609
|
+
A summary of the relationship between geographic precision and the number of decimal places in latitude and longitude degree values is available on [Wikipedia](http://en.wikipedia.org/wiki/Decimal_degrees#Accuracy). As an example: at the equator, latitude/longitude values with 4 decimal places give about 11 metres precision, whereas 5 decimal places gives roughly 1 metre precision.
|
1219
610
|
|
1220
611
|
|
1221
612
|
Troubleshooting
|
@@ -1251,26 +642,23 @@ For the most part, the speed of geocoding requests has little to do with the Geo
|
|
1251
642
|
|
1252
643
|
Take a look at the server's raw response. You can do this by getting the request URL in an app console:
|
1253
644
|
|
1254
|
-
Geocoder::Lookup.get(:
|
645
|
+
Geocoder::Lookup.get(:nominatim).query_url(Geocoder::Query.new("..."))
|
1255
646
|
|
1256
|
-
Replace `:
|
647
|
+
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.
|
1257
648
|
|
1258
649
|
You can also fetch the response in the console:
|
1259
650
|
|
1260
|
-
Geocoder::Lookup.get(:
|
1261
|
-
|
1262
|
-
|
1263
|
-
Reporting Issues
|
1264
|
-
----------------
|
651
|
+
Geocoder::Lookup.get(:nominatim).send(:fetch_raw_data, Geocoder::Query.new("..."))
|
1265
652
|
|
1266
|
-
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). Also avoid vague language like "it doesn't work." Please describe as specifically as you can what behavior you are actually seeing (eg: an error message? a nil return value?).
|
1267
653
|
|
1268
|
-
|
654
|
+
Known Issues
|
655
|
+
------------
|
1269
656
|
|
657
|
+
### Using `count` with Rails 4.1+
|
1270
658
|
|
1271
|
-
|
659
|
+
Due to [a change in ActiveRecord's `count` method](https://github.com/rails/rails/pull/10710) you will need to use `count(:all)` to explicitly count all columns ("*") when using a `near` scope. Using `near` and calling `count` with no argument will cause exceptions in many cases.
|
1272
660
|
|
1273
|
-
|
661
|
+
### Using `near` with `includes`
|
1274
662
|
|
1275
663
|
You cannot use the `near` scope with another scope that provides an `includes` option because the `SELECT` clause generated by `near` will overwrite it (or vice versa).
|
1276
664
|
|
@@ -1278,7 +666,7 @@ Instead of using `includes` to reduce the number of database queries, try using
|
|
1278
666
|
|
1279
667
|
# Pass a :select option to the near scope to get the columns you want.
|
1280
668
|
# Instead of City.near(...).includes(:venues), try:
|
1281
|
-
City.near("Omaha, NE", 20, :
|
669
|
+
City.near("Omaha, NE", 20, select: "cities.*, venues.*").joins(:venues)
|
1282
670
|
|
1283
671
|
# This preload call will normally trigger two queries regardless of the
|
1284
672
|
# number of results; one query on hotels, and one query on administrators.
|
@@ -1287,26 +675,9 @@ Instead of using `includes` to reduce the number of database queries, try using
|
|
1287
675
|
|
1288
676
|
If anyone has a more elegant solution to this problem I am very interested in seeing it.
|
1289
677
|
|
1290
|
-
|
678
|
+
### Using `near` with objects close to the 180th meridian
|
1291
679
|
|
1292
680
|
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.
|
1293
681
|
|
1294
682
|
|
1295
|
-
|
1296
|
-
------------
|
1297
|
-
|
1298
|
-
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.
|
1299
|
-
|
1300
|
-
For all contributions, please respect the following guidelines:
|
1301
|
-
|
1302
|
-
* 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.
|
1303
|
-
* Do not commit changes to files that are irrelevant to your feature or bugfix (eg: `.gitignore`).
|
1304
|
-
* Do not add dependencies on other gems.
|
1305
|
-
* Do not add unnecessary `require` statements which could cause LoadErrors on certain systems.
|
1306
|
-
* Remember: Geocoder needs to run outside of Rails. Don't assume things like ActiveSupport are available.
|
1307
|
-
* 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.
|
1308
|
-
* Be aware that the pull request review process is not immediate, and is generally proportional to the size of the pull request.
|
1309
|
-
* 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.
|
1310
|
-
|
1311
|
-
|
1312
|
-
Copyright (c) 2009-15 Alex Reisner, released under the MIT license
|
683
|
+
Copyright :copyright: 2009-2021 Alex Reisner, released under the MIT license.
|