Sha256: 7f9e7897435f5c83119c2245620944713055cb6e0c08378a42d8ad96fc1f7099

Contents?: true

Size: 1.27 KB

Versions: 9

Compression:

Stored size: 1.27 KB

Contents

# encoding: UTF-8
module Graticule
  module Distance
    #
    # The Haversine Formula works better at small distances than the Spherical Law of Cosines
    #
    # Thanks to Chris Veness (http://www.movable-type.co.uk/scripts/LatLong.html)
    # for distance formulas.
    #
    class Haversine < DistanceFormula

      # Calculate the distance between two Locations using the Haversine formula
      #
      #   Graticule::Distance::Haversine.distance(
      #     Graticule::Location.new(:latitude => 42.7654, :longitude => -86.1085),
      #     Graticule::Location.new(:latitude => 41.849838, :longitude => -87.648193)
      #   )
      #   #=> 101.061720831836
      #
      def self.distance(from, to, units = :miles)
        from_longitude  = from.longitude.to_radians
        from_latitude   = from.latitude.to_radians
        to_longitude    = to.longitude.to_radians
        to_latitude     = to.latitude.to_radians

        latitude_delta  = to_latitude - from_latitude
        longitude_delta = to_longitude - from_longitude

        a = sin(latitude_delta/2)**2 +
            cos(from_latitude) *
            cos(to_latitude) *
            sin(longitude_delta/2)**2

        c = 2 * atan2(sqrt(a), sqrt(1-a))

        d = EARTH_RADIUS[units.to_sym] * c
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
graticule-2.7.2 lib/graticule/distance/haversine.rb
graticule-2.7.1 lib/graticule/distance/haversine.rb
graticule-2.7.0 lib/graticule/distance/haversine.rb
graticule-2.6.0 lib/graticule/distance/haversine.rb
graticule-2.5.0 lib/graticule/distance/haversine.rb
graticule-2.4.0 lib/graticule/distance/haversine.rb
graticule-2.3.0 lib/graticule/distance/haversine.rb
graticule-2.2.0 lib/graticule/distance/haversine.rb
graticule-2.1.0 lib/graticule/distance/haversine.rb