Sha256: bb1fe6f39fd08110a5475556fd3da22af94281c1e86986999f9ad08a6ca79a6a

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

module Terraformer

  class MultiLineString < Geometry

    def initialize *args

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

      # must be an array of arrays of coordinates
      unless Array === coordinates &&
             Array === coordinates[0] &&
             Terraformer::Coordinate === coordinates[0][0]
        raise ArgumentError.new 'invalid coordinates for Terraformer::MultiLineString'
      end
    end

    def first_coordinate
      coordinates[0][0]
    end

    def line_strings
      coordinates.map {|ls| LineString.new ls}
    end

    def == obj
      super obj do |o|
        lses = line_strings.sort {|a,b| a.first_coordinate <=> b.first_coordinate }
        olses = o.line_strings.sort {|a,b| a.first_coordinate <=> b.first_coordinate }
        lses == olses
      end
    end

    def contains? obj
      case obj
      when Point
        line_strings.any? {|ls| ls.contains? obj}
      when MultiPoint
        obj.points.all? {|p| line_strings.any? {|ls| ls.contains? p}}
      when LineString
        line_strings.any? {|ls| ls == obj or ls.coordinates.slice_exists? obj.coordinates}
      when MultiLineString
        obj.line_strings.all? do |ols|
          line_strings.any? do |ls|
            ls == ols or ls.coordinates.slice_exists? ols.coordinates
          end
        end
      else
        raise ArgumentError.new "unsupported type: #{obj.type rescue obj.class}"
      end
    end

    def within? obj
      case obj
      when MultiLineString
        obj.contains? self
      when Polygon
        obj.contains? self
      when MultiPolygon
        obj.contains? self
      else
        raise ArgumentError.new "unsupported type: #{obj.type rescue obj.class}"
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
terraformer-0.2.1 lib/terraformer/multi_line_string.rb
terraformer-0.2.0 lib/terraformer/multi_line_string.rb