2 |
# Authors: |
# Authors: |
3 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> |
4 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
5 |
|
# Jonathan Coles <[email protected]> |
6 |
# |
# |
7 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
8 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
18 |
|
|
19 |
import Thuban.Lib.fileutil |
import Thuban.Lib.fileutil |
20 |
|
|
21 |
|
from Thuban.Model.color import Color |
22 |
|
|
23 |
|
from Thuban.Model.classification import * |
24 |
|
|
25 |
|
# |
26 |
|
# one level of indention |
27 |
|
# |
28 |
|
TAB = " " |
29 |
|
|
30 |
def relative_filename(dir, filename): |
def relative_filename(dir, filename): |
31 |
"""Return a filename relative to dir for the absolute file name absname. |
"""Return a filename relative to dir for the absolute file name absname. |
32 |
|
|
61 |
pass the namespaces to the default implementation. |
pass the namespaces to the default implementation. |
62 |
""" |
""" |
63 |
|
|
64 |
|
|
65 |
def __init__(self, session): |
def __init__(self, session): |
66 |
self.session = session |
self.session = session |
67 |
|
|
79 |
presence of a write method) all filenames will be absolut |
presence of a write method) all filenames will be absolut |
80 |
filenames. |
filenames. |
81 |
""" |
""" |
82 |
|
|
83 |
|
# keep track of how many levels of indentation to write |
84 |
|
self.indent_level = 0 |
85 |
|
# track whether an element is currently open. see open_element(). |
86 |
|
self.element_open = 0 |
87 |
|
|
88 |
if hasattr(file_or_filename, "write"): |
if hasattr(file_or_filename, "write"): |
89 |
# it's a file object |
# it's a file object |
90 |
self.file = file_or_filename |
self.file = file_or_filename |
96 |
self.write_header() |
self.write_header() |
97 |
self.write_session(self.session) |
self.write_session(self.session) |
98 |
|
|
99 |
def write_element(self, element, attrs, empty = 0, indentation = ""): |
assert(self.indent_level == 0) |
100 |
# Helper function to write an element open tag with attributes |
|
101 |
self.file.write("%s<%s" % (indentation, element)) |
def write_attribs(self, attrs): |
102 |
for name, value in attrs.items(): |
for name, value in attrs.items(): |
103 |
self.file.write(' %s="%s"' % (escape(name), escape(value))) |
self.file.write(' %s="%s"' % (escape(name), value)) |
104 |
if empty: |
|
105 |
|
def open_element(self, element, attrs = {}): |
106 |
|
|
107 |
|
# |
108 |
|
# we note when an element is opened so that if two open_element() |
109 |
|
# calls are made successively we can end the currently open |
110 |
|
# tag and will later write a proper close tag. otherwise, |
111 |
|
# if a close_element() call is made directly after an open_element() |
112 |
|
# call we will close the tag with a /> |
113 |
|
# |
114 |
|
if self.element_open == 1: |
115 |
|
self.file.write(">\n") |
116 |
|
|
117 |
|
self.element_open = 1 |
118 |
|
|
119 |
|
# Helper function to write an element open tag with attributes |
120 |
|
self.file.write("%s<%s" % (TAB*self.indent_level, element)) |
121 |
|
self.write_attribs(attrs) |
122 |
|
|
123 |
|
self.indent_level += 1 |
124 |
|
|
125 |
|
def close_element(self, element): |
126 |
|
self.indent_level -= 1 |
127 |
|
assert(self.indent_level >= 0) |
128 |
|
|
129 |
|
# see open_element() for an explanation |
130 |
|
if self.element_open == 1: |
131 |
|
self.element_open = 0 |
132 |
self.file.write("/>\n") |
self.file.write("/>\n") |
133 |
else: |
else: |
134 |
self.file.write(">\n") |
self.file.write("%s</%s>\n" % (TAB*self.indent_level, element)) |
135 |
|
|
136 |
|
def write_element(self, element, attrs = {}): |
137 |
|
"""write an element that won't need a closing tag""" |
138 |
|
self.open_element(element, attrs) |
139 |
|
self.close_element(element) |
140 |
|
|
141 |
def write_header(self): |
def write_header(self): |
142 |
"""Write the XML header""" |
"""Write the XML header""" |
143 |
write = self.file.write |
self.file.write('<?xml version="1.0" encoding="UTF-8"?>\n') |
144 |
write('<?xml version="1.0" encoding="UTF-8"?>\n') |
self.file.write('<!DOCTYPE session SYSTEM "thuban.dtd">\n') |
|
write('<!DOCTYPE session SYSTEM "thuban.dtd">\n') |
|
145 |
|
|
146 |
def write_session(self, session, attrs = None, namespaces = ()): |
def write_session(self, session, attrs = None, namespaces = ()): |
147 |
"""Write the session and its contents |
"""Write the session and its contents |
164 |
attrs["title"] = session.title |
attrs["title"] = session.title |
165 |
for name, uri in namespaces: |
for name, uri in namespaces: |
166 |
attrs["xmlns:" + name] = uri |
attrs["xmlns:" + name] = uri |
167 |
self.write_element("session", attrs) |
self.open_element("session", attrs) |
168 |
for map in session.Maps(): |
for map in session.Maps(): |
169 |
self.write_map(map) |
self.write_map(map) |
170 |
self.file.write('</session>\n') |
self.close_element("session") |
171 |
|
|
172 |
def write_map(self, map): |
def write_map(self, map): |
173 |
"""Write the map and its contents. |
"""Write the map and its contents. |
178 |
and finally call write_label_layer to write the label layer. |
and finally call write_label_layer to write the label layer. |
179 |
""" |
""" |
180 |
write = self.file.write |
write = self.file.write |
181 |
write('\t<map title="%s">\n' % escape(map.title)) |
self.open_element('map title="%s"' % escape(map.title)) |
182 |
self.write_projection(map.projection) |
self.write_projection(map.projection) |
183 |
for layer in map.Layers(): |
for layer in map.Layers(): |
184 |
self.write_layer(layer) |
self.write_layer(layer) |
185 |
self.write_label_layer(map.LabelLayer()) |
self.write_label_layer(map.LabelLayer()) |
186 |
write('\t</map>\n') |
self.close_element('map') |
187 |
|
|
188 |
def write_projection(self, projection): |
def write_projection(self, projection): |
189 |
"""Write the projection. |
"""Write the projection. |
190 |
""" |
""" |
191 |
if projection and len(projection.params) > 0: |
if projection and len(projection.params) > 0: |
192 |
self.file.write('\t\t<projection>\n') |
self.open_element("projection") |
193 |
for param in projection.params: |
for param in projection.params: |
194 |
self.file.write('\t\t\t<parameter value="%s"/>\n' |
self.write_element('parameter value="%s"' % escape(param)) |
195 |
% escape(param)) |
self.close_element("projection") |
|
self.file.write('\t\t</projection>\n') |
|
196 |
|
|
197 |
def write_layer(self, layer, attrs = None): |
def write_layer(self, layer, attrs = None): |
198 |
"""Write the layer. |
"""Write the layer. |
201 |
given, should be a mapping from attribute names to attribute |
given, should be a mapping from attribute names to attribute |
202 |
values. The values should not be XML-escaped yet. |
values. The values should not be XML-escaped yet. |
203 |
""" |
""" |
204 |
|
lc = layer.GetClassification() |
205 |
|
|
206 |
if attrs is None: |
if attrs is None: |
207 |
attrs = {} |
attrs = {} |
208 |
attrs["title"] = layer.title |
|
209 |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
attrs["title"] = layer.title |
210 |
attrs["stroke_width"] = str(layer.stroke_width) |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
211 |
fill = layer.fill |
attrs["stroke"] = lc.GetDefaultStroke().hex() |
212 |
if fill is None: |
attrs["stroke_width"] = str(lc.GetDefaultStrokeWidth()) |
213 |
attrs["fill"] = "None" |
attrs["fill"] = lc.GetDefaultFill().hex() |
214 |
else: |
|
215 |
attrs["fill"] = fill.hex() |
self.open_element("layer", attrs) |
216 |
stroke = layer.stroke |
self.write_classification(layer) |
217 |
if stroke is None: |
self.close_element("layer") |
218 |
attrs["stroke"] = "None" |
|
219 |
else: |
def write_classification(self, layer, attrs = None): |
220 |
attrs["stroke"] = stroke.hex() |
if attrs is None: |
221 |
self.write_element("layer", attrs, empty = 1, indentation = "\t\t") |
attrs = {} |
222 |
|
|
223 |
|
lc = layer.GetClassification() |
224 |
|
|
225 |
|
field = lc.GetField() |
226 |
|
|
227 |
|
# |
228 |
|
# there isn't a classification of anything |
229 |
|
# so don't do anything |
230 |
|
# |
231 |
|
if field is None: return |
232 |
|
|
233 |
|
attrs["field"] = field |
234 |
|
self.open_element("classification", attrs) |
235 |
|
|
236 |
|
|
237 |
|
# self.open_element("clnull") |
238 |
|
# write_class_data(lc.GetDefaultData()) |
239 |
|
# self.close_element("clnull") |
240 |
|
|
241 |
|
# just playing now with lambdas and dictionaries |
242 |
|
|
243 |
|
types = {ClassData.DEFAULT: |
244 |
|
[lambda p: 'clnull', |
245 |
|
lambda p: 'clnull'], |
246 |
|
ClassData.POINT: |
247 |
|
[lambda p: 'clpoint value="%s"' % |
248 |
|
str(p.GetValue()), |
249 |
|
lambda p: 'clpoint'], |
250 |
|
ClassData.RANGE: |
251 |
|
[lambda p: 'clrange min="%s" max="%s"' % |
252 |
|
(str(p.GetMin()), |
253 |
|
(str(p.GetMax()))), |
254 |
|
lambda p: 'clrange']} |
255 |
|
|
256 |
|
def write_class_data(data): |
257 |
|
dict = {'stroke' : data.GetStroke().hex(), |
258 |
|
'stroke_width': str(data.GetStrokeWidth()), |
259 |
|
'fill' : data.GetFill().hex()} |
260 |
|
t = data.GetType() |
261 |
|
self.open_element(types[t][0](data)) |
262 |
|
self.write_element("cldata", dict) |
263 |
|
self.close_element(types[t][1](data)) |
264 |
|
|
265 |
|
for i in lc: |
266 |
|
write_class_data(i) |
267 |
|
|
268 |
|
# for i in lc: |
269 |
|
# t = i.GetType() |
270 |
|
# self.open_element(types[t][0](i)) |
271 |
|
# write_class_data(i) |
272 |
|
# self.close_element(types[t][1](i)) |
273 |
|
|
274 |
|
# for p in lc: |
275 |
|
# type = p.GetType() |
276 |
|
# if p == ClassData.DEFAULT: |
277 |
|
# lopen = lclose = 'clnull' |
278 |
|
# elif p == ClassData.POINTS: |
279 |
|
# lopen = 'clpoint value="%s"' % escape(str(p.GetValue())) |
280 |
|
# lclose = 'clpoint' |
281 |
|
# elif p == ClassData.RANGES: |
282 |
|
# lopen = 'clrange min="%s" max="%s"' |
283 |
|
# % (escape(str(p.GetMin())), escape(str(p.GetMax())))) |
284 |
|
# lclose = 'clrange' |
285 |
|
|
286 |
|
# self.open_element(lopen) |
287 |
|
# write_class_data(p) |
288 |
|
# self.close_element(lclose) |
289 |
|
|
290 |
|
# if lc.points != {}: |
291 |
|
# for p in lc.points.values(): |
292 |
|
# self.open_element('clpoint value="%s"' % |
293 |
|
# (escape(str(p.GetValue())))) |
294 |
|
# write_class_data(p) |
295 |
|
# self.close_element('clpoint') |
296 |
|
# |
297 |
|
# if lc.ranges != []: |
298 |
|
# for p in lc.ranges: |
299 |
|
# self.open_element('clrange min="%s" max="%s"' |
300 |
|
# % (escape(str(p.GetMin())), escape(str(p.GetMax())))) |
301 |
|
# write_class_data(p) |
302 |
|
# self.close_element('clrange') |
303 |
|
|
304 |
|
self.close_element("classification") |
305 |
|
|
306 |
def write_label_layer(self, layer): |
def write_label_layer(self, layer): |
307 |
"""Write the label layer. |
"""Write the label layer. |
308 |
""" |
""" |
309 |
labels = layer.Labels() |
labels = layer.Labels() |
310 |
if labels: |
if labels: |
311 |
self.file.write('\t\t<labellayer>\n') |
self.open_element('labellayer') |
312 |
for label in labels: |
for label in labels: |
313 |
self.file.write(('\t\t\t<label x="%g" y="%g" text="%s"' |
self.write_element(('label x="%g" y="%g" text="%s"' |
314 |
' halign="%s" valign="%s"/>\n') |
' halign="%s" valign="%s"') |
315 |
% (label.x, label.y, label.text, label.halign, |
% (label.x, label.y, label.text, label.halign, |
316 |
label.valign)) |
label.valign)) |
317 |
self.file.write('\t\t</labellayer>\n') |
self.close_element('labellayer') |
318 |
|
|
319 |
|
|
320 |
|
|