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.



326
327
328
# File 'lib/kamelopard/classes.rb', line 326

def coordinates
  @coordinates
end

Instance Method Details

#<<(a) ⇒ Object

Alias for add_element



350
351
352
# File 'lib/kamelopard/classes.rb', line 350

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 ++



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/kamelopard/classes.rb', line 366

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



336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/kamelopard/classes.rb', line 336

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