class Mongoid::Geospatial::Point

Point

Attributes

x[R]
y[R]

Public Class Methods

demongoize(object) click to toggle source

Database -> Object

# File lib/mongoid_geospatial/fields/point.rb, line 119
def demongoize(object)
  Point.new(*object) if object
end
evolve(object) click to toggle source

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

# File lib/mongoid_geospatial/fields/point.rb, line 138
def evolve(object)
  object.respond_to?(:x) ? object.mongoize : object
end
from_array(ary) click to toggle source

Also makes life easier:

-> nil

# File lib/mongoid_geospatial/fields/point.rb, line 92
def from_array(ary)
  return nil if ary.empty?
  ary[0..1].map(&:to_f)
end
from_hash(hsh) click to toggle source

Throw error on wrong hash, just for a change.

# File lib/mongoid_geospatial/fields/point.rb, line 98
def from_hash(hsh)
  fail "Hash must have at least 2 items" if hsh.size < 2
  [from_hash_x(hsh), from_hash_y(hsh)]
end
from_hash_x(hsh) click to toggle source
# File lib/mongoid_geospatial/fields/point.rb, line 111
def from_hash_x(hsh)
  v = (Mongoid::Geospatial.lng_symbols & hsh.keys).first
  return hsh[v].to_f if !v.nil? && hsh[v]
  fail "Hash cannot contain #{Mongoid::Geospatial.lat_symbols.inspect} as the first item if there is no #{Mongoid::Geospatial.lng_symbols.inspect}" if Mongoid::Geospatial.lat_symbols.index(keys[0])
  values[0].to_f
end
from_hash_y(hsh) click to toggle source
# File lib/mongoid_geospatial/fields/point.rb, line 103
def from_hash_y(hsh)
  v = (Mongoid::Geospatial.lat_symbols & hsh.keys).first
  return hsh[v].to_f if !v.nil? && hsh[v]
  fail "Hash must contain #{Mongoid::Geospatial.lat_symbols.inspect} if Ruby version is less than 1.9" if RUBY_VERSION.to_f < 1.9
  fail "Hash cannot contain #{Mongoid::Geospatial.lng_symbols.inspect} as the second item if there is no #{Mongoid::Geospatial.lat_symbols.inspect}" if Mongoid::Geospatial.lng_symbols.index(hsh.keys[1])
  hsh.values[1].to_f
end
from_string(str) click to toggle source

Makes life easier: “” -> nil

# File lib/mongoid_geospatial/fields/point.rb, line 85
def from_string(str)
  return nil if str.empty?
  str.split(/,|\s/).reject(&:empty?).map(&:to_f)
end
mongoize(object) click to toggle source
# File lib/mongoid_geospatial/fields/point.rb, line 123
def mongoize(object)
  case object
  when Point  then object.mongoize
  when String then from_string(object)
  when Array  then from_array(object)
  when Hash   then from_hash(object)
  when NilClass then nil
  else
    return object.to_xy if object.respond_to?(:to_xy)
    fail 'Invalid Point'
  end
end
new(x = nil, y = nil) click to toggle source
# File lib/mongoid_geospatial/fields/point.rb, line 9
def initialize(x = nil, y = nil)
  return unless x && y
  @x, @y = x, y
end

Public Instance Methods

[](args) click to toggle source
# File lib/mongoid_geospatial/fields/point.rb, line 23
def [](args)
  mongoize[args]
end
each() { |x| ... } click to toggle source
# File lib/mongoid_geospatial/fields/point.rb, line 27
def each
  yield x
  yield y
end
geo_distance(other) click to toggle source
# File lib/mongoid_geospatial/wrappers/georuby.rb, line 14
def geo_distance(other)
  to_geo.spherical_distance(other.to_geo)
end
mongoize() click to toggle source

Object -> Database Let’s store NilClass if we are invalid.

# File lib/mongoid_geospatial/fields/point.rb, line 16
def mongoize
  return nil unless x && y
  [x, y]
end
Also aliased as: to_a, to_xy
radius(r = 1) click to toggle source
# File lib/mongoid_geospatial/fields/point.rb, line 41
def radius(r = 1)
  [mongoize, r]
end
radius_sphere(r = 1, unit = :km) click to toggle source
# File lib/mongoid_geospatial/fields/point.rb, line 45
def radius_sphere(r = 1, unit = :km)
  radius r.to_f / Mongoid::Geospatial.earth_radius[unit]
end
rgeo_distance(other) click to toggle source
# File lib/mongoid_geospatial/wrappers/rgeo.rb, line 12
def rgeo_distance other
  to_rgeo.distance other.to_rgeo
end
to_a()
Alias for: mongoize
to_geo() click to toggle source
# File lib/mongoid_geospatial/wrappers/georuby.rb, line 9
def to_geo
  return unless valid?
  GeoRuby::SimpleFeatures::Point.xy(x, y)
end
to_hash(xl = :x, yl = :y)
Alias for: to_hsh
to_hsh(xl = :x, yl = :y) click to toggle source
# File lib/mongoid_geospatial/fields/point.rb, line 36
def to_hsh(xl = :x, yl = :y)
  { xl => x, yl => y }
end
Also aliased as: to_hash
to_rgeo() click to toggle source
# File lib/mongoid_geospatial/wrappers/rgeo.rb, line 8
def to_rgeo
  RGeo::Geographic.spherical_factory.point x, y
end
to_s() click to toggle source
# File lib/mongoid_geospatial/fields/point.rb, line 32
def to_s
  "#{x}, #{y}"
end
to_xy()
Alias for: mongoize
valid?() click to toggle source
# File lib/mongoid_geospatial/fields/point.rb, line 49
def valid?
  x && y && x.is_a?(Numeric) && y.is_a?(Numeric)
end