17 |
|
|
18 |
import Thuban.Lib.fileutil |
import Thuban.Lib.fileutil |
19 |
|
|
|
from Thuban.Model.color import Color |
|
20 |
from Thuban.Model.layer import Layer, RasterLayer |
from Thuban.Model.layer import Layer, RasterLayer |
21 |
|
|
22 |
from Thuban.Model.classification import \ |
from Thuban.Model.classification import \ |
23 |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap |
24 |
|
from Thuban.Model.transientdb import AutoTransientTable, TransientJoinedTable |
25 |
|
from Thuban.Model.table import DBFTable, FIELDTYPE_STRING |
26 |
|
from Thuban.Model.data import DerivedShapeStore, ShapefileStore |
27 |
|
|
28 |
from Thuban.Model.xmlwriter import XMLWriter |
from Thuban.Model.xmlwriter import XMLWriter |
29 |
|
from postgisdb import PostGISConnection, PostGISShapeStore |
30 |
|
|
31 |
def relative_filename(dir, filename): |
def relative_filename(dir, filename): |
32 |
"""Return a filename relative to dir for the absolute file name absname. |
"""Return a filename relative to dir for the absolute file name absname. |
40 |
else: |
else: |
41 |
return filename |
return filename |
42 |
|
|
43 |
|
|
44 |
|
def unify_filename(filename): |
45 |
|
"""Return a 'unified' version of filename |
46 |
|
|
47 |
|
The .thuban files should be as platform independent as possible. |
48 |
|
Since they must contain filenames the filenames have to unified. We |
49 |
|
unify on unix-like filenames for now, which means we do nothing on a |
50 |
|
posix system and simply replace backslashes with slashes on windows |
51 |
|
""" |
52 |
|
if os.name == "posix": |
53 |
|
return filename |
54 |
|
elif os.name == "nt": |
55 |
|
return "/".join(filename.split("\\")) |
56 |
|
else: |
57 |
|
raise RuntimeError("Unsupported platform for unify_filename: %s" |
58 |
|
% os.name) |
59 |
|
|
60 |
|
def sort_data_stores(stores): |
61 |
|
"""Return a topologically sorted version of the sequence of data containers |
62 |
|
|
63 |
|
The list is sorted so that data containers that depend on other data |
64 |
|
containers have higher indexes than the containers they depend on. |
65 |
|
""" |
66 |
|
if not stores: |
67 |
|
return [] |
68 |
|
processed = {} |
69 |
|
result = [] |
70 |
|
todo = stores[:] |
71 |
|
while todo: |
72 |
|
# It doesn't really matter which if the items of todo is |
73 |
|
# processed next, but if we take the first one, the order is |
74 |
|
# preserved to some degree which makes writing some of the test |
75 |
|
# cases easier. |
76 |
|
container = todo.pop(0) |
77 |
|
if id(container) in processed: |
78 |
|
continue |
79 |
|
deps = [dep for dep in container.Dependencies() |
80 |
|
if id(dep) not in processed] |
81 |
|
if deps: |
82 |
|
todo.append(container) |
83 |
|
todo.extend(deps) |
84 |
|
else: |
85 |
|
result.append(container) |
86 |
|
processed[id(container)] = 1 |
87 |
|
return result |
88 |
|
|
89 |
|
|
90 |
class SessionSaver(XMLWriter): |
class SessionSaver(XMLWriter): |
91 |
|
|
92 |
"""Class to serialize a session into an XML file. |
"""Class to serialize a session into an XML file. |
103 |
def __init__(self, session): |
def __init__(self, session): |
104 |
XMLWriter.__init__(self) |
XMLWriter.__init__(self) |
105 |
self.session = session |
self.session = session |
106 |
|
# Map object ids to the ids used in the thuban files |
107 |
|
self.idmap = {} |
108 |
|
|
109 |
|
def get_id(self, obj): |
110 |
|
"""Return the id used in the thuban file for the object obj""" |
111 |
|
return self.idmap.get(id(obj)) |
112 |
|
|
113 |
|
def define_id(self, obj, value = None): |
114 |
|
if value is None: |
115 |
|
value = "D" + str(id(obj)) |
116 |
|
self.idmap[id(obj)] = value |
117 |
|
return value |
118 |
|
|
119 |
|
def has_id(self, obj): |
120 |
|
return self.idmap.has_key(id(obj)) |
121 |
|
|
122 |
|
def prepare_filename(self, filename): |
123 |
|
"""Return the string to use when writing filename to the thuban file |
124 |
|
|
125 |
|
The returned string is a unified version (only slashes as |
126 |
|
directory separators, see unify_filename) of filename expressed |
127 |
|
relative to the directory the .thuban file is written to. |
128 |
|
""" |
129 |
|
return unify_filename(relative_filename(self.dir, filename)) |
130 |
|
|
131 |
def write(self, file_or_filename): |
def write(self, file_or_filename): |
132 |
XMLWriter.write(self, file_or_filename) |
XMLWriter.write(self, file_or_filename) |
133 |
|
|
134 |
self.write_header("session", "thuban.dtd") |
self.write_header("session", "thuban-1.0.dtd") |
135 |
self.write_session(self.session) |
self.write_session(self.session) |
136 |
self.close() |
self.close() |
137 |
|
|
156 |
attrs["title"] = session.title |
attrs["title"] = session.title |
157 |
for name, uri in namespaces: |
for name, uri in namespaces: |
158 |
attrs["xmlns:" + name] = uri |
attrs["xmlns:" + name] = uri |
159 |
|
# default name space |
160 |
|
attrs["xmlns"] = \ |
161 |
|
"http://thuban.intevation.org/dtds/thuban-1.0.0.dtd" |
162 |
self.open_element("session", attrs) |
self.open_element("session", attrs) |
163 |
|
self.write_db_connections(session) |
164 |
|
self.write_data_containers(session) |
165 |
for map in session.Maps(): |
for map in session.Maps(): |
166 |
self.write_map(map) |
self.write_map(map) |
167 |
self.close_element("session") |
self.close_element("session") |
168 |
|
|
169 |
|
def write_db_connections(self, session): |
170 |
|
for conn in session.DBConnections(): |
171 |
|
if isinstance(conn, PostGISConnection): |
172 |
|
self.write_element("dbconnection", |
173 |
|
{"id": self.define_id(conn), |
174 |
|
"dbtype": "postgis", |
175 |
|
"host": conn.host, |
176 |
|
"port": conn.port, |
177 |
|
"user": conn.user, |
178 |
|
"dbname": conn.dbname}) |
179 |
|
else: |
180 |
|
raise ValueError("Can't handle db connection %r" % conn) |
181 |
|
|
182 |
|
def write_data_containers(self, session): |
183 |
|
containers = sort_data_stores(session.DataContainers()) |
184 |
|
for container in containers: |
185 |
|
if isinstance(container, AutoTransientTable): |
186 |
|
# AutoTransientTable instances are invisible in the |
187 |
|
# thuban files. They're only used internally. To make |
188 |
|
# sure that containers depending on AutoTransientTable |
189 |
|
# instances refer to the right real containers we give |
190 |
|
# the AutoTransientTable instances the same id as the |
191 |
|
# source they depend on. |
192 |
|
self.define_id(container, |
193 |
|
self.get_id(container.Dependencies()[0])) |
194 |
|
continue |
195 |
|
|
196 |
|
idvalue = self.define_id(container) |
197 |
|
if isinstance(container, ShapefileStore): |
198 |
|
self.define_id(container.Table(), idvalue) |
199 |
|
filename = self.prepare_filename(container.FileName()) |
200 |
|
self.write_element("fileshapesource", |
201 |
|
{"id": idvalue, "filename": filename, |
202 |
|
"filetype": "shapefile"}) |
203 |
|
elif isinstance(container, DerivedShapeStore): |
204 |
|
shapesource, table = container.Dependencies() |
205 |
|
self.write_element("derivedshapesource", |
206 |
|
{"id": idvalue, |
207 |
|
"shapesource": self.get_id(shapesource), |
208 |
|
"table": self.get_id(table)}) |
209 |
|
elif isinstance(container, PostGISShapeStore): |
210 |
|
conn = container.DBConnection() |
211 |
|
self.write_element("dbshapesource", |
212 |
|
{"id": idvalue, |
213 |
|
"dbconn": self.get_id(conn), |
214 |
|
"tablename": container.TableName()}) |
215 |
|
elif isinstance(container, DBFTable): |
216 |
|
filename = self.prepare_filename(container.FileName()) |
217 |
|
self.write_element("filetable", |
218 |
|
{"id": idvalue, |
219 |
|
"title": container.Title(), |
220 |
|
"filename": filename, |
221 |
|
"filetype": "DBF"}) |
222 |
|
elif isinstance(container, TransientJoinedTable): |
223 |
|
left, right = container.Dependencies() |
224 |
|
left_field = container.left_field |
225 |
|
right_field = container.right_field |
226 |
|
self.write_element("jointable", |
227 |
|
{"id": idvalue, |
228 |
|
"title": container.Title(), |
229 |
|
"right": self.get_id(right), |
230 |
|
"rightcolumn": right_field, |
231 |
|
"left": self.get_id(left), |
232 |
|
"leftcolumn": left_field, |
233 |
|
"jointype": container.JoinType()}) |
234 |
|
else: |
235 |
|
raise ValueError("Can't handle container %r" % container) |
236 |
|
|
237 |
|
|
238 |
def write_map(self, map): |
def write_map(self, map): |
239 |
"""Write the map and its contents. |
"""Write the map and its contents. |
240 |
|
|
254 |
"""Write the projection. |
"""Write the projection. |
255 |
""" |
""" |
256 |
if projection and len(projection.params) > 0: |
if projection and len(projection.params) > 0: |
257 |
self.open_element("projection", {"name": projection.GetName()}) |
attrs = {"name": projection.GetName()} |
258 |
|
epsg = projection.EPSGCode() |
259 |
|
if epsg is not None: |
260 |
|
attrs["epsg"] = epsg |
261 |
|
self.open_element("projection", attrs) |
262 |
for param in projection.params: |
for param in projection.params: |
263 |
self.write_element('parameter value="%s"' % |
self.write_element('parameter value="%s"' % |
264 |
self.encode(param)) |
self.encode(param)) |
279 |
attrs["visible"] = ("false", "true")[int(layer.Visible())] |
attrs["visible"] = ("false", "true")[int(layer.Visible())] |
280 |
|
|
281 |
if isinstance(layer, Layer): |
if isinstance(layer, Layer): |
282 |
attrs["filename"] = relative_filename(self.dir, |
attrs["shapestore"] = self.get_id(layer.ShapeStore()) |
|
layer.ShapeStore().FileName()) |
|
283 |
|
|
284 |
lc = layer.GetClassification() |
lc = layer.GetClassification() |
285 |
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
286 |
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
287 |
attrs["fill"] = lc.GetDefaultFill().hex() |
attrs["fill"] = lc.GetDefaultFill().hex() |
288 |
|
|
289 |
self.open_element("layer", attrs) |
self.open_element("layer", attrs) |
290 |
self.write_projection(layer.GetProjection()) |
self.write_projection(layer.GetProjection()) |
291 |
self.write_classification(layer) |
self.write_classification(layer) |
292 |
self.close_element("layer") |
self.close_element("layer") |
|
|
|
293 |
elif isinstance(layer, RasterLayer): |
elif isinstance(layer, RasterLayer): |
294 |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
attrs["filename"] = self.prepare_filename(layer.filename) |
295 |
self.open_element("rasterlayer", attrs) |
self.open_element("rasterlayer", attrs) |
296 |
self.write_projection(layer.GetProjection()) |
self.write_projection(layer.GetProjection()) |
297 |
self.close_element("rasterlayer") |
self.close_element("rasterlayer") |
304 |
|
|
305 |
lc = layer.GetClassification() |
lc = layer.GetClassification() |
306 |
|
|
307 |
field = lc.GetField() |
field = layer.GetClassificationColumn() |
308 |
|
|
309 |
# |
# |
310 |
# there isn't a classification of anything so do nothing |
# there isn't a classification of anything so do nothing |
311 |
# |
# |
312 |
if field is None: return |
if field is None: return |
313 |
|
|
314 |
attrs["field"] = field |
attrs["field"] = field |
315 |
attrs["field_type"] = str(lc.GetFieldType()) |
attrs["field_type"] = str(layer.GetFieldType(field)) |
316 |
self.open_element("classification", attrs) |
self.open_element("classification", attrs) |
317 |
|
|
318 |
for g in lc: |
for g in lc: |
319 |
if isinstance(g, ClassGroupDefault): |
if isinstance(g, ClassGroupDefault): |
320 |
open_el = 'clnull label="%s"' % self.encode(g.GetLabel()) |
open_el = 'clnull label="%s"' % self.encode(g.GetLabel()) |
321 |
close_el = 'clnull' |
close_el = 'clnull' |
322 |
elif isinstance(g, ClassGroupSingleton): |
elif isinstance(g, ClassGroupSingleton): |
323 |
|
if layer.GetFieldType(field) == FIELDTYPE_STRING: |
324 |
|
value = self.encode(g.GetValue()) |
325 |
|
else: |
326 |
|
value = str(g.GetValue()) |
327 |
open_el = 'clpoint label="%s" value="%s"' \ |
open_el = 'clpoint label="%s" value="%s"' \ |
328 |
% (self.encode(g.GetLabel()), str(g.GetValue())) |
% (self.encode(g.GetLabel()), value) |
329 |
close_el = 'clpoint' |
close_el = 'clpoint' |
330 |
elif isinstance(g, ClassGroupRange): |
elif isinstance(g, ClassGroupRange): |
331 |
open_el = 'clrange label="%s" range="%s"' \ |
open_el = 'clrange label="%s" range="%s"' \ |