Class: CoordinateList
- Inherits:
-
Object
- Object
- CoordinateList
- Defined in:
- lib/kamelopard/classes.rb
Overview
Helper class for KML objects which need to know about several points at once
Instance Attribute Summary collapse
-
#coordinates ⇒ Object
readonly
Returns the value of attribute coordinates.
Instance Method Summary collapse
-
#<<(a) ⇒ Object
Alias for add_element.
-
#add_element(a) ⇒ Object
Adds one or more elements to this CoordinateList.
-
#initialize(coords = nil) ⇒ CoordinateList
constructor
Accepts an optional array of coordinates in any format add_element accepts.
- #to_kml(indent = 0) ⇒ Object
Constructor Details
#initialize(coords = nil) ⇒ CoordinateList
Accepts an optional array of coordinates in any format add_element accepts
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/kamelopard/classes.rb', line 145 def initialize(coords = nil) # Internally we store coordinates as an array of three-element # arrays @coordinates = [] if not coords.nil? then add_element coords else @coordinates = [] end end |
Instance Attribute Details
#coordinates ⇒ Object (readonly)
Returns the value of attribute coordinates.
141 142 143 |
# File 'lib/kamelopard/classes.rb', line 141 def coordinates @coordinates end |
Instance Method Details
#<<(a) ⇒ Object
Alias for add_element
170 171 172 |
# File 'lib/kamelopard/classes.rb', line 170 def <<(a) add_element a end |
#add_element(a) ⇒ Object
Adds one or more elements to this CoordinateList. The argument can be in any of several formats:
-
An array of arrays of numeric objects, in the form [ longitude, latitude, altitude (optional) ]
-
A KMLPoint, or some other object that response to latitude, longitude, and altitude methods
-
An array of the above
-
Another CoordinateList, to append to this on
Note that this will not accept a one-dimensional array of numbers to add a single point. Instead, create a KMLPoint with those numbers, and pass it to add_element
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/kamelopard/classes.rb', line 183 def add_element(a) if a.kind_of? Enumerable then # We've got some sort of array or list. It could be a list of # floats, to become one coordinate, or it could be several # coordinates t = a.to_a.first if t.kind_of? Enumerable then # At this point we assume we've got an array of float-like # objects. The second-level arrays need to have two or three # entries -- long, lat, and (optionally) alt a.each do |i| if i.size < 2 then raise "There aren't enough objects here to make a 2- or 3-element coordinate" elsif i.size >= 3 then @coordinates << [ i[0].to_f, i[1].to_f, i[2].to_f ] else @coordinates << [ i[0].to_f, i[1].to_f ] end end elsif t.respond_to? 'longitude' and t.respond_to? 'latitude' and t.respond_to? 'altitude' then # This object can cough up a set of coordinates a.each do |i| @coordinates << [i.longitude, i.latitude, i.altitude] end else # I dunno what it is raise "Kamelopard can't understand this object as a coordinate" end elsif a.kind_of? CoordinateList then # Append this coordinate list @coordinates << a.coordinates else # This is one element. It better know how to make latitude, longitude, etc. if a.respond_to? 'longitude' and a.respond_to? 'latitude' and a.respond_to? 'altitude' then @coordinates << [a.longitude, a.latitude, a.altitude] else raise "Kamelopard can't understand this object as a coordinate" end end end |
#to_kml(indent = 0) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/kamelopard/classes.rb', line 156 def to_kml(indent = 0) k = "#{ ' ' * indent }<coordinates>\n#{ ' ' * indent } " if not @coordinates.nil? then @coordinates.each do |a| k << "#{ a[0] },#{ a[1] }" k << ",#{ a[2] }" if a.size > 2 k << ' ' end end k << "\n#{ ' ' * indent}</coordinates>\n" k end |