module Geocoder::Sql

def full_bearing(latitude, longitude, lat_attr, lon_attr, options = {})


http://www.beginningspatial.com/calculating_bearing_one_point_another
Based on:

SIN(), COS(), ASIN(), ATAN2().
For use with a database that supports MOD() and trigonometric functions

(:linear or :spherical).
and an options hash which must include a :bearing value
Fairly accurate bearing calculation. Takes a latitude, longitude,
#
def full_bearing(latitude, longitude, lat_attr, lon_attr, options = {})
  degrees_per_radian = Geocoder::Calculations::DEGREES_PER_RADIAN
  case options[:bearing] || Geocoder.config.distances
  when :linear
    "MOD(CAST(" +
      "(ATAN2( " +
        "((#{lon_attr} - #{longitude.to_f}) / #{degrees_per_radian}), " +
        "((#{lat_attr} - #{latitude.to_f}) / #{degrees_per_radian})" +
      ") * #{degrees_per_radian}) + 360 " +
    "AS decimal), 360)"
  when :spherical
    "MOD(CAST(" +
      "(ATAN2( " +
        "SIN( (#{lon_attr} - #{longitude.to_f}) / #{degrees_per_radian} ) * " +
        "COS( (#{lat_attr}) / #{degrees_per_radian} ), (" +
          "COS( (#{latitude.to_f}) / #{degrees_per_radian} ) * SIN( (#{lat_attr}) / #{degrees_per_radian})" +
        ") - (" +
          "SIN( (#{latitude.to_f}) / #{degrees_per_radian}) * COS((#{lat_attr}) / #{degrees_per_radian}) * " +
          "COS( (#{lon_attr} - #{longitude.to_f}) / #{degrees_per_radian})" +
        ")" +
      ") * #{degrees_per_radian}) + 360 " +
    "AS decimal), 360)"
  end
end