Sha256: ee0cfdd96d11fc7ae11400d38ec041ff1e20f8d1bcaa06f6ef1a46c8f13f2e7a

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require 'rgeo'
require 'mongoid/geospatial/ext/rgeo_spherical_point_impl'

module Mongoid
  module Geospatial
    # Wrapper to Rgeo's Point
    Point.class_eval do
      #
      # With RGeo support
      #
      # @return (RGeo::SphericalFactory::Point)
      def to_rgeo
        RGeo::Geographic.spherical_factory.point x, y
      end

      #
      # Distance with RGeo
      #
      # @return (Float)
      def rgeo_distance(other)
        to_rgeo.distance other.to_rgeo
      end
    end

    # Rgeo's GeometryField concept
    GeometryField.class_eval do

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

    # Wrapper to Rgeo's LineString
    LineString.class_eval do
      def to_rgeo
        RGeo::Geographic.spherical_factory.line_string(points)
      end
    end

    # Wrapper to Rgeo's Polygon
    Polygon.class_eval do
      def to_rgeo
        ring = RGeo::Geographic.spherical_factory.linear_ring(points)
        RGeo::Geographic.spherical_factory.polygon(ring)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-geospatial-4.0.1 lib/mongoid/geospatial/wrappers/rgeo.rb