Sha256: de30d908499f509cd30782f781cea9e25e037cff8ada4a28a996e83e1a96d2f2

Contents?: true

Size: 1.61 KB

Versions: 32

Compression:

Stored size: 1.61 KB

Contents

#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = Activity.rb -- Fit4Ruby - FIT file processing library for Ruby
#
# Copyright (c) 2015 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

module Fit4Ruby

  module GeoMath

    # 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.
    # @param p1_lat Latitude of the first point in polar degrees
    # @param p1_lon Longitude of the first point in polar degrees
    # @param p2_lat Latitude of the second point in polar degrees
    # @param p2_lon Longitude of the second point in polar degrees
    # @return Distance in meters
    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

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

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

  end

end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
fit4ruby-3.13.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.12.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.11.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.10.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.9.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.8.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.7.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.6.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.5.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.4.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.3.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.2.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.1.0 lib/fit4ruby/GeoMath.rb
fit4ruby-3.0.0 lib/fit4ruby/GeoMath.rb
fit4ruby-2.0.0 lib/fit4ruby/GeoMath.rb
fit4ruby-1.7.0 lib/fit4ruby/GeoMath.rb
fit4ruby-1.6.2 lib/fit4ruby/GeoMath.rb
fit4ruby-1.6.1 lib/fit4ruby/GeoMath.rb
fit4ruby-1.6.0 lib/fit4ruby/GeoMath.rb
fit4ruby-1.5.1 lib/fit4ruby/GeoMath.rb