Sha256: c7d30bf1c326f29713f6269dd843522e679ef0d03f1dc49a234f714ee2020a70

Contents?: true

Size: 1.71 KB

Versions: 8

Compression:

Stored size: 1.71 KB

Contents

module Terraformer

  class MultiLineString < 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 LineString === args[0]
        self.coordinates = args.map &:coordinates
      else
        super *args
      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|
        equal = true
        lses = line_strings.sort
        olses = o.line_strings.sort
        lses.each_with_index do |ls, i|
          equal = ls == olses[i]
          break unless equal
        end
        equal
      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

8 entries across 8 versions & 1 rubygems

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