]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - src/KML.py
Added the KML module with enough classes to export our block data as KML.
[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 def __init__(self, initial_text=''):
26 self.children = []
27 self.text = initial_text
28
29
30 def to_kml(self):
31 return self.render()
32
33
34 def render(self):
35 kml = self.text
36
37 for c in self.children:
38 kml += c.to_kml()
39
40 return kml
41
42
43
44 class Color(KmlObject):
45
46 def to_kml(self):
47 kml = '<color>'
48 kml += self.render()
49 kml += "</color>\n"
50 return kml
51
52
53
54 class Document(KmlObject):
55
56 def __init__(self, initial_text=''):
57 super(Document, self).__init__(initial_text)
58 self.styles = []
59
60
61 def to_kml(self):
62 kml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
63 kml += "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
64 kml += "<Document>\n"
65
66 kml += self.render()
67
68 kml += "</Document>\n"
69 kml += "</kml>\n"
70
71 return kml
72
73
74 def render(self):
75 kml = ''
76
77 for s in self.styles:
78 kml += s.to_kml()
79
80 for c in self.children:
81 kml += c.to_kml()
82
83 return kml
84
85
86
87 class Description(KmlObject):
88
89 def to_kml(self):
90 kml = '<description>'
91 kml += self.render()
92 kml += "</description>\n"
93 return kml
94
95
96
97 class Name(KmlObject):
98
99 def to_kml(self):
100 kml = '<name>'
101 kml += self.render()
102 kml += "</name>\n"
103 return kml
104
105
106
107 class Placemark(KmlObject):
108
109 def to_kml(self):
110 kml = "<Placemark>\n"
111 kml += self.render()
112 kml += "</Placemark>\n"
113 return kml
114
115
116
117 class PolyStyle(KmlObject):
118
119 def to_kml(self):
120 kml = "<PolyStyle>\n"
121 kml += self.render()
122 kml += "</PolyStyle>\n"
123 return kml
124
125
126
127 class Style(KmlObject):
128
129 def __init__(self, initial_text='', initial_id=None):
130 super(Style, self).__init__(initial_text)
131 self.id = initial_id
132
133
134 def to_kml(self):
135 kml = ''
136
137 if (self.id == None):
138 kml += "<Style>\n"
139 else:
140 kml += ("<Style id=\"%s\">\n" % self.id)
141
142 kml += self.render()
143 kml += "</Style>\n"
144
145 return kml
146
147
148
149 class StyleUrl(KmlObject):
150
151 def to_kml(self):
152 kml = '<styleUrl>'
153 kml += self.render()
154 kml += "</styleUrl>\n"
155 return kml
156
157
158
159 class RawText(KmlObject):
160 pass