-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathkml_file.rb
More file actions
38 lines (33 loc) · 1.13 KB
/
kml_file.rb
File metadata and controls
38 lines (33 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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 KMLFile
attr_accessor :objects
# The objects in the KML file
def objects
@objects ||= []
end
# Render the KML file
def render(xm=Builder::XmlMarkup.new(:indent => 2))
xm.instruct!
xmlnses = {}
xmlnses["xmlns".to_sym] = "http://www.opengis.net/kml/2.2"
xmlnses["xmlns:gx".to_sym] = "http://www.google.com/kml/ext/2.2"
xmlnses["xmlns:kml".to_sym] = "http://www.opengis.net/kml/2.2"
xmlnses["xmlns:atom".to_sym] = "http://www.w3.org/2005/Atom"
xm.kml(**xmlnses) {
objects.each { |o| o.render(xm) }
}
end
def save filename
File.open(filename, 'w') { |f| f.write render }
end
end
require 'kml/object'