1 |
bh |
454 |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
2 |
bh |
6 |
# Authors: |
3 |
|
|
# Jan-Oliver Wagner <[email protected]> |
4 |
|
|
# Bernhard Herzog <[email protected]> |
5 |
jonathan |
414 |
# Jonathan Coles <[email protected]> |
6 |
bh |
6 |
# |
7 |
|
|
# This program is free software under the GPL (>=v2) |
8 |
|
|
# Read the file COPYING coming with Thuban for details. |
9 |
|
|
|
10 |
|
|
""" |
11 |
|
|
Functions to save a session to a file |
12 |
|
|
""" |
13 |
|
|
|
14 |
|
|
__version__ = "$Revision$" |
15 |
|
|
|
16 |
jonathan |
440 |
# fix for people using python2.1 |
17 |
|
|
from __future__ import nested_scopes |
18 |
|
|
|
19 |
bh |
6 |
import os |
20 |
|
|
import string |
21 |
|
|
|
22 |
bh |
201 |
import Thuban.Lib.fileutil |
23 |
bh |
6 |
|
24 |
jonathan |
366 |
from Thuban.Model.color import Color |
25 |
|
|
|
26 |
jonathan |
429 |
from Thuban.Model.classification import * |
27 |
|
|
|
28 |
jonathan |
366 |
# |
29 |
|
|
# one level of indention |
30 |
|
|
# |
31 |
|
|
TAB = " " |
32 |
|
|
|
33 |
bh |
201 |
def relative_filename(dir, filename): |
34 |
|
|
"""Return a filename relative to dir for the absolute file name absname. |
35 |
|
|
|
36 |
|
|
This is almost the same as the function in fileutil, except that dir |
37 |
|
|
can be an empty string in which case filename will be returned |
38 |
|
|
unchanged. |
39 |
|
|
""" |
40 |
|
|
if dir: |
41 |
|
|
return Thuban.Lib.fileutil.relative_filename(dir, filename) |
42 |
|
|
else: |
43 |
|
|
return filename |
44 |
|
|
|
45 |
bh |
6 |
def escape(data): |
46 |
|
|
"""Escape &, \", ', <, and > in a string of data. |
47 |
|
|
""" |
48 |
|
|
data = string.replace(data, "&", "&") |
49 |
|
|
data = string.replace(data, "<", "<") |
50 |
|
|
data = string.replace(data, ">", ">") |
51 |
|
|
data = string.replace(data, '"', """) |
52 |
|
|
data = string.replace(data, "'", "'") |
53 |
|
|
return data |
54 |
|
|
|
55 |
bh |
268 |
class Saver: |
56 |
|
|
|
57 |
|
|
"""Class to serialize a session into an XML file. |
58 |
|
|
|
59 |
|
|
Applications built on top of Thuban may derive from this class and |
60 |
bh |
454 |
override or extend the methods to save additional information. This |
61 |
bh |
268 |
additional information should take the form of additional attributes |
62 |
|
|
or elements whose names are prefixed with a namespace. To define a |
63 |
|
|
namespace derived classes should extend the write_session method to |
64 |
|
|
pass the namespaces to the default implementation. |
65 |
|
|
""" |
66 |
|
|
|
67 |
jonathan |
366 |
|
68 |
bh |
268 |
def __init__(self, session): |
69 |
|
|
self.session = session |
70 |
|
|
|
71 |
|
|
def write(self, file_or_filename): |
72 |
|
|
"""Write the session to a file. |
73 |
|
|
|
74 |
|
|
The argument may be either a file object or a filename. If it's |
75 |
|
|
a filename, the file will be opened for writing. Files of |
76 |
|
|
shapefiles will be stored as filenames relative to the directory |
77 |
|
|
the file is stored in (as given by os.path.dirname(filename)) if |
78 |
|
|
they have a common parent directory other than the root |
79 |
|
|
directory. |
80 |
|
|
|
81 |
|
|
If the argument is a file object (which is determined by the |
82 |
|
|
presence of a write method) all filenames will be absolut |
83 |
|
|
filenames. |
84 |
|
|
""" |
85 |
jonathan |
366 |
|
86 |
jonathan |
391 |
# keep track of how many levels of indentation to write |
87 |
|
|
self.indent_level = 0 |
88 |
|
|
# track whether an element is currently open. see open_element(). |
89 |
|
|
self.element_open = 0 |
90 |
jonathan |
366 |
|
91 |
bh |
268 |
if hasattr(file_or_filename, "write"): |
92 |
|
|
# it's a file object |
93 |
|
|
self.file = file_or_filename |
94 |
|
|
self.dir = "" |
95 |
|
|
else: |
96 |
|
|
filename = file_or_filename |
97 |
|
|
self.dir = os.path.dirname(filename) |
98 |
|
|
self.file = open(filename, 'w') |
99 |
|
|
self.write_header() |
100 |
|
|
self.write_session(self.session) |
101 |
|
|
|
102 |
jonathan |
605 |
assert self.indent_level == 0 |
103 |
jonathan |
366 |
|
104 |
|
|
def write_attribs(self, attrs): |
105 |
bh |
268 |
for name, value in attrs.items(): |
106 |
jonathan |
440 |
self.file.write(' %s="%s"' % (escape(name), escape(value))) |
107 |
jonathan |
366 |
|
108 |
|
|
def open_element(self, element, attrs = {}): |
109 |
jonathan |
391 |
|
110 |
|
|
# |
111 |
|
|
# we note when an element is opened so that if two open_element() |
112 |
|
|
# calls are made successively we can end the currently open |
113 |
|
|
# tag and will later write a proper close tag. otherwise, |
114 |
|
|
# if a close_element() call is made directly after an open_element() |
115 |
|
|
# call we will close the tag with a /> |
116 |
|
|
# |
117 |
|
|
if self.element_open == 1: |
118 |
|
|
self.file.write(">\n") |
119 |
|
|
|
120 |
|
|
self.element_open = 1 |
121 |
|
|
|
122 |
jonathan |
366 |
# Helper function to write an element open tag with attributes |
123 |
|
|
self.file.write("%s<%s" % (TAB*self.indent_level, element)) |
124 |
|
|
self.write_attribs(attrs) |
125 |
bh |
268 |
|
126 |
jonathan |
366 |
self.indent_level += 1 |
127 |
|
|
|
128 |
|
|
def close_element(self, element): |
129 |
|
|
self.indent_level -= 1 |
130 |
jonathan |
605 |
assert self.indent_level >= 0 |
131 |
jonathan |
366 |
|
132 |
jonathan |
391 |
# see open_element() for an explanation |
133 |
|
|
if self.element_open == 1: |
134 |
|
|
self.element_open = 0 |
135 |
|
|
self.file.write("/>\n") |
136 |
|
|
else: |
137 |
|
|
self.file.write("%s</%s>\n" % (TAB*self.indent_level, element)) |
138 |
|
|
|
139 |
jonathan |
366 |
def write_element(self, element, attrs = {}): |
140 |
jonathan |
391 |
"""write an element that won't need a closing tag""" |
141 |
|
|
self.open_element(element, attrs) |
142 |
|
|
self.close_element(element) |
143 |
jonathan |
366 |
|
144 |
bh |
268 |
def write_header(self): |
145 |
|
|
"""Write the XML header""" |
146 |
jonathan |
366 |
self.file.write('<?xml version="1.0" encoding="UTF-8"?>\n') |
147 |
|
|
self.file.write('<!DOCTYPE session SYSTEM "thuban.dtd">\n') |
148 |
bh |
268 |
|
149 |
|
|
def write_session(self, session, attrs = None, namespaces = ()): |
150 |
|
|
"""Write the session and its contents |
151 |
|
|
|
152 |
|
|
By default, write a session element with the title attribute and |
153 |
|
|
call write_map for each map contained in the session. |
154 |
|
|
|
155 |
|
|
The optional argument attrs is for additional attributes and, if |
156 |
|
|
given, should be a mapping from attribute names to attribute |
157 |
|
|
values. The values should not be XML-escaped yet. |
158 |
|
|
|
159 |
|
|
The optional argument namespaces, if given, should be a sequence |
160 |
|
|
of (name, URI) pairs. The namespaces are written as namespace |
161 |
|
|
attributes into the session element. This is mainly useful for |
162 |
|
|
derived classes that need to store additional information in a |
163 |
|
|
thuban session file. |
164 |
|
|
""" |
165 |
|
|
if attrs is None: |
166 |
|
|
attrs = {} |
167 |
|
|
attrs["title"] = session.title |
168 |
|
|
for name, uri in namespaces: |
169 |
|
|
attrs["xmlns:" + name] = uri |
170 |
jonathan |
366 |
self.open_element("session", attrs) |
171 |
bh |
268 |
for map in session.Maps(): |
172 |
|
|
self.write_map(map) |
173 |
jonathan |
366 |
self.close_element("session") |
174 |
bh |
268 |
|
175 |
|
|
def write_map(self, map): |
176 |
|
|
"""Write the map and its contents. |
177 |
|
|
|
178 |
|
|
By default, write a map element element with the title |
179 |
|
|
attribute, call write_projection to write the projection |
180 |
|
|
element, call write_layer for each layer contained in the map |
181 |
|
|
and finally call write_label_layer to write the label layer. |
182 |
|
|
""" |
183 |
jonathan |
466 |
#write = self.file.write |
184 |
jonathan |
366 |
self.open_element('map title="%s"' % escape(map.title)) |
185 |
bh |
268 |
self.write_projection(map.projection) |
186 |
|
|
for layer in map.Layers(): |
187 |
|
|
self.write_layer(layer) |
188 |
|
|
self.write_label_layer(map.LabelLayer()) |
189 |
jonathan |
366 |
self.close_element('map') |
190 |
bh |
6 |
|
191 |
bh |
268 |
def write_projection(self, projection): |
192 |
|
|
"""Write the projection. |
193 |
|
|
""" |
194 |
|
|
if projection and len(projection.params) > 0: |
195 |
jonathan |
366 |
self.open_element("projection") |
196 |
bh |
268 |
for param in projection.params: |
197 |
jonathan |
366 |
self.write_element('parameter value="%s"' % escape(param)) |
198 |
|
|
self.close_element("projection") |
199 |
bh |
268 |
|
200 |
|
|
def write_layer(self, layer, attrs = None): |
201 |
|
|
"""Write the layer. |
202 |
|
|
|
203 |
|
|
The optional argument attrs is for additional attributes and, if |
204 |
|
|
given, should be a mapping from attribute names to attribute |
205 |
|
|
values. The values should not be XML-escaped yet. |
206 |
|
|
""" |
207 |
jonathan |
414 |
lc = layer.GetClassification() |
208 |
jonathan |
391 |
|
209 |
bh |
268 |
if attrs is None: |
210 |
|
|
attrs = {} |
211 |
jonathan |
429 |
|
212 |
|
|
attrs["title"] = layer.title |
213 |
|
|
attrs["filename"] = relative_filename(self.dir, layer.filename) |
214 |
jonathan |
466 |
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
215 |
|
|
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
216 |
jonathan |
429 |
attrs["fill"] = lc.GetDefaultFill().hex() |
217 |
bh |
268 |
|
218 |
jonathan |
366 |
self.open_element("layer", attrs) |
219 |
|
|
self.write_classification(layer) |
220 |
|
|
self.close_element("layer") |
221 |
|
|
|
222 |
|
|
def write_classification(self, layer, attrs = None): |
223 |
|
|
if attrs is None: |
224 |
|
|
attrs = {} |
225 |
|
|
|
226 |
jonathan |
414 |
lc = layer.GetClassification() |
227 |
jonathan |
366 |
|
228 |
jonathan |
429 |
field = lc.GetField() |
229 |
jonathan |
366 |
|
230 |
|
|
# |
231 |
|
|
# there isn't a classification of anything |
232 |
|
|
# so don't do anything |
233 |
|
|
# |
234 |
|
|
if field is None: return |
235 |
|
|
|
236 |
|
|
attrs["field"] = field |
237 |
jonathan |
466 |
attrs["field_type"] = str(lc.GetFieldType()) |
238 |
jonathan |
366 |
self.open_element("classification", attrs) |
239 |
|
|
|
240 |
jonathan |
429 |
|
241 |
|
|
# self.open_element("clnull") |
242 |
|
|
# write_class_data(lc.GetDefaultData()) |
243 |
|
|
# self.close_element("clnull") |
244 |
|
|
|
245 |
|
|
# just playing now with lambdas and dictionaries |
246 |
|
|
|
247 |
jonathan |
440 |
types = [[lambda p: 'clnull', |
248 |
|
|
lambda p: 'clnull'], |
249 |
|
|
[lambda p: 'clpoint value="%s"' % |
250 |
|
|
str(p.GetValue()), |
251 |
|
|
lambda p: 'clpoint'], |
252 |
|
|
[lambda p: 'clrange min="%s" max="%s"' % |
253 |
|
|
(str(p.GetMin()), |
254 |
|
|
(str(p.GetMax()))), |
255 |
|
|
lambda p: 'clrange']] |
256 |
jonathan |
429 |
|
257 |
jonathan |
440 |
def write_class_group(group): |
258 |
|
|
type = -1 |
259 |
|
|
if isinstance(group, ClassGroupDefault): type = 0 |
260 |
|
|
elif isinstance(group, ClassGroupSingleton): type = 1 |
261 |
|
|
elif isinstance(group, ClassGroupRange): type = 2 |
262 |
|
|
elif isinstance(group, ClassGroupMap): type = 3 |
263 |
jonathan |
605 |
assert type >= 0 |
264 |
jonathan |
366 |
|
265 |
jonathan |
440 |
if type <= 2: |
266 |
|
|
data = group.GetProperties() |
267 |
jonathan |
466 |
dict = {'stroke' : data.GetLineColor().hex(), |
268 |
|
|
'stroke_width': str(data.GetLineWidth()), |
269 |
jonathan |
440 |
'fill' : data.GetFill().hex()} |
270 |
|
|
|
271 |
|
|
self.open_element(types[type][0](group)) |
272 |
|
|
self.write_element("cldata", dict) |
273 |
|
|
self.close_element(types[type][1](group)) |
274 |
|
|
else: pass # XXX: we need to handle maps |
275 |
|
|
|
276 |
jonathan |
429 |
for i in lc: |
277 |
jonathan |
440 |
write_class_group(i) |
278 |
jonathan |
429 |
|
279 |
|
|
# for i in lc: |
280 |
|
|
# t = i.GetType() |
281 |
|
|
# self.open_element(types[t][0](i)) |
282 |
|
|
# write_class_data(i) |
283 |
|
|
# self.close_element(types[t][1](i)) |
284 |
|
|
|
285 |
|
|
# for p in lc: |
286 |
|
|
# type = p.GetType() |
287 |
|
|
# if p == ClassData.DEFAULT: |
288 |
|
|
# lopen = lclose = 'clnull' |
289 |
|
|
# elif p == ClassData.POINTS: |
290 |
|
|
# lopen = 'clpoint value="%s"' % escape(str(p.GetValue())) |
291 |
|
|
# lclose = 'clpoint' |
292 |
|
|
# elif p == ClassData.RANGES: |
293 |
|
|
# lopen = 'clrange min="%s" max="%s"' |
294 |
|
|
# % (escape(str(p.GetMin())), escape(str(p.GetMax())))) |
295 |
|
|
# lclose = 'clrange' |
296 |
|
|
|
297 |
|
|
# self.open_element(lopen) |
298 |
|
|
# write_class_data(p) |
299 |
|
|
# self.close_element(lclose) |
300 |
jonathan |
391 |
|
301 |
jonathan |
429 |
# if lc.points != {}: |
302 |
|
|
# for p in lc.points.values(): |
303 |
|
|
# self.open_element('clpoint value="%s"' % |
304 |
|
|
# (escape(str(p.GetValue())))) |
305 |
|
|
# write_class_data(p) |
306 |
|
|
# self.close_element('clpoint') |
307 |
|
|
# |
308 |
|
|
# if lc.ranges != []: |
309 |
|
|
# for p in lc.ranges: |
310 |
|
|
# self.open_element('clrange min="%s" max="%s"' |
311 |
|
|
# % (escape(str(p.GetMin())), escape(str(p.GetMax())))) |
312 |
|
|
# write_class_data(p) |
313 |
|
|
# self.close_element('clrange') |
314 |
jonathan |
366 |
|
315 |
|
|
self.close_element("classification") |
316 |
|
|
|
317 |
bh |
268 |
def write_label_layer(self, layer): |
318 |
|
|
"""Write the label layer. |
319 |
|
|
""" |
320 |
|
|
labels = layer.Labels() |
321 |
bh |
6 |
if labels: |
322 |
jonathan |
366 |
self.open_element('labellayer') |
323 |
bh |
6 |
for label in labels: |
324 |
jonathan |
366 |
self.write_element(('label x="%g" y="%g" text="%s"' |
325 |
|
|
' halign="%s" valign="%s"') |
326 |
bh |
268 |
% (label.x, label.y, label.text, label.halign, |
327 |
|
|
label.valign)) |
328 |
jonathan |
366 |
self.close_element('labellayer') |
329 |
bh |
6 |
|
330 |
bh |
268 |
|
331 |
|
|
|
332 |
|
|
def save_session(session, file, saver_class = None): |
333 |
|
|
"""Save the session session to a file. |
334 |
|
|
|
335 |
|
|
The file argument may either be a filename or an open file object. |
336 |
|
|
|
337 |
|
|
The optional argument saver_class is the class to use to serialize |
338 |
|
|
the session. By default or if it's None, the saver class will be |
339 |
|
|
Saver. |
340 |
|
|
|
341 |
|
|
If writing the session is successful call the session's |
342 |
|
|
UnsetModified method |
343 |
|
|
""" |
344 |
|
|
if saver_class is None: |
345 |
|
|
saver_class = Saver |
346 |
|
|
saver = saver_class(session) |
347 |
|
|
saver.write(file) |
348 |
|
|
|
349 |
bh |
6 |
# after a successful save consider the session unmodified. |
350 |
|
|
session.UnsetModified() |