Sha256: d49b25968ee60e64fe5065e9b0dbb4061ad6fbc9b3d950dd4fdc142446f5940f

Contents?: true

Size: 950 Bytes

Versions: 5

Compression:

Stored size: 950 Bytes

Contents

module Mongoid
  module Geospatial
    class GeometryField < Array

      def bounding_box
        max_x, min_x = -Float::MAX, Float::MAX
        max_y, min_y = -Float::MAX, Float::MAX
        each do |point|
          max_y = point[1] if point[1] > max_y
          min_y = point[1] if point[1] < min_y
          max_x = point[0] if point[0] > max_x
          min_x = point[0] if point[0] < min_x
        end
        [[min_x, min_y], [max_x, max_y]]
      end
      alias :bbox :bounding_box

      def center_point
        min, max = *bbox
        [min[0] + max[0] / 2.0, min[1] + max[1] / 2.0]
      end
      alias :center :center_point

      def radius r = 1
        [center, r]
      end

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


      class << self

        # Database -> Object
        def demongoize(o)
          self.new(o)
        end

      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongoid_geospatial-2.7.2 lib/mongoid_geospatial/fields/geometry_field.rb
mongoid_geospatial-2.7.1 lib/mongoid_geospatial/fields/geometry_field.rb
mongoid_geospatial-2.7.0 lib/mongoid_geospatial/fields/geometry_field.rb
mongoid_geospatial-2.5.1 lib/mongoid_geospatial/fields/geometry_field.rb
mongoid_geospatial-2.5.0 lib/mongoid_geospatial/fields/geometry_field.rb