23 |
|
|
24 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
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 |
53 |
data = string.replace(data, "'", "'") |
data = string.replace(data, "'", "'") |
54 |
return data |
return data |
55 |
|
|
56 |
class XMLSaver: |
class XMLWriter: |
57 |
"""Abstract XMLSaver. |
"""Abstract XMLWriter. |
58 |
|
|
59 |
Should be overridden to provide specific object saving functionality. |
Should be overridden to provide specific object saving functionality. |
60 |
""" |
""" |
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 |
class SessionSaver(XMLSaver): |
def encode(self, str): |
147 |
|
"""Assume that str is in Latin1, escape it, and encode it in UTF-8. |
148 |
|
|
149 |
|
If str is None, return None |
150 |
|
""" |
151 |
|
|
152 |
|
if str is not None: |
153 |
|
return escape(str).encode("utf8") |
154 |
|
else: |
155 |
|
return None |
156 |
|
|
157 |
|
class SessionSaver(XMLWriter): |
158 |
|
|
159 |
"""Class to serialize a session into an XML file. |
"""Class to serialize a session into an XML file. |
160 |
|
|
168 |
|
|
169 |
|
|
170 |
def __init__(self, session): |
def __init__(self, session): |
171 |
XMLSaver.__init__(self) |
XMLWriter.__init__(self) |
172 |
self.session = session |
self.session = session |
173 |
|
|
174 |
def write(self, file_or_filename): |
def write(self, file_or_filename): |
175 |
XMLSaver.write(self, file_or_filename) |
XMLWriter.write(self, file_or_filename) |
176 |
|
|
177 |
self.write_header("session", "thuban.dtd") |
self.write_header("session", "thuban.dtd") |
178 |
self.write_session(self.session) |
self.write_session(self.session) |
212 |
element, call write_layer for each layer contained in the map |
element, call write_layer for each layer contained in the map |
213 |
and finally call write_label_layer to write the label layer. |
and finally call write_label_layer to write the label layer. |
214 |
""" |
""" |
215 |
self.open_element('map title="%s"' % escape(map.title)) |
self.open_element('map title="%s"' % self.encode(map.title)) |
216 |
self.write_projection(map.projection) |
self.write_projection(map.projection) |
217 |
for layer in map.Layers(): |
for layer in map.Layers(): |
218 |
self.write_layer(layer) |
self.write_layer(layer) |
223 |
"""Write the projection. |
"""Write the projection. |
224 |
""" |
""" |
225 |
if projection and len(projection.params) > 0: |
if projection and len(projection.params) > 0: |
226 |
self.open_element("projection") |
self.open_element("projection", {"name": projection.GetName()}) |
227 |
for param in projection.params: |
for param in projection.params: |
228 |
self.write_element('parameter value="%s"' % escape(param)) |
self.write_element('parameter value="%s"' % |
229 |
|
self.encode(param)) |
230 |
self.close_element("projection") |
self.close_element("projection") |
231 |
|
|
232 |
def write_layer(self, layer, attrs = None): |
def write_layer(self, layer, attrs = None): |
246 |
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
247 |
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
248 |
attrs["fill"] = lc.GetDefaultFill().hex() |
attrs["fill"] = lc.GetDefaultFill().hex() |
249 |
|
attrs["visible"] = ("false", "true")[int(layer.Visible())] |
250 |
|
|
251 |
self.open_element("layer", attrs) |
self.open_element("layer", attrs) |
252 |
|
|
253 |
|
proj = layer.GetProjection() |
254 |
|
if proj is not None: |
255 |
|
self.write_projection(proj) |
256 |
|
|
257 |
self.write_classification(layer) |
self.write_classification(layer) |
258 |
|
|
259 |
self.close_element("layer") |
self.close_element("layer") |
260 |
|
|
261 |
def write_classification(self, layer, attrs = None): |
def write_classification(self, layer, attrs = None): |
277 |
self.open_element("classification", attrs) |
self.open_element("classification", attrs) |
278 |
|
|
279 |
|
|
280 |
types = [[lambda p: 'clnull label="%s"' % p.GetLabel(), |
types = [[lambda p: 'clnull label="%s"' % self.encode(p.GetLabel()), |
281 |
lambda p: 'clnull'], |
lambda p: 'clnull'], |
282 |
[lambda p: 'clpoint label="%s" value="%s"' % |
[lambda p: 'clpoint label="%s" value="%s"' % |
283 |
(p.GetLabel(), str(p.GetValue())), |
(self.encode(p.GetLabel()), str(p.GetValue())), |
284 |
lambda p: 'clpoint'], |
lambda p: 'clpoint'], |
285 |
[lambda p: 'clrange label="%s" min="%s" max="%s"' % |
[lambda p: 'clrange label="%s" range="%s"' % |
286 |
(p.GetLabel(), |
(self.encode(p.GetLabel()), |
287 |
str(p.GetMin()), (str(p.GetMax()))), |
str(p.GetRange())), |
288 |
lambda p: 'clrange']] |
lambda p: 'clrange']] |
289 |
|
|
290 |
def write_class_group(group): |
def write_class_group(group): |
320 |
for label in labels: |
for label in labels: |
321 |
self.write_element(('label x="%g" y="%g" text="%s"' |
self.write_element(('label x="%g" y="%g" text="%s"' |
322 |
' halign="%s" valign="%s"') |
' halign="%s" valign="%s"') |
323 |
% (label.x, label.y, label.text, label.halign, |
% (label.x, label.y, |
324 |
|
self.encode(label.text), |
325 |
|
label.halign, |
326 |
label.valign)) |
label.valign)) |
327 |
self.close_element('labellayer') |
self.close_element('labellayer') |
328 |
|
|