Class: Haversine
- Inherits:
-
Object
- Object
- Haversine
- Defined in:
- lib/haversine.rb
Overview
PI = 3.1415926535
Defined Under Namespace
Classes: Distance
Constant Summary collapse
- RAD_PER_DEG =
PI/180
0.017453293
- Rmiles =
the great circle distance d will be in whatever units R is in
3956
- Rkm =
radius of the great circle in miles
6371
- Rfeet =
radius in kilometers…some algorithms use 6367
Rmiles * 5282
- Rmeters =
radius in feet
Rkm * 1000
Class Method Summary collapse
-
.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’.
- .distances ⇒ Object
Class Method Details
.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'
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/haversine.rb', line 80 def self.distance( lat1, lon1, lat2, lon2 ) dlon = lon2 - lon1 dlat = lat2 - lat1 dlon_rad = dlon * RAD_PER_DEG dlat_rad = dlat * RAD_PER_DEG lat1_rad = lat1 * RAD_PER_DEG lon1_rad = lon1 * RAD_PER_DEG lat2_rad = lat2 * RAD_PER_DEG lon2_rad = lon2 * RAD_PER_DEG # puts "dlon: #{dlon}, dlon_rad: #{dlon_rad}, dlat: #{dlat}, dlat_rad: #{dlat_rad}" a = (Math.sin(dlat_rad/2))**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * (Math.sin(dlon_rad/2))**2 c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a)) dMi = Rmiles * c # delta between the two points in miles dKm = Rkm * c # delta in kilometers dFeet = Rfeet * c # delta in feet dMeters = Rmeters * c # delta in meters distances[:miles] = Distance.in_miles dMi distances[:km] = Distance.in_km dKm distances[:feet] = Distance.in_feet dFeet distances[:meters] = Distance.in_meters dMeters distances end |
.distances ⇒ Object
73 74 75 |
# File 'lib/haversine.rb', line 73 def self.distances @distances ||= Hash.new end |