Sha256: 8a03fda511cf24f2ad26c590a0b99f261eb187b8225344c6efb4f0f75294d6f4

Contents?: true

Size: 1.53 KB

Versions: 7

Compression:

Stored size: 1.53 KB

Contents

module Terraformer

  class MultiPoint < Geometry

    def initialize *args
      case
      when Point === args[0]
        self.coordinates = args.map &:coordinates
      else
        super *args
      end
    end

    def first_coordinate
      coordinates[0]
    end

    def points
      coordinates.map {|p| Point.new p}
    end

    def == obj
      super obj do |o|
        self.coordinates.sort == obj.coordinates.sort
      end
    end

    def contains? obj
      points.any? {|p| p.contains? obj}
    end

    def within? obj
      case obj
      when MultiPoint
        points.all? {|p| obj.contains? p}
      when LineString
        points.all? {|p| obj.contains? p}
      when MultiLineString
        points.all? {|p| obj.contains? p}
      when Polygon
        obj.contains? self
      when MultiPolygon
        points.all? {|p| obj.polygons.any? {|polygon| polygon.contains? p}}
      else
        raise ArgumentError.new "unsupported type: #{obj.type rescue obj.class}"
      end
    end

    def add_point p
      p = p.coordinates if Point === p
      raise ArugmentError unless Coordinate === p
      coordinates << p
    end
    alias_method :<<, :add_point

    def insert_point idx, p
      p = p.coordinates if Point === p
      raise ArugmentError unless Coordinate === p
      coordinates.insert idx, p
    end

    def remove_point p
      p = p.coordinates if Point === p
      raise ArugmentError unless Coordinate === p
      coordinates.delete p
    end

    def remove_point_at idx
      coordinates.delete_at idx
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
terraformer-0.0.9 lib/terraformer/multi_point.rb
terraformer-0.0.8 lib/terraformer/multi_point.rb
terraformer-0.0.7 lib/terraformer/multi_point.rb
terraformer-0.0.6 lib/terraformer/multi_point.rb
terraformer-0.0.4 lib/terraformer/multi_point.rb
terraformer-0.0.3 lib/terraformer/multi_point.rb
terraformer-0.0.2 lib/terraformer/multi_point.rb