22 |
|
|
23 |
from Thuban.Model.classification import \ |
from Thuban.Model.classification import \ |
24 |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap |
25 |
|
from Thuban.Model.transientdb import AutoTransientTable, TransientJoinedTable |
26 |
|
from Thuban.Model.table import DBFTable |
27 |
|
from Thuban.Model.data import DerivedShapeStore, ShapefileStore |
28 |
|
|
29 |
from Thuban.Model.xmlwriter import XMLWriter |
from Thuban.Model.xmlwriter import XMLWriter |
30 |
|
|
40 |
else: |
else: |
41 |
return filename |
return filename |
42 |
|
|
43 |
|
|
44 |
|
def sort_data_stores(stores): |
45 |
|
"""Return a topologically sorted version of the sequence of data containers |
46 |
|
|
47 |
|
The list is sorted so that data containers that depend on other data |
48 |
|
containers have higher indexes than the containers they depend on. |
49 |
|
""" |
50 |
|
if not stores: |
51 |
|
return [] |
52 |
|
processed = {} |
53 |
|
result = [] |
54 |
|
todo = stores[:] |
55 |
|
while todo: |
56 |
|
# It doesn't really matter which if the items of todo is |
57 |
|
# processed next, but if we take the first one, the order is |
58 |
|
# preserved to some degree which makes writing some of the test |
59 |
|
# cases easier. |
60 |
|
container = todo.pop(0) |
61 |
|
if id(container) in processed: |
62 |
|
continue |
63 |
|
deps = [dep for dep in container.Dependencies() |
64 |
|
if id(dep) not in processed] |
65 |
|
if deps: |
66 |
|
todo.append(container) |
67 |
|
todo.extend(deps) |
68 |
|
else: |
69 |
|
result.append(container) |
70 |
|
processed[id(container)] = 1 |
71 |
|
return result |
72 |
|
|
73 |
|
|
74 |
class SessionSaver(XMLWriter): |
class SessionSaver(XMLWriter): |
75 |
|
|
76 |
"""Class to serialize a session into an XML file. |
"""Class to serialize a session into an XML file. |
87 |
def __init__(self, session): |
def __init__(self, session): |
88 |
XMLWriter.__init__(self) |
XMLWriter.__init__(self) |
89 |
self.session = session |
self.session = session |
90 |
|
# Map object ids to the ids used in the thuban files |
91 |
|
self.idmap = {} |
92 |
|
|
93 |
|
def get_id(self, obj): |
94 |
|
"""Return the id used in the thuban file for the object obj""" |
95 |
|
return self.idmap.get(id(obj)) |
96 |
|
|
97 |
|
def define_id(self, obj, value = None): |
98 |
|
if value is None: |
99 |
|
value = "D" + str(id(obj)) |
100 |
|
self.idmap[id(obj)] = value |
101 |
|
return value |
102 |
|
|
103 |
|
def has_id(self, obj): |
104 |
|
return self.idmap.has_key(id(obj)) |
105 |
|
|
106 |
def write(self, file_or_filename): |
def write(self, file_or_filename): |
107 |
XMLWriter.write(self, file_or_filename) |
XMLWriter.write(self, file_or_filename) |
108 |
|
|
109 |
self.write_header("session", "thuban.dtd") |
self.write_header("session", "thuban-0.8.dtd") |
110 |
self.write_session(self.session) |
self.write_session(self.session) |
111 |
self.close() |
self.close() |
112 |
|
|
131 |
attrs["title"] = session.title |
attrs["title"] = session.title |
132 |
for name, uri in namespaces: |
for name, uri in namespaces: |
133 |
attrs["xmlns:" + name] = uri |
attrs["xmlns:" + name] = uri |
134 |
|
# default name space |
135 |
|
attrs["xmlns"] = \ |
136 |
|
"http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
137 |
self.open_element("session", attrs) |
self.open_element("session", attrs) |
138 |
|
self.write_data_containers(session) |
139 |
for map in session.Maps(): |
for map in session.Maps(): |
140 |
self.write_map(map) |
self.write_map(map) |
141 |
self.close_element("session") |
self.close_element("session") |
142 |
|
|
143 |
|
def write_data_containers(self, session): |
144 |
|
containers = sort_data_stores(session.DataContainers()) |
145 |
|
for container in containers: |
146 |
|
if isinstance(container, AutoTransientTable): |
147 |
|
# AutoTransientTable instances are invisible in the |
148 |
|
# thuban files. They're only used internally. To make |
149 |
|
# sure that containers depending on AutoTransientTable |
150 |
|
# instances refer to the right real containers we give |
151 |
|
# the AutoTransientTable instances the same id as the |
152 |
|
# source they depend on. |
153 |
|
self.define_id(container, |
154 |
|
self.get_id(container.Dependencies()[0])) |
155 |
|
continue |
156 |
|
|
157 |
|
idvalue = self.define_id(container) |
158 |
|
if isinstance(container, ShapefileStore): |
159 |
|
self.define_id(container.Table(), idvalue) |
160 |
|
filename = relative_filename(self.dir, container.FileName()) |
161 |
|
self.write_element("fileshapesource", |
162 |
|
{"id": idvalue, "filename": filename, |
163 |
|
"filetype": "shapefile"}) |
164 |
|
elif isinstance(container, DerivedShapeStore): |
165 |
|
shapesource, table = container.Dependencies() |
166 |
|
self.write_element("derivedshapesource", |
167 |
|
{"id": idvalue, |
168 |
|
"shapesource": self.get_id(shapesource), |
169 |
|
"table": self.get_id(table)}) |
170 |
|
elif isinstance(container, DBFTable): |
171 |
|
filename = relative_filename(self.dir, container.FileName()) |
172 |
|
self.write_element("filetable", |
173 |
|
{"id": idvalue, |
174 |
|
"title": container.Title(), |
175 |
|
"filename": filename, |
176 |
|
"filetype": "DBF"}) |
177 |
|
elif isinstance(container, TransientJoinedTable): |
178 |
|
left, right = container.Dependencies() |
179 |
|
left_field = container.left_field |
180 |
|
right_field = container.right_field |
181 |
|
self.write_element("jointable", |
182 |
|
{"id": idvalue, |
183 |
|
"title": container.Title(), |
184 |
|
"right": self.get_id(right), |
185 |
|
"rightcolumn": right_field, |
186 |
|
"left": self.get_id(left), |
187 |
|
"leftcolumn": left_field}) |
188 |
|
else: |
189 |
|
raise ValueError("Can't handle container %r" % container) |
190 |
|
|
191 |
|
|
192 |
def write_map(self, map): |
def write_map(self, map): |
193 |
"""Write the map and its contents. |
"""Write the map and its contents. |
194 |
|
|
229 |
attrs["visible"] = ("false", "true")[int(layer.Visible())] |
attrs["visible"] = ("false", "true")[int(layer.Visible())] |
230 |
|
|
231 |
if isinstance(layer, Layer): |
if isinstance(layer, Layer): |
232 |
attrs["filename"] = relative_filename(self.dir, |
attrs["shapestore"] = self.get_id(layer.ShapeStore()) |
|
layer.ShapeStore().FileName()) |
|
233 |
|
|
234 |
lc = layer.GetClassification() |
lc = layer.GetClassification() |
235 |
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
236 |
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
237 |
attrs["fill"] = lc.GetDefaultFill().hex() |
attrs["fill"] = lc.GetDefaultFill().hex() |
238 |
|
|
239 |
self.open_element("layer", attrs) |
self.open_element("layer", attrs) |
240 |
self.write_projection(layer.GetProjection()) |
self.write_projection(layer.GetProjection()) |
241 |
self.write_classification(layer) |
self.write_classification(layer) |
242 |
self.close_element("layer") |
self.close_element("layer") |
|
|
|
243 |
elif isinstance(layer, RasterLayer): |
elif isinstance(layer, RasterLayer): |
244 |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
245 |
self.open_element("rasterlayer", attrs) |
self.open_element("rasterlayer", attrs) |