Class: KMLAuthor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ KMLAuthor

Returns a new instance of KMLAuthor.



15
16
17
18
19
20
21
# File 'lib/kml/kml_writer.rb', line 15

def initialize(filename)
  @filename = filename.dup #Getting weird frozen error...
  unless @filename =~ /\.kml$/
    @filename << '.kml'
  end
  @openfile = File.open(@filename, 'w')
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#openfileObject (readonly)

Returns the value of attribute openfile.



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

def openfile
  @openfile
end

Instance Method Details

#write_folder(folder) ⇒ Object

The main workhorse – a beautiful recursive function (if I may say so) to write folders to the document



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kml/kml_writer.rb', line 31

def write_folder(folder)
  @openfile.write "<Folder>\n<name>#{folder[:name]}</name>\n"

  unless folder[:folders].nil?
    folder[:folders].each do |inner_folder|
      write_folder(inner_folder)
    end
  else
    unless folder[:features].empty?
      folder[:features].each do |feature|
        write_placemark(feature)
      end
    end
  end
  @openfile.write "</Folder>\n\n"
end

Properly close the kml file



93
94
95
# File 'lib/kml/kml_writer.rb', line 93

def write_footer
  @openfile.write("\n</Document>\n</kml>")
end

#write_header(title) ⇒ Object

Write the KML header information and give the the file a name



24
25
26
27
28
# File 'lib/kml/kml_writer.rb', line 24

def write_header(title)
  @openfile.write "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  @openfile.write "<kml xmlns=\"http://earth.google.com/kml/2.1\">\n"
  @openfile.write "<Document>\n<name>#{title}</name>\n\n"
end

#write_placemark(feature) ⇒ Object

Each placemark can be thought of as a feature in a geojson collection, there are many options, certainly not



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kml/kml_writer.rb', line 49

def write_placemark(feature)
  @openfile.write "<Placemark>\n"
  unless feature[:name].nil?
    @openfile.write "\t<name>#{feature[:name]}</name>\n"
  end
  if feature.has_key? :style
    @openfile.write "\t<styleUrl>#{feature[:style]}</styleUrl>\n"
  end
  if feature.has_key? :extended
    @openfile.write "\t<ExtendedData>\n"
    feature[:extended].each do |k,v|
      @openfile.write "\t\t<Data name=\"#{k}\">#{v}</Data>\n"
    end
    @openfile.write "\t</ExtendedData>\n"
  end
  if feature.has_key? :desc
    if feature.has_key? :link
      @openfile.write "\t<description>\n\t\t#{feature[:desc]}\n<![CDATA[<img src=\"#{feature[:link]}\">]]></description>\n"
    else
    @openfile.write "\t<description>\n\t\t#{feature[:desc]}\n\t</description>\n"
    end
  end
  if feature.has_key? :time
    @openfile.write "\t<TimeStamp>\n"
    @openfile.write "\t\t<when>#{feature[:time].iso8601}</when>\n"
    @openfile.write "\t</TimeStamp>"
  end
  if feature.has_key? :gxTrack
    @openfile.write %Q{\t<gx:Track>
    \t\t<altitudeMode>absolute</altitudeMode>
    \t\t<when>#{feature[:gxTrack][:start][:time].iso8601}</when>
    \t\t<when>#{feature[:gxTrack][:end][:time].iso8601}</when>
    \t\t<gx:coord>#{feature[:gxTrack][:start][:pos]}</gx:coord>
    \t\t<gx:coord>#{feature[:gxTrack][:end][:pos]}</gx:coord>
    \t</gx:Track>}
  end

  unless feature[:geometry].nil?
    @openfile.write "\t"+feature[:geometry].as_kml #What type of geometry do we need here?
  end
  @openfile.write "</Placemark>\n\n"
end