Module: Geocoder::Calculations
- Extended by:
- Calculations
- Included in:
- Calculations
- Defined in:
- lib/geocoder/calculations.rb
Constant Summary
- COMPASS_POINTS =
Compass point names, listed clockwise starting at North.
If you want bearings named using more, fewer, or different points override Geocoder::Calculations.COMPASS_POINTS with your own array.
%w[N NE E SE S SW W NW]
- EARTH_RADIUS =
Radius of the Earth, in kilometers. Value taken from: en.wikipedia.org/wiki/Earth_radius
6371.0- KM_IN_MI =
Conversion factor: multiply by kilometers to get miles.
0.621371192
Instance Method Summary (collapse)
-
- (Object) bearing_between(point1, point2, options = {})
Bearing between two points on Earth.
-
- (Object) bounding_box(point, radius, options = {})
Returns coordinates of the lower-left and upper-right corners of a box with the given point at its center.
-
- (Object) compass_point(bearing, points = COMPASS_POINTS)
Translate a bearing (float) into a compass direction (string, eg "North").
-
- (Object) distance_between(point1, point2, options = {})
Distance between two points on Earth (Haversine formula).
- - (Object) distance_to_radians(distance, units = :mi)
-
- (Object) earth_radius(units = :mi)
Radius of the Earth in the given units (:mi or :km).
-
- (Object) extract_coordinates(point)
Takes an object which is a [lat,lon] array, a geocodable string, or an object that implements to_coordinates and returns a
- lat,lon
-
array.
-
- (Object) geographic_center(points)
Compute the geographic center (aka geographic midpoint, center of gravity) for an array of geocoded objects and/or [lat,lon] arrays (can be mixed).
-
- (Object) km_in_mi
Conversion factor: km to mi.
-
- (Object) latitude_degree_distance(units = :mi)
Distance spanned by one degree of latitude in the given units.
-
- (Object) longitude_degree_distance(latitude, units = :mi)
Distance spanned by one degree of longitude at the given latitude.
-
- (Object) mi_in_km
Conversion factor: mi to km.
- - (Object) radians_to_distance(radians, units = :mi)
-
- (Object) to_degrees(*args)
Convert radians to degrees.
-
- (Object) to_kilometers(mi)
Convert miles to kilometers.
-
- (Object) to_miles(km)
Convert kilometers to miles.
-
- (Object) to_radians(*args)
Convert degrees to radians.
Instance Method Details
- (Object) bearing_between(point1, point2, options = {})
Bearing between two points on Earth. Returns a number of degrees from due north (clockwise).
See Geocoder::Calculations.distance_between for ways of specifying the points. Also accepts an options hash:
-
:method - :linear (default) or :spherical; the spherical method is ???correct??? in that it returns the shortest path (one along a great circle) but the linear method is the default as it is less confusing (returns due east or west when given two points with the same latitude)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/geocoder/calculations.rb', line 92 def bearing_between(point1, point2, = {}) # set default options [:method] = :linear unless [:method] == :spherical # convert to coordinate arrays point1 = extract_coordinates(point1) point2 = extract_coordinates(point2) # convert degrees to radians point1 = to_radians(point1) point2 = to_radians(point2) # compute deltas dlat = point2[0] - point1[0] dlon = point2[1] - point1[1] case [:method] when :linear y = dlon x = dlat when :spherical y = Math.sin(dlon) * Math.cos(point2[0]) x = Math.cos(point1[0]) * Math.sin(point2[0]) - Math.sin(point1[0]) * Math.cos(point2[0]) * Math.cos(dlon) end bearing = Math.atan2(x,y) # Answer is in radians counterclockwise from due east. # Convert to degrees clockwise from due north: (90 - to_degrees(bearing) + 360) % 360 end |
- (Object) bounding_box(point, radius, options = {})
Returns coordinates of the lower-left and upper-right corners of a box with the given point at its center. The radius is the shortest distance from the center point to any side of the box (the length of each side is twice the radius).
This is useful for finding corner points of a map viewport, or for roughly limiting the possible solutions in a geo-spatial search (ActiveRecord queries use it thusly).
See Geocoder::Calculations.distance_between for ways of specifying the point. Also accepts an options hash:
-
:units - :mi (default) or :km
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/geocoder/calculations.rb', line 182 def bounding_box(point, radius, = {}) lat,lon = extract_coordinates(point) radius = radius.to_f units = [:units] || :mi [ lat - (radius / latitude_degree_distance(units)), lon - (radius / longitude_degree_distance(lat, units)), lat + (radius / latitude_degree_distance(units)), lon + (radius / longitude_degree_distance(lat, units)) ] end |
- (Object) compass_point(bearing, points = COMPASS_POINTS)
Translate a bearing (float) into a compass direction (string, eg "North").
129 130 131 132 |
# File 'lib/geocoder/calculations.rb', line 129 def compass_point(bearing, points = COMPASS_POINTS) seg_size = 360 / points.size points[((bearing + (seg_size / 2)) % 360) / seg_size] end |
- (Object) distance_between(point1, point2, options = {})
Distance between two points on Earth (Haversine formula). Takes two points and an options hash. The points are given in the same way that points are given to all Geocoder methods that accept points as arguments. They can be:
-
an array of coordinates ([lat,lon])
-
a geocodable address (string)
-
a geocoded object (one which implements a to_coordinates method which returns a [lat,lon] array
The options hash supports:
-
:units - :mi (default) or :km
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/geocoder/calculations.rb', line 54 def distance_between(point1, point2, = {}) # set default options [:units] ||= :mi # convert to coordinate arrays point1 = extract_coordinates(point1) point2 = extract_coordinates(point2) # convert degrees to radians point1 = to_radians(point1) point2 = to_radians(point2) # compute deltas dlat = point2[0] - point1[0] dlon = point2[1] - point1[1] a = (Math.sin(dlat / 2))**2 + Math.cos(point1[0]) * (Math.sin(dlon / 2))**2 * Math.cos(point2[0]) c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a)) c * earth_radius([:units]) end |
- (Object) distance_to_radians(distance, units = :mi)
222 223 224 |
# File 'lib/geocoder/calculations.rb', line 222 def distance_to_radians(distance, units = :mi) distance.to_f / earth_radius(units) end |
- (Object) earth_radius(units = :mi)
Radius of the Earth in the given units (:mi or :km). Default is :mi.
247 248 249 |
# File 'lib/geocoder/calculations.rb', line 247 def earth_radius(units = :mi) units == :km ? EARTH_RADIUS : to_miles(EARTH_RADIUS) end |
- (Object) extract_coordinates(point)
Takes an object which is a [lat,lon] array, a geocodable string, or an object that implements to_coordinates and returns a
- lat,lon
-
array. Note that if a string is passed this may be a slow-
running method and may return nil.
271 272 273 274 275 276 277 |
# File 'lib/geocoder/calculations.rb', line 271 def extract_coordinates(point) case point when Array; point when String; Geocoder.coordinates(point) else point.to_coordinates end end |
- (Object) geographic_center(points)
Compute the geographic center (aka geographic midpoint, center of gravity) for an array of geocoded objects and/or [lat,lon] arrays (can be mixed). Any objects missing coordinates are ignored. Follows the procedure documented at www.geomidpoint.com/calculation.html.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/geocoder/calculations.rb', line 140 def geographic_center(points) # convert objects to [lat,lon] arrays and convert degrees to radians coords = points.map{ |p| to_radians(extract_coordinates(p)) } # convert to Cartesian coordinates x = []; y = []; z = [] coords.each do |p| x << Math.cos(p[0]) * Math.cos(p[1]) y << Math.cos(p[0]) * Math.sin(p[1]) z << Math.sin(p[0]) end # compute average coordinate values xa, ya, za = [x,y,z].map do |c| c.inject(0){ |tot,i| tot += i } / c.size.to_f end # convert back to latitude/longitude lon = Math.atan2(ya, xa) hyp = Math.sqrt(xa**2 + ya**2) lat = Math.atan2(za, hyp) # return answer in degrees to_degrees [lat, lon] end |
- (Object) km_in_mi
Conversion factor: km to mi.
254 255 256 |
# File 'lib/geocoder/calculations.rb', line 254 def km_in_mi KM_IN_MI end |
- (Object) latitude_degree_distance(units = :mi)
Distance spanned by one degree of latitude in the given units.
27 28 29 |
# File 'lib/geocoder/calculations.rb', line 27 def latitude_degree_distance(units = :mi) 2 * Math::PI * earth_radius(units) / 360 end |
- (Object) longitude_degree_distance(latitude, units = :mi)
Distance spanned by one degree of longitude at the given latitude. This ranges from around 69 miles at the equator to zero at the poles.
35 36 37 |
# File 'lib/geocoder/calculations.rb', line 35 def longitude_degree_distance(latitude, units = :mi) latitude_degree_distance(units) * Math.cos(to_radians(latitude)) end |
- (Object) mi_in_km
Conversion factor: mi to km.
261 262 263 |
# File 'lib/geocoder/calculations.rb', line 261 def mi_in_km 1.0 / KM_IN_MI end |
- (Object) radians_to_distance(radians, units = :mi)
226 227 228 |
# File 'lib/geocoder/calculations.rb', line 226 def radians_to_distance(radians, units = :mi) radians * earth_radius(units) end |
- (Object) to_degrees(*args)
Convert radians to degrees. If an array (or multiple arguments) is passed, converts each value and returns array.
213 214 215 216 217 218 219 220 |
# File 'lib/geocoder/calculations.rb', line 213 def to_degrees(*args) args = args.first if args.first.is_a?(Array) if args.size == 1 (args.first * 180.0) / Math::PI else args.map{ |i| to_degrees(i) } end end |
- (Object) to_kilometers(mi)
Convert miles to kilometers.
233 234 235 |
# File 'lib/geocoder/calculations.rb', line 233 def to_kilometers(mi) mi * mi_in_km end |
- (Object) to_miles(km)
Convert kilometers to miles.
240 241 242 |
# File 'lib/geocoder/calculations.rb', line 240 def to_miles(km) km * km_in_mi end |
- (Object) to_radians(*args)
Convert degrees to radians. If an array (or multiple arguments) is passed, converts each value and returns array.
199 200 201 202 203 204 205 206 |
# File 'lib/geocoder/calculations.rb', line 199 def to_radians(*args) args = args.first if args.first.is_a?(Array) if args.size == 1 args.first * (Math::PI / 180) else args.map{ |i| to_radians(i) } end end |