Class: Kamelopard::CoordinateList

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(coords = nil) ⇒ CoordinateList

Accepts an optional array of coordinates in any format add_element accepts



169
170
171
172
173
174
175
176
177
178
# File 'lib/kamelopard/classes.rb', line 169

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

#coordinatesObject (readonly)

Returns the value of attribute coordinates.



165
166
167
# File 'lib/kamelopard/classes.rb', line 165

def coordinates
  @coordinates
end

Instance Method Details

#<<(a) ⇒ Object

Alias for add_element



194
195
196
# File 'lib/kamelopard/classes.rb', line 194

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 Point, 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 Point with those numbers, and pass it to add_element



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/kamelopard/classes.rb', line 207

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(elem = nil) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/kamelopard/classes.rb', line 180

def to_kml(elem = nil)
    e = REXML::Element.new 'coordinates'
    t = ''
    @coordinates.each do |a|
        t << "#{ a[0] },#{ a[1] }"
        t << ",#{ a[2] }" if a.size > 2
        t << ' '
    end
    e.text = t.chomp(' ')
    elem.elements.add e unless elem.nil?
    e
end