Sha256: 951ea990ce21b7ce2eb6ddd5f953ad0d1751d407907d12cb5322b76e426ec1f9

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
 #  Latitude/longitude spherical geodesy formulae & scripts (c) Chris Veness 2002-2010            
 #   - www.movable-type.co.uk/scripts/latlong.html
 #                                                                                                

module GeoCalc::Calc
  module Distance  
    def distance_to point, precision = 4
      GeoCalc::Calc::Distance.distance_to self, point, precision
    end

    # Returns the distance from this point to the supplied point, in km 
    # (using Haversine formula)
    # 
    # from: Haversine formula - R. W. Sinnott, "Virtues of the Haversine",
    #       Sky and Telescope, vol 68, no 2, 1984
    # 
    # GeoPoint point: Latitude/longitude of destination point
    # - Numeric precision=4: number of significant digits to use for returned value
    #
    # Returns - Numeric distance in km between this point and destination point
  
    def self.distance_to base_point, point, precision = 4
      # default 4 sig figs reflects typical 0.3% accuracy of spherical model
      precision ||= 4

      lat1 = base_point.lat.to_rad
      lon1 = base_point.lon.to_rad

      lat2 = point.lat.to_rad
      lon2 = point.lon.to_rad

      dlat = lat2 - lat1
      dlon = lon2 - lon1

      a = Math.sin(dlat/2) * Math.sin(dlat/2) + Math.cos(lat1) * Math.cos(lat2) * Math.sin(dlon/2) * Math.sin(dlon/2)
      c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
      d = base_point.earth_radius_km * c
      d.round(precision)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geo_calc-0.7.1 lib/geo_calc/calc/distance.rb