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 |
|
from types import UnicodeType |
20 |
|
|
21 |
import Thuban.Lib.fileutil |
import Thuban.Lib.fileutil |
22 |
|
|
23 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
24 |
|
from Thuban.Model.layer import Layer, RasterLayer |
25 |
|
|
26 |
from Thuban.Model.classification import * |
from Thuban.Model.classification import \ |
27 |
|
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap |
28 |
|
|
29 |
# |
# |
30 |
# one level of indention |
# one level of indention |
140 |
|
|
141 |
def __write_attribs(self, attrs): |
def __write_attribs(self, attrs): |
142 |
for name, value in attrs.items(): |
for name, value in attrs.items(): |
143 |
self.file.write(' %s="%s"' % (escape(name), escape(value))) |
self.file.write(' %s="%s"' % (self.encode(name), |
144 |
|
self.encode(value))) |
145 |
|
|
146 |
|
def encode(self, str): |
147 |
|
"""Return an XML-escaped and UTF-8 encoded copy of the string str.""" |
148 |
|
|
149 |
|
esc = escape(str) |
150 |
|
|
151 |
|
if isinstance(esc, UnicodeType): |
152 |
|
return esc.encode("utf8") |
153 |
|
else: |
154 |
|
return unicode(escape(str),'latin1').encode("utf8") |
155 |
|
|
156 |
class SessionSaver(XMLWriter): |
class SessionSaver(XMLWriter): |
157 |
|
|
158 |
"""Class to serialize a session into an XML file. |
"""Class to serialize a session into an XML file. |
211 |
element, call write_layer for each layer contained in the map |
element, call write_layer for each layer contained in the map |
212 |
and finally call write_label_layer to write the label layer. |
and finally call write_label_layer to write the label layer. |
213 |
""" |
""" |
214 |
self.open_element('map title="%s"' % escape(map.title)) |
self.open_element('map title="%s"' % self.encode(map.title)) |
215 |
self.write_projection(map.projection) |
self.write_projection(map.projection) |
216 |
for layer in map.Layers(): |
for layer in map.Layers(): |
217 |
self.write_layer(layer) |
self.write_layer(layer) |
222 |
"""Write the projection. |
"""Write the projection. |
223 |
""" |
""" |
224 |
if projection and len(projection.params) > 0: |
if projection and len(projection.params) > 0: |
225 |
self.open_element("projection", |
self.open_element("projection", {"name": projection.GetName()}) |
|
{"name": escape(projection.GetName())}) |
|
226 |
for param in projection.params: |
for param in projection.params: |
227 |
self.write_element('parameter value="%s"' % escape(param)) |
self.write_element('parameter value="%s"' % |
228 |
|
self.encode(param)) |
229 |
self.close_element("projection") |
self.close_element("projection") |
230 |
|
|
231 |
def write_layer(self, layer, attrs = None): |
def write_layer(self, layer, attrs = None): |
235 |
given, should be a mapping from attribute names to attribute |
given, should be a mapping from attribute names to attribute |
236 |
values. The values should not be XML-escaped yet. |
values. The values should not be XML-escaped yet. |
237 |
""" |
""" |
|
lc = layer.GetClassification() |
|
238 |
|
|
239 |
if attrs is None: |
if attrs is None: |
240 |
attrs = {} |
attrs = {} |
241 |
|
|
242 |
attrs["title"] = layer.title |
attrs["title"] = layer.title |
243 |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
244 |
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
attrs["visible"] = ("false", "true")[int(layer.Visible())] |
245 |
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
|
246 |
attrs["fill"] = lc.GetDefaultFill().hex() |
if isinstance(layer, Layer): |
247 |
|
|
248 |
self.open_element("layer", attrs) |
lc = layer.GetClassification() |
249 |
|
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
250 |
|
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
251 |
|
attrs["fill"] = lc.GetDefaultFill().hex() |
252 |
|
|
253 |
proj = layer.GetProjection() |
self.open_element("layer", attrs) |
254 |
if proj is not None: |
self.write_projection(layer.GetProjection()) |
255 |
self.write_projection(proj) |
self.write_classification(layer) |
256 |
|
self.close_element("layer") |
257 |
|
|
258 |
self.write_classification(layer) |
elif isinstance(layer, RasterLayer): |
259 |
|
|
260 |
self.close_element("layer") |
self.open_element("rasterlayer", attrs) |
261 |
|
self.write_projection(layer.GetProjection()) |
262 |
|
self.close_element("rasterlayer") |
263 |
|
|
264 |
def write_classification(self, layer, attrs = None): |
def write_classification(self, layer, attrs = None): |
265 |
if attrs is None: |
if attrs is None: |
280 |
self.open_element("classification", attrs) |
self.open_element("classification", attrs) |
281 |
|
|
282 |
|
|
283 |
types = [[lambda p: 'clnull label="%s"' % p.GetLabel(), |
types = [[lambda p: 'clnull label="%s"' % self.encode(p.GetLabel()), |
284 |
lambda p: 'clnull'], |
lambda p: 'clnull'], |
285 |
[lambda p: 'clpoint label="%s" value="%s"' % |
[lambda p: 'clpoint label="%s" value="%s"' % |
286 |
(p.GetLabel(), str(p.GetValue())), |
(self.encode(p.GetLabel()), str(p.GetValue())), |
287 |
lambda p: 'clpoint'], |
lambda p: 'clpoint'], |
288 |
[lambda p: 'clrange label="%s" min="%s" max="%s"' % |
[lambda p: 'clrange label="%s" range="%s"' % |
289 |
(p.GetLabel(), |
(self.encode(p.GetLabel()), |
290 |
str(p.GetMin()), (str(p.GetMax()))), |
str(p.GetRange())), |
291 |
lambda p: 'clrange']] |
lambda p: 'clrange']] |
292 |
|
|
293 |
def write_class_group(group): |
def write_class_group(group): |
323 |
for label in labels: |
for label in labels: |
324 |
self.write_element(('label x="%g" y="%g" text="%s"' |
self.write_element(('label x="%g" y="%g" text="%s"' |
325 |
' halign="%s" valign="%s"') |
' halign="%s" valign="%s"') |
326 |
% (label.x, label.y, label.text, label.halign, |
% (label.x, label.y, |
327 |
|
self.encode(label.text), |
328 |
|
label.halign, |
329 |
label.valign)) |
label.valign)) |
330 |
self.close_element('labellayer') |
self.close_element('labellayer') |
331 |
|
|