geocoder-kb 1.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.travis.yml +31 -0
- data/CHANGELOG.md +384 -0
- data/LICENSE +20 -0
- data/README.md +1085 -0
- data/Rakefile +25 -0
- data/bin/geocode +5 -0
- data/examples/autoexpire_cache_dalli.rb +62 -0
- data/examples/autoexpire_cache_redis.rb +28 -0
- data/examples/cache_bypass.rb +48 -0
- data/examples/sidekiq_worker.rb +16 -0
- data/gemfiles/Gemfile.mongoid-2.4.x +16 -0
- data/lib/generators/geocoder/config/config_generator.rb +14 -0
- data/lib/generators/geocoder/config/templates/initializer.rb +21 -0
- data/lib/generators/geocoder/maxmind/geolite_city_generator.rb +28 -0
- data/lib/generators/geocoder/maxmind/geolite_country_generator.rb +28 -0
- data/lib/generators/geocoder/maxmind/templates/migration/geolite_city.rb +30 -0
- data/lib/generators/geocoder/maxmind/templates/migration/geolite_country.rb +17 -0
- data/lib/geocoder.rb +47 -0
- data/lib/geocoder/cache.rb +90 -0
- data/lib/geocoder/calculations.rb +428 -0
- data/lib/geocoder/cli.rb +121 -0
- data/lib/geocoder/configuration.rb +124 -0
- data/lib/geocoder/configuration_hash.rb +11 -0
- data/lib/geocoder/exceptions.rb +21 -0
- data/lib/geocoder/ip_address.rb +21 -0
- data/lib/geocoder/lookup.rb +102 -0
- data/lib/geocoder/lookups/amap.rb +55 -0
- data/lib/geocoder/lookups/baidu.rb +55 -0
- data/lib/geocoder/lookups/baidu_ip.rb +54 -0
- data/lib/geocoder/lookups/base.rb +302 -0
- data/lib/geocoder/lookups/bing.rb +59 -0
- data/lib/geocoder/lookups/dstk.rb +20 -0
- data/lib/geocoder/lookups/esri.rb +48 -0
- data/lib/geocoder/lookups/freegeoip.rb +47 -0
- data/lib/geocoder/lookups/geocoder_ca.rb +54 -0
- data/lib/geocoder/lookups/geocoder_us.rb +39 -0
- data/lib/geocoder/lookups/geocodio.rb +42 -0
- data/lib/geocoder/lookups/geoip2.rb +40 -0
- data/lib/geocoder/lookups/google.rb +67 -0
- data/lib/geocoder/lookups/google_places_details.rb +50 -0
- data/lib/geocoder/lookups/google_premier.rb +47 -0
- data/lib/geocoder/lookups/here.rb +62 -0
- data/lib/geocoder/lookups/mapquest.rb +60 -0
- data/lib/geocoder/lookups/maxmind.rb +90 -0
- data/lib/geocoder/lookups/maxmind_local.rb +58 -0
- data/lib/geocoder/lookups/nominatim.rb +52 -0
- data/lib/geocoder/lookups/okf.rb +43 -0
- data/lib/geocoder/lookups/opencagedata.rb +58 -0
- data/lib/geocoder/lookups/ovi.rb +62 -0
- data/lib/geocoder/lookups/pointpin.rb +68 -0
- data/lib/geocoder/lookups/postcode_anywhere_uk.rb +51 -0
- data/lib/geocoder/lookups/smarty_streets.rb +45 -0
- data/lib/geocoder/lookups/telize.rb +40 -0
- data/lib/geocoder/lookups/test.rb +44 -0
- data/lib/geocoder/lookups/yahoo.rb +88 -0
- data/lib/geocoder/lookups/yandex.rb +54 -0
- data/lib/geocoder/models/active_record.rb +50 -0
- data/lib/geocoder/models/base.rb +39 -0
- data/lib/geocoder/models/mongo_base.rb +64 -0
- data/lib/geocoder/models/mongo_mapper.rb +26 -0
- data/lib/geocoder/models/mongoid.rb +32 -0
- data/lib/geocoder/query.rb +111 -0
- data/lib/geocoder/railtie.rb +26 -0
- data/lib/geocoder/request.rb +25 -0
- data/lib/geocoder/results/amap.rb +85 -0
- data/lib/geocoder/results/baidu.rb +79 -0
- data/lib/geocoder/results/baidu_ip.rb +62 -0
- data/lib/geocoder/results/base.rb +67 -0
- data/lib/geocoder/results/bing.rb +48 -0
- data/lib/geocoder/results/dstk.rb +6 -0
- data/lib/geocoder/results/esri.rb +51 -0
- data/lib/geocoder/results/freegeoip.rb +45 -0
- data/lib/geocoder/results/geocoder_ca.rb +60 -0
- data/lib/geocoder/results/geocoder_us.rb +39 -0
- data/lib/geocoder/results/geocodio.rb +66 -0
- data/lib/geocoder/results/geoip2.rb +64 -0
- data/lib/geocoder/results/google.rb +124 -0
- data/lib/geocoder/results/google_places_details.rb +35 -0
- data/lib/geocoder/results/google_premier.rb +6 -0
- data/lib/geocoder/results/here.rb +62 -0
- data/lib/geocoder/results/mapquest.rb +51 -0
- data/lib/geocoder/results/maxmind.rb +135 -0
- data/lib/geocoder/results/maxmind_local.rb +49 -0
- data/lib/geocoder/results/nominatim.rb +94 -0
- data/lib/geocoder/results/okf.rb +106 -0
- data/lib/geocoder/results/opencagedata.rb +82 -0
- data/lib/geocoder/results/ovi.rb +62 -0
- data/lib/geocoder/results/pointpin.rb +44 -0
- data/lib/geocoder/results/postcode_anywhere_uk.rb +42 -0
- data/lib/geocoder/results/smarty_streets.rb +106 -0
- data/lib/geocoder/results/telize.rb +45 -0
- data/lib/geocoder/results/test.rb +33 -0
- data/lib/geocoder/results/yahoo.rb +55 -0
- data/lib/geocoder/results/yandex.rb +84 -0
- data/lib/geocoder/sql.rb +107 -0
- data/lib/geocoder/stores/active_record.rb +289 -0
- data/lib/geocoder/stores/base.rb +127 -0
- data/lib/geocoder/stores/mongo_base.rb +89 -0
- data/lib/geocoder/stores/mongo_mapper.rb +13 -0
- data/lib/geocoder/stores/mongoid.rb +13 -0
- data/lib/geocoder/version.rb +3 -0
- data/lib/hash_recursive_merge.rb +74 -0
- data/lib/maxmind_database.rb +109 -0
- data/lib/oauth_util.rb +112 -0
- data/lib/tasks/geocoder.rake +29 -0
- data/lib/tasks/maxmind.rake +73 -0
- data/test/fixtures/baidu_invalid_key +1 -0
- data/test/fixtures/baidu_ip_202_198_16_3 +19 -0
- data/test/fixtures/baidu_ip_invalid_key +1 -0
- data/test/fixtures/baidu_ip_no_results +1 -0
- data/test/fixtures/baidu_no_results +1 -0
- data/test/fixtures/baidu_reverse +1 -0
- data/test/fixtures/baidu_shanghai_pearl_tower +12 -0
- data/test/fixtures/bing_invalid_key +1 -0
- data/test/fixtures/bing_madison_square_garden +40 -0
- data/test/fixtures/bing_no_results +16 -0
- data/test/fixtures/bing_reverse +42 -0
- data/test/fixtures/cloudmade_invalid_key +1 -0
- data/test/fixtures/cloudmade_madison_square_garden +1 -0
- data/test/fixtures/cloudmade_no_results +1 -0
- data/test/fixtures/esri_madison_square_garden +59 -0
- data/test/fixtures/esri_no_results +8 -0
- data/test/fixtures/esri_reverse +21 -0
- data/test/fixtures/freegeoip_74_200_247_59 +12 -0
- data/test/fixtures/freegeoip_no_results +1 -0
- data/test/fixtures/geocoder_ca_madison_square_garden +1 -0
- data/test/fixtures/geocoder_ca_no_results +1 -0
- data/test/fixtures/geocoder_ca_reverse +34 -0
- data/test/fixtures/geocoder_us_madison_square_garden +1 -0
- data/test/fixtures/geocoder_us_no_results +1 -0
- data/test/fixtures/geocodio_1101_pennsylvania_ave +1 -0
- data/test/fixtures/geocodio_bad_api_key +3 -0
- data/test/fixtures/geocodio_invalid +4 -0
- data/test/fixtures/geocodio_no_results +1 -0
- data/test/fixtures/geocodio_over_query_limit +4 -0
- data/test/fixtures/google_garbage +456 -0
- data/test/fixtures/google_madison_square_garden +57 -0
- data/test/fixtures/google_no_city_data +44 -0
- data/test/fixtures/google_no_locality +51 -0
- data/test/fixtures/google_no_results +4 -0
- data/test/fixtures/google_over_limit +4 -0
- data/test/fixtures/google_places_details_invalid_request +4 -0
- data/test/fixtures/google_places_details_madison_square_garden +120 -0
- data/test/fixtures/google_places_details_no_results +4 -0
- data/test/fixtures/google_places_details_no_reviews +60 -0
- data/test/fixtures/google_places_details_no_types +66 -0
- data/test/fixtures/here_madison_square_garden +72 -0
- data/test/fixtures/here_no_results +8 -0
- data/test/fixtures/mapquest_error +16 -0
- data/test/fixtures/mapquest_invalid_api_key +16 -0
- data/test/fixtures/mapquest_invalid_request +16 -0
- data/test/fixtures/mapquest_madison_square_garden +52 -0
- data/test/fixtures/mapquest_no_results +16 -0
- data/test/fixtures/maxmind_24_24_24_21 +1 -0
- data/test/fixtures/maxmind_24_24_24_22 +1 -0
- data/test/fixtures/maxmind_24_24_24_23 +1 -0
- data/test/fixtures/maxmind_24_24_24_24 +1 -0
- data/test/fixtures/maxmind_74_200_247_59 +1 -0
- data/test/fixtures/maxmind_invalid_key +1 -0
- data/test/fixtures/maxmind_no_results +1 -0
- data/test/fixtures/nominatim_madison_square_garden +150 -0
- data/test/fixtures/nominatim_no_results +1 -0
- data/test/fixtures/nominatim_over_limit +1 -0
- data/test/fixtures/okf_kirstinmaki +67 -0
- data/test/fixtures/okf_no_results +4 -0
- data/test/fixtures/opencagedata_invalid_api_key +25 -0
- data/test/fixtures/opencagedata_invalid_request +26 -0
- data/test/fixtures/opencagedata_madison_square_garden +73 -0
- data/test/fixtures/opencagedata_no_results +29 -0
- data/test/fixtures/opencagedata_over_limit +31 -0
- data/test/fixtures/ovi_madison_square_garden +72 -0
- data/test/fixtures/ovi_no_results +8 -0
- data/test/fixtures/pointpin_10_10_10_10 +1 -0
- data/test/fixtures/pointpin_555_555_555_555 +1 -0
- data/test/fixtures/pointpin_80_111_555_555 +1 -0
- data/test/fixtures/pointpin_no_results +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_WR26NJ +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_generic_error +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_hampshire +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_key_limit_exceeded +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_no_results +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_romsey +1 -0
- data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_unknown_key +1 -0
- data/test/fixtures/smarty_streets_11211 +1 -0
- data/test/fixtures/smarty_streets_madison_square_garden +47 -0
- data/test/fixtures/smarty_streets_no_results +1 -0
- data/test/fixtures/telize_10_10_10_10 +1 -0
- data/test/fixtures/telize_555_555_555_555 +4 -0
- data/test/fixtures/telize_74_200_247_59 +1 -0
- data/test/fixtures/telize_no_results +1 -0
- data/test/fixtures/yahoo_error +1 -0
- data/test/fixtures/yahoo_invalid_key +2 -0
- data/test/fixtures/yahoo_madison_square_garden +52 -0
- data/test/fixtures/yahoo_no_results +10 -0
- data/test/fixtures/yahoo_over_limit +2 -0
- data/test/fixtures/yandex_canada_rue_dupuis_14 +446 -0
- data/test/fixtures/yandex_invalid_key +1 -0
- data/test/fixtures/yandex_kremlin +48 -0
- data/test/fixtures/yandex_new_york +1 -0
- data/test/fixtures/yandex_no_city_and_town +112 -0
- data/test/fixtures/yandex_no_results +16 -0
- data/test/integration/http_client_test.rb +31 -0
- data/test/mongoid_test_helper.rb +43 -0
- data/test/test_helper.rb +416 -0
- data/test/unit/active_record_test.rb +16 -0
- data/test/unit/cache_test.rb +37 -0
- data/test/unit/calculations_test.rb +220 -0
- data/test/unit/configuration_test.rb +55 -0
- data/test/unit/error_handling_test.rb +56 -0
- data/test/unit/geocoder_test.rb +78 -0
- data/test/unit/https_test.rb +17 -0
- data/test/unit/ip_address_test.rb +27 -0
- data/test/unit/lookup_test.rb +153 -0
- data/test/unit/lookups/bing_test.rb +68 -0
- data/test/unit/lookups/dstk_test.rb +26 -0
- data/test/unit/lookups/esri_test.rb +48 -0
- data/test/unit/lookups/freegeoip_test.rb +27 -0
- data/test/unit/lookups/geocoder_ca_test.rb +17 -0
- data/test/unit/lookups/geocodio_test.rb +55 -0
- data/test/unit/lookups/geoip2_test.rb +27 -0
- data/test/unit/lookups/google_places_details_test.rb +122 -0
- data/test/unit/lookups/google_premier_test.rb +22 -0
- data/test/unit/lookups/google_test.rb +84 -0
- data/test/unit/lookups/mapquest_test.rb +60 -0
- data/test/unit/lookups/maxmind_local_test.rb +28 -0
- data/test/unit/lookups/maxmind_test.rb +63 -0
- data/test/unit/lookups/nominatim_test.rb +31 -0
- data/test/unit/lookups/okf_test.rb +38 -0
- data/test/unit/lookups/opencagedata_test.rb +64 -0
- data/test/unit/lookups/pointpin_test.rb +30 -0
- data/test/unit/lookups/postcode_anywhere_uk_test.rb +70 -0
- data/test/unit/lookups/smarty_streets_test.rb +71 -0
- data/test/unit/lookups/telize_test.rb +36 -0
- data/test/unit/lookups/yahoo_test.rb +35 -0
- data/test/unit/method_aliases_test.rb +26 -0
- data/test/unit/model_test.rb +38 -0
- data/test/unit/mongoid_test.rb +47 -0
- data/test/unit/near_test.rb +87 -0
- data/test/unit/oauth_util_test.rb +31 -0
- data/test/unit/proxy_test.rb +37 -0
- data/test/unit/query_test.rb +52 -0
- data/test/unit/rake_task_test.rb +21 -0
- data/test/unit/request_test.rb +35 -0
- data/test/unit/result_test.rb +72 -0
- data/test/unit/test_mode_test.rb +70 -0
- metadata +294 -0
@@ -0,0 +1,428 @@
|
|
1
|
+
module Geocoder
|
2
|
+
module Calculations
|
3
|
+
extend self
|
4
|
+
|
5
|
+
##
|
6
|
+
# Compass point names, listed clockwise starting at North.
|
7
|
+
#
|
8
|
+
# If you want bearings named using more, fewer, or different points
|
9
|
+
# override Geocoder::Calculations.COMPASS_POINTS with your own array.
|
10
|
+
#
|
11
|
+
COMPASS_POINTS = %w[N NE E SE S SW W NW]
|
12
|
+
|
13
|
+
##
|
14
|
+
# Radius of the Earth, in kilometers.
|
15
|
+
# Value taken from: http://en.wikipedia.org/wiki/Earth_radius
|
16
|
+
#
|
17
|
+
EARTH_RADIUS = 6371.0
|
18
|
+
|
19
|
+
##
|
20
|
+
# Conversion factor: multiply by kilometers to get miles.
|
21
|
+
#
|
22
|
+
KM_IN_MI = 0.621371192
|
23
|
+
|
24
|
+
##
|
25
|
+
# Conversion factor: multiply by nautical miles to get miles.
|
26
|
+
#
|
27
|
+
KM_IN_NM = 0.539957
|
28
|
+
|
29
|
+
##
|
30
|
+
# Conversion factor: multiply by radians to get degrees.
|
31
|
+
#
|
32
|
+
DEGREES_PER_RADIAN = 57.2957795
|
33
|
+
|
34
|
+
# Not a number constant
|
35
|
+
NAN = defined?(::Float::NAN) ? ::Float::NAN : 0 / 0.0
|
36
|
+
|
37
|
+
##
|
38
|
+
# Returns true if all given arguments are valid latitude/longitude values.
|
39
|
+
#
|
40
|
+
def coordinates_present?(*args)
|
41
|
+
args.each do |a|
|
42
|
+
# note that Float::NAN != Float::NAN
|
43
|
+
# still, this could probably be improved:
|
44
|
+
return false if (!a.is_a?(Numeric) or a.to_s == "NaN")
|
45
|
+
end
|
46
|
+
true
|
47
|
+
end
|
48
|
+
|
49
|
+
##
|
50
|
+
# Distance spanned by one degree of latitude in the given units.
|
51
|
+
#
|
52
|
+
def latitude_degree_distance(units = nil)
|
53
|
+
units ||= Geocoder.config.units
|
54
|
+
2 * Math::PI * earth_radius(units) / 360
|
55
|
+
end
|
56
|
+
|
57
|
+
##
|
58
|
+
# Distance spanned by one degree of longitude at the given latitude.
|
59
|
+
# This ranges from around 69 miles at the equator to zero at the poles.
|
60
|
+
#
|
61
|
+
def longitude_degree_distance(latitude, units = nil)
|
62
|
+
units ||= Geocoder.config.units
|
63
|
+
latitude_degree_distance(units) * Math.cos(to_radians(latitude))
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# Distance between two points on Earth (Haversine formula).
|
68
|
+
# Takes two points and an options hash.
|
69
|
+
# The points are given in the same way that points are given to all
|
70
|
+
# Geocoder methods that accept points as arguments. They can be:
|
71
|
+
#
|
72
|
+
# * an array of coordinates ([lat,lon])
|
73
|
+
# * a geocodable address (string)
|
74
|
+
# * a geocoded object (one which implements a +to_coordinates+ method
|
75
|
+
# which returns a [lat,lon] array
|
76
|
+
#
|
77
|
+
# The options hash supports:
|
78
|
+
#
|
79
|
+
# * <tt>:units</tt> - <tt>:mi</tt> or <tt>:km</tt>
|
80
|
+
# Use Geocoder.configure(:units => ...) to configure default units.
|
81
|
+
#
|
82
|
+
def distance_between(point1, point2, options = {})
|
83
|
+
|
84
|
+
# set default options
|
85
|
+
options[:units] ||= Geocoder.config.units
|
86
|
+
|
87
|
+
# convert to coordinate arrays
|
88
|
+
point1 = extract_coordinates(point1)
|
89
|
+
point2 = extract_coordinates(point2)
|
90
|
+
|
91
|
+
# convert degrees to radians
|
92
|
+
point1 = to_radians(point1)
|
93
|
+
point2 = to_radians(point2)
|
94
|
+
|
95
|
+
# compute deltas
|
96
|
+
dlat = point2[0] - point1[0]
|
97
|
+
dlon = point2[1] - point1[1]
|
98
|
+
|
99
|
+
a = (Math.sin(dlat / 2))**2 + Math.cos(point1[0]) *
|
100
|
+
(Math.sin(dlon / 2))**2 * Math.cos(point2[0])
|
101
|
+
c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a))
|
102
|
+
c * earth_radius(options[:units])
|
103
|
+
end
|
104
|
+
|
105
|
+
##
|
106
|
+
# Bearing between two points on Earth.
|
107
|
+
# Returns a number of degrees from due north (clockwise).
|
108
|
+
#
|
109
|
+
# See Geocoder::Calculations.distance_between for
|
110
|
+
# ways of specifying the points. Also accepts an options hash:
|
111
|
+
#
|
112
|
+
# * <tt>:method</tt> - <tt>:linear</tt> or <tt>:spherical</tt>;
|
113
|
+
# the spherical method is "correct" in that it returns the shortest path
|
114
|
+
# (one along a great circle) but the linear method is less confusing
|
115
|
+
# (returns due east or west when given two points with the same latitude).
|
116
|
+
# Use Geocoder.configure(:distances => ...) to configure calculation method.
|
117
|
+
#
|
118
|
+
# Based on: http://www.movable-type.co.uk/scripts/latlong.html
|
119
|
+
#
|
120
|
+
def bearing_between(point1, point2, options = {})
|
121
|
+
|
122
|
+
# set default options
|
123
|
+
options[:method] ||= Geocoder.config.distances
|
124
|
+
options[:method] = :linear unless options[:method] == :spherical
|
125
|
+
|
126
|
+
# convert to coordinate arrays
|
127
|
+
point1 = extract_coordinates(point1)
|
128
|
+
point2 = extract_coordinates(point2)
|
129
|
+
|
130
|
+
# convert degrees to radians
|
131
|
+
point1 = to_radians(point1)
|
132
|
+
point2 = to_radians(point2)
|
133
|
+
|
134
|
+
# compute deltas
|
135
|
+
dlat = point2[0] - point1[0]
|
136
|
+
dlon = point2[1] - point1[1]
|
137
|
+
|
138
|
+
case options[:method]
|
139
|
+
when :linear
|
140
|
+
y = dlon
|
141
|
+
x = dlat
|
142
|
+
|
143
|
+
when :spherical
|
144
|
+
y = Math.sin(dlon) * Math.cos(point2[0])
|
145
|
+
x = Math.cos(point1[0]) * Math.sin(point2[0]) -
|
146
|
+
Math.sin(point1[0]) * Math.cos(point2[0]) * Math.cos(dlon)
|
147
|
+
end
|
148
|
+
|
149
|
+
bearing = Math.atan2(x,y)
|
150
|
+
# Answer is in radians counterclockwise from due east.
|
151
|
+
# Convert to degrees clockwise from due north:
|
152
|
+
(90 - to_degrees(bearing) + 360) % 360
|
153
|
+
end
|
154
|
+
|
155
|
+
##
|
156
|
+
# Translate a bearing (float) into a compass direction (string, eg "North").
|
157
|
+
#
|
158
|
+
def compass_point(bearing, points = COMPASS_POINTS)
|
159
|
+
seg_size = 360 / points.size
|
160
|
+
points[((bearing + (seg_size / 2)) % 360) / seg_size]
|
161
|
+
end
|
162
|
+
|
163
|
+
##
|
164
|
+
# Compute the geographic center (aka geographic midpoint, center of
|
165
|
+
# gravity) for an array of geocoded objects and/or [lat,lon] arrays
|
166
|
+
# (can be mixed). Any objects missing coordinates are ignored. Follows
|
167
|
+
# the procedure documented at http://www.geomidpoint.com/calculation.html.
|
168
|
+
#
|
169
|
+
def geographic_center(points)
|
170
|
+
|
171
|
+
# convert objects to [lat,lon] arrays and convert degrees to radians
|
172
|
+
coords = points.map{ |p| to_radians(extract_coordinates(p)) }
|
173
|
+
|
174
|
+
# convert to Cartesian coordinates
|
175
|
+
x = []; y = []; z = []
|
176
|
+
coords.each do |p|
|
177
|
+
x << Math.cos(p[0]) * Math.cos(p[1])
|
178
|
+
y << Math.cos(p[0]) * Math.sin(p[1])
|
179
|
+
z << Math.sin(p[0])
|
180
|
+
end
|
181
|
+
|
182
|
+
# compute average coordinate values
|
183
|
+
xa, ya, za = [x,y,z].map do |c|
|
184
|
+
c.inject(0){ |tot,i| tot += i } / c.size.to_f
|
185
|
+
end
|
186
|
+
|
187
|
+
# convert back to latitude/longitude
|
188
|
+
lon = Math.atan2(ya, xa)
|
189
|
+
hyp = Math.sqrt(xa**2 + ya**2)
|
190
|
+
lat = Math.atan2(za, hyp)
|
191
|
+
|
192
|
+
# return answer in degrees
|
193
|
+
to_degrees [lat, lon]
|
194
|
+
end
|
195
|
+
|
196
|
+
##
|
197
|
+
# Returns coordinates of the southwest and northeast corners of a box
|
198
|
+
# with the given point at its center. The radius is the shortest distance
|
199
|
+
# from the center point to any side of the box (the length of each side
|
200
|
+
# is twice the radius).
|
201
|
+
#
|
202
|
+
# This is useful for finding corner points of a map viewport, or for
|
203
|
+
# roughly limiting the possible solutions in a geo-spatial search
|
204
|
+
# (ActiveRecord queries use it thusly).
|
205
|
+
#
|
206
|
+
# See Geocoder::Calculations.distance_between for
|
207
|
+
# ways of specifying the point. Also accepts an options hash:
|
208
|
+
#
|
209
|
+
# * <tt>:units</tt> - <tt>:mi</tt> or <tt>:km</tt>.
|
210
|
+
# Use Geocoder.configure(:units => ...) to configure default units.
|
211
|
+
#
|
212
|
+
def bounding_box(point, radius, options = {})
|
213
|
+
lat,lon = extract_coordinates(point)
|
214
|
+
radius = radius.to_f
|
215
|
+
units = options[:units] || Geocoder.config.units
|
216
|
+
[
|
217
|
+
lat - (radius / latitude_degree_distance(units)),
|
218
|
+
lon - (radius / longitude_degree_distance(lat, units)),
|
219
|
+
lat + (radius / latitude_degree_distance(units)),
|
220
|
+
lon + (radius / longitude_degree_distance(lat, units))
|
221
|
+
]
|
222
|
+
end
|
223
|
+
|
224
|
+
##
|
225
|
+
# Random point within a circle of provided radius centered
|
226
|
+
# around the provided point
|
227
|
+
# Takes one point, one radius, and an options hash.
|
228
|
+
# The points are given in the same way that points are given to all
|
229
|
+
# Geocoder methods that accept points as arguments. They can be:
|
230
|
+
#
|
231
|
+
# * an array of coordinates ([lat,lon])
|
232
|
+
# * a geocodable address (string)
|
233
|
+
# * a geocoded object (one which implements a +to_coordinates+ method
|
234
|
+
# which returns a [lat,lon] array
|
235
|
+
#
|
236
|
+
# The options hash supports:
|
237
|
+
#
|
238
|
+
# * <tt>:units</tt> - <tt>:mi</tt> or <tt>:km</tt>
|
239
|
+
# Use Geocoder.configure(:units => ...) to configure default units.
|
240
|
+
def random_point_near(center, radius, options = {})
|
241
|
+
|
242
|
+
# set default options
|
243
|
+
options[:units] ||= Geocoder.config.units
|
244
|
+
|
245
|
+
# convert to coordinate arrays
|
246
|
+
center = extract_coordinates(center)
|
247
|
+
|
248
|
+
earth_circumference = 2 * Math::PI * earth_radius(options[:units])
|
249
|
+
max_degree_delta = 360.0 * (radius / earth_circumference)
|
250
|
+
|
251
|
+
# random bearing in radians
|
252
|
+
theta = 2 * Math::PI * rand
|
253
|
+
|
254
|
+
# random radius, use the square root to ensure a uniform
|
255
|
+
# distribution of points over the circle
|
256
|
+
r = Math.sqrt(rand) * max_degree_delta
|
257
|
+
|
258
|
+
delta_lat, delta_long = [r * Math.cos(theta), r * Math.sin(theta)]
|
259
|
+
[center[0] + delta_lat, center[1] + delta_long]
|
260
|
+
end
|
261
|
+
|
262
|
+
##
|
263
|
+
# Given a start point, distance, and heading (in degrees), provides
|
264
|
+
# an endpoint.
|
265
|
+
# The starting point is given in the same way that points are given to all
|
266
|
+
# Geocoder methods that accept points as arguments. It can be:
|
267
|
+
#
|
268
|
+
# * an array of coordinates ([lat,lon])
|
269
|
+
# * a geocodable address (string)
|
270
|
+
# * a geocoded object (one which implements a +to_coordinates+ method
|
271
|
+
# which returns a [lat,lon] array
|
272
|
+
#
|
273
|
+
def endpoint(start, heading, distance, options = {})
|
274
|
+
options[:units] ||= Geocoder.config.units
|
275
|
+
radius = earth_radius(options[:units])
|
276
|
+
|
277
|
+
start = extract_coordinates(start)
|
278
|
+
|
279
|
+
# convert degrees to radians
|
280
|
+
start = to_radians(start)
|
281
|
+
|
282
|
+
lat = start[0]
|
283
|
+
lon = start[1]
|
284
|
+
heading = to_radians(heading)
|
285
|
+
distance = distance.to_f
|
286
|
+
|
287
|
+
end_lat = Math.asin(Math.sin(lat)*Math.cos(distance/radius) +
|
288
|
+
Math.cos(lat)*Math.sin(distance/radius)*Math.cos(heading))
|
289
|
+
|
290
|
+
end_lon = lon+Math.atan2(Math.sin(heading)*Math.sin(distance/radius)*Math.cos(lat),
|
291
|
+
Math.cos(distance/radius)-Math.sin(lat)*Math.sin(end_lat))
|
292
|
+
|
293
|
+
to_degrees [end_lat, end_lon]
|
294
|
+
end
|
295
|
+
|
296
|
+
##
|
297
|
+
# Convert degrees to radians.
|
298
|
+
# If an array (or multiple arguments) is passed,
|
299
|
+
# converts each value and returns array.
|
300
|
+
#
|
301
|
+
def to_radians(*args)
|
302
|
+
args = args.first if args.first.is_a?(Array)
|
303
|
+
if args.size == 1
|
304
|
+
args.first * (Math::PI / 180)
|
305
|
+
else
|
306
|
+
args.map{ |i| to_radians(i) }
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
##
|
311
|
+
# Convert radians to degrees.
|
312
|
+
# If an array (or multiple arguments) is passed,
|
313
|
+
# converts each value and returns array.
|
314
|
+
#
|
315
|
+
def to_degrees(*args)
|
316
|
+
args = args.first if args.first.is_a?(Array)
|
317
|
+
if args.size == 1
|
318
|
+
(args.first * 180.0) / Math::PI
|
319
|
+
else
|
320
|
+
args.map{ |i| to_degrees(i) }
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
def distance_to_radians(distance, units = nil)
|
325
|
+
units ||= Geocoder.config.units
|
326
|
+
distance.to_f / earth_radius(units)
|
327
|
+
end
|
328
|
+
|
329
|
+
def radians_to_distance(radians, units = nil)
|
330
|
+
units ||= Geocoder.config.units
|
331
|
+
radians * earth_radius(units)
|
332
|
+
end
|
333
|
+
|
334
|
+
##
|
335
|
+
# Convert miles to kilometers.
|
336
|
+
#
|
337
|
+
def to_kilometers(mi)
|
338
|
+
mi * mi_in_km
|
339
|
+
end
|
340
|
+
|
341
|
+
##
|
342
|
+
# Convert kilometers to miles.
|
343
|
+
#
|
344
|
+
def to_miles(km)
|
345
|
+
km * km_in_mi
|
346
|
+
end
|
347
|
+
|
348
|
+
##
|
349
|
+
# Convert kilometers to nautical miles.
|
350
|
+
#
|
351
|
+
def to_nautical_miles(km)
|
352
|
+
km * km_in_nm
|
353
|
+
end
|
354
|
+
|
355
|
+
##
|
356
|
+
# Radius of the Earth in the given units (:mi or :km).
|
357
|
+
# Use Geocoder.configure(:units => ...) to configure default units.
|
358
|
+
#
|
359
|
+
def earth_radius(units = nil)
|
360
|
+
units ||= Geocoder.config.units
|
361
|
+
case units
|
362
|
+
when :km; EARTH_RADIUS
|
363
|
+
when :mi; to_miles(EARTH_RADIUS)
|
364
|
+
when :nm; to_nautical_miles(EARTH_RADIUS)
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
##
|
369
|
+
# Conversion factor: km to mi.
|
370
|
+
#
|
371
|
+
def km_in_mi
|
372
|
+
KM_IN_MI
|
373
|
+
end
|
374
|
+
|
375
|
+
##
|
376
|
+
# Conversion factor: km to nm.
|
377
|
+
#
|
378
|
+
def km_in_nm
|
379
|
+
KM_IN_NM
|
380
|
+
end
|
381
|
+
|
382
|
+
|
383
|
+
|
384
|
+
##
|
385
|
+
# Conversion factor: mi to km.
|
386
|
+
#
|
387
|
+
def mi_in_km
|
388
|
+
1.0 / KM_IN_MI
|
389
|
+
end
|
390
|
+
|
391
|
+
##
|
392
|
+
# Conversion factor: nm to km.
|
393
|
+
#
|
394
|
+
def nm_in_km
|
395
|
+
1.0 / KM_IN_NM
|
396
|
+
end
|
397
|
+
|
398
|
+
##
|
399
|
+
# Takes an object which is a [lat,lon] array, a geocodable string,
|
400
|
+
# or an object that implements +to_coordinates+ and returns a
|
401
|
+
# [lat,lon] array. Note that if a string is passed this may be a slow-
|
402
|
+
# running method and may return nil.
|
403
|
+
#
|
404
|
+
def extract_coordinates(point)
|
405
|
+
case point
|
406
|
+
when Array
|
407
|
+
if point.size == 2
|
408
|
+
lat, lon = point
|
409
|
+
if !lat.nil? && lat.respond_to?(:to_f) and
|
410
|
+
!lon.nil? && lon.respond_to?(:to_f)
|
411
|
+
then
|
412
|
+
return [ lat.to_f, lon.to_f ]
|
413
|
+
end
|
414
|
+
end
|
415
|
+
when String
|
416
|
+
point = Geocoder.coordinates(point) and return point
|
417
|
+
else
|
418
|
+
if point.respond_to?(:to_coordinates)
|
419
|
+
if Array === array = point.to_coordinates
|
420
|
+
return extract_coordinates(array)
|
421
|
+
end
|
422
|
+
end
|
423
|
+
end
|
424
|
+
[ NAN, NAN ]
|
425
|
+
end
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
data/lib/geocoder/cli.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'geocoder'
|
2
|
+
require 'optparse'
|
3
|
+
|
4
|
+
module Geocoder
|
5
|
+
class Cli
|
6
|
+
|
7
|
+
def self.run(args, out = STDOUT)
|
8
|
+
show_url = false
|
9
|
+
show_json = false
|
10
|
+
|
11
|
+
# remove arguments that are probably coordinates so they are not
|
12
|
+
# processed as arguments (eg: -31.96047031,115.84274631)
|
13
|
+
coords = args.select{ |i| i.match(/^-\d/) }
|
14
|
+
args -= coords
|
15
|
+
|
16
|
+
OptionParser.new{ |opts|
|
17
|
+
opts.banner = "Usage:\n geocode [options] <location>"
|
18
|
+
opts.separator "\nOptions: "
|
19
|
+
|
20
|
+
opts.on("-k <key>", "--key <key>",
|
21
|
+
"Key for geocoding API (usually optional). Enclose multi-part keys in quotes and separate parts by spaces") do |key|
|
22
|
+
if (key_parts = key.split(/\s+/)).size > 1
|
23
|
+
Geocoder.configure(:api_key => key_parts)
|
24
|
+
else
|
25
|
+
Geocoder.configure(:api_key => key)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
opts.on("-l <language>", "--language <language>",
|
30
|
+
"Language of output (see API docs for valid choices)") do |language|
|
31
|
+
Geocoder.configure(:language => language)
|
32
|
+
end
|
33
|
+
|
34
|
+
opts.on("-p <proxy>", "--proxy <proxy>",
|
35
|
+
"HTTP proxy server to use (user:pass@host:port)") do |proxy|
|
36
|
+
Geocoder.configure(:http_proxy => proxy)
|
37
|
+
end
|
38
|
+
|
39
|
+
opts.on("-s <service>", Geocoder::Lookup.all_services_except_test, "--service <service>",
|
40
|
+
"Geocoding service: #{Geocoder::Lookup.all_services_except_test * ', '}") do |service|
|
41
|
+
Geocoder.configure(:lookup => service.to_sym)
|
42
|
+
Geocoder.configure(:ip_lookup => service.to_sym)
|
43
|
+
end
|
44
|
+
|
45
|
+
opts.on("-t <seconds>", "--timeout <seconds>",
|
46
|
+
"Maximum number of seconds to wait for API response") do |timeout|
|
47
|
+
Geocoder.configure(:timeout => timeout.to_i)
|
48
|
+
end
|
49
|
+
|
50
|
+
opts.on("-j", "--json", "Print API's raw JSON response") do
|
51
|
+
show_json = true
|
52
|
+
end
|
53
|
+
|
54
|
+
opts.on("-u", "--url", "Print URL for API query instead of result") do
|
55
|
+
show_url = true
|
56
|
+
end
|
57
|
+
|
58
|
+
opts.on_tail("-v", "--version", "Print version number") do
|
59
|
+
require "geocoder/version"
|
60
|
+
out << "Geocoder #{Geocoder::VERSION}\n"
|
61
|
+
exit
|
62
|
+
end
|
63
|
+
|
64
|
+
opts.on_tail("-h", "--help", "Print this help") do
|
65
|
+
out << "Look up geographic information about a location.\n\n"
|
66
|
+
out << opts
|
67
|
+
out << "\nCreated and maintained by Alex Reisner, available under the MIT License.\n"
|
68
|
+
out << "Report bugs and contribute at http://github.com/alexreisner/geocoder\n"
|
69
|
+
exit
|
70
|
+
end
|
71
|
+
}.parse!(args)
|
72
|
+
|
73
|
+
# concatenate args with coords that might have been removed
|
74
|
+
# before option processing
|
75
|
+
query = (args + coords).join(" ")
|
76
|
+
|
77
|
+
if query == ""
|
78
|
+
out << "Please specify a location (run `geocode -h` for more info).\n"
|
79
|
+
exit 1
|
80
|
+
end
|
81
|
+
|
82
|
+
if show_url and show_json
|
83
|
+
out << "You can only specify one of -j and -u.\n"
|
84
|
+
exit 2
|
85
|
+
end
|
86
|
+
|
87
|
+
if show_url
|
88
|
+
q = Geocoder::Query.new(query)
|
89
|
+
out << q.url + "\n"
|
90
|
+
exit 0
|
91
|
+
end
|
92
|
+
|
93
|
+
if show_json
|
94
|
+
q = Geocoder::Query.new(query)
|
95
|
+
out << q.lookup.send(:fetch_raw_data, q) + "\n"
|
96
|
+
exit 0
|
97
|
+
end
|
98
|
+
|
99
|
+
if (result = Geocoder.search(query).first)
|
100
|
+
google = Geocoder::Lookup.get(:google)
|
101
|
+
lines = [
|
102
|
+
["Latitude", result.latitude],
|
103
|
+
["Longitude", result.longitude],
|
104
|
+
["Full address", result.address],
|
105
|
+
["City", result.city],
|
106
|
+
["State/province", result.state],
|
107
|
+
["Postal code", result.postal_code],
|
108
|
+
["Country", result.country],
|
109
|
+
["Google map", google.map_link_url(result.coordinates)],
|
110
|
+
]
|
111
|
+
lines.each do |line|
|
112
|
+
out << (line[0] + ": ").ljust(18) + line[1].to_s + "\n"
|
113
|
+
end
|
114
|
+
exit 0
|
115
|
+
else
|
116
|
+
out << "Location '#{query}' not found.\n"
|
117
|
+
exit 1
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|