13 |
|
|
14 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
15 |
|
|
|
# fix for people using python2.1 |
|
|
from __future__ import nested_scopes |
|
|
|
|
16 |
import os |
import os |
|
import string |
|
17 |
|
|
18 |
import Thuban.Lib.fileutil |
import Thuban.Lib.fileutil |
19 |
|
|
20 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
21 |
|
from Thuban.Model.layer import Layer, RasterLayer |
22 |
|
|
23 |
from Thuban.Model.classification import \ |
from Thuban.Model.classification import \ |
24 |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap |
25 |
|
|
26 |
# |
from Thuban.Model.xmlwriter import XMLWriter |
|
# one level of indention |
|
|
# |
|
|
TAB = " " |
|
27 |
|
|
28 |
def relative_filename(dir, filename): |
def relative_filename(dir, filename): |
29 |
"""Return a filename relative to dir for the absolute file name absname. |
"""Return a filename relative to dir for the absolute file name absname. |
37 |
else: |
else: |
38 |
return filename |
return filename |
39 |
|
|
|
def escape(data): |
|
|
"""Escape &, \", ', <, and > in a string of data. |
|
|
""" |
|
|
data = string.replace(data, "&", "&") |
|
|
data = string.replace(data, "<", "<") |
|
|
data = string.replace(data, ">", ">") |
|
|
data = string.replace(data, '"', """) |
|
|
data = string.replace(data, "'", "'") |
|
|
return data |
|
|
|
|
|
class XMLWriter: |
|
|
"""Abstract XMLWriter. |
|
|
|
|
|
Should be overridden to provide specific object saving functionality. |
|
|
""" |
|
|
|
|
|
def __init__(self): |
|
|
self.filename = None |
|
|
pass |
|
|
|
|
|
def write(self, file_or_filename): |
|
|
"""Write the session to a file. |
|
|
|
|
|
The argument may be either a file object or a filename. If it's |
|
|
a filename, the file will be opened for writing. Files of |
|
|
shapefiles will be stored as filenames relative to the directory |
|
|
the file is stored in (as given by os.path.dirname(filename)) if |
|
|
they have a common parent directory other than the root |
|
|
directory. |
|
|
|
|
|
If the argument is a file object (which is determined by the |
|
|
presence of a write method) all filenames will be absolute |
|
|
filenames. |
|
|
""" |
|
|
|
|
|
# keep track of how many levels of indentation to write |
|
|
self.indent_level = 0 |
|
|
# track whether an element is currently open. see open_element(). |
|
|
self.element_open = 0 |
|
|
|
|
|
if hasattr(file_or_filename, "write"): |
|
|
# it's a file object |
|
|
self.file = file_or_filename |
|
|
self.dir = "" |
|
|
else: |
|
|
self.filename = file_or_filename |
|
|
self.dir = os.path.dirname(self.filename) |
|
|
self.file = open(self.filename, 'w') |
|
|
|
|
|
def close(self): |
|
|
assert self.indent_level == 0 |
|
|
if self.filename is not None: |
|
|
self.file.close() |
|
|
|
|
|
def write_header(self, doctype, system): |
|
|
"""Write the XML header""" |
|
|
self.file.write('<?xml version="1.0" encoding="UTF-8"?>\n') |
|
|
self.file.write('<!DOCTYPE %s SYSTEM "%s">\n' % (doctype, system)) |
|
|
|
|
|
def open_element(self, element, attrs = {}): |
|
|
|
|
|
# |
|
|
# we note when an element is opened so that if two open_element() |
|
|
# calls are made successively we can end the currently open |
|
|
# tag and will later write a proper close tag. otherwise, |
|
|
# if a close_element() call is made directly after an open_element() |
|
|
# call we will close the tag with a /> |
|
|
# |
|
|
if self.element_open == 1: |
|
|
self.file.write(">\n") |
|
|
|
|
|
self.element_open = 1 |
|
|
|
|
|
# Helper function to write an element open tag with attributes |
|
|
self.file.write("%s<%s" % (TAB*self.indent_level, element)) |
|
|
self.__write_attribs(attrs) |
|
|
|
|
|
self.indent_level += 1 |
|
|
|
|
|
def close_element(self, element): |
|
|
self.indent_level -= 1 |
|
|
assert self.indent_level >= 0 |
|
|
|
|
|
# see open_element() for an explanation |
|
|
if self.element_open == 1: |
|
|
self.element_open = 0 |
|
|
self.file.write("/>\n") |
|
|
else: |
|
|
self.file.write("%s</%s>\n" % (TAB*self.indent_level, element)) |
|
|
|
|
|
def write_element(self, element, attrs = {}): |
|
|
"""write an element that won't need a closing tag""" |
|
|
self.open_element(element, attrs) |
|
|
self.close_element(element) |
|
|
|
|
|
def __write_attribs(self, attrs): |
|
|
for name, value in attrs.items(): |
|
|
self.file.write(' %s="%s"' % (self.encode(name), |
|
|
self.encode(value))) |
|
|
|
|
|
def encode(self, str): |
|
|
"""Assume that str is in Latin1, escape it, and encode it in UTF-8. |
|
|
|
|
|
If str is None, return None |
|
|
""" |
|
|
|
|
|
if str is not None: |
|
|
return unicode(escape(str),'latin1').encode("utf8") |
|
|
#return escape(str).encode("utf8") |
|
|
else: |
|
|
return None |
|
|
|
|
40 |
class SessionSaver(XMLWriter): |
class SessionSaver(XMLWriter): |
41 |
|
|
42 |
"""Class to serialize a session into an XML file. |
"""Class to serialize a session into an XML file. |
119 |
given, should be a mapping from attribute names to attribute |
given, should be a mapping from attribute names to attribute |
120 |
values. The values should not be XML-escaped yet. |
values. The values should not be XML-escaped yet. |
121 |
""" |
""" |
|
lc = layer.GetClassification() |
|
122 |
|
|
123 |
if attrs is None: |
if attrs is None: |
124 |
attrs = {} |
attrs = {} |
125 |
|
|
126 |
attrs["title"] = layer.title |
attrs["title"] = layer.title |
127 |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
attrs["visible"] = ("false", "true")[int(layer.Visible())] |
|
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
|
|
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
|
|
attrs["fill"] = lc.GetDefaultFill().hex() |
|
|
attrs["visible"] = ("false", "true")[int(layer.Visible())] |
|
|
|
|
|
self.open_element("layer", attrs) |
|
|
|
|
|
proj = layer.GetProjection() |
|
|
if proj is not None: |
|
|
self.write_projection(proj) |
|
|
|
|
|
self.write_classification(layer) |
|
128 |
|
|
129 |
self.close_element("layer") |
if isinstance(layer, Layer): |
130 |
|
attrs["filename"] = relative_filename(self.dir, |
131 |
|
layer.ShapeStore().FileName()) |
132 |
|
|
133 |
|
lc = layer.GetClassification() |
134 |
|
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
135 |
|
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
136 |
|
attrs["fill"] = lc.GetDefaultFill().hex() |
137 |
|
|
138 |
|
self.open_element("layer", attrs) |
139 |
|
self.write_projection(layer.GetProjection()) |
140 |
|
self.write_classification(layer) |
141 |
|
self.close_element("layer") |
142 |
|
|
143 |
|
elif isinstance(layer, RasterLayer): |
144 |
|
attrs["filename"] = relative_filename(self.dir, layer.filename) |
145 |
|
self.open_element("rasterlayer", attrs) |
146 |
|
self.write_projection(layer.GetProjection()) |
147 |
|
self.close_element("rasterlayer") |
148 |
|
|
149 |
def write_classification(self, layer, attrs = None): |
def write_classification(self, layer, attrs = None): |
150 |
|
"""Write Classification information.""" |
151 |
|
|
152 |
if attrs is None: |
if attrs is None: |
153 |
attrs = {} |
attrs = {} |
154 |
|
|
157 |
field = lc.GetField() |
field = lc.GetField() |
158 |
|
|
159 |
# |
# |
160 |
# there isn't a classification of anything |
# there isn't a classification of anything so do nothing |
|
# so don't do anything |
|
161 |
# |
# |
162 |
if field is None: return |
if field is None: return |
163 |
|
|
165 |
attrs["field_type"] = str(lc.GetFieldType()) |
attrs["field_type"] = str(lc.GetFieldType()) |
166 |
self.open_element("classification", attrs) |
self.open_element("classification", attrs) |
167 |
|
|
168 |
|
for g in lc: |
169 |
types = [[lambda p: 'clnull label="%s"' % self.encode(p.GetLabel()), |
if isinstance(g, ClassGroupDefault): |
170 |
lambda p: 'clnull'], |
open_el = 'clnull label="%s"' % self.encode(g.GetLabel()) |
171 |
[lambda p: 'clpoint label="%s" value="%s"' % |
close_el = 'clnull' |
172 |
(self.encode(p.GetLabel()), str(p.GetValue())), |
elif isinstance(g, ClassGroupSingleton): |
173 |
lambda p: 'clpoint'], |
open_el = 'clpoint label="%s" value="%s"' \ |
174 |
[lambda p: 'clrange label="%s" range="%s"' % |
% (self.encode(g.GetLabel()), str(g.GetValue())) |
175 |
(self.encode(p.GetLabel()), |
close_el = 'clpoint' |
176 |
str(p.GetRange())), |
elif isinstance(g, ClassGroupRange): |
177 |
lambda p: 'clrange']] |
open_el = 'clrange label="%s" range="%s"' \ |
178 |
|
% (self.encode(g.GetLabel()), str(g.GetRange())) |
179 |
def write_class_group(group): |
close_el = 'clrange' |
180 |
type = -1 |
else: |
181 |
if isinstance(group, ClassGroupDefault): type = 0 |
assert False, _("Unsupported group type in classification") |
182 |
elif isinstance(group, ClassGroupSingleton): type = 1 |
continue |
183 |
elif isinstance(group, ClassGroupRange): type = 2 |
|
184 |
elif isinstance(group, ClassGroupMap): type = 3 |
data = g.GetProperties() |
185 |
assert type >= 0 |
dict = {'stroke' : data.GetLineColor().hex(), |
186 |
|
'stroke_width': str(data.GetLineWidth()), |
187 |
if type <= 2: |
'fill' : data.GetFill().hex()} |
188 |
data = group.GetProperties() |
|
189 |
dict = {'stroke' : data.GetLineColor().hex(), |
self.open_element(open_el) |
190 |
'stroke_width': str(data.GetLineWidth()), |
self.write_element("cldata", dict) |
191 |
'fill' : data.GetFill().hex()} |
self.close_element(close_el) |
|
|
|
|
self.open_element(types[type][0](group)) |
|
|
self.write_element("cldata", dict) |
|
|
self.close_element(types[type][1](group)) |
|
|
else: pass # XXX: we need to handle maps |
|
|
|
|
|
for i in lc: |
|
|
write_class_group(i) |
|
192 |
|
|
193 |
self.close_element("classification") |
self.close_element("classification") |
194 |
|
|