Class: Mongoid::Geospatial::Point

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mongoid_geospatial/fields/point.rb,
lib/mongoid_geospatial/wrappers/rgeo.rb,
lib/mongoid_geospatial/wrappers/georuby.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = nil, y = nil) ⇒ Point

Returns a new instance of Point.



7
8
9
10
11
# File 'lib/mongoid_geospatial/fields/point.rb', line 7

def initialize(x = nil, y = nil)
  return unless x
  ll = y ? [x, y] : x.split(/,|\s/).reject(&:empty?)
  @x, @y = ll.map(&:to_f)
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



5
6
7
# File 'lib/mongoid_geospatial/fields/point.rb', line 5

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



5
6
7
# File 'lib/mongoid_geospatial/fields/point.rb', line 5

def y
  @y
end

Class Method Details

.demongoize(object) ⇒ Object

Database -> Object



72
73
74
# File 'lib/mongoid_geospatial/fields/point.rb', line 72

def demongoize(object)
  Point.new(*object) if object
end

.evolve(object) ⇒ Object

Converts the object that was supplied to a criteria into a database friendly form.



87
88
89
# File 'lib/mongoid_geospatial/fields/point.rb', line 87

def evolve(object)
  object.respond_to?(:x) ? object.mongoize : object
end

.mongoize(object) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/mongoid_geospatial/fields/point.rb', line 76

def mongoize(object)
  case object
  when Point then object.mongoize
  when Array then Geospatial.from_array(object)
  when Hash then Geospatial.from_hash(object)
  else object.mongoize
  end
end

Instance Method Details

#[](args) ⇒ Object



20
21
22
# File 'lib/mongoid_geospatial/fields/point.rb', line 20

def [](args)
  mongoize[args]
end

#each {|x| ... } ⇒ Object

Yields:

  • (x)


24
25
26
27
# File 'lib/mongoid_geospatial/fields/point.rb', line 24

def each
  yield x
  yield y
end

#geo_distance(other) ⇒ Object



14
15
16
# File 'lib/mongoid_geospatial/wrappers/georuby.rb', line 14

def geo_distance other
  to_geo.spherical_distance(other.to_geo)
end

#mongoizeObject

Object -> Database



14
15
16
# File 'lib/mongoid_geospatial/fields/point.rb', line 14

def mongoize
  [x, y]
end

#radius(r = 1) ⇒ Object



38
39
40
# File 'lib/mongoid_geospatial/fields/point.rb', line 38

def radius r = 1
  [mongoize, r]
end

#radius_sphere(r = 1, unit = :km) ⇒ Object



42
43
44
# File 'lib/mongoid_geospatial/fields/point.rb', line 42

def radius_sphere r = 1, unit = :km
  radius r.to_f/Mongoid::Geospatial.earth_radius[unit]
end

#rgeo_distance(other) ⇒ Object



12
13
14
# File 'lib/mongoid_geospatial/wrappers/rgeo.rb', line 12

def rgeo_distance other
  to_rgeo.distance other.to_rgeo
end

#to_aObject

Object -> Database



17
18
19
# File 'lib/mongoid_geospatial/fields/point.rb', line 17

def mongoize
  [x, y]
end

#to_geoObject



10
11
12
# File 'lib/mongoid_geospatial/wrappers/georuby.rb', line 10

def to_geo
  GeoRuby::SimpleFeatures::Point.xy(x, y)
end

#to_hsh(xl = :x, yl = :y) ⇒ Object Also known as: to_hash



33
34
35
# File 'lib/mongoid_geospatial/fields/point.rb', line 33

def to_hsh xl = :x, yl = :y
  {xl => x, yl => y}
end

#to_rgeoObject



8
9
10
# File 'lib/mongoid_geospatial/wrappers/rgeo.rb', line 8

def to_rgeo
  RGeo::Geographic.spherical_factory.point x, y
end

#to_sObject



29
30
31
# File 'lib/mongoid_geospatial/fields/point.rb', line 29

def to_s
  "#{x}, #{y}"
end

#to_xyObject

Object -> Database



18
19
20
# File 'lib/mongoid_geospatial/fields/point.rb', line 18

def mongoize
  [x, y]
end