Class: Birdwatcher::KML

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

Overview

Note:

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
<<-FOOT
</Document>
</kml>
FOOT

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ KML

Class initializer

Parameters:

  • attributes (Hash) (defaults to: {})

    Document attributes

See Also:



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

Parameters:

  • id (String)

    Folder ID

  • attributes (Hash)

    Folder attributes

See Also:



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

Parameters:

  • attributes (Hash)

    Placemark attributes

See Also:



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

Parameters:

  • folder_id (String)
  • attributes (Hash)

    Placemark attributes

Raises:

See Also:



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

#generateObject

Generate the KML document

Returns:

  • the final 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 += generate_document_footer
end