ultra-fast-mod 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. checksums.yaml +7 -0
  2. data/geocoder-1.8.6/CHANGELOG.md +669 -0
  3. data/geocoder-1.8.6/LICENSE +20 -0
  4. data/geocoder-1.8.6/README.md +827 -0
  5. data/geocoder-1.8.6/bin/console +13 -0
  6. data/geocoder-1.8.6/bin/geocode +5 -0
  7. data/geocoder-1.8.6/examples/app_defined_lookup_services.rb +22 -0
  8. data/geocoder-1.8.6/examples/cache_bypass.rb +48 -0
  9. data/geocoder-1.8.6/examples/reverse_geocode_job.rb +40 -0
  10. data/geocoder-1.8.6/lib/easting_northing.rb +171 -0
  11. data/geocoder-1.8.6/lib/generators/geocoder/config/config_generator.rb +14 -0
  12. data/geocoder-1.8.6/lib/generators/geocoder/config/templates/initializer.rb +27 -0
  13. data/geocoder-1.8.6/lib/generators/geocoder/maxmind/geolite_city_generator.rb +30 -0
  14. data/geocoder-1.8.6/lib/generators/geocoder/maxmind/geolite_country_generator.rb +30 -0
  15. data/geocoder-1.8.6/lib/generators/geocoder/maxmind/templates/migration/geolite_city.rb +30 -0
  16. data/geocoder-1.8.6/lib/generators/geocoder/maxmind/templates/migration/geolite_country.rb +17 -0
  17. data/geocoder-1.8.6/lib/generators/geocoder/migration_version.rb +15 -0
  18. data/geocoder-1.8.6/lib/geocoder/cache.rb +77 -0
  19. data/geocoder-1.8.6/lib/geocoder/cache_stores/base.rb +40 -0
  20. data/geocoder-1.8.6/lib/geocoder/cache_stores/generic.rb +35 -0
  21. data/geocoder-1.8.6/lib/geocoder/cache_stores/redis.rb +34 -0
  22. data/geocoder-1.8.6/lib/geocoder/calculations.rb +420 -0
  23. data/geocoder-1.8.6/lib/geocoder/cli.rb +121 -0
  24. data/geocoder-1.8.6/lib/geocoder/configuration.rb +151 -0
  25. data/geocoder-1.8.6/lib/geocoder/configuration_hash.rb +11 -0
  26. data/geocoder-1.8.6/lib/geocoder/esri_token.rb +38 -0
  27. data/geocoder-1.8.6/lib/geocoder/exceptions.rb +40 -0
  28. data/geocoder-1.8.6/lib/geocoder/ip_address.rb +36 -0
  29. data/geocoder-1.8.6/lib/geocoder/kernel_logger.rb +25 -0
  30. data/geocoder-1.8.6/lib/geocoder/logger.rb +47 -0
  31. data/geocoder-1.8.6/lib/geocoder/lookup.rb +155 -0
  32. data/geocoder-1.8.6/lib/geocoder/lookups/abstract_api.rb +46 -0
  33. data/geocoder-1.8.6/lib/geocoder/lookups/amap.rb +63 -0
  34. data/geocoder-1.8.6/lib/geocoder/lookups/amazon_location_service.rb +58 -0
  35. data/geocoder-1.8.6/lib/geocoder/lookups/azure.rb +56 -0
  36. data/geocoder-1.8.6/lib/geocoder/lookups/baidu.rb +63 -0
  37. data/geocoder-1.8.6/lib/geocoder/lookups/baidu_ip.rb +30 -0
  38. data/geocoder-1.8.6/lib/geocoder/lookups/ban_data_gouv_fr.rb +143 -0
  39. data/geocoder-1.8.6/lib/geocoder/lookups/base.rb +351 -0
  40. data/geocoder-1.8.6/lib/geocoder/lookups/bing.rb +82 -0
  41. data/geocoder-1.8.6/lib/geocoder/lookups/db_ip_com.rb +52 -0
  42. data/geocoder-1.8.6/lib/geocoder/lookups/esri.rb +114 -0
  43. data/geocoder-1.8.6/lib/geocoder/lookups/freegeoip.rb +62 -0
  44. data/geocoder-1.8.6/lib/geocoder/lookups/geoapify.rb +78 -0
  45. data/geocoder-1.8.6/lib/geocoder/lookups/geocoder_ca.rb +53 -0
  46. data/geocoder-1.8.6/lib/geocoder/lookups/geocodio.rb +42 -0
  47. data/geocoder-1.8.6/lib/geocoder/lookups/geoip2.rb +49 -0
  48. data/geocoder-1.8.6/lib/geocoder/lookups/geoportail_lu.rb +65 -0
  49. data/geocoder-1.8.6/lib/geocoder/lookups/google.rb +100 -0
  50. data/geocoder-1.8.6/lib/geocoder/lookups/google_places_details.rb +64 -0
  51. data/geocoder-1.8.6/lib/geocoder/lookups/google_places_search.rb +75 -0
  52. data/geocoder-1.8.6/lib/geocoder/lookups/google_premier.rb +61 -0
  53. data/geocoder-1.8.6/lib/geocoder/lookups/here.rb +73 -0
  54. data/geocoder-1.8.6/lib/geocoder/lookups/ip2location.rb +71 -0
  55. data/geocoder-1.8.6/lib/geocoder/lookups/ip2location_io.rb +62 -0
  56. data/geocoder-1.8.6/lib/geocoder/lookups/ip2location_lite.rb +40 -0
  57. data/geocoder-1.8.6/lib/geocoder/lookups/ipapi_com.rb +82 -0
  58. data/geocoder-1.8.6/lib/geocoder/lookups/ipbase.rb +49 -0
  59. data/geocoder-1.8.6/lib/geocoder/lookups/ipdata_co.rb +62 -0
  60. data/geocoder-1.8.6/lib/geocoder/lookups/ipgeolocation.rb +51 -0
  61. data/geocoder-1.8.6/lib/geocoder/lookups/ipinfo_io.rb +44 -0
  62. data/geocoder-1.8.6/lib/geocoder/lookups/ipinfo_io_lite.rb +42 -0
  63. data/geocoder-1.8.6/lib/geocoder/lookups/ipqualityscore.rb +50 -0
  64. data/geocoder-1.8.6/lib/geocoder/lookups/ipregistry.rb +68 -0
  65. data/geocoder-1.8.6/lib/geocoder/lookups/ipstack.rb +63 -0
  66. data/geocoder-1.8.6/lib/geocoder/lookups/latlon.rb +58 -0
  67. data/geocoder-1.8.6/lib/geocoder/lookups/location_iq.rb +54 -0
  68. data/geocoder-1.8.6/lib/geocoder/lookups/mapbox.rb +60 -0
  69. data/geocoder-1.8.6/lib/geocoder/lookups/mapquest.rb +58 -0
  70. data/geocoder-1.8.6/lib/geocoder/lookups/maxmind.rb +90 -0
  71. data/geocoder-1.8.6/lib/geocoder/lookups/maxmind_geoip2.rb +70 -0
  72. data/geocoder-1.8.6/lib/geocoder/lookups/maxmind_local.rb +71 -0
  73. data/geocoder-1.8.6/lib/geocoder/lookups/melissa_street.rb +41 -0
  74. data/geocoder-1.8.6/lib/geocoder/lookups/nationaal_georegister_nl.rb +38 -0
  75. data/geocoder-1.8.6/lib/geocoder/lookups/nominatim.rb +64 -0
  76. data/geocoder-1.8.6/lib/geocoder/lookups/opencagedata.rb +65 -0
  77. data/geocoder-1.8.6/lib/geocoder/lookups/osmnames.rb +57 -0
  78. data/geocoder-1.8.6/lib/geocoder/lookups/pc_miler.rb +87 -0
  79. data/geocoder-1.8.6/lib/geocoder/lookups/pdok_nl.rb +43 -0
  80. data/geocoder-1.8.6/lib/geocoder/lookups/pelias.rb +63 -0
  81. data/geocoder-1.8.6/lib/geocoder/lookups/photon.rb +89 -0
  82. data/geocoder-1.8.6/lib/geocoder/lookups/pickpoint.rb +41 -0
  83. data/geocoder-1.8.6/lib/geocoder/lookups/pointpin.rb +69 -0
  84. data/geocoder-1.8.6/lib/geocoder/lookups/postcode_anywhere_uk.rb +50 -0
  85. data/geocoder-1.8.6/lib/geocoder/lookups/postcodes_io.rb +31 -0
  86. data/geocoder-1.8.6/lib/geocoder/lookups/smarty_streets.rb +68 -0
  87. data/geocoder-1.8.6/lib/geocoder/lookups/telize.rb +75 -0
  88. data/geocoder-1.8.6/lib/geocoder/lookups/tencent.rb +59 -0
  89. data/geocoder-1.8.6/lib/geocoder/lookups/test.rb +49 -0
  90. data/geocoder-1.8.6/lib/geocoder/lookups/twogis.rb +58 -0
  91. data/geocoder-1.8.6/lib/geocoder/lookups/uk_ordnance_survey_names.rb +59 -0
  92. data/geocoder-1.8.6/lib/geocoder/lookups/yandex.rb +61 -0
  93. data/geocoder-1.8.6/lib/geocoder/models/active_record.rb +51 -0
  94. data/geocoder-1.8.6/lib/geocoder/models/base.rb +39 -0
  95. data/geocoder-1.8.6/lib/geocoder/models/mongo_base.rb +62 -0
  96. data/geocoder-1.8.6/lib/geocoder/models/mongo_mapper.rb +26 -0
  97. data/geocoder-1.8.6/lib/geocoder/models/mongoid.rb +32 -0
  98. data/geocoder-1.8.6/lib/geocoder/query.rb +125 -0
  99. data/geocoder-1.8.6/lib/geocoder/railtie.rb +26 -0
  100. data/geocoder-1.8.6/lib/geocoder/request.rb +114 -0
  101. data/geocoder-1.8.6/lib/geocoder/results/abstract_api.rb +146 -0
  102. data/geocoder-1.8.6/lib/geocoder/results/amap.rb +87 -0
  103. data/geocoder-1.8.6/lib/geocoder/results/amazon_location_service.rb +62 -0
  104. data/geocoder-1.8.6/lib/geocoder/results/azure.rb +65 -0
  105. data/geocoder-1.8.6/lib/geocoder/results/baidu.rb +75 -0
  106. data/geocoder-1.8.6/lib/geocoder/results/baidu_ip.rb +62 -0
  107. data/geocoder-1.8.6/lib/geocoder/results/ban_data_gouv_fr.rb +282 -0
  108. data/geocoder-1.8.6/lib/geocoder/results/base.rb +79 -0
  109. data/geocoder-1.8.6/lib/geocoder/results/bing.rb +52 -0
  110. data/geocoder-1.8.6/lib/geocoder/results/db_ip_com.rb +58 -0
  111. data/geocoder-1.8.6/lib/geocoder/results/esri.rb +78 -0
  112. data/geocoder-1.8.6/lib/geocoder/results/freegeoip.rb +40 -0
  113. data/geocoder-1.8.6/lib/geocoder/results/geoapify.rb +179 -0
  114. data/geocoder-1.8.6/lib/geocoder/results/geocoder_ca.rb +60 -0
  115. data/geocoder-1.8.6/lib/geocoder/results/geocodio.rb +78 -0
  116. data/geocoder-1.8.6/lib/geocoder/results/geoip2.rb +76 -0
  117. data/geocoder-1.8.6/lib/geocoder/results/geoportail_lu.rb +71 -0
  118. data/geocoder-1.8.6/lib/geocoder/results/google.rb +150 -0
  119. data/geocoder-1.8.6/lib/geocoder/results/google_places_details.rb +39 -0
  120. data/geocoder-1.8.6/lib/geocoder/results/google_places_search.rb +52 -0
  121. data/geocoder-1.8.6/lib/geocoder/results/google_premier.rb +6 -0
  122. data/geocoder-1.8.6/lib/geocoder/results/here.rb +77 -0
  123. data/geocoder-1.8.6/lib/geocoder/results/ip2location.rb +22 -0
  124. data/geocoder-1.8.6/lib/geocoder/results/ip2location_io.rb +21 -0
  125. data/geocoder-1.8.6/lib/geocoder/results/ip2location_lite.rb +47 -0
  126. data/geocoder-1.8.6/lib/geocoder/results/ipapi_com.rb +45 -0
  127. data/geocoder-1.8.6/lib/geocoder/results/ipbase.rb +40 -0
  128. data/geocoder-1.8.6/lib/geocoder/results/ipdata_co.rb +40 -0
  129. data/geocoder-1.8.6/lib/geocoder/results/ipgeolocation.rb +59 -0
  130. data/geocoder-1.8.6/lib/geocoder/results/ipinfo_io.rb +48 -0
  131. data/geocoder-1.8.6/lib/geocoder/results/ipinfo_io_lite.rb +16 -0
  132. data/geocoder-1.8.6/lib/geocoder/results/ipqualityscore.rb +54 -0
  133. data/geocoder-1.8.6/lib/geocoder/results/ipregistry.rb +304 -0
  134. data/geocoder-1.8.6/lib/geocoder/results/ipstack.rb +60 -0
  135. data/geocoder-1.8.6/lib/geocoder/results/latlon.rb +71 -0
  136. data/geocoder-1.8.6/lib/geocoder/results/location_iq.rb +6 -0
  137. data/geocoder-1.8.6/lib/geocoder/results/mapbox.rb +80 -0
  138. data/geocoder-1.8.6/lib/geocoder/results/mapquest.rb +48 -0
  139. data/geocoder-1.8.6/lib/geocoder/results/maxmind.rb +130 -0
  140. data/geocoder-1.8.6/lib/geocoder/results/maxmind_geoip2.rb +9 -0
  141. data/geocoder-1.8.6/lib/geocoder/results/maxmind_local.rb +44 -0
  142. data/geocoder-1.8.6/lib/geocoder/results/melissa_street.rb +46 -0
  143. data/geocoder-1.8.6/lib/geocoder/results/nationaal_georegister_nl.rb +62 -0
  144. data/geocoder-1.8.6/lib/geocoder/results/nominatim.rb +121 -0
  145. data/geocoder-1.8.6/lib/geocoder/results/opencagedata.rb +100 -0
  146. data/geocoder-1.8.6/lib/geocoder/results/osmnames.rb +56 -0
  147. data/geocoder-1.8.6/lib/geocoder/results/pc_miler.rb +98 -0
  148. data/geocoder-1.8.6/lib/geocoder/results/pdok_nl.rb +62 -0
  149. data/geocoder-1.8.6/lib/geocoder/results/pelias.rb +58 -0
  150. data/geocoder-1.8.6/lib/geocoder/results/photon.rb +119 -0
  151. data/geocoder-1.8.6/lib/geocoder/results/pickpoint.rb +6 -0
  152. data/geocoder-1.8.6/lib/geocoder/results/pointpin.rb +40 -0
  153. data/geocoder-1.8.6/lib/geocoder/results/postcode_anywhere_uk.rb +42 -0
  154. data/geocoder-1.8.6/lib/geocoder/results/postcodes_io.rb +40 -0
  155. data/geocoder-1.8.6/lib/geocoder/results/smarty_streets.rb +142 -0
  156. data/geocoder-1.8.6/lib/geocoder/results/telize.rb +40 -0
  157. data/geocoder-1.8.6/lib/geocoder/results/tencent.rb +72 -0
  158. data/geocoder-1.8.6/lib/geocoder/results/test.rb +33 -0
  159. data/geocoder-1.8.6/lib/geocoder/results/twogis.rb +76 -0
  160. data/geocoder-1.8.6/lib/geocoder/results/uk_ordnance_survey_names.rb +59 -0
  161. data/geocoder-1.8.6/lib/geocoder/results/yandex.rb +292 -0
  162. data/geocoder-1.8.6/lib/geocoder/sql.rb +110 -0
  163. data/geocoder-1.8.6/lib/geocoder/stores/active_record.rb +329 -0
  164. data/geocoder-1.8.6/lib/geocoder/stores/base.rb +115 -0
  165. data/geocoder-1.8.6/lib/geocoder/stores/mongo_base.rb +58 -0
  166. data/geocoder-1.8.6/lib/geocoder/stores/mongo_mapper.rb +13 -0
  167. data/geocoder-1.8.6/lib/geocoder/stores/mongoid.rb +13 -0
  168. data/geocoder-1.8.6/lib/geocoder/util.rb +29 -0
  169. data/geocoder-1.8.6/lib/geocoder/version.rb +3 -0
  170. data/geocoder-1.8.6/lib/geocoder.rb +48 -0
  171. data/geocoder-1.8.6/lib/maxmind_database.rb +109 -0
  172. data/geocoder-1.8.6/lib/tasks/geocoder.rake +54 -0
  173. data/geocoder-1.8.6/lib/tasks/maxmind.rake +73 -0
  174. data/ultra-fast-mod.gemspec +12 -0
  175. metadata +214 -0
