Class: RCAP::Base::Circle
- Includes:
- Validation
- Defined in:
- lib/rcap/base/circle.rb
Direct Known Subclasses
Constant Summary collapse
- XML_ELEMENT_NAME =
'circle'- XPATH =
'cap:circle'- RADIUS_KEY =
'radius'- RADIUS_INDEX =
2
Constants inherited from Point
Point::LATTITUDE_INDEX, Point::LATTITUDE_KEY, Point::LONGITUDE_INDEX, Point::LONGITUDE_KEY, Point::MAX_LATTITUDE, Point::MAX_LONGITUDE, Point::MIN_LATTITUDE, Point::MIN_LONGITUDE
Instance Attribute Summary collapse
-
#radius ⇒ Numeric
Expresed in kilometers.
Attributes inherited from Point
Class Method Summary collapse
- .from_a(circle_array) ⇒ Circle
- .from_h(circle_hash) ⇒ Circle
- .from_xml_element(circle_xml_element) ⇒ Circle
- .from_yaml_data(circle_yaml_data) ⇒ Circle
-
.parse_circle_string(circle_string) ⇒ Array(Float,Float,Float)
Parses a circle string of the form lattitude,longitude radius.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Two circles are equivalent if their lattitude, longitude and radius are equal.
-
#initialize {|_self| ... } ⇒ Circle
constructor
A new instance of Circle.
- #inspect ⇒ String
- #to_a ⇒ Array(Numeric,Numeric,Numeric)
-
#to_geojson ⇒ Object
Returns GeoJSON representation of the circle in the form of a Point with radius property.
- #to_h ⇒ Hash
-
#to_s ⇒ String
Returns a string representation of the circle of the form lattitude,longitude radius.
- #to_xml ⇒ String
- #to_xml_element ⇒ REXML::Element
Methods included from Validation
#errors, included, #valid?, #validate
Constructor Details
#initialize {|_self| ... } ⇒ Circle
Returns a new instance of Circle.
20 21 22 |
# File 'lib/rcap/base/circle.rb', line 20 def initialize yield(self) if block_given? end |
Instance Attribute Details
#radius ⇒ Numeric
Returns Expresed in kilometers.
7 8 9 |
# File 'lib/rcap/base/circle.rb', line 7 def radius @radius end |
Class Method Details
.from_a(circle_array) ⇒ Circle
121 122 123 124 125 126 127 |
# File 'lib/rcap/base/circle.rb', line 121 def self.from_a(circle_array) new do |circle| circle.longitude = circle_array[LONGITUDE_INDEX].to_f circle.lattitude = circle_array[LATTITUDE_INDEX].to_f circle.radius = circle_array[RADIUS_INDEX].to_f end end |
.from_h(circle_hash) ⇒ Circle
104 105 106 107 108 109 110 |
# File 'lib/rcap/base/circle.rb', line 104 def self.from_h(circle_hash) new do |circle| circle.radius = circle_hash[RADIUS_KEY].to_f circle.lattitude = circle_hash[LATTITUDE_KEY].to_f circle.longitude = circle_hash[LONGITUDE_KEY].to_f end end |
.from_xml_element(circle_xml_element) ⇒ Circle
71 72 73 |
# File 'lib/rcap/base/circle.rb', line 71 def self.from_xml_element(circle_xml_element) from_a(parse_circle_string(circle_xml_element.text)) end |
.from_yaml_data(circle_yaml_data) ⇒ Circle
85 86 87 88 89 90 91 92 |
# File 'lib/rcap/base/circle.rb', line 85 def self.from_yaml_data(circle_yaml_data) lattitude, longitude, radius = circle_yaml_data new do |circle| circle.lattitude = lattitude.to_f circle.longitude = longitude.to_f circle.radius = radius.to_f end end |
.parse_circle_string(circle_string) ⇒ Array(Float,Float,Float)
Parses a circle string of the form
lattitude,longitude radius
63 64 65 66 67 |
# File 'lib/rcap/base/circle.rb', line 63 def self.parse_circle_string(circle_string) coordinates, radius = circle_string.split(' ') lattitude, longitude = coordinates.split(',') [lattitude, longitude, radius].map { |e| e.to_f } end |
Instance Method Details
#==(other) ⇒ true, false
Two circles are equivalent if their lattitude, longitude and radius are equal.
79 80 81 |
# File 'lib/rcap/base/circle.rb', line 79 def ==(other) to_a == other.to_a end |
#to_a ⇒ Array(Numeric,Numeric,Numeric)
113 114 115 |
# File 'lib/rcap/base/circle.rb', line 113 def to_a [@lattitude, @longitude, @radius] end |
#to_geojson ⇒ Object
Returns GeoJSON representation of the circle in the form of a Point with radius property
26 27 28 29 30 31 |
# File 'lib/rcap/base/circle.rb', line 26 def to_geojson { 'type' => 'Feature', 'geometry' => { 'type' => 'Point', 'coordinates' => [@longitude, @lattitude] }, 'properties' => { 'radius' => @radius } }.to_json end |
#to_h ⇒ Hash
96 97 98 99 100 |
# File 'lib/rcap/base/circle.rb', line 96 def to_h RCAP.attribute_values_to_hash([RADIUS_KEY, @radius], [LATTITUDE_KEY, @lattitude], [LONGITUDE_KEY, @longitude]) end |
#to_s ⇒ String
Returns a string representation of the circle of the form
lattitude,longitude radius
37 38 39 |
# File 'lib/rcap/base/circle.rb', line 37 def to_s "#{ @lattitude },#{ @longitude } #{ @radius }" end |
#to_xml ⇒ String
54 55 56 |
# File 'lib/rcap/base/circle.rb', line 54 def to_xml to_xml_element.to_s end |
#to_xml_element ⇒ REXML::Element
47 48 49 50 51 |
# File 'lib/rcap/base/circle.rb', line 47 def to_xml_element xml_element = REXML::Element.new(XML_ELEMENT_NAME) xml_element.add_text(to_s) xml_element end |