13 |
|
|
14 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
15 |
|
|
|
# fix for people using python2.1 |
|
|
from __future__ import nested_scopes |
|
|
|
|
16 |
import os |
import os |
17 |
import string |
import string |
18 |
|
|
19 |
import Thuban.Lib.fileutil |
import Thuban.Lib.fileutil |
20 |
|
|
21 |
from Thuban.Model.color import Color |
from Thuban.Model.classification import \ |
22 |
|
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap |
|
from Thuban.Model.classification import * |
|
23 |
|
|
24 |
# |
# |
25 |
# one level of indention |
# one level of indention |
135 |
|
|
136 |
def __write_attribs(self, attrs): |
def __write_attribs(self, attrs): |
137 |
for name, value in attrs.items(): |
for name, value in attrs.items(): |
138 |
self.file.write(' %s="%s"' % (escape(name), escape(value))) |
self.file.write(' %s="%s"' % (self.encode(name), |
139 |
|
self.encode(value))) |
140 |
|
|
141 |
|
def encode(self, str): |
142 |
|
"""Return an XML-escaped and UTF-8 encoded copy of the string str. |
143 |
|
|
144 |
|
Assume that the argument is a bytestring in Latin 1. |
145 |
|
""" |
146 |
|
return unicode(escape(str),'latin1').encode("utf8") |
147 |
|
|
148 |
|
|
149 |
class SessionSaver(XMLWriter): |
class SessionSaver(XMLWriter): |
150 |
|
|
151 |
"""Class to serialize a session into an XML file. |
"""Class to serialize a session into an XML file. |
204 |
element, call write_layer for each layer contained in the map |
element, call write_layer for each layer contained in the map |
205 |
and finally call write_label_layer to write the label layer. |
and finally call write_label_layer to write the label layer. |
206 |
""" |
""" |
207 |
self.open_element('map title="%s"' % escape(map.title)) |
self.open_element('map title="%s"' % self.encode(map.title)) |
208 |
self.write_projection(map.projection) |
self.write_projection(map.projection) |
209 |
for layer in map.Layers(): |
for layer in map.Layers(): |
210 |
self.write_layer(layer) |
self.write_layer(layer) |
215 |
"""Write the projection. |
"""Write the projection. |
216 |
""" |
""" |
217 |
if projection and len(projection.params) > 0: |
if projection and len(projection.params) > 0: |
218 |
self.open_element("projection", |
self.open_element("projection", {"name": projection.GetName()}) |
|
{"name": escape(projection.GetName())}) |
|
219 |
for param in projection.params: |
for param in projection.params: |
220 |
self.write_element('parameter value="%s"' % escape(param)) |
self.write_element('parameter value="%s"' % |
221 |
|
self.encode(param)) |
222 |
self.close_element("projection") |
self.close_element("projection") |
223 |
|
|
224 |
def write_layer(self, layer, attrs = None): |
def write_layer(self, layer, attrs = None): |
269 |
self.open_element("classification", attrs) |
self.open_element("classification", attrs) |
270 |
|
|
271 |
|
|
272 |
types = [[lambda p: 'clnull label="%s"' % p.GetLabel(), |
types = [[lambda p: 'clnull label="%s"' % self.encode(p.GetLabel()), |
273 |
lambda p: 'clnull'], |
lambda p: 'clnull'], |
274 |
[lambda p: 'clpoint label="%s" value="%s"' % |
[lambda p: 'clpoint label="%s" value="%s"' % |
275 |
(p.GetLabel(), str(p.GetValue())), |
(self.encode(p.GetLabel()), str(p.GetValue())), |
276 |
lambda p: 'clpoint'], |
lambda p: 'clpoint'], |
277 |
[lambda p: 'clrange label="%s" min="%s" max="%s"' % |
[lambda p: 'clrange label="%s" range="%s"' % |
278 |
(p.GetLabel(), |
(self.encode(p.GetLabel()), |
279 |
str(p.GetMin()), (str(p.GetMax()))), |
str(p.GetRange())), |
280 |
lambda p: 'clrange']] |
lambda p: 'clrange']] |
281 |
|
|
282 |
def write_class_group(group): |
def write_class_group(group): |
312 |
for label in labels: |
for label in labels: |
313 |
self.write_element(('label x="%g" y="%g" text="%s"' |
self.write_element(('label x="%g" y="%g" text="%s"' |
314 |
' halign="%s" valign="%s"') |
' halign="%s" valign="%s"') |
315 |
% (label.x, label.y, label.text, label.halign, |
% (label.x, label.y, |
316 |
|
self.encode(label.text), |
317 |
|
label.halign, |
318 |
label.valign)) |
label.valign)) |
319 |
self.close_element('labellayer') |
self.close_element('labellayer') |
320 |
|
|