Class: RCAP::Base::Circle

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

Direct Known Subclasses

CAP_1_0::Circle, CAP_1_1::Circle, CAP_1_2::Circle

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

Attributes inherited from Point

#lattitude, #longitude

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

#errors, included, #valid?, #validate

Constructor Details

#initialize {|_self| ... } ⇒ Circle

Returns a new instance of Circle.

Parameters:

  • attributes (Hash)

Yields:

  • (_self)

Yield Parameters:



20
21
22
# File 'lib/rcap/base/circle.rb', line 20

def initialize
  yield(self) if block_given?
end

Instance Attribute Details

#radiusNumeric

Returns Expresed in kilometers.

Returns:

  • (Numeric)

    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

Parameters:

Returns:



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

Parameters:

  • circle_hash (Hash)

Returns:



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

Parameters:

  • circle_xml_element (REXML::Element)

Returns:



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

Parameters:

  • circle_yaml_data (Array(Numeric, Numeric, Numeric))

    lattitude, longitude, radius

Returns:



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

Parameters:

Returns:



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.

Parameters:

Returns:

  • (true, false)


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

def ==(other)
  to_a == other.to_a
end

#inspectString

Returns:



42
43
44
# File 'lib/rcap/base/circle.rb', line 42

def inspect
  "(#{ self })"
end

#to_aArray(Numeric,Numeric,Numeric)

Returns:

  • (Array(Numeric,Numeric,Numeric))


113
114
115
# File 'lib/rcap/base/circle.rb', line 113

def to_a
  [@lattitude, @longitude, @radius]
end

#to_geojsonObject

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_hHash

Returns:

  • (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_sString

Returns a string representation of the circle of the form

lattitude,longitude radius

Returns:



37
38
39
# File 'lib/rcap/base/circle.rb', line 37

def to_s
  "#{ @lattitude },#{ @longitude } #{ @radius }"
end

#to_xmlString

Returns:



54
55
56
# File 'lib/rcap/base/circle.rb', line 54

def to_xml
  to_xml_element.to_s
end

#to_xml_elementREXML::Element

Returns:

  • (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