Module: Fit4Ruby::GeoMath

Defined in:
lib/fit4ruby/GeoMath.rb

Class Method Summary collapse

Class Method Details

.cos(deg) ⇒ Object



44
45
46
# File 'lib/fit4ruby/GeoMath.rb', line 44

def GeoMath.cos(deg)
  Math.cos(deg_to_rad(deg))
end

.deg_to_rad(deg) ⇒ Object



48
49
50
# File 'lib/fit4ruby/GeoMath.rb', line 48

def GeoMath.deg_to_rad(deg)
  deg * Math::PI / 180
end

.distance(p1_lat, p1_lon, p2_lat, p2_lon) ⇒ Object

This method uses the ellipsoidal earth projected to a plane formula prescribed by the FCC in 47 CFR 73.208 for distances not exceeding 475 km /295 miles.

Parameters:

  • p1_lat

    Latitude of the first point in polar degrees

  • p1_lon

    Longitude of the first point in polar degrees

  • p2_lat

    Latitude of the second point in polar degrees

  • p2_lon

    Longitude of the second point in polar degrees

Returns:

  • Distance in meters



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fit4ruby/GeoMath.rb', line 25

def GeoMath.distance(p1_lat, p1_lon, p2_lat, p2_lon)
  # Difference in latitude and longitude
  delta_lat = p2_lat - p1_lat
  delta_lon = p2_lon - p1_lon

  # Mean latitude
  mean_lat = (p1_lat + p2_lat) / 2

  # kilometers per degree of latitude difference
  k1 = 111.13209 - 0.56606 * cos(2 * mean_lat) +
       0.00120 * cos(4 * mean_lat)
  # kilometers per degree of longitude difference
  k2 = 111.41513 * cos(mean_lat) -
       0.09455 * cos(3 * mean_lat) +
       0.00012 * cos(5 * mean_lat)

  Math.sqrt(((k1 * delta_lat)) ** 2 + (k2 * delta_lon) ** 2) * 1000.0
end