Sha256: d37d69b2a9ec8f807ade00bccdd9033b597f328966e9faa2a309949e01af34e7

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

require 'rgeo'
require 'mongoid_geospatial/extensions/rgeo_spherical_point_impl'

module Mongoid
  module Geospatial

    class Point
      def to_geo
        RGeo::Geographic.spherical_factory.point x, y
      end

      def distance other
        to_geo.distance other.to_geo
      end

      def self.mongoize(obj)
        case obj
        when RGeo::Geographic::SphericalPointImpl then [obj.x, obj.y]
        when Point then obj.mongoize
        when Array then obj.to_xy
        when Hash  then obj.to_xy
        else obj
        end
      end
    end

    class GeometryField
      private
      def points
        self.map do |pair|
          RGeo::Geographic.spherical_factory.point(*pair)
        end
      end
    end

    class Line < GeometryField
      def to_geo
        RGeo::Geographic.spherical_factory.line_string points
      end

    end

    class Polygon < GeometryField
      def to_geo
        ring = RGeo::Geographic.spherical_factory.linear_ring points
        RGeo::Geographic.spherical_factory.polygon ring
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongoid_geospatial-2.8.3 lib/mongoid_geospatial/wrappers/rgeo.rb
mongoid_geospatial-2.8.2 lib/mongoid_geospatial/wrappers/rgeo.rb
mongoid_geospatial-2.8.1 lib/mongoid_geospatial/wrappers/rgeo.rb
mongoid_geospatial-2.8.0 lib/mongoid_geospatial/wrappers/rgeo.rb