Module: Kamelopard::CoordinateList

Included in:
LineString, LinearRing
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

Instance Attribute Details

#coordinatesObject

Returns the value of attribute coordinates.



207
208
209
# File 'lib/kamelopard/classes.rb', line 207

def coordinates
  @coordinates
end

Instance Method Details

#<<(a) ⇒ Object

Alias for add_element



231
232
233
# File 'lib/kamelopard/classes.rb', line 231

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 - XXX The above stipulation is a weakness that needs fixing +



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/kamelopard/classes.rb', line 247

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

#coordinates_to_kml(elem = nil) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/kamelopard/classes.rb', line 217

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