Class: Polygon

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Validation
Defined in:
lib/generators/rcap/models/templates/models/polygon.rb

Overview

A Polygon object is valid if

  • it has a minimum of three points

  • each Point object in the points collection is valid

Constant Summary collapse

XML_ELEMENT_NAME =

:nodoc:

'polygon'
XPATH =

:nodoc:

"cap:#{ XML_ELEMENT_NAME }"
POINTS_KEY =

:nodoc:

'points'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Polygon

Returns a new instance of Polygon.



16
17
18
# File 'lib/generators/rcap/models/templates/models/polygon.rb', line 16

def initialize( attributes = {})
  @points = Array( attributes[ :points ])
end

Instance Attribute Details

#pointsObject (readonly)

Collection of Point objects.



8
9
10
# File 'lib/generators/rcap/models/templates/models/polygon.rb', line 8

def points
  @points
end

Class Method Details

.from_h(polygon_hash) ⇒ Object

:nodoc:



72
73
74
# File 'lib/generators/rcap/models/templates/models/polygon.rb', line 72

def self.from_h( polygon_hash ) # :nodoc:
  self.new( :points => polygon_hash[ POINTS_KEY ].map{ |point_hash| Point.from_h( point_hash )})
end

.from_xml_element(polygon_xml_element) ⇒ Object

:nodoc:



46
47
48
49
50
# File 'lib/generators/rcap/models/templates/models/polygon.rb', line 46

def self.from_xml_element( polygon_xml_element ) # :nodoc:
  coordinates = self.parse_polygon_string( polygon_xml_element.text )
  points = coordinates.map{ |lattitude, longitude| RCAP::Point.new( :lattitude => lattitude, :longitude => longitude )}[0..-2]
  polygon = self.new( :points => points )
end

.from_yaml_data(polygon_yaml_data) ⇒ Object

:nodoc:



60
61
62
63
64
# File 'lib/generators/rcap/models/templates/models/polygon.rb', line 60

def self.from_yaml_data( polygon_yaml_data ) # :nodoc:
  self.new(
    :points => Array( polygon_yaml_data ).map{ |lattitude, longitude| Point.new( :lattitude => lattitude, :longitude => longitude )}
  )
end

.parse_polygon_string(polygon_string) ⇒ Object

:nodoc:



42
43
44
# File 'lib/generators/rcap/models/templates/models/polygon.rb', line 42

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

Instance Method Details

#==(other) ⇒ Object

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



38
39
40
# File 'lib/generators/rcap/models/templates/models/polygon.rb', line 38

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

#inspectObject

:nodoc:



27
28
29
# File 'lib/generators/rcap/models/templates/models/polygon.rb', line 27

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

#to_hObject

:nodoc:



68
69
70
# File 'lib/generators/rcap/models/templates/models/polygon.rb', line 68

def to_h # :nodoc:
  { POINTS_KEY => self.points.map{ |point| point.to_h }}
end

#to_sObject

where each point is formatted with RCAP::Point#to_s



23
24
25
# File 'lib/generators/rcap/models/templates/models/polygon.rb', line 23

def to_s
  (@points + [ @points.first ]).join( ' ' )
end

#to_xml_elementObject

:nodoc:



31
32
33
34
35
# File 'lib/generators/rcap/models/templates/models/polygon.rb', line 31

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

#to_yaml(options = {}) ⇒ Object

:nodoc:



53
54
55
56
57
58
# File 'lib/generators/rcap/models/templates/models/polygon.rb', line 53

def to_yaml( options = {} ) # :nodoc:
  yaml_points = self.points.map{ |point| [ point.lattitude, point.longitude ]}
  def yaml_points.to_yaml_style; :inline; end

  yaml_points.to_yaml( options )
end