Class: KMLFile

Inherits:
Object
  • Object
show all
Defined in:
lib/kml_file.rb

Overview

The KMLFile class is the top level class for constructing KML files. To create a new KML file create a KMLFile instance and add KML objects to its objects. For example:

f = KMLFile.new
f.objects << Placemark.new(
  :name => 'Simple placemark',
  :description => 'Attached to the ground. Intelligently places itself at the height of the underlying terrain.',
  :geometry => Point.new(:coordinates=>'-122.0822035425683,37.42228990140251,0')
)
puts f.render

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(io) ⇒ Object

Parse the KML file using nokogiri <kml xmlns=“www.opengis.net/kml/2.2”>

<NetworkLinkControl> ... </NetworkLinkControl>
<!-- 0 or 1 Feature elements -->

</kml>



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kml_file.rb', line 35

def self.parse(io)
  kml = self.new
  doc = Nokogiri::XML::Document.parse(io)
  doc.root.element_children.each do |cld|
    case cld.name
    when 'Document'
      kml.features << KML::Document.parse(cld)
    when 'Folder'
    when 'NetworkLink'
    when 'Placemark'
    when 'GroundOverlay'
    when 'PhotoOverlay'
    when 'ScreenOverlay'
    end
  end
  kml
end

Instance Method Details

#objectsObject Also known as: features

The objects in the KML file



13
14
15
# File 'lib/kml_file.rb', line 13

def objects
  @objects ||= []
end

#render(xm = Builder::XmlMarkup.new(:indent => 2)) ⇒ Object

Render the KML file



19
20
21
22
23
24
# File 'lib/kml_file.rb', line 19

def render(xm=Builder::XmlMarkup.new(:indent => 2))
  xm.instruct!
  xm.kml(:xmlns => 'http://earth.google.com/kml/2.1'){
    objects.each { |o| o.render(xm) }
  }
end

#save(filename) ⇒ Object



26
27
28
# File 'lib/kml_file.rb', line 26

def save filename
  File.open(filename, 'w') { |f| f.write render }
end