Class: RCAP::Base::Polygon

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/rcap/base/polygon.rb

Direct Known Subclasses

CAP_1_0::Polygon, CAP_1_1::Polygon, CAP_1_2::Polygon

Constant Summary

XML_ELEMENT_NAME =
'polygon'
XPATH =
"cap:#{ XML_ELEMENT_NAME }"
POINTS_KEY =
'points'

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Polygon) initialize {|_self| ... }

A new instance of Polygon

Parameters:

  • attributes (Hash)

Yields:

  • (_self)

Yield Parameters:



18
19
20
21
# File 'lib/rcap/base/polygon.rb', line 18

def initialize
  @points =  []
  yield( self ) if block_given?
end

Instance Attribute Details

- (Array<Point>) points (readonly)

Collection of RCAP::Base::Point objects

Returns:



7
8
9
# File 'lib/rcap/base/polygon.rb', line 7

def points
  @points
end

Class Method Details

+ (Polygon) from_h(polygon_hash)

Returns:



103
104
105
106
107
108
109
110
111
112
# File 'lib/rcap/base/polygon.rb', line 103

def self.from_h( polygon_hash )
  self.new do |polygon|
    Array( polygon_hash[ POINTS_KEY ]).each do |point_array|
      polygon.add_point do |point|
        point.lattitude = point_array[ Point::LATTITUDE_INDEX ].to_f
        point.longitude = point_array[ Point::LONGITUDE_INDEX ].to_f
      end
    end
  end
end

+ (Polygon) from_xml_element(polygon_xml_element)

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rcap/base/polygon.rb', line 50

def self.from_xml_element( polygon_xml_element )
  if !polygon_xml_element.text.nil? && !polygon_xml_element.text.empty?
    coordinates = self.parse_polygon_string( polygon_xml_element.text )
    self.new do |polygon|
      coordinates.each do |lattitude, longitude|
        polygon.add_point do |point|
          point.lattitude = lattitude.to_f
          point.longitude = longitude.to_f
        end
      end
    end
  else
    self.new
  end
end

+ (Polygon) from_yaml_data(polygon_yaml_data)

Returns:



84
85
86
87
88
89
90
91
92
93
# File 'lib/rcap/base/polygon.rb', line 84

def self.from_yaml_data( polygon_yaml_data )
  self.new do |polygon|
    Array( polygon_yaml_data ).each do |lattitude, longitude|
      polygon.add_point do |point|
        point.lattitude = lattitude.to_f
        point.longitude = longitude.to_f
      end
    end
  end
end

+ (Array<Array(Numeric,Numeric)>) parse_polygon_string(polygon_string)

Returns:



79
80
81
# File 'lib/rcap/base/polygon.rb', line 79

def self.parse_polygon_string( polygon_string )
  polygon_string.split( ' ' ).map{ |coordinate_string| coordinate_string.split( ',' ).map{|coordinate| coordinate.to_f }}
end

Instance Method Details

- (true, false) ==(other)

Two polygons are equivalent if their collection of points is equivalent.

Returns:

  • (true, false)


74
75
76
# File 'lib/rcap/base/polygon.rb', line 74

def ==( other )
  @points == other.points
end

- (Object) add_point {|point| ... }

Yields:

  • (point)


23
24
25
26
27
28
# File 'lib/rcap/base/polygon.rb', line 23

def add_point
  point = self.point_class.new
  yield( point ) if block_given?
  self.points << point
  point
end

- (String) inspect

Returns:



38
39
40
# File 'lib/rcap/base/polygon.rb', line 38

def inspect
  "(#{ @points.map{|point| point.inspect}.join(', ')})"
end

- (Hash) to_h

Returns:

  • (Hash)


115
116
117
# File 'lib/rcap/base/polygon.rb', line 115

def to_h
  { POINTS_KEY => @points.map{ |point| point.to_a }}
end

- (Object) to_s

Returns a string representation of the polygon of the form

points[0] points[1] points[2] ...

where each point is formatted with Point#to_s



33
34
35
# File 'lib/rcap/base/polygon.rb', line 33

def to_s
  @points.join( ' ' )
end

- (String) to_xml

Returns:



67
68
69
# File 'lib/rcap/base/polygon.rb', line 67

def to_xml
  self.to_xml_element.to_s
end

- (REXML::Element) to_xml_element

Returns:

  • (REXML::Element)


43
44
45
46
47
# File 'lib/rcap/base/polygon.rb', line 43

def to_xml_element
  xml_element = REXML::Element.new( XML_ELEMENT_NAME )
  xml_element.add_text( self.to_s )
  xml_element
end

- (String) to_yaml(options = {})

Returns:



96
97
98
# File 'lib/rcap/base/polygon.rb', line 96

def to_yaml( options = {} )
  @points.map{ |point| [ point.lattitude, point.longitude ]}.to_yaml( options )
end