]> gitweb.michael.orlitzky.com - dead/census-tools.git/blob - src/KML.py
26e8e71d551437caa72617c52bdfe7ce41717774
[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 kml = self.OPEN_TAG
36 kml += self.render()
37 kml += self.CLOSE_TAG + "\n"
38 return kml
39
40
41 def render(self):
42 kml = escape(self.text)
43
44 for c in self.children:
45 kml += c.to_kml()
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 LineString(KmlObject):
114
115 OPEN_TAG = '<LineString>'
116 CLOSE_TAG = '</LineString>'
117
118 @classmethod
119 def parse_linestrings(self, kml):
120 """
121 Parse each <LineString>...</LineString> block from the KML.
122 """
123 linestrings = []
124
125 search_idx = kml.find(self.OPEN_TAG, 0)
126
127 while (search_idx != -1):
128 # No reason to keep the tag around.
129 ls_start = search_idx + len(self.OPEN_TAG)
130 ls_tag_end = kml.find(self.CLOSE_TAG, ls_start)
131 ls = kml[ ls_start : ls_tag_end ]
132 linestrings.append(ls)
133 search_idx = kml.find(self.OPEN_TAG, (ls_tag_end + len(self.CLOSE_TAG)))
134
135 return linestrings
136
137
138 @classmethod
139 def parse_coordinates_from_linestrings(self, linestrings):
140 coords = []
141
142 for ls in linestrings:
143 c_tag_start = ls.find('<coordinates>')
144 c_start = c_tag_start + len('<coordinates>')
145 c_end = ls.find('</coordinates>')
146 c = ls[ c_start : c_end ]
147 coords.append(c)
148
149 return coords
150
151
152 @classmethod
153 def tuples_from_kml(self, kml):
154 """
155 Parse one or more linestrings from a KML document.
156 Return a list of tuples.
157 """
158 ls = self.parse_linestrings(kml)
159 cs = self.parse_coordinates_from_linestrings(ls)
160
161 tuples = []
162
163 for c in cs:
164 pointstrings = c.strip().split()
165 for point in pointstrings:
166 components = point.strip().split(',')
167 if (len(components) >= 2):
168 # Project the three-dimensional vector onto the
169 # x-y plane. I don't think we're going to run
170 # in to any linestrings in 3d.
171 tuples.append( (float(components[0]), float(components[1])) )
172
173 return tuples
174
175
176 class Name(KmlObject):
177
178 OPEN_TAG = '<name>'
179 CLOSE_TAG = '</name>'
180
181
182
183 class Placemark(KmlObject):
184
185 OPEN_TAG = '<Placemark>'
186 CLOSE_TAG = '</Placemark>'
187
188
189
190 class PolyStyle(KmlObject):
191
192 OPEN_TAG = '<PolyStyle>'
193 CLOSE_TAG = '</PolyStyle>'
194
195
196
197 class Style(KmlObject):
198
199 OPEN_TAG = "<Style id=\"%s\">"
200 CLOSE_TAG = '</Style>'
201
202 def __init__(self, initial_text='', initial_id=''):
203 super(Style, self).__init__(initial_text)
204 self.id = initial_id
205
206
207 def to_kml(self):
208 kml = ''
209 kml += (self.OPEN_TAG % self.id + "\n")
210 kml += self.render()
211 kml += self.CLOSE_TAG + "\n"
212
213 return kml
214
215
216 def print_kml(self):
217 print (self.OPEN_TAG % self.id)
218 self.render_to_stdout()
219 print self.CLOSE_TAG
220
221
222
223 class StyleUrl(KmlObject):
224
225 OPEN_TAG = '<styleUrl>'
226 CLOSE_TAG = '</styleUrl>'
227
228
229
230 class RawText(KmlObject):
231
232 def to_kml(self):
233 return self.text
234
235 def print_kml(self):
236 if (len(self.text) > 0):
237 print self.text