Class: RubyVolt::Meta::Geography::Polygon

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_volt/meta/geography.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rings = []) ⇒ Polygon

In the wire protocol the first ring is still the exterior boundary and subsequent rings are holes. However, in the wire protocol all rings, exterior and hole alike, must be counter clockwise, and the last point should not be equal to the first point.



109
110
111
112
# File 'lib/ruby_volt/meta/geography.rb', line 109

def initialize(rings = [])
  @rings = []
  rings.each {|ring| add_ring(ring)}
end

Instance Attribute Details

#ringsObject (readonly)

Returns the value of attribute rings.



105
106
107
# File 'lib/ruby_volt/meta/geography.rb', line 105

def rings
  @rings
end

Instance Method Details

#==(other) ⇒ Object



158
159
160
# File 'lib/ruby_volt/meta/geography.rb', line 158

def ==(other)
  other.is_a?(Polygon) && (rings.size == other.rings.size) && eval(rings.map.with_index {|ring, index| ring == other.rings[index]}.join("&&"))||true
end

#add_ring(ring) ⇒ Object

Raises:

  • (::ArgumentError)


114
115
116
117
# File 'lib/ruby_volt/meta/geography.rb', line 114

def add_ring(ring)
  raise(::ArgumentError, "LineString has to be represented by a pseudo container RubyVolt::Meta::Geography::Ring") unless ring.is_a?(Ring)
  @rings << ring
end

#fork_transform_rings!Object



127
128
129
# File 'lib/ruby_volt/meta/geography.rb', line 127

def fork_transform_rings!
  Polygon.new(transform_rings)
end

#has_holes?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/ruby_volt/meta/geography.rb', line 123

def has_holes?
  rings.size > 1
end

#holesObject



119
120
121
# File 'lib/ruby_volt/meta/geography.rb', line 119

def holes
  has_holes? ? rings[1..-1] : []
end

#inspectObject



146
147
148
# File 'lib/ruby_volt/meta/geography.rb', line 146

def inspect
  to_wkt
end

#to_sObject



150
151
152
# File 'lib/ruby_volt/meta/geography.rb', line 150

def to_s
  rings.join(", ")
end

#to_wktObject



154
155
156
# File 'lib/ruby_volt/meta/geography.rb', line 154

def to_wkt
  "POLYGON(#{to_s})"
end

#transform_ringsObject



136
137
138
139
140
141
142
143
144
# File 'lib/ruby_volt/meta/geography.rb', line 136

def transform_rings
  # To transform a ring [from Java] representation to wire protocol representation one must:
  # • Remove the last vertex, which is the same as the first vertex,
  # • Transform the coordinates to XYZPoint values, and
  # • Reverse the order of the rings from the second to the end.
  rings.map.with_index do |ring, index|
    index > 0 ? ring.fork_reverse_points : ring
  end.compact
end

#transform_rings!Object



131
132
133
134
# File 'lib/ruby_volt/meta/geography.rb', line 131

def transform_rings!
  @rings = transform_rings
  self
end