Sha256: 5495ac92868e0cfa08b840ac0c2ffcb521ed8c8493b3b849904464799b13a1d9

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

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

module Mongoid
  #
  # Wrappers for RGeo
  # https://github.com/rgeo/rgeo
  #
  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

2 entries across 2 versions & 1 rubygems

Version Path
mongoid-geospatial-5.1.0 lib/mongoid/geospatial/wrappers/rgeo.rb
mongoid-geospatial-5.0.0 lib/mongoid/geospatial/wrappers/rgeo.rb