]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - src/KML.py
Updated the KML classes with a print_kml() method.
[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
7
8 class KmlObject(object):
9 """
10 The base class of all KML elements, according to the reference:
11
12 http://code.google.com/apis/kml/documentation/kmlreference.html
13
14 Every other class in this module should derive from
15 KmlObject. This class provides a default constructor which creates
16 some necessary attributes, and default implementations of to_kml()
17 and render().
18
19 The to_kml() methods of our subclasses will, in general, generate
20 an opening tag, render themselves (whatever that means), and then
21 generate a closing tag. A call to render() generally returns the
22 element's text, and renders its children recursively.
23 """
24
25 OPEN_TAG = ''
26 CLOSE_TAG = ''
27
28 def __init__(self, initial_text=''):
29 self.children = []
30 self.text = initial_text
31
32
33 def to_kml(self):
34 return self.render()
35
36
37 def render(self):
38 kml = self.OPEN_TAG
39 kml += self.text
40
41 for c in self.children:
42 kml += c.to_kml()
43
44 kml += self.CLOSE_TAG + "\n"
45
46 return kml
47
48
49 def print_kml(self):
50 print self.OPEN_TAG
51 self.render_to_stdout()
52 print self.CLOSE_TAG
53
54
55 def render_to_stdout(self):
56 if not (len(self.text) == 0):
57 print self.text
58
59 for c in self.children:
60 c.print_kml()
61
62
63
64 class Color(KmlObject):
65
66 OPEN_TAG = '<color>'
67 CLOSE_TAG = '</color>'
68
69
70 class Document(KmlObject):
71
72 OPEN_TAG = """<?xml version=\"1.0\" encoding=\"UTF-8\"?>
73 <kml xmlns=\"http://www.opengis.net/kml/2.2\">
74 <Document>"""
75
76 CLOSE_TAG = """</Document>
77 </kml>"""
78
79 def __init__(self, initial_text=''):
80 super(Document, self).__init__(initial_text)
81 self.styles = []
82
83
84 def render(self):
85 kml = ''
86
87 for s in self.styles:
88 kml += s.to_kml()
89
90 for c in self.children:
91 kml += c.to_kml()
92
93 return kml
94
95
96 def render_to_stdout(self):
97 for s in self.styles:
98 s.print_kml()
99
100 for c in self.children:
101 c.print_kml()
102
103
104
105 class Description(KmlObject):
106
107 OPEN_TAG = '<description>'
108 CLOSE_TAG = '</description>'
109
110
111
112 class Name(KmlObject):
113
114 OPEN_TAG = '<name>'
115 CLOSE_TAG = '</name>'
116
117
118
119 class Placemark(KmlObject):
120
121 OPEN_TAG = '<Placemark>'
122 CLOSE_TAG = '</Placemark>'
123
124
125
126 class PolyStyle(KmlObject):
127
128 OPEN_TAG = '<PolyStyle>'
129 CLOSE_TAG = '</PolyStyle>'
130
131
132
133 class Style(KmlObject):
134
135 OPEN_TAG = "<Style id=\"%s\">"
136 CLOSE_TAG = '</Style>'
137
138 def __init__(self, initial_text='', initial_id=''):
139 super(Style, self).__init__(initial_text)
140 self.id = initial_id
141
142
143 def to_kml(self):
144 kml = ''
145 kml += (self.OPEN_TAG % self.id + "\n")
146 kml += self.render()
147 kml += self.CLOSE_TAG + "\n"
148
149 return kml
150
151
152 def print_kml(self):
153 print (self.OPEN_TAG % self.id)
154 self.render_to_stdout()
155 print self.CLOSE_TAG
156
157
158
159 class StyleUrl(KmlObject):
160
161 OPEN_TAG = '<styleUrl>'
162 CLOSE_TAG = '</styleUrl>'
163
164
165
166 class RawText(KmlObject):
167 pass