geocoder 1.2.6 → 1.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +266 -1
- data/LICENSE +1 -1
- data/README.md +530 -804
- data/bin/console +13 -0
- data/examples/app_defined_lookup_services.rb +22 -0
- data/examples/reverse_geocode_job.rb +40 -0
- data/lib/easting_northing.rb +171 -0
- data/lib/generators/geocoder/config/templates/initializer.rb +22 -16
- data/lib/generators/geocoder/maxmind/geolite_city_generator.rb +2 -0
- data/lib/generators/geocoder/maxmind/geolite_country_generator.rb +2 -0
- data/lib/generators/geocoder/maxmind/templates/migration/geolite_city.rb +1 -1
- data/lib/generators/geocoder/maxmind/templates/migration/geolite_country.rb +1 -1
- data/lib/generators/geocoder/migration_version.rb +15 -0
- data/lib/geocoder/cache.rb +20 -33
- data/lib/geocoder/cache_stores/base.rb +40 -0
- data/lib/geocoder/cache_stores/generic.rb +35 -0
- data/lib/geocoder/cache_stores/redis.rb +34 -0
- data/lib/geocoder/calculations.rb +30 -38
- data/lib/geocoder/cli.rb +2 -2
- data/lib/geocoder/configuration.rb +36 -9
- data/lib/geocoder/configuration_hash.rb +4 -4
- data/lib/geocoder/esri_token.rb +38 -0
- data/lib/geocoder/exceptions.rb +19 -0
- data/lib/geocoder/ip_address.rb +26 -11
- data/lib/geocoder/kernel_logger.rb +25 -0
- data/lib/geocoder/logger.rb +47 -0
- data/lib/geocoder/lookup.rb +63 -13
- data/lib/geocoder/lookups/abstract_api.rb +46 -0
- data/lib/geocoder/lookups/amap.rb +63 -0
- data/lib/geocoder/lookups/amazon_location_service.rb +55 -0
- data/lib/geocoder/lookups/baidu.rb +17 -9
- data/lib/geocoder/lookups/baidu_ip.rb +7 -31
- data/lib/geocoder/lookups/ban_data_gouv_fr.rb +143 -0
- data/lib/geocoder/lookups/base.rb +75 -26
- data/lib/geocoder/lookups/bing.rb +38 -15
- data/lib/geocoder/lookups/db_ip_com.rb +52 -0
- data/lib/geocoder/lookups/dstk.rb +4 -2
- data/lib/geocoder/lookups/esri.rb +78 -12
- data/lib/geocoder/lookups/freegeoip.rb +22 -7
- data/lib/geocoder/lookups/geoapify.rb +78 -0
- data/lib/geocoder/lookups/geocoder_ca.rb +5 -6
- data/lib/geocoder/lookups/geocodio.rb +8 -8
- data/lib/geocoder/lookups/geoip2.rb +13 -4
- data/lib/geocoder/lookups/geoportail_lu.rb +65 -0
- data/lib/geocoder/lookups/google.rb +44 -11
- data/lib/geocoder/lookups/google_places_details.rb +31 -17
- data/lib/geocoder/lookups/google_places_search.rb +76 -0
- data/lib/geocoder/lookups/google_premier.rb +15 -1
- data/lib/geocoder/lookups/here.rb +38 -27
- data/lib/geocoder/lookups/ip2location.rb +71 -0
- data/lib/geocoder/lookups/ipapi_com.rb +82 -0
- data/lib/geocoder/lookups/ipbase.rb +49 -0
- data/lib/geocoder/lookups/ipdata_co.rb +62 -0
- data/lib/geocoder/lookups/ipgeolocation.rb +51 -0
- data/lib/geocoder/lookups/ipinfo_io.rb +44 -0
- data/lib/geocoder/lookups/ipqualityscore.rb +50 -0
- data/lib/geocoder/lookups/ipregistry.rb +68 -0
- data/lib/geocoder/lookups/ipstack.rb +63 -0
- data/lib/geocoder/lookups/latlon.rb +58 -0
- data/lib/geocoder/lookups/location_iq.rb +54 -0
- data/lib/geocoder/lookups/mapbox.rb +59 -0
- data/lib/geocoder/lookups/mapquest.rb +7 -9
- data/lib/geocoder/lookups/maxmind.rb +7 -7
- data/lib/geocoder/lookups/maxmind_geoip2.rb +70 -0
- data/lib/geocoder/lookups/maxmind_local.rb +16 -3
- data/lib/geocoder/lookups/melissa_street.rb +41 -0
- data/lib/geocoder/lookups/nationaal_georegister_nl.rb +38 -0
- data/lib/geocoder/lookups/nominatim.rb +18 -6
- data/lib/geocoder/lookups/opencagedata.rb +16 -9
- data/lib/geocoder/lookups/osmnames.rb +57 -0
- data/lib/geocoder/lookups/pc_miler.rb +85 -0
- data/lib/geocoder/lookups/pelias.rb +63 -0
- data/lib/geocoder/lookups/photon.rb +89 -0
- data/lib/geocoder/lookups/pickpoint.rb +41 -0
- data/lib/geocoder/lookups/pointpin.rb +14 -13
- data/lib/geocoder/lookups/postcode_anywhere_uk.rb +7 -8
- data/lib/geocoder/lookups/postcodes_io.rb +31 -0
- data/lib/geocoder/lookups/smarty_streets.rb +29 -6
- data/lib/geocoder/lookups/telize.rb +42 -7
- data/lib/geocoder/lookups/tencent.rb +59 -0
- data/lib/geocoder/lookups/test.rb +5 -0
- data/lib/geocoder/lookups/twogis.rb +58 -0
- data/lib/geocoder/lookups/uk_ordnance_survey_names.rb +59 -0
- data/lib/geocoder/lookups/yandex.rb +20 -13
- data/lib/geocoder/models/active_record.rb +4 -3
- data/lib/geocoder/models/mongo_base.rb +0 -2
- data/lib/geocoder/query.rb +15 -1
- data/lib/geocoder/railtie.rb +1 -1
- data/lib/geocoder/request.rb +103 -14
- data/lib/geocoder/results/abstract_api.rb +146 -0
- data/lib/geocoder/results/amap.rb +87 -0
- data/lib/geocoder/results/amazon_location_service.rb +57 -0
- data/lib/geocoder/results/baidu.rb +10 -14
- data/lib/geocoder/results/ban_data_gouv_fr.rb +282 -0
- data/lib/geocoder/results/base.rb +13 -1
- data/lib/geocoder/results/bing.rb +5 -1
- data/lib/geocoder/results/db_ip_com.rb +58 -0
- data/lib/geocoder/results/esri.rb +35 -8
- data/lib/geocoder/results/freegeoip.rb +2 -7
- data/lib/geocoder/results/geoapify.rb +179 -0
- data/lib/geocoder/results/geocoder_ca.rb +3 -3
- data/lib/geocoder/results/geocodio.rb +15 -3
- data/lib/geocoder/results/geoip2.rb +37 -25
- data/lib/geocoder/results/geoportail_lu.rb +71 -0
- data/lib/geocoder/results/google.rb +26 -0
- data/lib/geocoder/results/google_places_details.rb +4 -0
- data/lib/geocoder/results/google_places_search.rb +52 -0
- data/lib/geocoder/results/here.rb +30 -15
- data/lib/geocoder/results/ip2location.rb +22 -0
- data/lib/geocoder/results/ipapi_com.rb +45 -0
- data/lib/geocoder/results/ipbase.rb +40 -0
- data/lib/geocoder/results/ipdata_co.rb +40 -0
- data/lib/geocoder/results/ipgeolocation.rb +59 -0
- data/lib/geocoder/results/ipinfo_io.rb +48 -0
- data/lib/geocoder/results/ipqualityscore.rb +54 -0
- data/lib/geocoder/results/ipregistry.rb +304 -0
- data/lib/geocoder/results/ipstack.rb +60 -0
- data/lib/geocoder/results/latlon.rb +71 -0
- data/lib/geocoder/results/location_iq.rb +6 -0
- data/lib/geocoder/results/mapbox.rb +63 -0
- data/lib/geocoder/results/mapquest.rb +5 -8
- data/lib/geocoder/results/maxmind.rb +0 -5
- data/lib/geocoder/results/maxmind_geoip2.rb +9 -0
- data/lib/geocoder/results/maxmind_local.rb +0 -5
- data/lib/geocoder/results/melissa_street.rb +46 -0
- data/lib/geocoder/results/nationaal_georegister_nl.rb +62 -0
- data/lib/geocoder/results/nominatim.rb +41 -14
- data/lib/geocoder/results/opencagedata.rb +20 -2
- data/lib/geocoder/results/osmnames.rb +56 -0
- data/lib/geocoder/results/pc_miler.rb +98 -0
- data/lib/geocoder/results/pelias.rb +58 -0
- data/lib/geocoder/results/photon.rb +119 -0
- data/lib/geocoder/results/pickpoint.rb +6 -0
- data/lib/geocoder/results/pointpin.rb +0 -4
- data/lib/geocoder/results/postcodes_io.rb +40 -0
- data/lib/geocoder/results/smarty_streets.rb +55 -19
- 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/twogis.rb +76 -0
- data/lib/geocoder/results/uk_ordnance_survey_names.rb +59 -0
- data/lib/geocoder/results/yandex.rb +240 -32
- data/lib/geocoder/sql.rb +9 -6
- data/lib/geocoder/stores/active_record.rb +49 -10
- data/lib/geocoder/stores/base.rb +2 -14
- data/lib/geocoder/stores/mongo_base.rb +0 -31
- data/lib/geocoder/util.rb +29 -0
- data/lib/geocoder/version.rb +1 -1
- data/lib/geocoder.rb +2 -1
- data/lib/maxmind_database.rb +9 -9
- data/lib/tasks/geocoder.rake +29 -4
- data/lib/tasks/maxmind.rake +1 -1
- metadata +91 -169
- data/.gitignore +0 -6
- data/.travis.yml +0 -31
- data/Rakefile +0 -25
- data/examples/autoexpire_cache_dalli.rb +0 -62
- data/examples/autoexpire_cache_redis.rb +0 -28
- data/gemfiles/Gemfile.mongoid-2.4.x +0 -16
- data/lib/geocoder/lookups/geocoder_us.rb +0 -39
- data/lib/geocoder/lookups/okf.rb +0 -43
- data/lib/geocoder/lookups/ovi.rb +0 -62
- data/lib/geocoder/lookups/yahoo.rb +0 -88
- data/lib/geocoder/results/geocoder_us.rb +0 -39
- data/lib/geocoder/results/okf.rb +0 -106
- data/lib/geocoder/results/ovi.rb +0 -62
- data/lib/geocoder/results/yahoo.rb +0 -55
- data/lib/hash_recursive_merge.rb +0 -74
- data/lib/oauth_util.rb +0 -112
- data/test/fixtures/baidu_invalid_key +0 -1
- data/test/fixtures/baidu_ip_202_198_16_3 +0 -19
- data/test/fixtures/baidu_ip_invalid_key +0 -1
- data/test/fixtures/baidu_ip_no_results +0 -1
- data/test/fixtures/baidu_no_results +0 -1
- data/test/fixtures/baidu_reverse +0 -1
- data/test/fixtures/baidu_shanghai_pearl_tower +0 -12
- data/test/fixtures/bing_invalid_key +0 -1
- data/test/fixtures/bing_madison_square_garden +0 -40
- data/test/fixtures/bing_no_results +0 -16
- data/test/fixtures/bing_reverse +0 -42
- data/test/fixtures/cloudmade_invalid_key +0 -1
- data/test/fixtures/cloudmade_madison_square_garden +0 -1
- data/test/fixtures/cloudmade_no_results +0 -1
- data/test/fixtures/esri_madison_square_garden +0 -59
- data/test/fixtures/esri_no_results +0 -8
- data/test/fixtures/esri_reverse +0 -21
- data/test/fixtures/freegeoip_74_200_247_59 +0 -12
- data/test/fixtures/freegeoip_no_results +0 -1
- data/test/fixtures/geocoder_ca_madison_square_garden +0 -1
- data/test/fixtures/geocoder_ca_no_results +0 -1
- data/test/fixtures/geocoder_ca_reverse +0 -34
- data/test/fixtures/geocoder_us_madison_square_garden +0 -1
- data/test/fixtures/geocoder_us_no_results +0 -1
- data/test/fixtures/geocodio_1101_pennsylvania_ave +0 -1
- data/test/fixtures/geocodio_bad_api_key +0 -3
- data/test/fixtures/geocodio_invalid +0 -4
- data/test/fixtures/geocodio_no_results +0 -1
- data/test/fixtures/geocodio_over_query_limit +0 -4
- data/test/fixtures/google_garbage +0 -456
- data/test/fixtures/google_madison_square_garden +0 -57
- data/test/fixtures/google_no_city_data +0 -44
- data/test/fixtures/google_no_locality +0 -51
- data/test/fixtures/google_no_results +0 -4
- data/test/fixtures/google_over_limit +0 -4
- data/test/fixtures/google_places_details_invalid_request +0 -4
- data/test/fixtures/google_places_details_madison_square_garden +0 -120
- data/test/fixtures/google_places_details_no_results +0 -4
- data/test/fixtures/google_places_details_no_reviews +0 -60
- data/test/fixtures/google_places_details_no_types +0 -66
- data/test/fixtures/here_madison_square_garden +0 -72
- data/test/fixtures/here_no_results +0 -8
- data/test/fixtures/mapquest_error +0 -16
- data/test/fixtures/mapquest_invalid_api_key +0 -16
- data/test/fixtures/mapquest_invalid_request +0 -16
- data/test/fixtures/mapquest_madison_square_garden +0 -52
- data/test/fixtures/mapquest_no_results +0 -16
- data/test/fixtures/maxmind_24_24_24_21 +0 -1
- data/test/fixtures/maxmind_24_24_24_22 +0 -1
- data/test/fixtures/maxmind_24_24_24_23 +0 -1
- data/test/fixtures/maxmind_24_24_24_24 +0 -1
- data/test/fixtures/maxmind_74_200_247_59 +0 -1
- data/test/fixtures/maxmind_invalid_key +0 -1
- data/test/fixtures/maxmind_no_results +0 -1
- data/test/fixtures/nominatim_madison_square_garden +0 -150
- data/test/fixtures/nominatim_no_results +0 -1
- data/test/fixtures/nominatim_over_limit +0 -1
- data/test/fixtures/okf_kirstinmaki +0 -67
- data/test/fixtures/okf_no_results +0 -4
- data/test/fixtures/opencagedata_invalid_api_key +0 -25
- data/test/fixtures/opencagedata_invalid_request +0 -26
- data/test/fixtures/opencagedata_madison_square_garden +0 -73
- data/test/fixtures/opencagedata_no_results +0 -29
- data/test/fixtures/opencagedata_over_limit +0 -31
- data/test/fixtures/ovi_madison_square_garden +0 -72
- data/test/fixtures/ovi_no_results +0 -8
- data/test/fixtures/pointpin_10_10_10_10 +0 -1
- data/test/fixtures/pointpin_555_555_555_555 +0 -1
- data/test/fixtures/pointpin_80_111_555_555 +0 -1
- data/test/fixtures/pointpin_no_results +0 -1
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_WR26NJ +0 -1
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_generic_error +0 -1
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_hampshire +0 -1
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_key_limit_exceeded +0 -1
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_no_results +0 -1
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_romsey +0 -1
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_unknown_key +0 -1
- data/test/fixtures/smarty_streets_11211 +0 -1
- data/test/fixtures/smarty_streets_madison_square_garden +0 -47
- data/test/fixtures/smarty_streets_no_results +0 -1
- data/test/fixtures/telize_10_10_10_10 +0 -1
- data/test/fixtures/telize_555_555_555_555 +0 -4
- data/test/fixtures/telize_74_200_247_59 +0 -1
- data/test/fixtures/telize_no_results +0 -1
- data/test/fixtures/yahoo_error +0 -1
- data/test/fixtures/yahoo_invalid_key +0 -2
- data/test/fixtures/yahoo_madison_square_garden +0 -52
- data/test/fixtures/yahoo_no_results +0 -10
- data/test/fixtures/yahoo_over_limit +0 -2
- data/test/fixtures/yandex_canada_rue_dupuis_14 +0 -446
- data/test/fixtures/yandex_invalid_key +0 -1
- data/test/fixtures/yandex_kremlin +0 -48
- data/test/fixtures/yandex_new_york +0 -1
- data/test/fixtures/yandex_no_city_and_town +0 -112
- data/test/fixtures/yandex_no_results +0 -16
- data/test/integration/http_client_test.rb +0 -31
- data/test/mongoid_test_helper.rb +0 -43
- data/test/test_helper.rb +0 -416
- data/test/unit/active_record_test.rb +0 -16
- data/test/unit/cache_test.rb +0 -37
- data/test/unit/calculations_test.rb +0 -220
- data/test/unit/configuration_test.rb +0 -55
- data/test/unit/error_handling_test.rb +0 -56
- data/test/unit/geocoder_test.rb +0 -78
- data/test/unit/https_test.rb +0 -17
- data/test/unit/ip_address_test.rb +0 -27
- data/test/unit/lookup_test.rb +0 -153
- data/test/unit/lookups/bing_test.rb +0 -68
- data/test/unit/lookups/dstk_test.rb +0 -26
- data/test/unit/lookups/esri_test.rb +0 -48
- data/test/unit/lookups/freegeoip_test.rb +0 -27
- data/test/unit/lookups/geocoder_ca_test.rb +0 -17
- data/test/unit/lookups/geocodio_test.rb +0 -55
- data/test/unit/lookups/geoip2_test.rb +0 -27
- data/test/unit/lookups/google_places_details_test.rb +0 -122
- data/test/unit/lookups/google_premier_test.rb +0 -22
- data/test/unit/lookups/google_test.rb +0 -84
- data/test/unit/lookups/mapquest_test.rb +0 -60
- data/test/unit/lookups/maxmind_local_test.rb +0 -28
- data/test/unit/lookups/maxmind_test.rb +0 -63
- data/test/unit/lookups/nominatim_test.rb +0 -31
- data/test/unit/lookups/okf_test.rb +0 -38
- data/test/unit/lookups/opencagedata_test.rb +0 -64
- data/test/unit/lookups/pointpin_test.rb +0 -30
- data/test/unit/lookups/postcode_anywhere_uk_test.rb +0 -70
- data/test/unit/lookups/smarty_streets_test.rb +0 -71
- data/test/unit/lookups/telize_test.rb +0 -36
- data/test/unit/lookups/yahoo_test.rb +0 -35
- data/test/unit/method_aliases_test.rb +0 -26
- data/test/unit/model_test.rb +0 -38
- data/test/unit/mongoid_test.rb +0 -47
- data/test/unit/near_test.rb +0 -87
- data/test/unit/oauth_util_test.rb +0 -31
- data/test/unit/proxy_test.rb +0 -37
- data/test/unit/query_test.rb +0 -52
- data/test/unit/rake_task_test.rb +0 -21
- data/test/unit/request_test.rb +0 -35
- data/test/unit/result_test.rb +0 -72
- data/test/unit/test_mode_test.rb +0 -70
data/README.md
CHANGED
|
@@ -1,960 +1,704 @@
|
|
|
1
1
|
Geocoder
|
|
2
2
|
========
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
**Complete geocoding solution for Ruby.**
|
|
5
5
|
|
|
6
|
+
[](http://badge.fury.io/rb/geocoder)
|
|
7
|
+
[](https://codeclimate.com/github/alexreisner/geocoder)
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
-------------
|
|
9
|
+
Key features:
|
|
9
10
|
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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).
|
|
14
17
|
|
|
18
|
+
Compatibility:
|
|
15
19
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
Installation
|
|
23
|
-
------------
|
|
24
|
-
|
|
25
|
-
Install Geocoder like any other Ruby gem:
|
|
26
|
-
|
|
27
|
-
gem install geocoder
|
|
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.
|
|
28
24
|
|
|
29
|
-
Or, if you're using Rails/Bundler, add this to your Gemfile:
|
|
30
25
|
|
|
31
|
-
|
|
26
|
+
Table of Contents
|
|
27
|
+
-----------------
|
|
32
28
|
|
|
33
|
-
|
|
29
|
+
Basic Features:
|
|
34
30
|
|
|
35
|
-
|
|
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
36
|
|
|
37
|
+
Advanced Features:
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
|
|
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)
|
|
40
47
|
|
|
41
|
-
|
|
48
|
+
The Rest:
|
|
42
49
|
|
|
43
|
-
|
|
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)
|
|
44
55
|
|
|
45
|
-
|
|
46
|
-
rake db:migrate
|
|
56
|
+
See Also:
|
|
47
57
|
|
|
48
|
-
|
|
58
|
+
* [Guide to Geocoding APIs](https://github.com/alexreisner/geocoder/blob/master/README_API_GUIDE.md) (formerly part of this README)
|
|
49
59
|
|
|
50
|
-
Next, your model must tell Geocoder which method returns your object's geocodable address:
|
|
51
60
|
|
|
52
|
-
|
|
53
|
-
|
|
61
|
+
Basic Search
|
|
62
|
+
------------
|
|
54
63
|
|
|
55
|
-
|
|
64
|
+
In its simplest form, Geocoder takes an address and searches for its latitude/longitude coordinates:
|
|
56
65
|
|
|
57
|
-
|
|
58
|
-
|
|
66
|
+
```ruby
|
|
67
|
+
results = Geocoder.search("Paris")
|
|
68
|
+
results.first.coordinates
|
|
69
|
+
# => [48.856614, 2.3522219] # latitude and longitude
|
|
70
|
+
```
|
|
59
71
|
|
|
60
|
-
|
|
72
|
+
The reverse is possible too. Given coordinates, it finds an address:
|
|
61
73
|
|
|
62
|
-
|
|
74
|
+
```ruby
|
|
75
|
+
results = Geocoder.search([48.856614, 2.3522219])
|
|
76
|
+
results.first.address
|
|
77
|
+
# => "Hôtel de Ville, 75004 Paris, France"
|
|
78
|
+
```
|
|
63
79
|
|
|
64
|
-
|
|
80
|
+
You can also look up the location of an IP address:
|
|
65
81
|
|
|
66
|
-
|
|
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
|
+
```
|
|
67
89
|
|
|
68
|
-
|
|
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).
|
|
69
91
|
|
|
70
|
-
but if you store address components (city, state, country, etc) in separate fields you can instead define a method called `address` that combines them into a single string which will be used to query the geocoding service.
|
|
71
92
|
|
|
72
|
-
|
|
93
|
+
Geocoding Objects
|
|
94
|
+
-----------------
|
|
73
95
|
|
|
74
|
-
|
|
75
|
-
geocoded_by :address # can also be an IP address
|
|
76
|
-
after_validation :geocode # auto-fetch coordinates
|
|
96
|
+
To automatically geocode your objects:
|
|
77
97
|
|
|
78
|
-
|
|
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:
|
|
79
99
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
100
|
+
```ruby
|
|
101
|
+
def address
|
|
102
|
+
[street, city, state, country].compact.join(', ')
|
|
103
|
+
end
|
|
104
|
+
```
|
|
83
105
|
|
|
84
|
-
|
|
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.)
|
|
85
107
|
|
|
86
|
-
|
|
108
|
+
**3.** In your model, tell geocoder where to find the object's address:
|
|
87
109
|
|
|
88
|
-
|
|
110
|
+
```ruby
|
|
111
|
+
geocoded_by :address
|
|
112
|
+
```
|
|
89
113
|
|
|
90
|
-
|
|
114
|
+
This adds a `geocode` method which you can invoke via callback:
|
|
91
115
|
|
|
92
|
-
|
|
116
|
+
```ruby
|
|
117
|
+
after_validation :geocode
|
|
118
|
+
```
|
|
93
119
|
|
|
94
|
-
|
|
120
|
+
Reverse geocoding (given lat/lon coordinates, find an address) is similar:
|
|
95
121
|
|
|
96
|
-
|
|
122
|
+
```ruby
|
|
123
|
+
reverse_geocoded_by :latitude, :longitude
|
|
124
|
+
after_validation :reverse_geocode
|
|
125
|
+
```
|
|
97
126
|
|
|
98
|
-
|
|
99
|
-
geocoded_by :address, :skip_index => true
|
|
127
|
+
With any geocoded objects, you can do the following:
|
|
100
128
|
|
|
101
|
-
|
|
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
|
+
```
|
|
102
134
|
|
|
103
|
-
|
|
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.
|
|
104
136
|
|
|
105
|
-
|
|
137
|
+
### One More Thing for MongoDB!
|
|
106
138
|
|
|
107
|
-
|
|
139
|
+
Before you can call `geocoded_by` you'll need to include the necessary module using one of the following:
|
|
108
140
|
|
|
109
|
-
|
|
141
|
+
```ruby
|
|
142
|
+
include Geocoder::Model::Mongoid
|
|
143
|
+
include Geocoder::Model::MongoMapper
|
|
144
|
+
```
|
|
110
145
|
|
|
111
|
-
###
|
|
146
|
+
### Latitude/Longitude Order in MongoDB
|
|
112
147
|
|
|
113
|
-
|
|
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:
|
|
114
149
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
The exact code will vary depending on the method you use for your geocodable string, but it would be something like this:
|
|
150
|
+
```ruby
|
|
151
|
+
obj.to_coordinates # => [37.7941013, -122.3951096] # [lat, lon]
|
|
152
|
+
```
|
|
119
153
|
|
|
120
|
-
|
|
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:
|
|
121
155
|
|
|
156
|
+
```ruby
|
|
157
|
+
obj.coordinates # => [-122.3951096, 37.7941013] # [lon, lat]
|
|
158
|
+
```
|
|
122
159
|
|
|
123
|
-
|
|
124
|
-
-------------------------------
|
|
160
|
+
So, be careful.
|
|
125
161
|
|
|
126
|
-
|
|
162
|
+
### Use Outside of Rails
|
|
127
163
|
|
|
128
|
-
|
|
129
|
-
result = request.location
|
|
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:
|
|
130
165
|
|
|
131
|
-
|
|
166
|
+
```ruby
|
|
167
|
+
extend Geocoder::Model::ActiveRecord
|
|
168
|
+
```
|
|
132
169
|
|
|
133
|
-
See _Advanced Geocoding_ below for more information about `Geocoder::Result` objects.
|
|
134
170
|
|
|
171
|
+
Geospatial Database Queries
|
|
172
|
+
---------------------------
|
|
135
173
|
|
|
136
|
-
|
|
137
|
-
-------------------------------
|
|
174
|
+
### For ActiveRecord models:
|
|
138
175
|
|
|
139
176
|
To find objects by location, use the following scopes:
|
|
140
177
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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
|
+
```
|
|
147
185
|
|
|
148
186
|
With geocoded objects you can do things like this:
|
|
149
187
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
+
```
|
|
155
195
|
|
|
156
|
-
|
|
196
|
+
### For MongoDB-backed models:
|
|
157
197
|
|
|
158
|
-
|
|
159
|
-
Geocoder.coordinates("25 Main St, Cooperstown, NY")
|
|
160
|
-
=> [42.700149, -74.922767]
|
|
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.
|
|
161
199
|
|
|
162
|
-
# distance (in miles) between Eiffel Tower and Empire State Building
|
|
163
|
-
Geocoder::Calculations.distance_between([47.858205,2.294359], [40.748433,-73.985655])
|
|
164
|
-
=> 3619.77359999382
|
|
165
200
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
=> [35.14968, -90.048929]
|
|
201
|
+
Geocoding HTTP Requests
|
|
202
|
+
-----------------------
|
|
169
203
|
|
|
170
|
-
|
|
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:
|
|
171
205
|
|
|
206
|
+
```ruby
|
|
207
|
+
# returns Geocoder::Result object
|
|
208
|
+
result = request.location
|
|
209
|
+
```
|
|
172
210
|
|
|
173
|
-
|
|
174
|
-
--------------------
|
|
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).
|
|
175
212
|
|
|
176
|
-
|
|
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.
|
|
177
214
|
|
|
178
|
-
* `obj.distance` - number of miles from the search point to this object
|
|
179
|
-
* `obj.bearing` - direction from the search point to this object
|
|
180
215
|
|
|
181
|
-
|
|
216
|
+
Geocoding Service ("Lookup") Configuration
|
|
217
|
+
------------------------------------------
|
|
182
218
|
|
|
183
|
-
|
|
184
|
-
* `180` - due south
|
|
185
|
-
* `90` - due east
|
|
186
|
-
* `270` - due west
|
|
187
|
-
* `230.1` - southwest
|
|
188
|
-
* `359.9` - almost due north
|
|
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).
|
|
189
220
|
|
|
190
|
-
|
|
221
|
+
To create a Rails initializer with sample configuration:
|
|
191
222
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
223
|
+
```sh
|
|
224
|
+
rails generate geocoder:config
|
|
225
|
+
```
|
|
195
226
|
|
|
196
|
-
|
|
227
|
+
Some common options are:
|
|
197
228
|
|
|
198
|
-
|
|
229
|
+
```ruby
|
|
230
|
+
# config/initializers/geocoder.rb
|
|
231
|
+
Geocoder.configure(
|
|
232
|
+
# street address geocoding service (default :nominatim)
|
|
233
|
+
lookup: :yandex,
|
|
199
234
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
obj.bearing_from(obj2) # bearing from obj2 to obj
|
|
235
|
+
# IP address geocoding service (default :ipinfo_io)
|
|
236
|
+
ip_lookup: :maxmind,
|
|
203
237
|
|
|
204
|
-
|
|
238
|
+
# to use an API key:
|
|
239
|
+
api_key: "...",
|
|
205
240
|
|
|
241
|
+
# geocoding service request timeout, in seconds (default 3):
|
|
242
|
+
timeout: 5,
|
|
206
243
|
|
|
207
|
-
|
|
208
|
-
|
|
244
|
+
# set default units to kilometers:
|
|
245
|
+
units: :km,
|
|
209
246
|
|
|
210
|
-
|
|
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
|
+
```
|
|
211
255
|
|
|
212
|
-
|
|
213
|
-
geocoded_by :address, :coordinates => :coords # MongoDB
|
|
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:
|
|
214
257
|
|
|
215
|
-
|
|
258
|
+
```ruby
|
|
259
|
+
# with Google:
|
|
260
|
+
Geocoder.search("Middletown", bounds: [[40.6,-77.9], [39.9,-75.9]])
|
|
261
|
+
```
|
|
216
262
|
|
|
217
|
-
|
|
218
|
-
geocoded_by :end_address, latitude: :end_latitude, longitude: :end_longitude
|
|
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:
|
|
219
264
|
|
|
220
|
-
|
|
265
|
+
```ruby
|
|
266
|
+
# Nominatim's `countrycodes` parameter:
|
|
267
|
+
Geocoder.search("Rome", params: {countrycodes: "us,ca"})
|
|
221
268
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
269
|
+
# Google's `region` parameter:
|
|
270
|
+
Geocoder.search("Rome", params: {region: "..."})
|
|
271
|
+
```
|
|
225
272
|
|
|
226
|
-
|
|
273
|
+
### Configuring Multiple Services
|
|
227
274
|
|
|
228
|
-
|
|
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:
|
|
229
276
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
end
|
|
277
|
+
```ruby
|
|
278
|
+
Geocoder.configure(
|
|
233
279
|
|
|
234
|
-
|
|
280
|
+
timeout: 2,
|
|
281
|
+
cache: Redis.new,
|
|
235
282
|
|
|
236
|
-
|
|
237
|
-
|
|
283
|
+
yandex: {
|
|
284
|
+
api_key: "...",
|
|
285
|
+
timeout: 5
|
|
286
|
+
},
|
|
238
287
|
|
|
239
|
-
|
|
288
|
+
baidu: {
|
|
289
|
+
api_key: "..."
|
|
290
|
+
},
|
|
240
291
|
|
|
241
|
-
|
|
292
|
+
maxmind: {
|
|
293
|
+
api_key: "...",
|
|
294
|
+
service: :omni
|
|
295
|
+
}
|
|
242
296
|
|
|
243
|
-
|
|
297
|
+
)
|
|
298
|
+
```
|
|
244
299
|
|
|
245
|
-
|
|
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.
|
|
246
301
|
|
|
247
|
-
def geocoder_lookup
|
|
248
|
-
if country_code == "RU"
|
|
249
|
-
:yandex
|
|
250
|
-
elsif country_code == "CN"
|
|
251
|
-
:baidu
|
|
252
|
-
else
|
|
253
|
-
:google
|
|
254
|
-
end
|
|
255
|
-
end
|
|
256
302
|
|
|
303
|
+
Performance and Optimization
|
|
304
|
+
----------------------------
|
|
257
305
|
|
|
258
|
-
|
|
259
|
-
-----------------
|
|
306
|
+
### Database Indices
|
|
260
307
|
|
|
261
|
-
|
|
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:
|
|
262
309
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
Venue.within_bounding_box(box)
|
|
310
|
+
```ruby
|
|
311
|
+
add_index :table, [:latitude, :longitude]
|
|
312
|
+
```
|
|
267
313
|
|
|
268
|
-
|
|
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:
|
|
269
315
|
|
|
270
|
-
|
|
271
|
-
|
|
316
|
+
```ruby
|
|
317
|
+
include Geocoder::Model::Mongoid
|
|
318
|
+
geocoded_by :address, skip_index: true
|
|
319
|
+
```
|
|
272
320
|
|
|
273
|
-
|
|
321
|
+
### Avoiding Unnecessary API Requests
|
|
274
322
|
|
|
275
|
-
|
|
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:
|
|
276
324
|
|
|
277
|
-
|
|
325
|
+
* an address is present
|
|
326
|
+
* the address has been changed since last save (or it has never been saved)
|
|
278
327
|
|
|
328
|
+
The exact code will vary depending on the method you use for your geocodable string, but it would be something like this:
|
|
279
329
|
|
|
280
|
-
|
|
281
|
-
|
|
330
|
+
```ruby
|
|
331
|
+
after_validation :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }
|
|
332
|
+
```
|
|
282
333
|
|
|
283
|
-
|
|
334
|
+
### Caching
|
|
284
335
|
|
|
285
|
-
|
|
286
|
-
if geo = results.first
|
|
287
|
-
obj.city = geo.city
|
|
288
|
-
obj.zipcode = geo.postal_code
|
|
289
|
-
obj.country = geo.country_code
|
|
290
|
-
end
|
|
291
|
-
end
|
|
292
|
-
after_validation :reverse_geocode
|
|
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:
|
|
293
337
|
|
|
294
|
-
|
|
338
|
+
```ruby
|
|
339
|
+
Geocoder.configure(cache: Redis.new)
|
|
340
|
+
```
|
|
295
341
|
|
|
296
|
-
|
|
297
|
-
* `result.longitude` - float
|
|
298
|
-
* `result.coordinates` - array of the above two
|
|
299
|
-
* `result.address` - string
|
|
300
|
-
* `result.city` - string
|
|
301
|
-
* `result.state` - string
|
|
302
|
-
* `result.state_code` - string
|
|
303
|
-
* `result.postal_code` - string
|
|
304
|
-
* `result.country` - string
|
|
305
|
-
* `result.country_code` - string
|
|
306
|
-
|
|
307
|
-
If you're familiar with the results returned by the geocoding service you're using you can access even more data, but you'll need to be familiar with the particular `Geocoder::Result` object you're using and the structure of your geocoding service's responses. (See below for links to geocoding service documentation.)
|
|
342
|
+
This example uses Redis, but the cache store can be any object that supports these methods:
|
|
308
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).
|
|
309
348
|
|
|
310
|
-
|
|
311
|
-
------------------------------------------
|
|
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).
|
|
312
350
|
|
|
313
|
-
|
|
351
|
+
When using Rails use the Generic cache store as an adapter around `Rails.cache`:
|
|
314
352
|
|
|
315
|
-
|
|
353
|
+
```ruby
|
|
354
|
+
Geocoder.configure(cache: Geocoder::CacheStore::Generic.new(Rails.cache, {}))
|
|
355
|
+
```
|
|
316
356
|
|
|
317
|
-
|
|
357
|
+
You can also set a custom prefix to be used for cache keys:
|
|
318
358
|
|
|
319
|
-
|
|
359
|
+
```ruby
|
|
360
|
+
Geocoder.configure(cache_options: { prefix: "..." })
|
|
361
|
+
```
|
|
320
362
|
|
|
321
|
-
|
|
322
|
-
Geocoder.configure(
|
|
363
|
+
By default the prefix is `geocoder:`
|
|
323
364
|
|
|
324
|
-
|
|
325
|
-
:lookup => :yandex,
|
|
365
|
+
If you need to expire cached content:
|
|
326
366
|
|
|
327
|
-
|
|
328
|
-
|
|
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
|
+
```
|
|
329
375
|
|
|
330
|
-
|
|
331
|
-
:api_key => "...",
|
|
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.
|
|
332
377
|
|
|
333
|
-
|
|
334
|
-
:timeout => 5,
|
|
378
|
+
_Before you implement caching in your app please be sure that doing so does not violate the Terms of Service for your geocoding service._
|
|
335
379
|
|
|
336
|
-
|
|
337
|
-
:units => :km,
|
|
380
|
+
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).
|
|
338
381
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
:cache_prefix => "..."
|
|
382
|
+
Advanced Model Configuration
|
|
383
|
+
----------------------------
|
|
342
384
|
|
|
343
|
-
|
|
385
|
+
You are not stuck with the `latitude` and `longitude` database column names (with ActiveRecord) or the `coordinates` array (Mongo) for storing coordinates. For example:
|
|
344
386
|
|
|
345
|
-
|
|
387
|
+
```ruby
|
|
388
|
+
geocoded_by :address, latitude: :lat, longitude: :lon # ActiveRecord
|
|
389
|
+
geocoded_by :address, coordinates: :coords # MongoDB
|
|
390
|
+
```
|
|
346
391
|
|
|
347
|
-
|
|
348
|
-
Geocoder.search("Paris", :bounds => [[32.1,-95.9], [33.9,-94.3]])
|
|
392
|
+
For reverse geocoding, you can specify the attribute where the address will be stored. For example:
|
|
349
393
|
|
|
350
|
-
|
|
394
|
+
```ruby
|
|
395
|
+
reverse_geocoded_by :latitude, :longitude, address: :loc # ActiveRecord
|
|
396
|
+
reverse_geocoded_by :coordinates, address: :street_address # MongoDB
|
|
397
|
+
```
|
|
351
398
|
|
|
352
|
-
|
|
353
|
-
Geocoder.search("Paris", :params => {:countrycodes => "gb,de,fr,es,us"})
|
|
399
|
+
To specify geocoding parameters in your model:
|
|
354
400
|
|
|
355
|
-
|
|
401
|
+
```ruby
|
|
402
|
+
geocoded_by :address, params: {region: "..."}
|
|
403
|
+
```
|
|
356
404
|
|
|
357
|
-
|
|
405
|
+
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:
|
|
358
406
|
|
|
359
|
-
|
|
360
|
-
|
|
407
|
+
```ruby
|
|
408
|
+
geocoded_by :address, lookup: lambda{ |obj| obj.geocoder_lookup }
|
|
361
409
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
410
|
+
def geocoder_lookup
|
|
411
|
+
if country_code == "RU"
|
|
412
|
+
:yandex
|
|
413
|
+
elsif country_code == "CN"
|
|
414
|
+
:baidu
|
|
415
|
+
else
|
|
416
|
+
:nominatim
|
|
417
|
+
end
|
|
418
|
+
end
|
|
419
|
+
```
|
|
366
420
|
|
|
367
|
-
|
|
368
|
-
:api_key => "..."
|
|
369
|
-
},
|
|
421
|
+
### Custom Result Handling
|
|
370
422
|
|
|
371
|
-
|
|
372
|
-
:api_key => "...",
|
|
373
|
-
:service => :omni
|
|
374
|
-
}
|
|
423
|
+
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:
|
|
375
424
|
|
|
376
|
-
|
|
425
|
+
```ruby
|
|
426
|
+
reverse_geocoded_by :latitude, :longitude do |obj,results|
|
|
427
|
+
if geo = results.first
|
|
428
|
+
obj.city = geo.city
|
|
429
|
+
obj.zipcode = geo.postal_code
|
|
430
|
+
obj.country = geo.country_code
|
|
431
|
+
end
|
|
432
|
+
end
|
|
377
433
|
|
|
378
|
-
|
|
434
|
+
after_validation :reverse_geocode
|
|
435
|
+
```
|
|
379
436
|
|
|
437
|
+
Every `Geocoder::Result` object, `result`, provides the following data:
|
|
380
438
|
|
|
381
|
-
|
|
439
|
+
* `result.latitude` - float
|
|
440
|
+
* `result.longitude` - float
|
|
441
|
+
* `result.coordinates` - array of the above two in the form of `[lat,lon]`
|
|
442
|
+
* `result.address` - string
|
|
443
|
+
* `result.city` - string
|
|
444
|
+
* `result.state` - string
|
|
445
|
+
* `result.state_code` - string
|
|
446
|
+
* `result.postal_code` - string
|
|
447
|
+
* `result.country` - string
|
|
448
|
+
* `result.country_code` - string
|
|
382
449
|
|
|
383
|
-
|
|
450
|
+
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.
|
|
384
451
|
|
|
385
|
-
|
|
452
|
+
### Forward and Reverse Geocoding in the Same Model
|
|
386
453
|
|
|
387
|
-
|
|
388
|
-
* **Key signup**: https://developers.google.com/maps/documentation/business/
|
|
389
|
-
* **Quota**: 2,500 requests/day, 100,000 with Google Maps API Premier
|
|
390
|
-
* **Region**: world
|
|
391
|
-
* **SSL support**: yes
|
|
392
|
-
* **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)
|
|
393
|
-
* **Extra options**: `:bounds` - pass SW and NE coordinates as an array of two arrays to bias results towards a viewport
|
|
394
|
-
* **Documentation**: http://code.google.com/apis/maps/documentation/geocoding/#JSON
|
|
395
|
-
* **Terms of Service**: http://code.google.com/apis/maps/terms.html#section_10_12
|
|
396
|
-
* **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..."
|
|
397
|
-
* **Notes**: To use Google Premier set `Geocoder.configure(:lookup => :google_premier, :api_key => [key, client, channel])`.
|
|
454
|
+
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:
|
|
398
455
|
|
|
399
|
-
|
|
456
|
+
* one for storing the fetched address (when reverse geocoding)
|
|
457
|
+
* one for providing an address to use when fetching coordinates (forward geocoding)
|
|
400
458
|
|
|
401
|
-
|
|
459
|
+
For example:
|
|
402
460
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
* **SSL support**: yes
|
|
408
|
-
* **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)
|
|
409
|
-
* **Documentation**: https://developers.google.com/places/documentation/details
|
|
410
|
-
* **Terms of Service**: https://developers.google.com/places/policies
|
|
411
|
-
* **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."
|
|
461
|
+
```ruby
|
|
462
|
+
class Venue
|
|
463
|
+
# build an address from street, city, and state attributes
|
|
464
|
+
geocoded_by :address_from_components
|
|
412
465
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
* **Quota**: unlimited, but subject to usage fees
|
|
418
|
-
* **Region**: world
|
|
419
|
-
* **SSL support**: no
|
|
420
|
-
* **Languages**: en, fr, de, it, es, pt, nl, zh, ja, ko
|
|
421
|
-
* **Documentation**: http://developer.yahoo.com/boss/geo/docs/index.html
|
|
422
|
-
* **Terms of Service**: http://info.yahoo.com/legal/us/yahoo/boss/tou/?pir=ucJPcJ1ibUn.h.d.lVmlcbcEkoHjwJ_PvxG9SLK9VIbIQAw1XFrnDqY-
|
|
423
|
-
* **Limitations**: No mass downloads, no commercial map production based on the data, no storage of data except for caching.
|
|
424
|
-
|
|
425
|
-
#### Bing (`:bing`)
|
|
426
|
-
|
|
427
|
-
* **API key**: required (set `Geocoder.configure(:lookup => :bing, :api_key => key)`)
|
|
428
|
-
* **Key signup**: http://www.bingmapsportal.com
|
|
429
|
-
* **Quota**: 50,000 requests/24 hrs
|
|
430
|
-
* **Region**: world
|
|
431
|
-
* **SSL support**: no
|
|
432
|
-
* **Languages**: ?
|
|
433
|
-
* **Documentation**: http://msdn.microsoft.com/en-us/library/ff701715.aspx
|
|
434
|
-
* **Terms of Service**: http://www.microsoft.com/maps/product/terms.html
|
|
435
|
-
* **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."
|
|
436
|
-
|
|
437
|
-
#### Nominatim (`:nominatim`)
|
|
438
|
-
|
|
439
|
-
* **API key**: none
|
|
440
|
-
* **Quota**: 1 request/second
|
|
441
|
-
* **Region**: world
|
|
442
|
-
* **SSL support**: no
|
|
443
|
-
* **Languages**: ?
|
|
444
|
-
* **Documentation**: http://wiki.openstreetmap.org/wiki/Nominatim
|
|
445
|
-
* **Terms of Service**: http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy
|
|
446
|
-
* **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 CC-BY-SA (you must provide attribution).
|
|
447
|
-
|
|
448
|
-
#### OpenCageData (`:opencagedata`)
|
|
449
|
-
|
|
450
|
-
* **API key**: required
|
|
451
|
-
* **Key signup**: http://geocoder.opencagedata.com
|
|
452
|
-
* **Quota**: 2500 requests / day, then ability to purchase more (free during beta)
|
|
453
|
-
* **Region**: world
|
|
454
|
-
* **SSL support**: yes
|
|
455
|
-
* **Languages**: worldwide
|
|
456
|
-
* **Documentation**: http://geocoder.opencagedata.com/api.html
|
|
457
|
-
* **Limitations**: Data licensed under CC-BY-SA or (you must provide attribution).
|
|
458
|
-
|
|
459
|
-
#### Yandex (`:yandex`)
|
|
460
|
-
|
|
461
|
-
* **API key**: none
|
|
462
|
-
* **Quota**: 25000 requests / day
|
|
463
|
-
* **Region**: world
|
|
464
|
-
* **SSL support**: no
|
|
465
|
-
* **Languages**: Russian, Belarusian, Ukrainian, English, Turkish (only for maps of Turkey)
|
|
466
|
-
* **Documentation**: http://api.yandex.com.tr/maps/doc/intro/concepts/intro.xml
|
|
467
|
-
* **Terms of Service**: http://api.yandex.com.tr/maps/doc/intro/concepts/intro.xml#rules
|
|
468
|
-
* **Limitations**: ?
|
|
469
|
-
|
|
470
|
-
#### Geocoder.ca (`:geocoder_ca`)
|
|
471
|
-
|
|
472
|
-
* **API key**: none
|
|
473
|
-
* **Quota**: ?
|
|
474
|
-
* **Region**: US and Canada
|
|
475
|
-
* **SSL support**: no
|
|
476
|
-
* **Languages**: English
|
|
477
|
-
* **Documentation**: ?
|
|
478
|
-
* **Terms of Service**: http://geocoder.ca/?terms=1
|
|
479
|
-
* **Limitations**: "Under no circumstances can our data be re-distributed or re-sold by anyone to other parties without our written permission."
|
|
480
|
-
|
|
481
|
-
#### Geocoder.us (`:geocoder_us`)
|
|
482
|
-
|
|
483
|
-
* **API key**: HTTP Basic Auth
|
|
484
|
-
* **Sign up**: http://geocoder.us/user/signup
|
|
485
|
-
* **Quota**: You can purchase 20,000 credits at a time for $50
|
|
486
|
-
* **Region**: US
|
|
487
|
-
* **SSL support**: no
|
|
488
|
-
* **Languages**: English
|
|
489
|
-
* **Documentation**: http://geocoder.us/help/
|
|
490
|
-
* **Terms of Service**: http://geocoder.us/terms.shtml
|
|
491
|
-
* **Limitations**: ?
|
|
492
|
-
|
|
493
|
-
#### Mapquest (`:mapquest`)
|
|
494
|
-
|
|
495
|
-
* **API key**: required
|
|
496
|
-
* **Key signup**: http://developer.mapquest.com/web/products/open
|
|
497
|
-
* **Quota**: ?
|
|
498
|
-
* **HTTP Headers**: in order to use the licensed API you can configure the http_headers to include a referer as so:
|
|
499
|
-
`Geocoder.configure(:http_headers => { "Referer" => "http://foo.com" })`
|
|
500
|
-
You can also allow a blank referer from the API management console via mapquest but it is potentially a security risk that someone else could use your API key from another domain.
|
|
501
|
-
* **Region**: world
|
|
502
|
-
* **SSL support**: no
|
|
503
|
-
* **Languages**: English
|
|
504
|
-
* **Documentation**: http://www.mapquestapi.com/geocoding/
|
|
505
|
-
* **Terms of Service**: http://info.mapquest.com/terms-of-use/
|
|
506
|
-
* **Limitations**: ?
|
|
507
|
-
* **Notes**: You can specify the licensed API by setting: `Geocoder.configure(:mapquest => {:licensed => true})` (defaults to free "open" version)
|
|
508
|
-
|
|
509
|
-
#### Ovi/Nokia (`:ovi`)
|
|
510
|
-
|
|
511
|
-
* **API key**: not required, but performance restricted without it
|
|
512
|
-
* **Quota**: ?
|
|
513
|
-
* **Region**: world
|
|
514
|
-
* **SSL support**: no
|
|
515
|
-
* **Languages**: English
|
|
516
|
-
* **Documentation**: http://api.maps.ovi.com/devguide/overview.html
|
|
517
|
-
* **Terms of Service**: http://www.developer.nokia.com/Develop/Maps/TC.html
|
|
518
|
-
* **Limitations**: ?
|
|
519
|
-
|
|
520
|
-
#### Here/Nokia (`:here`)
|
|
521
|
-
|
|
522
|
-
* **API key**: required
|
|
523
|
-
* **Quota**: Depending on the API key
|
|
524
|
-
* **Region**: world
|
|
525
|
-
* **SSL support**: yes
|
|
526
|
-
* **Languages**: The preferred language of address elements in the result. Language code must be provided according to RFC 4647 standard.
|
|
527
|
-
* **Documentation**: http://developer.here.com/rest-apis/documentation/geocoder
|
|
528
|
-
* **Terms of Service**: http://developer.here.com/faqs#l&t
|
|
529
|
-
* **Limitations**: ?
|
|
530
|
-
|
|
531
|
-
#### ESRI (`:esri`)
|
|
532
|
-
|
|
533
|
-
* **API key**: none
|
|
534
|
-
* **Quota**: Required for some scenarios (see Terms of Service)
|
|
535
|
-
* **Region**: world
|
|
536
|
-
* **SSL support**: yes
|
|
537
|
-
* **Languages**: English
|
|
538
|
-
* **Documentation**: http://resources.arcgis.com/en/help/arcgis-online-geocoding-rest-api/
|
|
539
|
-
* **Terms of Service**: http://www.esri.com/legal/software-license
|
|
540
|
-
* **Limitations**: ?
|
|
541
|
-
* **Notes**: You can specify which projection you want to use by setting, for example: `Geocoder.configure(:esri => {:outSR => 102100})`.
|
|
542
|
-
|
|
543
|
-
#### Data Science Toolkit (`:dstk`)
|
|
544
|
-
|
|
545
|
-
Data Science Toolkit provides an API whose reponse format is like Google's but which can be set up as a privately hosted service.
|
|
546
|
-
|
|
547
|
-
* **API key**: none
|
|
548
|
-
* **Quota**: None quota if you are self-hosting the service.
|
|
549
|
-
* **Region**: world
|
|
550
|
-
* **SSL support**: ?
|
|
551
|
-
* **Languages**: en
|
|
552
|
-
* **Documentation**: http://www.datasciencetoolkit.org/developerdocs
|
|
553
|
-
* **Terms of Service**: http://www.datasciencetoolkit.org/developerdocs#googlestylegeocoder
|
|
554
|
-
* **Limitations**: No reverse geocoding.
|
|
555
|
-
* **Notes**: If you are hosting your own DSTK server you will need to configure the host name, eg: `Geocoder.configure(:lookup => :dstk, :host => "localhost:4567")`.
|
|
556
|
-
|
|
557
|
-
#### Baidu (`:baidu`)
|
|
558
|
-
|
|
559
|
-
* **API key**: required
|
|
560
|
-
* **Quota**: No quota limits for geocoding
|
|
561
|
-
* **Region**: China
|
|
562
|
-
* **SSL support**: no
|
|
563
|
-
* **Languages**: Chinese (Simplified)
|
|
564
|
-
* **Documentation**: http://developer.baidu.com/map/webservice-geocoding.htm
|
|
565
|
-
* **Terms of Service**: http://developer.baidu.com/map/law.htm
|
|
566
|
-
* **Limitations**: Only good for non-commercial use. For commercial usage please check http://developer.baidu.com/map/question.htm#qa0013
|
|
567
|
-
* **Notes**: To use Baidu set `Geocoder.configure(:lookup => :baidu, :api_key => "your_api_key")`.
|
|
568
|
-
|
|
569
|
-
#### Geocodio (`:geocodio`)
|
|
570
|
-
|
|
571
|
-
* **API key**: required
|
|
572
|
-
* **Quota**: 2,500 free requests/day then purchase $.001 for each
|
|
573
|
-
* **Region**: US
|
|
574
|
-
* **SSL support**: no
|
|
575
|
-
* **Languages**: en
|
|
576
|
-
* **Documentation**: http://geocod.io/docs
|
|
577
|
-
* **Terms of Service**: http://geocod.io/terms-of-use
|
|
578
|
-
* **Limitations**: ?
|
|
579
|
-
|
|
580
|
-
#### SmartyStreets (`:smarty_streets`)
|
|
581
|
-
|
|
582
|
-
* **API key**: requires auth_id and auth_token (set `Geocoder.configure(:api_key => [id, token])`)
|
|
583
|
-
* **Quota**: 10,000 free, 250/month then purchase at sliding scale.
|
|
584
|
-
* **Region**: US
|
|
585
|
-
* **SSL support**: yes
|
|
586
|
-
* **Languages**: en
|
|
587
|
-
* **Documentation**: http://smartystreets.com/kb/liveaddress-api/rest-endpoint
|
|
588
|
-
* **Terms of Service**: http://smartystreets.com/legal/terms-of-service
|
|
589
|
-
* **Limitations**: No reverse geocoding.
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
#### OKF Geocoder (`:okf`)
|
|
593
|
-
|
|
594
|
-
* **API key**: none
|
|
595
|
-
* **Quota**: none
|
|
596
|
-
* **Region**: FI
|
|
597
|
-
* **SSL support**: no
|
|
598
|
-
* **Languages**: fi
|
|
599
|
-
* **Documentation**: http://books.okf.fi/geocoder/_full/
|
|
600
|
-
* **Terms of Service**: http://www.itella.fi/liitteet/palvelutjatuotteet/yhteystietopalvelut/Postinumeropalvelut-Palvelukuvausjakayttoehdot.pdf
|
|
601
|
-
* **Limitations**: ?
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
#### PostcodeAnywhere Uk (`:postcode_anywhere_uk`)
|
|
605
|
-
|
|
606
|
-
This uses the PostcodeAnywhere UK Geocode service, this will geocode any string from UK postcode, placename, point of interest or location.
|
|
607
|
-
|
|
608
|
-
* **API key**: required
|
|
609
|
-
* **Quota**: Dependant on service plan?
|
|
610
|
-
* **Region**: UK
|
|
611
|
-
* **SSL support**: yes
|
|
612
|
-
* **Languages**: English
|
|
613
|
-
* **Documentation**: [http://www.postcodeanywhere.co.uk/Support/WebService/Geocoding/UK/Geocode/2/](http://www.postcodeanywhere.co.uk/Support/WebService/Geocoding/UK/Geocode/2/)
|
|
614
|
-
* **Terms of Service**: ?
|
|
615
|
-
* **Limitations**: ?
|
|
616
|
-
* **Notes**: To use PostcodeAnywhere you must include an API key: `Geocoder.configure(:lookup => :postcode_anywhere_uk, :api_key => 'your_api_key')`.
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
### IP Address Services
|
|
620
|
-
|
|
621
|
-
#### FreeGeoIP (`:freegeoip`)
|
|
622
|
-
|
|
623
|
-
* **API key**: none
|
|
624
|
-
* **Quota**: 10000 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.
|
|
625
|
-
* **Region**: world
|
|
626
|
-
* **SSL support**: no
|
|
627
|
-
* **Languages**: English
|
|
628
|
-
* **Documentation**: http://github.com/fiorix/freegeoip/blob/master/README.md
|
|
629
|
-
* **Terms of Service**: ?
|
|
630
|
-
* **Limitations**: ?
|
|
631
|
-
* **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: "..."})`.
|
|
632
|
-
|
|
633
|
-
#### Pointpin (`:pointpin`)
|
|
634
|
-
|
|
635
|
-
* **API key**: required
|
|
636
|
-
* **Quota**: 50,000/mo for €9 through 1m/mo for €49
|
|
637
|
-
* **Region**: world
|
|
638
|
-
* **SSL support**: yes
|
|
639
|
-
* **Languages**: English
|
|
640
|
-
* **Documentation**: https://pointp.in/docs/get-started
|
|
641
|
-
* **Terms of Service**: https://pointp.in/terms
|
|
642
|
-
* **Limitations**: ?
|
|
643
|
-
* **Notes**: To use Pointpin set `Geocoder.configure(:ip_lookup => :pointpin, :api_key => "your_pointpin_api_key")`.
|
|
644
|
-
|
|
645
|
-
#### Telize (`:telize`)
|
|
646
|
-
|
|
647
|
-
* **API key**: none
|
|
648
|
-
* **Quota**: none
|
|
649
|
-
* **Region**: world
|
|
650
|
-
* **SSL support**: no
|
|
651
|
-
* **Languages**: English
|
|
652
|
-
* **Documentation**: http://www.telize.com/
|
|
653
|
-
* **Terms of Service**: ?
|
|
654
|
-
* **Limitations**: ?
|
|
655
|
-
|
|
656
|
-
#### MaxMind Web Services (`:maxmind`)
|
|
657
|
-
|
|
658
|
-
* **API key**: required
|
|
659
|
-
* **Quota**: Request Packs can be purchased
|
|
660
|
-
* **Region**: world
|
|
661
|
-
* **SSL support**: yes
|
|
662
|
-
* **Languages**: English
|
|
663
|
-
* **Documentation**: http://www.maxmind.com/app/web_services
|
|
664
|
-
* **Terms of Service**: ?
|
|
665
|
-
* **Limitations**: ?
|
|
666
|
-
* **Notes**: You must specify which MaxMind service you are using in your configuration. For example: `Geocoder.configure(:maxmind => {:service => :omni})`.
|
|
667
|
-
|
|
668
|
-
#### MaxMind Local (`:maxmind_local`) - EXPERIMENTAL
|
|
669
|
-
|
|
670
|
-
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.
|
|
671
|
-
|
|
672
|
-
* **API key**: none (requires the GeoLite City database which can be downloaded from [MaxMind](http://dev.maxmind.com/geoip/legacy/geolite/))
|
|
673
|
-
* **Quota**: none
|
|
674
|
-
* **Region**: world
|
|
675
|
-
* **SSL support**: N/A
|
|
676
|
-
* **Languages**: English
|
|
677
|
-
* **Documentation**: http://www.maxmind.com/en/city
|
|
678
|
-
* **Terms of Service**: ?
|
|
679
|
-
* **Limitations**: ?
|
|
680
|
-
* **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.**
|
|
681
|
-
|
|
682
|
-
**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:
|
|
683
|
-
|
|
684
|
-
Geocoder.configure(ip_lookup: :maxmind_local, maxmind_local: {file: File.join('folder', 'GeoLiteCity.dat')})
|
|
685
|
-
|
|
686
|
-
**To use a CSV file** you must import it into an SQL database. The GeoLite *City* and *Country* packages are supported. Configure like so:
|
|
687
|
-
|
|
688
|
-
Geocoder.configure(ip_lookup: :maxmind_local, maxmind_local: {package: :city})
|
|
689
|
-
|
|
690
|
-
You can generate ActiveRecord migrations and download and import data via provided rake tasks:
|
|
691
|
-
|
|
692
|
-
# generate migration to create tables
|
|
693
|
-
rails generate geocoder:maxmind:geolite PACKAGE=city
|
|
694
|
-
|
|
695
|
-
# download, unpack, and import data
|
|
696
|
-
rake geocoder:maxmind:geolite:load PACKAGE=city
|
|
697
|
-
|
|
698
|
-
You can replace `city` with `country` in any of the above tasks, generators, and configurations.
|
|
699
|
-
|
|
700
|
-
#### Baidu IP (`:baidu_ip`)
|
|
701
|
-
|
|
702
|
-
* **API key**: required
|
|
703
|
-
* **Quota**: No quota limits for geocoding
|
|
704
|
-
* **Region**: China
|
|
705
|
-
* **SSL support**: no
|
|
706
|
-
* **Languages**: Chinese (Simplified)
|
|
707
|
-
* **Documentation**: http://developer.baidu.com/map/webservice-geocoding.htm
|
|
708
|
-
* **Terms of Service**: http://developer.baidu.com/map/law.htm
|
|
709
|
-
* **Limitations**: Only good for non-commercial use. For commercial usage please check http://developer.baidu.com/map/question.htm#qa0013
|
|
710
|
-
* **Notes**: To use Baidu set `Geocoder.configure(:lookup => :baidu_ip, :api_key => "your_api_key")`.
|
|
711
|
-
|
|
712
|
-
#### GeoLite2 (`:geoip2`)
|
|
713
|
-
|
|
714
|
-
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.
|
|
715
|
-
|
|
716
|
-
* **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/geoip2/))
|
|
717
|
-
* **Quota**: none
|
|
718
|
-
* **Region**: world
|
|
719
|
-
* **SSL support**: N/A
|
|
720
|
-
* **Languages**: English
|
|
721
|
-
* **Documentation**: http://www.maxmind.com/en/city
|
|
722
|
-
* **Terms of Service**: ?
|
|
723
|
-
* **Limitations**: ?
|
|
724
|
-
* **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:
|
|
725
|
-
|
|
726
|
-
Geocoder.configure(
|
|
727
|
-
ip_lookup: :geoip2,
|
|
728
|
-
geoip2: {
|
|
729
|
-
file: File.join('folder', 'GeoLite2-City.mmdb')
|
|
730
|
-
}
|
|
731
|
-
)
|
|
732
|
-
|
|
733
|
-
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`:
|
|
734
|
-
|
|
735
|
-
Geocoder.configure(
|
|
736
|
-
ip_lookup: :geoip2,
|
|
737
|
-
geoip2: {
|
|
738
|
-
lib: 'hive_geoip2',
|
|
739
|
-
file: File.join('folder', 'GeoLite2-City.mmdb')
|
|
740
|
-
}
|
|
741
|
-
)
|
|
742
|
-
|
|
743
|
-
Caching
|
|
744
|
-
-------
|
|
466
|
+
# store the fetched address in the full_address attribute
|
|
467
|
+
reverse_geocoded_by :latitude, :longitude, address: :full_address
|
|
468
|
+
end
|
|
469
|
+
```
|
|
745
470
|
|
|
746
|
-
|
|
471
|
+
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:
|
|
747
472
|
|
|
748
|
-
|
|
473
|
+
```ruby
|
|
474
|
+
class Venue
|
|
475
|
+
geocoded_by :address,
|
|
476
|
+
latitude: :fetched_latitude,
|
|
477
|
+
longitude: :fetched_longitude
|
|
478
|
+
reverse_geocoded_by :latitude, :longitude
|
|
479
|
+
end
|
|
480
|
+
```
|
|
749
481
|
|
|
750
|
-
This example uses Redis, but the cache store can be any object that supports these methods:
|
|
751
482
|
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
* `store#del(url)` - deletes a value
|
|
483
|
+
Advanced Database Queries
|
|
484
|
+
-------------------------
|
|
755
485
|
|
|
756
|
-
|
|
486
|
+
*The following apply to ActiveRecord only. For MongoDB, please use the built-in geospatial features.*
|
|
757
487
|
|
|
758
|
-
|
|
488
|
+
The default `near` search looks for objects within a circle. To search within a doughnut or ring use the `:min_radius` option:
|
|
759
489
|
|
|
760
|
-
|
|
490
|
+
```ruby
|
|
491
|
+
Venue.near("Austin, TX", 200, min_radius: 40)
|
|
492
|
+
```
|
|
761
493
|
|
|
762
|
-
|
|
494
|
+
To search within a rectangle (note that results will *not* include `distance` and `bearing` attributes):
|
|
763
495
|
|
|
764
|
-
|
|
496
|
+
```ruby
|
|
497
|
+
sw_corner = [40.71, 100.23]
|
|
498
|
+
ne_corner = [36.12, 88.65]
|
|
499
|
+
Venue.within_bounding_box(sw_corner, ne_corner)
|
|
500
|
+
```
|
|
765
501
|
|
|
766
|
-
|
|
767
|
-
Geocoder::Lookup.get(:google).cache.expire("http://...") # expire cached result for a specific URL
|
|
768
|
-
Geocoder::Lookup.get(:google).cache.expire(:all) # expire cached results for Google Lookup
|
|
769
|
-
# expire all cached results for all Lookups.
|
|
770
|
-
# Be aware that this methods spawns a new Lookup object for each Service
|
|
771
|
-
Geocoder::Lookup.all_services.each{|service| Geocoder::Lookup.get(service).cache.expire(:all)}
|
|
502
|
+
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:
|
|
772
503
|
|
|
773
|
-
|
|
504
|
+
```ruby
|
|
505
|
+
Venue.near([40.71, 99.23], :effective_radius)
|
|
506
|
+
```
|
|
774
507
|
|
|
775
|
-
|
|
508
|
+
If you store multiple sets of coordinates for each object, you can specify latitude and longitude columns to use for a search:
|
|
776
509
|
|
|
777
|
-
|
|
510
|
+
```ruby
|
|
511
|
+
Venue.near("Paris", 50, latitude: :secondary_latitude, longitude: :secondary_longitude)
|
|
512
|
+
```
|
|
778
513
|
|
|
514
|
+
### Distance and Bearing
|
|
779
515
|
|
|
780
|
-
|
|
781
|
-
-----------------------------------------------
|
|
516
|
+
When you run a geospatial query, the returned objects have two attributes added:
|
|
782
517
|
|
|
783
|
-
|
|
518
|
+
* `obj.distance` - number of miles from the search point to this object
|
|
519
|
+
* `obj.bearing` - direction from the search point to this object
|
|
784
520
|
|
|
785
|
-
|
|
786
|
-
* one for providing an address to use when fetching coordinates (forward geocoding)
|
|
521
|
+
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:
|
|
787
522
|
|
|
788
|
-
|
|
523
|
+
* `0` - due north
|
|
524
|
+
* `180` - due south
|
|
525
|
+
* `90` - due east
|
|
526
|
+
* `270` - due west
|
|
527
|
+
* `230.1` - southwest
|
|
528
|
+
* `359.9` - almost due north
|
|
789
529
|
|
|
790
|
-
|
|
530
|
+
You can convert these to compass point names via provided method:
|
|
791
531
|
|
|
792
|
-
|
|
793
|
-
|
|
532
|
+
```ruby
|
|
533
|
+
Geocoder::Calculations.compass_point(355) # => "N"
|
|
534
|
+
Geocoder::Calculations.compass_point(45) # => "NE"
|
|
535
|
+
Geocoder::Calculations.compass_point(208) # => "SW"
|
|
536
|
+
```
|
|
794
537
|
|
|
795
|
-
|
|
796
|
-
reverse_geocoded_by :latitude, :longitude, :address => :full_address
|
|
797
|
-
end
|
|
538
|
+
_Note: when running queries on SQLite, `distance` and `bearing` are provided for consistency only. They are not very accurate._
|
|
798
539
|
|
|
799
|
-
|
|
540
|
+
For more advanced geospatial querying, please see the [rgeo gem](https://github.com/rgeo/rgeo).
|
|
800
541
|
|
|
801
|
-
class Venue
|
|
802
542
|
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
:longitude => :fetched_longitude # same here
|
|
543
|
+
Geospatial Calculations
|
|
544
|
+
-----------------------
|
|
806
545
|
|
|
807
|
-
|
|
808
|
-
end
|
|
546
|
+
The `Geocoder::Calculations` module contains some useful methods:
|
|
809
547
|
|
|
810
|
-
|
|
548
|
+
```ruby
|
|
549
|
+
# find the distance between two arbitrary points
|
|
550
|
+
Geocoder::Calculations.distance_between([47.858205,2.294359], [40.748433,-73.985655])
|
|
551
|
+
=> 3619.77359999382 # in configured units (default miles)
|
|
811
552
|
|
|
812
|
-
|
|
553
|
+
# find the geographic center (aka center of gravity) of objects or points
|
|
554
|
+
Geocoder::Calculations.geographic_center([city1, city2, [40.22,-73.99], city4])
|
|
555
|
+
=> [35.14968, -90.048929]
|
|
556
|
+
```
|
|
813
557
|
|
|
814
|
-
|
|
558
|
+
See [the code](https://github.com/alexreisner/geocoder/blob/master/lib/geocoder/calculations.rb) for more!
|
|
815
559
|
|
|
816
|
-
class Venue
|
|
817
560
|
|
|
818
|
-
|
|
561
|
+
Batch Geocoding
|
|
562
|
+
---------------
|
|
819
563
|
|
|
820
|
-
|
|
564
|
+
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:
|
|
821
565
|
|
|
822
|
-
|
|
566
|
+
```sh
|
|
567
|
+
rake geocode:all CLASS=YourModel
|
|
568
|
+
```
|
|
823
569
|
|
|
824
|
-
|
|
570
|
+
If you need reverse geocoding instead, call the task with REVERSE=true:
|
|
825
571
|
|
|
826
|
-
|
|
572
|
+
```sh
|
|
573
|
+
rake geocode:all CLASS=YourModel REVERSE=true
|
|
574
|
+
```
|
|
827
575
|
|
|
828
|
-
|
|
576
|
+
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:
|
|
829
577
|
|
|
830
|
-
|
|
578
|
+
```sh
|
|
579
|
+
rake geocode:all CLASS=YourModel SLEEP=0.25 BATCH=100
|
|
580
|
+
```
|
|
831
581
|
|
|
832
|
-
|
|
833
|
-
after_validation :geocode, :if => :has_location, :unless => :has_coordinates
|
|
582
|
+
To avoid exceeding per-day limits you can add a `LIMIT` option. However, this will ignore the `BATCH` value, if provided.
|
|
834
583
|
|
|
835
|
-
|
|
584
|
+
```sh
|
|
585
|
+
rake geocode:all CLASS=YourModel LIMIT=1000
|
|
586
|
+
```
|
|
836
587
|
|
|
837
|
-
Use Outside of Rails
|
|
838
|
-
--------------------
|
|
839
588
|
|
|
840
|
-
|
|
589
|
+
Testing
|
|
590
|
+
-------
|
|
841
591
|
|
|
842
|
-
|
|
592
|
+
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`
|
|
593
|
+
|
|
594
|
+
```ruby
|
|
595
|
+
Geocoder.configure(lookup: :test, ip_lookup: :test)
|
|
596
|
+
```
|
|
597
|
+
|
|
598
|
+
Add stubs to define the results that will be returned:
|
|
599
|
+
|
|
600
|
+
```ruby
|
|
601
|
+
Geocoder::Lookup::Test.add_stub(
|
|
602
|
+
"New York, NY", [
|
|
603
|
+
{
|
|
604
|
+
'coordinates' => [40.7143528, -74.0059731],
|
|
605
|
+
'address' => 'New York, NY, USA',
|
|
606
|
+
'state' => 'New York',
|
|
607
|
+
'state_code' => 'NY',
|
|
608
|
+
'country' => 'United States',
|
|
609
|
+
'country_code' => 'US'
|
|
610
|
+
}
|
|
611
|
+
]
|
|
612
|
+
)
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
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:
|
|
616
|
+
|
|
617
|
+
```ruby
|
|
618
|
+
Geocoder::Lookup::Test.set_default_stub(
|
|
619
|
+
[
|
|
620
|
+
{
|
|
621
|
+
'coordinates' => [40.7143528, -74.0059731],
|
|
622
|
+
'address' => 'New York, NY, USA',
|
|
623
|
+
'state' => 'New York',
|
|
624
|
+
'state_code' => 'NY',
|
|
625
|
+
'country' => 'United States',
|
|
626
|
+
'country_code' => 'US'
|
|
627
|
+
}
|
|
628
|
+
]
|
|
629
|
+
)
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
You may also delete a single stub, or reset all stubs _including the default stub_:
|
|
633
|
+
|
|
634
|
+
```ruby
|
|
635
|
+
Geocoder::Lookup::Test.delete_stub('New York, NY')
|
|
636
|
+
Geocoder::Lookup::Test.reset
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
Notes:
|
|
640
|
+
|
|
641
|
+
- Keys must be strings (not symbols) when calling `add_stub` or `set_default_stub`. For example `'country' =>` not `:country =>`.
|
|
642
|
+
- 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.
|
|
843
643
|
|
|
844
|
-
This returns an array of `Geocoder::Result` objects with all data provided by the geocoding service.
|
|
845
644
|
|
|
645
|
+
Error Handling
|
|
646
|
+
--------------
|
|
846
647
|
|
|
847
|
-
|
|
848
|
-
------------------------------
|
|
648
|
+
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:
|
|
849
649
|
|
|
850
|
-
|
|
650
|
+
```ruby
|
|
651
|
+
Geocoder.configure(always_raise: [SocketError, Timeout::Error])
|
|
652
|
+
```
|
|
851
653
|
|
|
852
|
-
|
|
654
|
+
You can also do this to raise all exceptions:
|
|
853
655
|
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
'latitude' => 40.7143528,
|
|
858
|
-
'longitude' => -74.0059731,
|
|
859
|
-
'address' => 'New York, NY, USA',
|
|
860
|
-
'state' => 'New York',
|
|
861
|
-
'state_code' => 'NY',
|
|
862
|
-
'country' => 'United States',
|
|
863
|
-
'country_code' => 'US'
|
|
864
|
-
}
|
|
865
|
-
]
|
|
866
|
-
)
|
|
656
|
+
```ruby
|
|
657
|
+
Geocoder.configure(always_raise: :all)
|
|
658
|
+
```
|
|
867
659
|
|
|
868
|
-
|
|
660
|
+
The raise-able exceptions are:
|
|
869
661
|
|
|
870
|
-
|
|
662
|
+
```ruby
|
|
663
|
+
SocketError
|
|
664
|
+
Timeout::Error
|
|
665
|
+
Geocoder::OverQueryLimitError
|
|
666
|
+
Geocoder::RequestDenied
|
|
667
|
+
Geocoder::InvalidRequest
|
|
668
|
+
Geocoder::InvalidApiKey
|
|
669
|
+
Geocoder::ServiceUnavailable
|
|
670
|
+
```
|
|
871
671
|
|
|
872
|
-
|
|
873
|
-
[
|
|
874
|
-
{
|
|
875
|
-
'latitude' => 40.7143528,
|
|
876
|
-
'longitude' => -74.0059731,
|
|
877
|
-
'address' => 'New York, NY, USA',
|
|
878
|
-
'state' => 'New York',
|
|
879
|
-
'state_code' => 'NY',
|
|
880
|
-
'country' => 'United States',
|
|
881
|
-
'country_code' => 'US'
|
|
882
|
-
}
|
|
883
|
-
]
|
|
884
|
-
)
|
|
672
|
+
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.
|
|
885
673
|
|
|
886
|
-
Any query that hasn't been explicitly stubbed will return that result.
|
|
887
674
|
|
|
888
675
|
Command Line Interface
|
|
889
676
|
----------------------
|
|
890
677
|
|
|
891
678
|
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:
|
|
892
679
|
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
Numeric Data Types and Precision
|
|
906
|
-
--------------------------------
|
|
907
|
-
|
|
908
|
-
Geocoder works with any numeric data type (e.g. float, double, decimal) on which trig (and other mathematical) functions can be performed.
|
|
909
|
-
|
|
910
|
-
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.
|
|
911
|
-
|
|
912
|
-
Notes on MongoDB
|
|
913
|
-
----------------
|
|
914
|
-
|
|
915
|
-
### The Near Method
|
|
916
|
-
|
|
917
|
-
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.
|
|
918
|
-
|
|
919
|
-
### Latitude/Longitude Order
|
|
920
|
-
|
|
921
|
-
Coordinates are generally printed and spoken as latitude, then longitude ([lat,lon]). Geocoder respects this convention and always expects method arguments to be given in [lat,lon] order. However, MongoDB requires that coordinates be stored in [lon,lat] order as per the GeoJSON spec (http://geojson.org/geojson-spec.html#positions), so internally they are stored "backwards." However, this does not affect order of arguments to methods when using Mongoid or MongoMapper.
|
|
922
|
-
|
|
923
|
-
To access an object's coordinates in the conventional order, use the `to_coordinates` instance method provided by Geocoder. For example:
|
|
924
|
-
|
|
925
|
-
obj.to_coordinates # => [37.7941013, -122.3951096] # [lat, lon]
|
|
680
|
+
```sh
|
|
681
|
+
$ geocode 29.951,-90.081
|
|
682
|
+
Latitude: 29.952211
|
|
683
|
+
Longitude: -90.080563
|
|
684
|
+
Full address: 1500 Sugar Bowl Dr, New Orleans, LA 70112, USA
|
|
685
|
+
City: New Orleans
|
|
686
|
+
State/province: Louisiana
|
|
687
|
+
Postal code: 70112
|
|
688
|
+
Country: United States
|
|
689
|
+
Map: http://maps.google.com/maps?q=29.952211,-90.080563
|
|
690
|
+
```
|
|
926
691
|
|
|
927
|
-
|
|
692
|
+
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.
|
|
928
693
|
|
|
929
|
-
obj.coordinates # => [-122.3951096, 37.7941013] # [lon, lat]
|
|
930
694
|
|
|
931
|
-
|
|
695
|
+
Technical Discussions
|
|
696
|
+
---------------------
|
|
932
697
|
|
|
933
|
-
|
|
934
|
-
-----------------------------
|
|
935
|
-
|
|
936
|
-
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:
|
|
937
|
-
|
|
938
|
-
extend Geocoder::Model::ActiveRecord
|
|
939
|
-
|
|
940
|
-
Optimisation of Distance Queries
|
|
941
|
-
--------------------------------
|
|
942
|
-
|
|
943
|
-
In MySQL and Postgres the finding of objects near a given point is speeded up by using a bounding box to limit the number of points over which a full distance calculation needs to be done.
|
|
944
|
-
|
|
945
|
-
To take advantage of this optimisation you need to add a composite index on latitude and longitude. In your Rails migration:
|
|
946
|
-
|
|
947
|
-
add_index :table, [:latitude, :longitude]
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
Distance Queries in SQLite
|
|
951
|
-
--------------------------
|
|
698
|
+
### Distance Queries in SQLite
|
|
952
699
|
|
|
953
700
|
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.
|
|
954
701
|
|
|
955
|
-
|
|
956
|
-
### Discussion
|
|
957
|
-
|
|
958
702
|
There are few options for finding objects near a given point in SQLite without installing extensions:
|
|
959
703
|
|
|
960
704
|
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.
|
|
@@ -965,36 +709,11 @@ There are few options for finding objects near a given point in SQLite without i
|
|
|
965
709
|
|
|
966
710
|
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.
|
|
967
711
|
|
|
712
|
+
### Numeric Data Types and Precision
|
|
968
713
|
|
|
969
|
-
|
|
970
|
-
-----
|
|
971
|
-
|
|
972
|
-
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:
|
|
973
|
-
|
|
974
|
-
http://github.com/alexreisner/geocoder_test
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
Error Handling
|
|
978
|
-
--------------
|
|
979
|
-
|
|
980
|
-
By default Geocoder will rescue any exceptions raised by calls to a geocoding service and return an empty array (using warn() to inform you of the error). 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:
|
|
981
|
-
|
|
982
|
-
Geocoder.configure(:always_raise => [SocketError, TimeoutError])
|
|
983
|
-
|
|
984
|
-
You can also do this to raise all exceptions:
|
|
985
|
-
|
|
986
|
-
Geocoder.configure(:always_raise => :all)
|
|
987
|
-
|
|
988
|
-
The raise-able exceptions are:
|
|
989
|
-
|
|
990
|
-
SocketError
|
|
991
|
-
TimeoutError
|
|
992
|
-
Geocoder::OverQueryLimitError
|
|
993
|
-
Geocoder::RequestDenied
|
|
994
|
-
Geocoder::InvalidRequest
|
|
995
|
-
Geocoder::InvalidApiKey
|
|
714
|
+
Geocoder works with any numeric data type (e.g. float, double, decimal) on which trig (and other mathematical) functions can be performed.
|
|
996
715
|
|
|
997
|
-
|
|
716
|
+
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.
|
|
998
717
|
|
|
999
718
|
|
|
1000
719
|
Troubleshooting
|
|
@@ -1004,8 +723,10 @@ Troubleshooting
|
|
|
1004
723
|
|
|
1005
724
|
If you get one of these errors:
|
|
1006
725
|
|
|
1007
|
-
|
|
1008
|
-
|
|
726
|
+
```ruby
|
|
727
|
+
uninitialized constant Geocoder::Model::Mongoid
|
|
728
|
+
uninitialized constant Geocoder::Model::Mongoid::Mongo
|
|
729
|
+
```
|
|
1009
730
|
|
|
1010
731
|
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.
|
|
1011
732
|
|
|
@@ -1016,57 +737,62 @@ A lot of debugging time can be saved by understanding how Geocoder works with Ac
|
|
|
1016
737
|
* using the `pluck` method (selects only a single column)
|
|
1017
738
|
* specifying another model through `includes` (selects columns from other tables)
|
|
1018
739
|
|
|
740
|
+
### Geocoding is Slow
|
|
741
|
+
|
|
742
|
+
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.
|
|
743
|
+
|
|
744
|
+
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.
|
|
745
|
+
|
|
746
|
+
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.
|
|
747
|
+
|
|
748
|
+
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.
|
|
749
|
+
|
|
1019
750
|
### Unexpected Responses from Geocoding Services
|
|
1020
751
|
|
|
1021
752
|
Take a look at the server's raw response. You can do this by getting the request URL in an app console:
|
|
1022
753
|
|
|
1023
|
-
|
|
754
|
+
```ruby
|
|
755
|
+
Geocoder::Lookup.get(:nominatim).query_url(Geocoder::Query.new("..."))
|
|
756
|
+
```
|
|
1024
757
|
|
|
1025
|
-
Replace `:
|
|
758
|
+
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.
|
|
1026
759
|
|
|
1027
760
|
You can also fetch the response in the console:
|
|
1028
761
|
|
|
1029
|
-
|
|
762
|
+
```ruby
|
|
763
|
+
Geocoder::Lookup.get(:nominatim).send(:fetch_raw_data, Geocoder::Query.new("..."))
|
|
764
|
+
```
|
|
1030
765
|
|
|
1031
766
|
|
|
1032
|
-
|
|
1033
|
-
|
|
767
|
+
Known Issues
|
|
768
|
+
------------
|
|
1034
769
|
|
|
1035
|
-
|
|
770
|
+
### Using `count` with Rails 4.1+
|
|
1036
771
|
|
|
772
|
+
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.
|
|
1037
773
|
|
|
1038
|
-
|
|
1039
|
-
-----------
|
|
774
|
+
### Using `near` with `includes`
|
|
1040
775
|
|
|
1041
776
|
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).
|
|
1042
777
|
|
|
1043
778
|
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:
|
|
1044
779
|
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
780
|
+
```ruby
|
|
781
|
+
# Pass a :select option to the near scope to get the columns you want.
|
|
782
|
+
# Instead of City.near(...).includes(:venues), try:
|
|
783
|
+
City.near("Omaha, NE", 20, select: "cities.*, venues.*").joins(:venues)
|
|
1048
784
|
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
785
|
+
# This preload call will normally trigger two queries regardless of the
|
|
786
|
+
# number of results; one query on hotels, and one query on administrators.
|
|
787
|
+
# Instead of Hotel.near(...).includes(:administrator), try:
|
|
788
|
+
Hotel.near("London, UK", 50).joins(:administrator).preload(:administrator)
|
|
789
|
+
```
|
|
1053
790
|
|
|
1054
791
|
If anyone has a more elegant solution to this problem I am very interested in seeing it.
|
|
1055
792
|
|
|
793
|
+
### Using `near` with objects close to the 180th meridian
|
|
1056
794
|
|
|
1057
|
-
|
|
1058
|
-
------------
|
|
1059
|
-
|
|
1060
|
-
Contributions are welcome via pull requests on Github. Please respect the following guidelines:
|
|
1061
|
-
|
|
1062
|
-
* 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.
|
|
1063
|
-
* Do not commit changes to files that are irrelevant to your feature or bugfix (eg: `.gitignore`).
|
|
1064
|
-
* Do not add dependencies on other gems.
|
|
1065
|
-
* Do not add unnecessary `require` statements which could cause LoadErrors on certain systems.
|
|
1066
|
-
* Remember: Geocoder needs to run outside of Rails. Don't assume things like ActiveSupport are available.
|
|
1067
|
-
* Do not add to base configuration options; instead document required lookup-specific options in the README.
|
|
1068
|
-
* 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.
|
|
1069
|
-
* Be aware that the pull request review process is not immediate, and is generally proportional to the size of the pull request.
|
|
795
|
+
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.
|
|
1070
796
|
|
|
1071
797
|
|
|
1072
|
-
Copyright
|
|
798
|
+
Copyright :copyright: 2009-2021 Alex Reisner, released under the MIT license.
|