Sha256: d6fed09f1b21b4795dccdce80b5ec26d3ea5d513d41cdbc694e5a821dc50ae1d

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

module Terraformer

  class MultiPolygon < Geometry

    def initialize *args
      case
      when Coordinate === args[0] # only one
        self.coordinates = [[Coordinate.from_array(args)]]
      when Array === args[0] # multiple?
        self.coordinates = [Coordinate.from(args)]
      when Polygon === args[0]
        self.coordinates = args.map &:coordinates
      else
        super *args
      end
    end

    def first_coordinate
      coordinates[0][0][0]
    end

    def polygons
      coordinates.map {|p| Polygon.new *p}
    end

    def == obj
      super obj do |o|
        equal = true
        ps = polygons.sort
        ops = o.polygons.sort
        ps.each_with_index do |p, i|
          equal = p == ops[i]
          break unless equal
        end
        equal
      end
    end

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

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

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
terraformer-0.0.6 lib/terraformer/multi_polygon.rb
terraformer-0.0.4 lib/terraformer/multi_polygon.rb
terraformer-0.0.3 lib/terraformer/multi_polygon.rb
terraformer-0.0.2 lib/terraformer/multi_polygon.rb