class GPX::Haversine

Class is responsible for calculating distance between two geo points (it considers point height)

Constants

EARTH_RADIUS
RADIAN_PER_DEGREE

Public Class Methods

distance(first_point, second_point) click to toggle source
# File lib/gpx/haversine.rb, line 8
def self.distance(first_point, second_point)
        first_latitude_rad = first_point.latitude.to_f * RADIAN_PER_DEGREE
        second_latitude_rad = second_point.latitude.to_f * RADIAN_PER_DEGREE

        delta_longitude = (second_point.longitude.to_f - first_point.longitude.to_f) * RADIAN_PER_DEGREE
        delta_latitude =  (second_point.latitude.to_f - first_point.latitude.to_f) * RADIAN_PER_DEGREE

        elevation = (first_point.elevation.to_f / 1000.0 - second_point.elevation.to_f / 1000.0).abs

        distance = calculate_distance(first_latitude_rad, second_latitude_rad, delta_latitude, delta_longitude)
        triangulate(EARTH_RADIUS * distance, elevation)
end

Private Class Methods

calculate_distance(first_latitude_rad, second_latitude_rad, delta_latitude, delta_longitude) click to toggle source
# File lib/gpx/haversine.rb, line 22
def self.calculate_distance(first_latitude_rad, second_latitude_rad, delta_latitude, delta_longitude)
        coefficient = (Math::sin(delta_latitude/2.0)**2.0) +
                                Math::cos(first_latitude_rad) * Math::cos(second_latitude_rad) * (Math::sin(delta_longitude/2.0)**2.0)

        2.0 * Math::atan2(Math::sqrt(coefficient), Math::sqrt(1.0-coefficient))
end
triangulate(distance, height) click to toggle source
# File lib/gpx/haversine.rb, line 29
def self.triangulate (distance, height)
        Math::sqrt(distance**2 + height**2)
end