lib/mongoid_geospatial/fields/point.rb in mongoid_geospatial-2.2.0 vs lib/mongoid_geospatial/fields/point.rb in mongoid_geospatial-2.3.0
- old
+ new
@@ -1,11 +1,11 @@
module Mongoid
module Geospatial
class Point
attr_accessor :x, :y
- def initialize(x, y)
+ def initialize(x=nil, y=nil)
@x, @y = x, y
end
# Object -> Database
def mongoize
@@ -16,15 +16,51 @@
def [](args)
mongoize[args]
end
+ def to_hsh xl = :x, yl = :y
+ {xl => x, yl => y}
+ end
+ alias :to_hash :to_hsh
+
+ def radius r = 1
+ [mongoize, r]
+ end
+
+ def radius_sphere r = 1, unit = :km
+ radius r.to_f/Mongoid::Geospatial.earth_radius[unit]
+ end
+
+ #
+ # Distance calculation methods. Thinking about not using it
+ # One needs to choose and external lib. GeoRuby or RGeo
+ #
+ # #Return the distance between the 2D points (ie taking care only of the x and y coordinates), assuming
+ # #the points are in projected coordinates. Euclidian distance in whatever unit the x and y ordinates are.
+ # def euclidian_distance(point)
+ # Math.sqrt((point.x - x)**2 + (point.y - y)**2)
+ # end
+
+ # # Spherical distance in meters, using 'Haversine' formula.
+ # # with a radius of 6471000m
+ # # Assumes x is the lon and y the lat, in degrees (Changed in version 1.1).
+ # # The user has to make sure using this distance makes sense (ie she should be in latlon coordinates)
+ # def spherical_distance(point,r=6370997.0)
+ # dlat = (point.lat - lat) * DEG2RAD / 2
+ # dlon = (point.lon - lon) * DEG2RAD / 2
+
+ # a = Math.sin(dlat)**2 + Math.cos(lat * DEG2RAD) * Math.cos(point.lat * DEG2RAD) * Math.sin(dlon)**2
+ # c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
+ # r * c
+ # end
+
class << self
# Database -> Object
def demongoize(object)
# return unless object && !object.empty?
- Point.new(object[0], object[1])
+ Point.new(*object)
end
def mongoize(object)
case object
when Point then object.mongoize