]>
gitweb.michael.orlitzky.com - dead/census-tools.git/blob - src/KML.py
2 Utility classes for working with the Keyhole Markup Language (KML).
6 from xml
.sax
.saxutils
import escape
9 class KmlObject(object):
11 The base class of all KML elements, according to the reference:
13 http://code.google.com/apis/kml/documentation/kmlreference.html
15 Every other class in this module should derive from
16 KmlObject. This class provides a default constructor which creates
17 some necessary attributes, and default implementations of to_kml()
20 The to_kml() methods of our subclasses will, in general, generate
21 an opening tag, render themselves (whatever that means), and then
22 generate a closing tag. A call to render() generally returns the
23 element's text, and renders its children recursively.
29 def __init__(self
, initial_text
=''):
31 self
.text
= initial_text
40 kml
+= escape(self
.text
)
42 for c
in self
.children
:
45 kml
+= self
.CLOSE_TAG
+ "\n"
52 self
.render_to_stdout()
56 def render_to_stdout(self
):
57 if (len(self
.text
) > 0):
58 print escape(self
.text
)
60 for c
in self
.children
:
65 class Color(KmlObject
):
68 CLOSE_TAG
= '</color>'
71 class Document(KmlObject
):
73 OPEN_TAG
= """<?xml version=\"1.0\" encoding=\"UTF-8\"?>
74 <kml xmlns=\"http://www.opengis.net/kml/2.2\">
77 CLOSE_TAG
= """</Document>
80 def __init__(self
, initial_text
=''):
81 super(Document
, self
).__init
__(initial_text
)
91 for c
in self
.children
:
97 def render_to_stdout(self
):
101 for c
in self
.children
:
106 class Description(KmlObject
):
108 OPEN_TAG
= '<description>'
109 CLOSE_TAG
= '</description>'
113 class Name(KmlObject
):
116 CLOSE_TAG
= '</name>'
120 class Placemark(KmlObject
):
122 OPEN_TAG
= '<Placemark>'
123 CLOSE_TAG
= '</Placemark>'
127 class PolyStyle(KmlObject
):
129 OPEN_TAG
= '<PolyStyle>'
130 CLOSE_TAG
= '</PolyStyle>'
134 class Style(KmlObject
):
136 OPEN_TAG
= "<Style id=\"%s\">"
137 CLOSE_TAG
= '</Style>'
139 def __init__(self
, initial_text
='', initial_id
=''):
140 super(Style
, self
).__init
__(initial_text
)
146 kml
+= (self
.OPEN_TAG
% self
.id + "\n")
148 kml
+= self
.CLOSE_TAG
+ "\n"
154 print (self
.OPEN_TAG
% self
.id)
155 self
.render_to_stdout()
160 class StyleUrl(KmlObject
):
162 OPEN_TAG
= '<styleUrl>'
163 CLOSE_TAG
= '</styleUrl>'
167 class RawText(KmlObject
):
173 if (len(self
.text
) > 0):