Class: RCAP::Base::Polygon
- Inherits:
-
Object
- Object
- RCAP::Base::Polygon
show all
- Includes:
- Validation
- Defined in:
- lib/rcap/base/polygon.rb
Constant Summary
collapse
- XML_ELEMENT_NAME =
'polygon'
- XPATH =
"cap:#{ XML_ELEMENT_NAME }"
- POINTS_KEY =
'points'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Validation
#errors, included, #valid?, #validate
Constructor Details
#initialize {|_self| ... } ⇒ Polygon
Returns a new instance of Polygon.
18
19
20
21
|
# File 'lib/rcap/base/polygon.rb', line 18
def initialize
@points = []
yield(self) if block_given?
end
|
Instance Attribute Details
7
8
9
|
# File 'lib/rcap/base/polygon.rb', line 7
def points
@points
end
|
Class Method Details
.from_h(polygon_hash) ⇒ Polygon
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/rcap/base/polygon.rb', line 114
def self.from_h(polygon_hash)
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
|
.from_xml_element(polygon_xml_element) ⇒ Polygon
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/rcap/base/polygon.rb', line 56
def self.from_xml_element(polygon_xml_element)
if !polygon_xml_element.text.nil? && !polygon_xml_element.text.empty?
coordinates = parse_polygon_string(polygon_xml_element.text)
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
new
end
end
|
.from_yaml_data(polygon_yaml_data) ⇒ Polygon
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/rcap/base/polygon.rb', line 90
def self.from_yaml_data(polygon_yaml_data)
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
|
.parse_polygon_string(polygon_string) ⇒ Array<Array(Numeric,Numeric)>
85
86
87
|
# File 'lib/rcap/base/polygon.rb', line 85
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
#==(other) ⇒ true, false
Two polygons are equivalent if their collection of points is equivalent.
80
81
82
|
# File 'lib/rcap/base/polygon.rb', line 80
def ==(other)
@points == other.points
end
|
#add_point {|point| ... } ⇒ Object
23
24
25
26
27
28
|
# File 'lib/rcap/base/polygon.rb', line 23
def add_point
point = point_class.new
yield(point) if block_given?
points << point
point
end
|
44
45
46
|
# File 'lib/rcap/base/polygon.rb', line 44
def inspect
"(#{ @points.map { |point| point.inspect }.join(', ')})"
end
|
#to_geojson ⇒ Object
Returns GeoJSON representation of the polygon
31
32
33
34
|
# File 'lib/rcap/base/polygon.rb', line 31
def to_geojson
coordinates = @points.map { |point| [point.longitude, point.lattitude] }
{ 'type' => 'Polygon', 'coordinates' => [coordinates] }.to_json
end
|
#to_h ⇒ Hash
126
127
128
|
# File 'lib/rcap/base/polygon.rb', line 126
def to_h
{ POINTS_KEY => @points.map { |point| point.to_a } }
end
|
#to_s ⇒ Object
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
39
40
41
|
# File 'lib/rcap/base/polygon.rb', line 39
def to_s
@points.join(' ')
end
|
73
74
75
|
# File 'lib/rcap/base/polygon.rb', line 73
def to_xml
to_xml_element.to_s
end
|
#to_xml_element ⇒ REXML::Element
49
50
51
52
53
|
# File 'lib/rcap/base/polygon.rb', line 49
def to_xml_element
xml_element = REXML::Element.new(XML_ELEMENT_NAME)
xml_element.add_text(to_s)
xml_element
end
|
#to_yaml(options = {}) ⇒ String
107
108
109
|
# File 'lib/rcap/base/polygon.rb', line 107
def to_yaml(options = {})
to_yaml_data.to_yaml(options)
end
|
#to_yaml_data ⇒ Hash
102
103
104
|
# File 'lib/rcap/base/polygon.rb', line 102
def to_yaml_data
@points.map { |point| [point.lattitude, point.longitude] }
end
|