Module: GeoSpatial

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongo_geo.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#distance_from(pt) ⇒ Object

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mongo_geo.rb', line 70

def distance_from(pt)
  name = self.class.geo_key_name
  return nil if name.nil?
  raise(ArgumentError) unless [Array, Hash].include?(pt.class)
  
  loc = self.send(name)
  loc = loc.values if loc.is_a?(Hash)
  pt = pt.values if pt.is_a?(Hash)
  
  dx = loc[0] - pt[0]
  dy = loc[1] - pt[1]
  Math.sqrt((dx ** 2) + (dy ** 2))
end

#neighbors(opts = {}) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/mongo_geo.rb', line 84

def neighbors(opts = {})
  opts = {:skip => 0, :limit => 10}.merge(opts)
  location = self.class.geo_key_name.to_sym
  
  self.class.name.constantize.where(
    location.near => self.send(self.class.geo_key_name)
  ).skip(opts[:skip]).limit(opts[:limit] + 1).to_a.reject { |n| n.id == self.id }
end