Class: Birdwatcher::KML
- Inherits:
-
Object
- Object
- Birdwatcher::KML
- Defined in:
- lib/birdwatcher/kml.rb
Overview
Attribute values ARE NOT automatically escaped. All values will have to be given in an HTML escaped fashion if there is a risk that they might contain unexpected or dangerous HTML.
KML Document generator
KML is a file format used to display geographic data in an Earth browser such as Google Earth. You can create KML files to pinpoint locations, add image overlays, and expose rich data in new ways. KML is an international standard maintained by the Open Geospatial Consortium, Inc. (OGC).
This class supports generating basic KML documents with Placemarks and Folders.
Defined Under Namespace
Classes: Error, UnknownFolderError
Constant Summary collapse
- DOCUMENT_HEADER =
KML document header
<<-HEAD <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Document> HEAD
- DOCUMENT_FOOTER =
KML document footer
<<-FOOT </Document> </kml> FOOT
Instance Method Summary collapse
-
#add_folder(id, attributes) ⇒ Object
Add a Folder.
-
#add_placemark(attributes) ⇒ Object
Add a Placemark.
-
#add_placemark_to_folder(folder_id, attributes) ⇒ Object
Add a Placemark to a Folder.
-
#generate ⇒ Object
Generate the KML document.
-
#initialize(attributes = {}) ⇒ KML
constructor
Class initializer.
Constructor Details
#initialize(attributes = {}) ⇒ KML
Class initializer
36 37 38 39 40 |
# File 'lib/birdwatcher/kml.rb', line 36 def initialize(attributes = {}) @attributes = attributes @folders = {} @placemarks = [] end |
Instance Method Details
#add_folder(id, attributes) ⇒ Object
Add a Folder
56 57 58 59 60 |
# File 'lib/birdwatcher/kml.rb', line 56 def add_folder(id, attributes) @folders[id] = { :placemarks => [] }.merge(attributes) end |
#add_placemark(attributes) ⇒ Object
Add a Placemark
46 47 48 |
# File 'lib/birdwatcher/kml.rb', line 46 def add_placemark(attributes) @placemarks << attributes end |
#add_placemark_to_folder(folder_id, attributes) ⇒ Object
Add a Placemark to a Folder
69 70 71 72 |
# File 'lib/birdwatcher/kml.rb', line 69 def add_placemark_to_folder(folder_id, attributes) fail(UnknownFolderError, "There is no folder with id: #{folder_id}") unless @folders.key?(folder_id) @folders[folder_id][:placemarks] << attributes end |
#generate ⇒ Object
Generate the KML document
77 78 79 80 81 82 |
# File 'lib/birdwatcher/kml.rb', line 77 def generate output = generate_document_header @folders.each_pair { |id, attributes| output += generate_folder(id, attributes) } output += @placemarks.map { |p| generate_placemark(p) }.join output += end |