Class: Haversine
- Inherits:
-
Object
- Object
- Haversine
- Defined in:
- lib/haversine.rb
Defined Under Namespace
Classes: Distance
Constant Summary collapse
- RAD_PER_DEG =
PI/180
0.017453293
Class Method Summary collapse
- .calc(dlat, lat1, lat2, dlon) ⇒ Object
-
.distance(lat1, lon1, lat2, lon2, units = :meters) ⇒ 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’.
- .units ⇒ Object
- .wants?(unit_opts, unit) ⇒ Boolean
Class Method Details
.calc(dlat, lat1, lat2, dlon) ⇒ Object
149 150 151 |
# File 'lib/haversine.rb', line 149 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, units = :meters) ⇒ 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'
139 140 141 142 143 144 145 146 147 |
# File 'lib/haversine.rb', line 139 def self.distance( lat1, lon1, lat2, lon2, units = :meters ) dlon = lon2 - lon1 dlat = lat2 - lat1 a = calc(dlat, lat1, lat2, dlon) c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a)) Distance.new c end |
.units ⇒ Object
43 44 45 |
# File 'lib/haversine.rb', line 43 def units [:miles, :km, :feet, :meters] end |
.wants?(unit_opts, unit) ⇒ Boolean
153 154 155 |
# File 'lib/haversine.rb', line 153 def self.wants? unit_opts, unit unit_opts == unit || unit_opts[unit] end |