Method: Dse::Geometry::Polygon#initialize

Defined in:
lib/dse/geometry/polygon.rb

#initialize(*args) ⇒ Polygon

Returns a new instance of Polygon.

Examples:

Construct an empty Polygon

polygon = Polygon.new

Construct a Polygon with LineString objects.

exterior_ring = LineString.new(Point.new(0, 0), Point.new(10, 0), Point.new(10, 10), Point.new(0, 0))
interior_ring = LineString.new(Point.new(1, 1), Point.new(1, 5), Point.new(5, 1), Point.new(1, 1))
polygon = Polygon.new(exterior_ring, interior_ring)

Construct a line-string with a wkt string.

polygon = Polygon.new('POLYGON ((0.0 0.0, 10.0 0.0, 10.0 10.0, 0.0 0.0), ' \
                      '(1.0 1.0, 1.0 5.0, 5.0 1.0, 1.0 1.0))')

Parameters:

  • args (Array<LineString>, Array<String>)

    varargs-style arguments in two forms:

    • ordered collection of linear-rings that make up this polygon. Can be empty.
    • one-element string array with the wkt representation.


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dse/geometry/polygon.rb', line 44

def initialize(*args)
  # The constructor has two forms:
  # 1. 0 or more LineString objects where the first is the exterior ring and the rest are interior rings.
  # 2. one String arg as the wkt representation.

  if args.size == 1 && args.first.is_a?(String)
    # subsitute eol chars in the string with a space.
    wkt = args.first.gsub(EOL_RE, ' ')
    # Consolidate whitespace before/after commas and parens.
    wkt.gsub!(/\s*([,\(\)])\s*/, '\1')
    if wkt == 'POLYGON EMPTY'
      @rings = [].freeze
    else
      match = wkt.match(WKT_RE)
      raise ArgumentError, "#{wkt.inspect} is not a valid WKT representation of a polygon" unless match
      @rings = parse_wkt_internal(match[1])
    end
  else
    @rings = args.freeze
    @rings.each do |ring|
      Cassandra::Util.assert_instance_of(LineString, ring, "#{ring.inspect} is not a LineString")
    end
  end
end