Module: Mongoid::Spacial

Defined in:
lib/mongoid_spacial/spacial.rb,
lib/mongoid_spacial/spacial/version.rb,
lib/mongoid_spacial/spacial/document.rb,
lib/mongoid_spacial/spacial/formulas.rb,
lib/mongoid_spacial/spacial/geo_near_results.rb

Defined Under Namespace

Modules: Document, Formulas Classes: GeoNearResults

Constant Summary collapse

EARTH_RADIUS_KM =

taken directly from mongodb

6371
EARTH_RADIUS =
{
  :km => EARTH_RADIUS_KM,
  :m  => EARTH_RADIUS_KM*1000,
  :mi => EARTH_RADIUS_KM*0.621371192, # taken directly from mongodb
  :ft => EARTH_RADIUS_KM*5280*0.621371192,
}
RAD_PER_DEG =
Math::PI/180
LNG_SYMBOLS =
[:x, :lon, :long, :lng, :longitude]
LAT_SYMBOLS =
[:y, :lat, :latitude]
VERSION =
"0.2.17"
@@lng_symbols =
LNG_SYMBOLS.dup
@@lat_symbols =
LAT_SYMBOLS.dup
@@earth_radius =
EARTH_RADIUS.dup
@@paginator =
:array
@@default_per_page =
25
@@spherical_distance_formula =
:n_vector

Class Method Summary collapse

Class Method Details

.distance(p1, p2, opts = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongoid_spacial/spacial.rb', line 22

def self.distance(p1,p2,opts = {})
  opts[:formula] ||= (opts[:spherical]) ? @@spherical_distance_formula : :pythagorean_theorem
  p1 = p1.to_lng_lat if p1.respond_to?(:to_lng_lat)
  p2 = p2.to_lng_lat if p2.respond_to?(:to_lng_lat)

  rads = Formulas.send(opts[:formula], p1, p2)

  if unit = earth_radius[opts[:unit]]
    opts[:unit] = (rads.instance_variable_get("@radian")) ? unit : unit * RAD_PER_DEG
  end

  rads *= opts[:unit].to_f if opts[:unit]
  rads
end