Skip to content

Instantly share code, notes, and snippets.

@dolzenko
Created November 26, 2010 15:25
Show Gist options
  • Save dolzenko/716842 to your computer and use it in GitHub Desktop.
Save dolzenko/716842 to your computer and use it in GitHub Desktop.
module Graticule
module Distance
class Spherical < DistanceFormula
def self.to_sql(options)
options = {
:units => :miles,
:latitude_column => 'latitude',
:longitude_column => 'longitude'
}.merge(options)
%{(ACOS(LEAST(1,
SIN(RADIANS(#{options[:latitude]})) *
SIN(RADIANS(#{options[:latitude_column]})) +
COS(RADIANS(#{options[:latitude]})) *
COS(RADIANS(#{options[:latitude_column]})) *
COS(RADIANS(#{options[:longitude_column]}) - RADIANS(#{options[:longitude]}))
)) * #{Graticule::Distance::EARTH_RADIUS[options[:units].to_sym]})
}.gsub("\n", '').squeeze(" ")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment