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


430
431
432
# File 'lib/kamelopard/classes.rb', line 430

def coordinates
  @coordinates
end

Instance Method Details

#<<(a) ⇒ Object

Alias for add_element


454
455
456
# File 'lib/kamelopard/classes.rb', line 454

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


470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/kamelopard/classes.rb', line 470

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


440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/kamelopard/classes.rb', line 440

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