Class: GeoDistance::Haversine

Inherits:
DistanceFormula show all
Defined in:
lib/geo-distance/haversine.rb

Class Method Summary collapse

Methods inherited from DistanceFormula

#initialize

Constructor Details

This class inherits a constructor from GeoDistance::DistanceFormula

Class Method Details

.calc(dlat, lat1, lat2, dlon) ⇒ Object



51
52
53
# File 'lib/geo-distance/haversine.rb', line 51

def self.calc dlat, lat1, lat2, dlon
  (Math.sin(dlat.rpd/2))**2 + Math.cos(lat1.rpd) * Math.cos((lat2.rpd)) * (Math.sin(dlon.rpd/2))**2    
end

.distance(lat1, lon1, lat2, lon2) ⇒ Object

given two lat/lon points, compute the distance between the two points using the haversine formula

the result will be a Hash of distances which are key'd by 'mi','km','ft', and 'm'


41
42
43
44
45
46
47
48
49
# File 'lib/geo-distance/haversine.rb', line 41

def self.distance( lat1, lon1, lat2, lon2) 
  dlon = lon2 - lon1
  dlat = lat2 - lat1

  a = calc(dlat, lat1, lat2, dlon)
  c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a))

  GeoDistance::Distance.new c
end