Class: GPX::Haversine

Inherits:
Object
  • Object
show all
Defined in:
lib/gpx/haversine.rb

Overview

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

Constant Summary collapse

RADIAN_PER_DEGREE =
Math::PI/180.0
EARTH_RADIUS =
6371.00079

Class Method Summary collapse

Class Method Details

.distance(first_point, second_point) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# 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
  
  triangulate(EARTH_RADIUS * calculate_distance(first_latitude_rad, second_latitude_rad, delta_latitude, delta_longitude), elevation)
end