]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - src/KML.py
Added XML escaping to the KML classes.
[dead/census-tools.git] / src / KML.py
1 """
2 Utility classes for working with the Keyhole Markup Language (KML).
3 """
4
5 import sys
6 from xml.sax.saxutils import escape
7
8
9 class KmlObject(object):
10 """
11 The base class of all KML elements, according to the reference:
12
13 http://code.google.com/apis/kml/documentation/kmlreference.html
14
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()
18 and render().
19
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.
24 """
25
26 OPEN_TAG = ''
27 CLOSE_TAG = ''
28
29 def __init__(self, initial_text=''):
30 self.children = []
31 self.text = initial_text
32
33
34 def to_kml(self):
35 return self.render()
36
37
38 def render(self):
39 kml = self.OPEN_TAG
40 kml += escape(self.text)
41
42 for c in self.children:
43 kml += c.to_kml()
44
45 kml += self.CLOSE_TAG + "\n"
46
47 return kml
48
49
50 def print_kml(self):
51 print self.OPEN_TAG
52 self.render_to_stdout()
53 print self.CLOSE_TAG
54
55
56 def render_to_stdout(self):
57 if (len(self.text) > 0):
58 print escape(self.text)
59
60 for c in self.children:
61 c.print_kml()
62
63
64
65 class Color(KmlObject):
66
67 OPEN_TAG = '<color>'
68 CLOSE_TAG = '</color>'
69
70
71 class Document(KmlObject):
72
73 OPEN_TAG = """<?xml version=\"1.0\" encoding=\"UTF-8\"?>
74 <kml xmlns=\"http://www.opengis.net/kml/2.2\">
75 <Document>"""
76
77 CLOSE_TAG = """</Document>
78 </kml>"""
79
80 def __init__(self, initial_text=''):
81 super(Document, self).__init__(initial_text)
82 self.styles = []
83
84
85 def render(self):
86 kml = ''
87
88 for s in self.styles:
89 kml += s.to_kml()
90
91 for c in self.children:
92 kml += c.to_kml()
93
94 return kml
95
96
97 def render_to_stdout(self):
98 for s in self.styles:
99 s.print_kml()
100
101 for c in self.children:
102 c.print_kml()
103
104
105
106 class Description(KmlObject):
107
108 OPEN_TAG = '<description>'
109 CLOSE_TAG = '</description>'
110
111
112
113 class Name(KmlObject):
114
115 OPEN_TAG = '<name>'
116 CLOSE_TAG = '</name>'
117
118
119
120 class Placemark(KmlObject):
121
122 OPEN_TAG = '<Placemark>'
123 CLOSE_TAG = '</Placemark>'
124
125
126
127 class PolyStyle(KmlObject):
128
129 OPEN_TAG = '<PolyStyle>'
130 CLOSE_TAG = '</PolyStyle>'
131
132
133
134 class Style(KmlObject):
135
136 OPEN_TAG = "<Style id=\"%s\">"
137 CLOSE_TAG = '</Style>'
138
139 def __init__(self, initial_text='', initial_id=''):
140 super(Style, self).__init__(initial_text)
141 self.id = initial_id
142
143
144 def to_kml(self):
145 kml = ''
146 kml += (self.OPEN_TAG % self.id + "\n")
147 kml += self.render()
148 kml += self.CLOSE_TAG + "\n"
149
150 return kml
151
152
153 def print_kml(self):
154 print (self.OPEN_TAG % self.id)
155 self.render_to_stdout()
156 print self.CLOSE_TAG
157
158
159
160 class StyleUrl(KmlObject):
161
162 OPEN_TAG = '<styleUrl>'
163 CLOSE_TAG = '</styleUrl>'
164
165
166
167 class RawText(KmlObject):
168
169 def to_kml(self):
170 return self.text
171
172 def print_kml(self):
173 if (len(self.text) > 0):
174 print self.text