Module: Haversine
- Defined in:
- lib/haversine.rb,
lib/haversine/distance.rb
Defined Under Namespace
Classes: Distance
Constant Summary collapse
- RAD_PER_DEG =
Math::PI / 180
Class Method Summary collapse
-
.calc(dlat, lat1, lat2, dlon) ⇒ Object
TODO How can this be more descriptively named?.
-
.distance(lat1, lon1, lat2 = nil, lon2 = nil) ⇒ Object
given two lat/lon points, compute the distance between the two points using the haversine formula.
-
.rpd(num) ⇒ Object
Radians per degree.
Class Method Details
.calc(dlat, lat1, lat2, dlon) ⇒ Object
TODO How can this be more descriptively named?
57 58 59 |
# File 'lib/haversine.rb', line 57 def self.calc(dlat, lat1, lat2, dlon) (Math.sin(rpd(dlat)/2))**2 + Math.cos(rpd(lat1)) * Math.cos((rpd(lat2))) * (Math.sin(rpd(dlon)/2))**2 end |
.distance(lat1, lon1, lat2 = nil, lon2 = nil) ⇒ Object
given two lat/lon points, compute the distance between the two points using the haversine formula
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/haversine.rb', line 38 def self.distance(lat1, lon1, lat2=nil, lon2=nil) # Accept two arrays of points in addition to four coordinates if lat1.is_a?(Array) && lon1.is_a?(Array) lat2, lon2 = lon1 lat1, lon1 = lat1 elsif lat2.nil? || lon2.nil? raise ArgumentError end dlon = lon2 - lon1 dlat = lat2 - lat1 a = calc(dlat, lat1, lat2, dlon) c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a)) Haversine::Distance.new(c) end |
.rpd(num) ⇒ Object
Radians per degree
62 63 64 |
# File 'lib/haversine.rb', line 62 def self.rpd(num) num * RAD_PER_DEG end |