@@ -0,0 +1,827 @@
1
+ Geocoder
2
+ ========
3
+
4
+ **Complete geocoding solution for Ruby.**
5
+
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
+
9
+ Key features:
10
+
11
+ * Forward and reverse geocoding.
12
+ * IP address geocoding.
13
+ * Connects to more than 40 APIs worldwide.
14
+ * Performance-enhancing features like caching.
15
+ * Integrates with ActiveRecord and Mongoid.
16
+ * Basic geospatial queries: search within radius (or rectangle, or ring).
17
+
18
+ Compatibility:
19
+
20
+ * Ruby versions: 2.1+, and JRuby.
21
+ * Databases: MySQL, PostgreSQL, SQLite, and MongoDB.
22
+ * Rails: 5.x, 6.x, and 7.x.
23
+ * Works outside of Rails with the `json` (for MRI) or `json_pure` (for JRuby) gem.
24
+
25
+
26
+ Table of Contents
27
+ -----------------
28
+
29
+ Basic Features:
30
+
31
+ * [Basic Search](#basic-search)
32
+ * [Geocoding Objects](#geocoding-objects)
33
+ * [Geospatial Database Queries](#geospatial-database-queries)
34
+ * [Geocoding HTTP Requests](#geocoding-http-requests)
35
+ * [Geocoding Service ("Lookup") Configuration](#geocoding-service-lookup-configuration)
36
+
37
+ Advanced Features:
38
+
39
+ * [Performance and Optimization](#performance-and-optimization)
40
+ * [Advanced Model Configuration](#advanced-model-configuration)
41
+ * [Advanced Database Queries](#advanced-database-queries)
42
+ * [Geospatial Calculations](#geospatial-calculations)
43
+ * [Batch Geocoding](#batch-geocoding)
44
+ * [Testing](#testing)
45
+ * [Error Handling](#error-handling)
46
+ * [Command Line Interface](#command-line-interface)
47
+
48
+ The Rest:
49
+
50
+ * [Technical Discussions](#technical-discussions)
51
+ * [Troubleshooting](#troubleshooting)
52
+ * [Known Issues](#known-issues)
53
+ * [Reporting Issues](https://github.com/alexreisner/geocoder/blob/master/CONTRIBUTING.md#reporting-bugs)
54
+ * [Contributing](https://github.com/alexreisner/geocoder/blob/master/CONTRIBUTING.md#making-changes)
55
+
56
+ See Also:
57
+
58
+ * [Guide to Geocoding APIs](https://github.com/alexreisner/geocoder/blob/master/README_API_GUIDE.md) (formerly part of this README)
59
+
60
+
61
+ Basic Search
62
+ ------------
63
+
64
+ In its simplest form, Geocoder takes an address and searches for its latitude/longitude coordinates:
65
+
66
+ ```ruby
67
+ results = Geocoder.search("Paris")
68
+ results.first.coordinates
69
+ # => [48.856614, 2.3522219] # latitude and longitude
70
+ ```
71
+
72
+ The reverse is possible too. Given coordinates, it finds an address:
73
+
74
+ ```ruby
75
+ results = Geocoder.search([48.856614, 2.3522219])
76
+ results.first.address
77
+ # => "Hôtel de Ville, 75004 Paris, France"
78
+ ```
79
+
80
+ You can also look up the location of an IP address:
81
+
82
+ ```ruby
83
+ results = Geocoder.search("172.56.21.89")
84
+ results.first.coordinates
85
+ # => [30.267153, -97.7430608]
86
+ results.first.country
87
+ # => "United States"
88
+ ```
89
+
90
+ **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).
91
+
92
+
93
+ Geocoding Objects
94
+ -----------------
95
+
96
+ To automatically geocode your objects:
97
+
98
+ **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:
99
+
100
+ ```ruby
101
+ def address
102
+ [street, city, state, country].compact.join(', ')
103
+ end
104
+ ```
105
+
106
+ **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.)
107
+
108
+ **3.** In your model, tell geocoder where to find the object's address:
109
+
110
+ ```ruby
111
+ geocoded_by :address
112
+ ```
113
+
114
+ This adds a `geocode` method which you can invoke via callback:
115
+
116
+ ```ruby
117
+ after_validation :geocode
118
+ ```
119
+
120
+ Reverse geocoding (given lat/lon coordinates, find an address) is similar:
121
+
122
+ ```ruby
123
+ reverse_geocoded_by :latitude, :longitude
124
+ after_validation :reverse_geocode
125
+ ```
126
+
127
+ With any geocoded objects, you can do the following:
128
+
129
+ ```ruby
130
+ obj.distance_to([43.9,-98.6]) # distance from obj to point
131
+ obj.bearing_to([43.9,-98.6]) # bearing from obj to point
132
+ obj.bearing_from(obj2) # bearing from obj2 to obj
133
+ ```
134
+
135
+ 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.
136
+
137
+ ### One More Thing for MongoDB!
138
+
139
+ Before you can call `geocoded_by` you'll need to include the necessary module using one of the following:
140
+
141
+ ```ruby
142
+ include Geocoder::Model::Mongoid
143
+ include Geocoder::Model::MongoMapper
144
+ ```
145
+
146
+ ### Latitude/Longitude Order in MongoDB
147
+
148
+ 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:
149
+
150
+ ```ruby
151
+ obj.to_coordinates # => [37.7941013, -122.3951096] # [lat, lon]
152
+ ```
153
+
154
+ 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:
155
+
156
+ ```ruby
157
+ obj.coordinates # => [-122.3951096, 37.7941013] # [lon, lat]
158
+ ```
159
+
160
+ So, be careful.
161
+
162
+ ### Use Outside of Rails
163
+
164
+ 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:
165
+
166
+ ```ruby
167
+ extend Geocoder::Model::ActiveRecord
168
+ ```
169
+
170
+
171
+ Geospatial Database Queries
172
+ ---------------------------
173
+
174
+ ### For ActiveRecord models:
175
+
176
+ To find objects by location, use the following scopes:
177
+
178
+ ```ruby
179
+ Venue.near('Omaha, NE, US') # venues within 20 miles of Omaha
180
+ Venue.near([40.71, -100.23], 50) # venues within 50 miles of a point
181
+ Venue.near([40.71, -100.23], 50, units: :km) # venues within 50 kilometres of a point
182
+ Venue.geocoded # venues with coordinates
183
+ Venue.not_geocoded # venues without coordinates
184
+ ```
185
+
186
+ With geocoded objects you can do things like this:
187
+
188
+ ```ruby
189
+ if obj.geocoded?
190
+ obj.nearbys(30) # other objects within 30 miles
191
+ obj.distance_from([40.714,-100.234]) # distance from arbitrary point to object
192
+ obj.bearing_to("Paris, France") # direction from object to arbitrary point
193
+ end
194
+ ```
195
+
196
+ ### For MongoDB-backed models:
197
+
198
+ 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.
199
+
200
+
201
+ Geocoding HTTP Requests
202
+ -----------------------
203
+
204
+ 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:
205
+
206
+ ```ruby
207
+ # returns Geocoder::Result object
208
+ result = request.location
209
+ ```
210
+
211
+ **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).
212
+
213
+ 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.
214
+
215
+
216
+ Geocoding Service ("Lookup") Configuration
217
+ ------------------------------------------
218
+
219
+ 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).
220
+
221
+ To create a Rails initializer with sample configuration:
222
+
223
+ ```sh
224
+ rails generate geocoder:config
225
+ ```
226
+
227
+ Some common options are:
228
+
229
+ ```ruby
230
+ # config/initializers/geocoder.rb
231
+ Geocoder.configure(
232
+ # street address geocoding service (default :nominatim)
233
+ lookup: :yandex,
234
+
235
+ # IP address geocoding service (default :ipinfo_io)
236
+ ip_lookup: :maxmind,
237
+
238
+ # to use an API key:
239
+ api_key: "...",
240
+
241
+ # geocoding service request timeout, in seconds (default 3):
242
+ timeout: 5,
243
+
244
+ # set default units to kilometers:
245
+ units: :km,
246
+
247
+ # caching (see Caching section below for details):
248
+ cache: Redis.new,
249
+ cache_options: {
250
+ expiration: 1.day, # Defaults to `nil`
251
+ prefix: "another_key:" # Defaults to `geocoder:`
252
+ }
253
+ )
254
+ ```
255
+
256
+ 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:
257
+
258
+ ```ruby
259
+ # with Google:
260
+ Geocoder.search("Middletown", bounds: [[40.6,-77.9], [39.9,-75.9]])
261
+ ```
262
+
263
+ 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:
264
+
265
+ ```ruby
266
+ # Nominatim's `countrycodes` parameter:
267
+ Geocoder.search("Rome", params: {countrycodes: "us,ca"})
268
+
269
+ # Google's `region` parameter:
270
+ Geocoder.search("Rome", params: {region: "..."})
271
+ ```
272
+
273
+ ### Configuring Multiple Services
274
+
275
+ You can configure multiple geocoding services at once by using the service's name as a key for a sub-configuration hash, like this:
276
+
277
+ ```ruby
278
+ Geocoder.configure(
279
+
280
+ timeout: 2,
281
+ cache: Redis.new,
282
+
283
+ yandex: {
284
+ api_key: "...",
285
+ timeout: 5
286
+ },
287
+
288
+ baidu: {
289
+ api_key: "..."
290
+ },
291
+
292
+ maxmind: {
293
+ api_key: "...",
294
+ service: :omni
295
+ }
296
+
297
+ )
298
+ ```
299
+
300
+ Lookup-specific settings override global settings so, in this example, the timeout for all lookups is 2 seconds, except for Yandex which is 5.
301
+
302
+
303
+ Performance and Optimization
304
+ ----------------------------
305
+
306
+ ### Database Indices
307
+
308
+ 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:
309
+
310
+ ```ruby
311
+ add_index :table, [:latitude, :longitude]
312
+ ```
313
+
314
+ 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:
315
+
316
+ ```ruby
317
+ include Geocoder::Model::Mongoid
318
+ geocoded_by :address, skip_index: true
319
+ ```
320
+
321
+ ### Avoiding Unnecessary API Requests
322
+
323
+ 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:
324
+
325
+ * an address is present
326
+ * the address has been changed since last save (or it has never been saved)
327
+
328
+ The exact code will vary depending on the method you use for your geocodable string, but it would be something like this:
329
+
330
+ ```ruby
331
+ after_validation :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }
332
+ ```
333
+
334
+ ### Caching
335
+
336
+ 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:
337
+
338
+ ```ruby
339
+ Geocoder.configure(cache: Redis.new)
340
+ ```
341
+
342
+ This example uses Redis, but the cache store can be any object that supports these methods:
343
+
344
+ * `store#[](key)` or `#get` or `#read` - retrieves a value
345
+ * `store#[]=(key, value)` or `#set` or `#write` - stores a value
346
+ * `store#del(url)` - deletes a value
347
+ * `store#keys` - (Optional) Returns array of keys. Used if you wish to expire the entire cache (see below).
348
+
349
+ 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).
350
+
351
+ When using Rails use the Generic cache store as an adapter around `Rails.cache`:
352
+
353
+ ```ruby
354
+ Geocoder.configure(cache: Geocoder::CacheStore::Generic.new(Rails.cache, {}))
355
+ ```
356
+
357
+ You can also set a custom prefix to be used for cache keys:
358
+
359
+ ```ruby
360
+ Geocoder.configure(cache_options: { prefix: "..." })
361
+ ```
362
+
363
+ By default the prefix is `geocoder:`
364
+
365
+ If you need to expire cached content:
366
+
367
+ ```ruby
368
+ Geocoder::Lookup.get(Geocoder.config[:lookup]).cache.expire(:all) # expire cached results for current Lookup
369
+ Geocoder::Lookup.get(:nominatim).cache.expire("http://...") # expire cached result for a specific URL
370
+ Geocoder::Lookup.get(:nominatim).cache.expire(:all) # expire cached results for Nominatim
371
+ # expire all cached results for all Lookups.
372
+ # Be aware that this methods spawns a new Lookup object for each Service
373
+ Geocoder::Lookup.all_services.each{|service| Geocoder::Lookup.get(service).cache.expire(:all)}
374
+ ```
375
+
376
+ 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.
377
+
378
+ In addition to conventional cache stores like Redis, it's possible to keep your cache in the database using `ActiveRecord`. For example see [this gist](https://gist.github.com/shqear93/4b07153b4ca7e4e4a41da492679f6c0e).
379
+
380
+
381
+ _Before you implement caching in your app please be sure that doing so does not violate the Terms of Service for your geocoding service._
382
+
383
+ Not all services support caching, [check the service limitations in the API guide for more information](https://github.com/alexreisner/geocoder/blob/master/README_API_GUIDE.md).
384
+
385
+ Advanced Model Configuration
386
+ ----------------------------
387
+
388
+ You are not stuck with the `latitude` and `longitude` database column names (with ActiveRecord) or the `coordinates` array (Mongo) for storing coordinates. For example:
389
+
390
+ ```ruby
391
+ geocoded_by :address, latitude: :lat, longitude: :lon # ActiveRecord
392
+ geocoded_by :address, coordinates: :coords # MongoDB
393
+ ```
394
+
395
+ For reverse geocoding, you can specify the attribute where the address will be stored. For example:
396
+
397
+ ```ruby
398
+ reverse_geocoded_by :latitude, :longitude, address: :loc # ActiveRecord
399
+ reverse_geocoded_by :coordinates, address: :street_address # MongoDB
400
+ ```
401
+
402
+ To specify geocoding parameters in your model:
403
+
404
+ ```ruby
405
+ geocoded_by :address, params: {region: "..."}
406
+ ```
407
+
408
+ 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:
409
+
410
+ ```ruby
411
+ geocoded_by :address, lookup: lambda{ |obj| obj.geocoder_lookup }
412
+
413
+ def geocoder_lookup
414
+ if country_code == "RU"
415
+ :yandex
416
+ elsif country_code == "CN"
417
+ :baidu
418
+ else
419
+ :nominatim
420
+ end
421
+ end
422
+ ```
423
+
424
+ ### Custom Result Handling
425
+
426
+ 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:
427
+
428
+ ```ruby
429
+ reverse_geocoded_by :latitude, :longitude do |obj,results|
430
+ if geo = results.first
431
+ obj.city = geo.city
432
+ obj.zipcode = geo.postal_code
433
+ obj.country = geo.country_code
434
+ end
435
+ end
436
+
437
+ after_validation :reverse_geocode
438
+ ```
439
+
440
+ Every `Geocoder::Result` object, `result`, provides the following data:
441
+
442
+ * `result.latitude` - float
443
+ * `result.longitude` - float
444
+ * `result.coordinates` - array of the above two in the form of `[lat,lon]`
445
+ * `result.address` - string
446
+ * `result.city` - string
447
+ * `result.state` - string
448
+ * `result.state_code` - string
449
+ * `result.postal_code` - string
450
+ * `result.country` - string
451
+ * `result.country_code` - string
452
+
453
+ 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.
454
+
455
+ ### Forward and Reverse Geocoding in the Same Model
456
+
457
+ 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:
458
+
459
+ * one for storing the fetched address (when reverse geocoding)
460
+ * one for providing an address to use when fetching coordinates (forward geocoding)
461
+
462
+ For example:
463
+
464
+ ```ruby
465
+ class Venue
466
+ # build an address from street, city, and state attributes
467
+ geocoded_by :address_from_components
468
+
469
+ # store the fetched address in the full_address attribute
470
+ reverse_geocoded_by :latitude, :longitude, address: :full_address
471
+ end
472
+ ```
473
+
474
+ 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:
475
+
476
+ ```ruby
477
+ class Venue
478
+ geocoded_by :address,
479
+ latitude: :fetched_latitude,
480
+ longitude: :fetched_longitude
481
+ reverse_geocoded_by :latitude, :longitude
482
+ end
483
+ ```
484
+
485
+
486
+ Advanced Database Queries
487
+ -------------------------
488
+
489
+ *The following apply to ActiveRecord only. For MongoDB, please use the built-in geospatial features.*
490
+
491
+ The default `near` search looks for objects within a circle. To search within a doughnut or ring use the `:min_radius` option:
492
+
493
+ ```ruby
494
+ Venue.near("Austin, TX", 200, min_radius: 40)
495
+ ```
496
+
497
+ To search within a rectangle (note that results will *not* include `distance` and `bearing` attributes):
498
+
499
+ ```ruby
500
+ sw_corner = [40.71, 100.23]
501
+ ne_corner = [36.12, 88.65]
502
+ Venue.within_bounding_box(sw_corner, ne_corner)
503
+ ```
504
+
505
+ 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:
506
+
507
+ ```ruby
508
+ Venue.near([40.71, 99.23], :effective_radius)
509
+ ```
510
+
511
+ If you store multiple sets of coordinates for each object, you can specify latitude and longitude columns to use for a search:
512
+
513
+ ```ruby
514
+ Venue.near("Paris", 50, latitude: :secondary_latitude, longitude: :secondary_longitude)
515
+ ```
516
+
517
+ ### Distance and Bearing
518
+
519
+ When you run a geospatial query, the returned objects have two attributes added:
520
+
521
+ * `obj.distance` - number of miles from the search point to this object
522
+ * `obj.bearing` - direction from the search point to this object
523
+
524
+ 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:
525
+
526
+ * `0` - due north
527
+ * `180` - due south
528
+ * `90` - due east
529
+ * `270` - due west
530
+ * `230.1` - southwest
531
+ * `359.9` - almost due north
532
+
533
+ You can convert these to compass point names via provided method:
534
+
535
+ ```ruby
536
+ Geocoder::Calculations.compass_point(355) # => "N"
537
+ Geocoder::Calculations.compass_point(45) # => "NE"
538
+ Geocoder::Calculations.compass_point(208) # => "SW"
539
+ ```
540
+
541
+ _Note: when running queries on SQLite, `distance` and `bearing` are provided for consistency only. They are not very accurate._
542
+
543
+ For more advanced geospatial querying, please see the [rgeo gem](https://github.com/rgeo/rgeo).
544
+
545
+
546
+ Geospatial Calculations
547
+ -----------------------
548
+
549
+ The `Geocoder::Calculations` module contains some useful methods:
550
+
551
+ ```ruby
552
+ # find the distance between two arbitrary points
553
+ Geocoder::Calculations.distance_between([47.858205,2.294359], [40.748433,-73.985655])
554
+ => 3619.77359999382 # in configured units (default miles)
555
+
556
+ # find the geographic center (aka center of gravity) of objects or points
557
+ Geocoder::Calculations.geographic_center([city1, city2, [40.22,-73.99], city4])
558
+ => [35.14968, -90.048929]
559
+ ```
560
+
561
+ See [the code](https://github.com/alexreisner/geocoder/blob/master/lib/geocoder/calculations.rb) for more!
562
+
563
+
564
+ Batch Geocoding
565
+ ---------------
566
+
567
+ 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:
568
+
569
+ ```sh
570
+ rake geocode:all CLASS=YourModel
571
+ ```
572
+
573
+ If you need reverse geocoding instead, call the task with REVERSE=true:
574
+
575
+ ```sh
576
+ rake geocode:all CLASS=YourModel REVERSE=true
577
+ ```
578
+
579
+ 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:
580
+
581
+ ```sh
582
+ rake geocode:all CLASS=YourModel SLEEP=0.25 BATCH=100
583
+ ```
584
+
585
+ To avoid exceeding per-day limits you can add a `LIMIT` option. However, this will ignore the `BATCH` value, if provided.
586
+
587
+ ```sh
588
+ rake geocode:all CLASS=YourModel LIMIT=1000
589
+ ```
590
+
591
+
592
+ Testing
593
+ -------
594
+
595
+ 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`
596
+
597
+ ```ruby
598
+ Geocoder.configure(lookup: :test, ip_lookup: :test)
599
+ ```
600
+
601
+ Add stubs to define the results that will be returned:
602
+
603
+ ```ruby
604
+ Geocoder::Lookup::Test.add_stub(
605
+ "New York, NY", [
606
+ {
607
+ 'coordinates' => [40.7143528, -74.0059731],
608
+ 'address' => 'New York, NY, USA',
609
+ 'state' => 'New York',
610
+ 'state_code' => 'NY',
611
+ 'country' => 'United States',
612
+ 'country_code' => 'US'
613
+ }
614
+ ]
615
+ )
616
+ ```
617
+
618
+ 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:
619
+
620
+ ```ruby
621
+ Geocoder::Lookup::Test.set_default_stub(
622
+ [
623
+ {
624
+ 'coordinates' => [40.7143528, -74.0059731],
625
+ 'address' => 'New York, NY, USA',
626
+ 'state' => 'New York',
627
+ 'state_code' => 'NY',
628
+ 'country' => 'United States',
629
+ 'country_code' => 'US'
630
+ }
631
+ ]
632
+ )
633
+ ```
634
+
635
+ You may also delete a single stub, or reset all stubs _including the default stub_:
636
+
637
+ ```ruby
638
+ Geocoder::Lookup::Test.delete_stub('New York, NY')
639
+ Geocoder::Lookup::Test.reset
640
+ ```
641
+
642
+ Notes:
643
+
644
+ - Keys must be strings (not symbols) when calling `add_stub` or `set_default_stub`. For example `'country' =>` not `:country =>`.
645
+ - 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.
646
+
647
+
648
+ Error Handling
649
+ --------------
650
+
651
+ 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:
652
+
653
+ ```ruby
654
+ Geocoder.configure(always_raise: [SocketError, Timeout::Error])
655
+ ```
656
+
657
+ You can also do this to raise all exceptions:
658
+
659
+ ```ruby
660
+ Geocoder.configure(always_raise: :all)
661
+ ```
662
+
663
+ The raise-able exceptions are:
664
+
665
+ ```ruby
666
+ SocketError
667
+ Timeout::Error
668
+ Geocoder::OverQueryLimitError
669
+ Geocoder::RequestDenied
670
+ Geocoder::InvalidRequest
671
+ Geocoder::InvalidApiKey
672
+ Geocoder::ServiceUnavailable
673
+ ```
674
+
675
+ 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.
676
+
677
+
678
+ Command Line Interface
679
+ ----------------------
680
+
681
+ When you install the Geocoder gem it adds a `geocode` command to your shell. You can search for a street address, IP address, postal code, coordinates, etc just like you can with the Geocoder.search method for example:
682
+
683
+ ```sh
684
+ $ geocode 29.951,-90.081
685
+ Latitude: 29.952211
686
+ Longitude: -90.080563
687
+ Full address: 1500 Sugar Bowl Dr, New Orleans, LA 70112, USA
688
+ City: New Orleans
689
+ State/province: Louisiana
690
+ Postal code: 70112
691
+ Country: United States
692
+ Map: http://maps.google.com/maps?q=29.952211,-90.080563
693
+ ```
694
+
695
+ 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.
696
+
697
+
698
+ Technical Discussions
699
+ ---------------------
700
+
701
+ ### Distance Queries in SQLite
702
+
703
+ 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.
704
+
705
+ There are few options for finding objects near a given point in SQLite without installing extensions:
706
+
707
+ 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.
708
+
709
+ 2. Load all objects into memory and compute distances between them using the `Geocoder::Calculations.distance_between` method. This will produce accurate results but will be very slow (and use a lot of memory) if you have a lot of objects in your database.
710
+
711
+ 3. If you have a large number of objects (so you can't use approach #2) and you need accurate results (better than approach #1 will give), you can use a combination of the two. Get all the objects within a square around your center point, and then eliminate the ones that are too far away using `Geocoder::Calculations.distance_between`.
712
+
713
+ 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.
714
+
715
+ ### Numeric Data Types and Precision
716
+
717
+ Geocoder works with any numeric data type (e.g. float, double, decimal) on which trig (and other mathematical) functions can be performed.
718
+
719
+ 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.
720
+
721
+
722
+ Troubleshooting
723
+ ---------------
724
+
725
+ ### Mongoid
726
+
727
+ If you get one of these errors:
728
+
729
+ ```ruby
730
+ uninitialized constant Geocoder::Model::Mongoid
731
+ uninitialized constant Geocoder::Model::Mongoid::Mongo
732
+ ```
733
+
734
+ you should check your Gemfile to make sure the Mongoid gem is listed _before_ Geocoder. If Mongoid isn't loaded when Geocoder is initialized, Geocoder will not load support for Mongoid.
735
+
736
+ ### ActiveRecord
737
+
738
+ A lot of debugging time can be saved by understanding how Geocoder works with ActiveRecord. When you use the `near` scope or the `nearbys` method of a geocoded object, Geocoder creates an ActiveModel::Relation object which adds some attributes (eg: distance, bearing) to the SELECT clause. It also adds a condition to the WHERE clause to check that distance is within the given radius. Because the SELECT clause is modified, anything else that modifies the SELECT clause may produce strange results, for example:
739
+
740
+ * using [`select` method (selects one or more columns)](https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-select)
741
+ * using the [`pluck` method (gets an array with selecting one or more columns)](https://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-pluck)
742
+ * The same problem will appear with [ActiveRecord's `ids` method](https://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-ids).
743
+ * specifying another model through [`includes` (selects columns from other tables)](https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-includes)
744
+ * See also Known Issues [using-near-with-includes](#using-near-with-select) section.
745
+
746
+ If you get an error in the above cases, try the following:
747
+
748
+ ```ruby
749
+ # Use the :select option with the near scope to get the columns you want.
750
+ # Instead of City.near(...).select(:id, :name), try:
751
+ City.near("Omaha, NE", 20, select: "id, name")
752
+
753
+ # Pass a :select option to the near scope to get the columns you want.
754
+ # Then, Ruby's built-in pluck method gets arrays you want.
755
+ # Instead of City.near(...).pluck(:id) or City.near(...).ids,, try:
756
+ City.near("Omaha, NE", 20, select: "id, name").to_a.pluck(:id, :name)
757
+ City.near("Omaha, NE", 20, select: "id").to_a.pluck(:id)
758
+
759
+ # Pass a :select option to the near scope to get the columns you want.
760
+ # Instead of City.near(...).includes(:venues), try:
761
+ City.near("Omaha, NE", 20, select: "cities.*, venues.*").joins(:venues)
762
+
763
+ # This preload call will normally trigger two queries regardless of the
764
+ # number of results; one query on hotels, and one query on administrators.
765
+ # Instead of Hotel.near(...).includes(:administrator), try:
766
+ Hotel.near("London, UK", 50).joins(:administrator).preload(:administrator)
767
+ ```
768
+
769
+ ### Geocoding is Slow
770
+
771
+ With most lookups, addresses are translated into coordinates via an API that must be accessed through the Internet. These requests are subject to the same bandwidth constraints as every other HTTP request, and will vary in speed depending on network conditions. Furthermore, many of the services supported by Geocoder are free and thus very popular. Often they cannot keep up with demand and their response times become quite bad.
772
+
773
+ If your application requires quick geocoding responses you will probably need to pay for a non-free service, or--if you're doing IP address geocoding--use a lookup that doesn't require an external (network-accessed) service.
774
+
775
+ For IP address lookups in Rails applications, it is generally NOT a good idea to run `request.location` during a synchronous page load without understanding the speed/behavior of your configured lookup. If the lookup becomes slow, so will your website.
776
+
777
+ For the most part, the speed of geocoding requests has little to do with the Geocoder gem. Please take the time to learn about your configured lookup before posting performance-related issues.
778
+
779
+ ### Unexpected Responses from Geocoding Services
780
+
781
+ Take a look at the server's raw response. You can do this by getting the request URL in an app console:
782
+
783
+ ```ruby
784
+ Geocoder::Lookup.get(:nominatim).query_url(Geocoder::Query.new("..."))
785
+ ```
786
+
787
+ 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.
788
+
789
+ You can also fetch the response in the console:
790
+
791
+ ```ruby
792
+ Geocoder::Lookup.get(:nominatim).send(:fetch_raw_data, Geocoder::Query.new("..."))
793
+ ```
794
+
795
+
796
+ Known Issues
797
+ ------------
798
+
799
+ ### Using `count` with Rails 4.1+
800
+
801
+ 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.
802
+
803
+ ### Using `near` with `includes`
804
+
805
+ 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).
806
+
807
+ Instead of using `includes` to reduce the number of database queries, try using `joins` with either the `:select` option or a call to `preload`. For example:
808
+
809
+ ```ruby
810
+ # Pass a :select option to the near scope to get the columns you want.
811
+ # Instead of City.near(...).includes(:venues), try:
812
+ City.near("Omaha, NE", 20, select: "cities.*, venues.*").joins(:venues)
813
+
814
+ # This preload call will normally trigger two queries regardless of the
815
+ # number of results; one query on hotels, and one query on administrators.
816
+ # Instead of Hotel.near(...).includes(:administrator), try:
817
+ Hotel.near("London, UK", 50).joins(:administrator).preload(:administrator)
818
+ ```
819
+
820
+ If anyone has a more elegant solution to this problem I am very interested in seeing it.
821
+
822
+ ### Using `near` with objects close to the 180th meridian
823
+
824
+ 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.
825
+
826
+
827
+ Copyright :copyright: 2009-2021 Alex Reisner, released under the MIT license.