1 |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
# Copyright (c) 2001-2005 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> (2004-2005) |
4 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> (2001-2004) |
5 |
# Jonathan Coles <[email protected]> |
# Jonathan Coles <[email protected]> (2003) |
6 |
|
# Frank Koormann <[email protected]> (2003) |
7 |
# |
# |
8 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
9 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
13 |
""" |
""" |
14 |
|
|
15 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
16 |
|
# $Source$ |
17 |
# fix for people using python2.1 |
# $Id$ |
|
from __future__ import nested_scopes |
|
18 |
|
|
19 |
import os |
import os |
|
import string |
|
20 |
|
|
21 |
import Thuban.Lib.fileutil |
import Thuban.Lib.fileutil |
22 |
|
|
23 |
from Thuban.Model.color import Color |
from Thuban.Model.layer import Layer, RasterLayer |
24 |
|
|
25 |
from Thuban.Model.classification import * |
from Thuban.Model.classification import \ |
26 |
|
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, \ |
27 |
|
ClassGroupPattern, ClassGroupMap |
28 |
|
from Thuban.Model.transientdb import AutoTransientTable, TransientJoinedTable |
29 |
|
from Thuban.Model.table import DBFTable, FIELDTYPE_STRING |
30 |
|
from Thuban.Model.data import DerivedShapeStore, FileShapeStore, \ |
31 |
|
SHAPETYPE_POINT |
32 |
|
|
33 |
# |
from Thuban.Model.xmlwriter import XMLWriter |
34 |
# one level of indention |
from postgisdb import PostGISConnection, PostGISShapeStore |
|
# |
|
|
TAB = " " |
|
35 |
|
|
36 |
def relative_filename(dir, filename): |
def relative_filename(dir, filename): |
37 |
"""Return a filename relative to dir for the absolute file name absname. |
"""Return a filename relative to dir for the absolute file name absname. |
45 |
else: |
else: |
46 |
return filename |
return filename |
47 |
|
|
|
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 |
|
48 |
|
|
49 |
class XMLWriter: |
def unify_filename(filename): |
50 |
"""Abstract XMLWriter. |
"""Return a 'unified' version of filename |
51 |
|
|
52 |
Should be overridden to provide specific object saving functionality. |
The .thuban files should be as platform independent as possible. |
53 |
|
Since they must contain filenames the filenames have to unified. We |
54 |
|
unify on unix-like filenames for now, which means we do nothing on a |
55 |
|
posix system and simply replace backslashes with slashes on windows |
56 |
""" |
""" |
57 |
|
if os.name == "posix": |
58 |
|
return filename |
59 |
|
elif os.name == "nt": |
60 |
|
return "/".join(filename.split("\\")) |
61 |
|
else: |
62 |
|
raise RuntimeError("Unsupported platform for unify_filename: %s" |
63 |
|
% os.name) |
64 |
|
|
65 |
def __init__(self): |
def sort_data_stores(stores): |
66 |
self.filename = None |
"""Return a topologically sorted version of the sequence of data containers |
|
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. |
|
|
""" |
|
67 |
|
|
68 |
# keep track of how many levels of indentation to write |
The list is sorted so that data containers that depend on other data |
69 |
self.indent_level = 0 |
containers have higher indexes than the containers they depend on. |
70 |
# track whether an element is currently open. see open_element(). |
""" |
71 |
self.element_open = 0 |
if not stores: |
72 |
|
return [] |
73 |
if hasattr(file_or_filename, "write"): |
processed = {} |
74 |
# it's a file object |
result = [] |
75 |
self.file = file_or_filename |
todo = stores[:] |
76 |
self.dir = "" |
while todo: |
77 |
else: |
# It doesn't really matter which if the items of todo is |
78 |
self.filename = file_or_filename |
# processed next, but if we take the first one, the order is |
79 |
self.dir = os.path.dirname(self.filename) |
# preserved to some degree which makes writing some of the test |
80 |
self.file = open(self.filename, 'w') |
# cases easier. |
81 |
|
container = todo.pop(0) |
82 |
def close(self): |
if id(container) in processed: |
83 |
assert self.indent_level == 0 |
continue |
84 |
if self.filename is not None: |
deps = [dep for dep in container.Dependencies() |
85 |
self.file.close() |
if id(dep) not in processed] |
86 |
|
if deps: |
87 |
def write_header(self, doctype, system): |
todo.append(container) |
88 |
"""Write the XML header""" |
todo.extend(deps) |
|
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") |
|
89 |
else: |
else: |
90 |
self.file.write("%s</%s>\n" % (TAB*self.indent_level, element)) |
result.append(container) |
91 |
|
processed[id(container)] = 1 |
92 |
|
return result |
93 |
|
|
94 |
|
def bool2str(b): |
95 |
|
if b: return "true" |
96 |
|
else: return "false" |
97 |
|
|
|
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"' % (escape(name), escape(value))) |
|
|
|
|
98 |
class SessionSaver(XMLWriter): |
class SessionSaver(XMLWriter): |
99 |
|
|
100 |
"""Class to serialize a session into an XML file. |
"""Class to serialize a session into an XML file. |
111 |
def __init__(self, session): |
def __init__(self, session): |
112 |
XMLWriter.__init__(self) |
XMLWriter.__init__(self) |
113 |
self.session = session |
self.session = session |
114 |
|
# Map object ids to the ids used in the thuban files |
115 |
|
self.idmap = {} |
116 |
|
|
117 |
|
def get_id(self, obj): |
118 |
|
"""Return the id used in the thuban file for the object obj""" |
119 |
|
return self.idmap.get(id(obj)) |
120 |
|
|
121 |
|
def define_id(self, obj, value = None): |
122 |
|
if value is None: |
123 |
|
value = "D" + str(id(obj)) |
124 |
|
self.idmap[id(obj)] = value |
125 |
|
return value |
126 |
|
|
127 |
|
def has_id(self, obj): |
128 |
|
return self.idmap.has_key(id(obj)) |
129 |
|
|
130 |
|
def prepare_filename(self, filename): |
131 |
|
"""Return the string to use when writing filename to the thuban file |
132 |
|
|
133 |
|
The returned string is a unified version (only slashes as |
134 |
|
directory separators, see unify_filename) of filename expressed |
135 |
|
relative to the directory the .thuban file is written to. |
136 |
|
""" |
137 |
|
return unify_filename(relative_filename(self.dir, filename)) |
138 |
|
|
139 |
def write(self, file_or_filename): |
def write(self, file_or_filename): |
140 |
XMLWriter.write(self, file_or_filename) |
XMLWriter.write(self, file_or_filename) |
141 |
|
|
142 |
self.write_header("session", "thuban.dtd") |
self.write_header("session", "thuban-1.1.dtd") |
143 |
self.write_session(self.session) |
self.write_session(self.session) |
144 |
self.close() |
self.close() |
145 |
|
|
164 |
attrs["title"] = session.title |
attrs["title"] = session.title |
165 |
for name, uri in namespaces: |
for name, uri in namespaces: |
166 |
attrs["xmlns:" + name] = uri |
attrs["xmlns:" + name] = uri |
167 |
|
# default name space |
168 |
|
attrs["xmlns"] = \ |
169 |
|
"http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
170 |
self.open_element("session", attrs) |
self.open_element("session", attrs) |
171 |
|
self.write_db_connections(session) |
172 |
|
self.write_data_containers(session) |
173 |
for map in session.Maps(): |
for map in session.Maps(): |
174 |
self.write_map(map) |
self.write_map(map) |
175 |
self.close_element("session") |
self.close_element("session") |
176 |
|
|
177 |
|
def write_db_connections(self, session): |
178 |
|
for conn in session.DBConnections(): |
179 |
|
if isinstance(conn, PostGISConnection): |
180 |
|
self.write_element("dbconnection", |
181 |
|
{"id": self.define_id(conn), |
182 |
|
"dbtype": "postgis", |
183 |
|
"host": conn.host, |
184 |
|
"port": conn.port, |
185 |
|
"user": conn.user, |
186 |
|
"dbname": conn.dbname}) |
187 |
|
else: |
188 |
|
raise ValueError("Can't handle db connection %r" % conn) |
189 |
|
|
190 |
|
def write_data_containers(self, session): |
191 |
|
containers = sort_data_stores(session.DataContainers()) |
192 |
|
for container in containers: |
193 |
|
if isinstance(container, AutoTransientTable): |
194 |
|
# AutoTransientTable instances are invisible in the |
195 |
|
# thuban files. They're only used internally. To make |
196 |
|
# sure that containers depending on AutoTransientTable |
197 |
|
# instances refer to the right real containers we give |
198 |
|
# the AutoTransientTable instances the same id as the |
199 |
|
# source they depend on. |
200 |
|
self.define_id(container, |
201 |
|
self.get_id(container.Dependencies()[0])) |
202 |
|
continue |
203 |
|
|
204 |
|
idvalue = self.define_id(container) |
205 |
|
if isinstance(container, FileShapeStore): |
206 |
|
self.define_id(container.Table(), idvalue) |
207 |
|
filename = self.prepare_filename(container.FileName()) |
208 |
|
self.write_element("fileshapesource", |
209 |
|
{"id": idvalue, "filename": filename, |
210 |
|
"filetype": container.FileType()}) |
211 |
|
elif isinstance(container, DerivedShapeStore): |
212 |
|
shapesource, table = container.Dependencies() |
213 |
|
self.write_element("derivedshapesource", |
214 |
|
{"id": idvalue, |
215 |
|
"shapesource": self.get_id(shapesource), |
216 |
|
"table": self.get_id(table)}) |
217 |
|
elif isinstance(container, PostGISShapeStore): |
218 |
|
conn = container.DBConnection() |
219 |
|
self.write_element("dbshapesource", |
220 |
|
{"id": idvalue, |
221 |
|
"dbconn": self.get_id(conn), |
222 |
|
"tablename": container.TableName(), |
223 |
|
"id_column": container.IDColumn().name, |
224 |
|
"geometry_column": |
225 |
|
container.GeometryColumn().name, |
226 |
|
}) |
227 |
|
elif isinstance(container, DBFTable): |
228 |
|
filename = self.prepare_filename(container.FileName()) |
229 |
|
self.write_element("filetable", |
230 |
|
{"id": idvalue, |
231 |
|
"title": container.Title(), |
232 |
|
"filename": filename, |
233 |
|
"filetype": "DBF"}) |
234 |
|
elif isinstance(container, TransientJoinedTable): |
235 |
|
left, right = container.Dependencies() |
236 |
|
left_field = container.left_field |
237 |
|
right_field = container.right_field |
238 |
|
self.write_element("jointable", |
239 |
|
{"id": idvalue, |
240 |
|
"title": container.Title(), |
241 |
|
"right": self.get_id(right), |
242 |
|
"rightcolumn": right_field, |
243 |
|
"left": self.get_id(left), |
244 |
|
"leftcolumn": left_field, |
245 |
|
"jointype": container.JoinType()}) |
246 |
|
else: |
247 |
|
raise ValueError("Can't handle container %r" % container) |
248 |
|
|
249 |
|
|
250 |
def write_map(self, map): |
def write_map(self, map): |
251 |
"""Write the map and its contents. |
"""Write the map and its contents. |
252 |
|
|
255 |
element, call write_layer for each layer contained in the map |
element, call write_layer for each layer contained in the map |
256 |
and finally call write_label_layer to write the label layer. |
and finally call write_label_layer to write the label layer. |
257 |
""" |
""" |
258 |
self.open_element('map title="%s"' % escape(map.title)) |
self.open_element('map title="%s"' % self.encode(map.title)) |
259 |
self.write_projection(map.projection) |
self.write_projection(map.projection) |
260 |
for layer in map.Layers(): |
for layer in map.Layers(): |
261 |
self.write_layer(layer) |
self.write_layer(layer) |
266 |
"""Write the projection. |
"""Write the projection. |
267 |
""" |
""" |
268 |
if projection and len(projection.params) > 0: |
if projection and len(projection.params) > 0: |
269 |
self.open_element("projection") |
attrs = {"name": projection.GetName()} |
270 |
|
epsg = projection.EPSGCode() |
271 |
|
if epsg is not None: |
272 |
|
attrs["epsg"] = epsg |
273 |
|
self.open_element("projection", attrs) |
274 |
for param in projection.params: |
for param in projection.params: |
275 |
self.write_element('parameter value="%s"' % escape(param)) |
self.write_element('parameter value="%s"' % |
276 |
|
self.encode(param)) |
277 |
self.close_element("projection") |
self.close_element("projection") |
278 |
|
|
279 |
def write_layer(self, layer, attrs = None): |
def write_layer(self, layer, attrs = None): |
283 |
given, should be a mapping from attribute names to attribute |
given, should be a mapping from attribute names to attribute |
284 |
values. The values should not be XML-escaped yet. |
values. The values should not be XML-escaped yet. |
285 |
""" |
""" |
|
lc = layer.GetClassification() |
|
286 |
|
|
287 |
if attrs is None: |
if attrs is None: |
288 |
attrs = {} |
attrs = {} |
289 |
|
|
290 |
attrs["title"] = layer.title |
attrs["title"] = layer.title |
291 |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
attrs["visible"] = bool2str(layer.Visible()) |
292 |
attrs["stroke"] = lc.GetDefaultLineColor().hex() |
|
293 |
attrs["stroke_width"] = str(lc.GetDefaultLineWidth()) |
if isinstance(layer, Layer): |
294 |
attrs["fill"] = lc.GetDefaultFill().hex() |
attrs["shapestore"] = self.get_id(layer.ShapeStore()) |
295 |
|
self.open_element("layer", attrs) |
296 |
self.open_element("layer", attrs) |
self.write_projection(layer.GetProjection()) |
297 |
self.write_classification(layer) |
self.write_classification(layer) |
298 |
self.close_element("layer") |
self.close_element("layer") |
299 |
|
elif isinstance(layer, RasterLayer): |
300 |
|
attrs["filename"] = self.prepare_filename(layer.filename) |
301 |
|
|
302 |
|
masknames = ["none", "bit", "alpha"] |
303 |
|
|
304 |
|
if layer.MaskType() != layer.MASK_BIT: |
305 |
|
attrs["masktype"] = masknames[layer.MaskType()] |
306 |
|
|
307 |
|
if layer.Opacity() != 1: |
308 |
|
attrs["opacity"] = str(layer.Opacity()) |
309 |
|
|
310 |
|
self.open_element("rasterlayer", attrs) |
311 |
|
self.write_projection(layer.GetProjection()) |
312 |
|
self.close_element("rasterlayer") |
313 |
|
|
314 |
def write_classification(self, layer, attrs = None): |
def write_classification(self, layer, attrs = None): |
315 |
|
"""Write Classification information.""" |
316 |
|
|
317 |
if attrs is None: |
if attrs is None: |
318 |
attrs = {} |
attrs = {} |
319 |
|
|
320 |
lc = layer.GetClassification() |
lc = layer.GetClassification() |
321 |
|
|
322 |
field = lc.GetField() |
field = layer.GetClassificationColumn() |
|
|
|
|
# |
|
|
# there isn't a classification of anything |
|
|
# so don't do anything |
|
|
# |
|
|
if field is None: return |
|
323 |
|
|
324 |
attrs["field"] = field |
if field is not None: |
325 |
attrs["field_type"] = str(lc.GetFieldType()) |
attrs["field"] = field |
326 |
self.open_element("classification", attrs) |
attrs["field_type"] = str(layer.GetFieldType(field)) |
327 |
|
|
328 |
|
self.open_element("classification", attrs) |
329 |
|
|
330 |
types = [[lambda p: 'clnull label="%s"' % p.GetLabel(), |
for g in lc: |
331 |
lambda p: 'clnull'], |
if isinstance(g, ClassGroupDefault): |
332 |
[lambda p: 'clpoint label="%s" value="%s"' % |
open_el = 'clnull label="%s"' % self.encode(g.GetLabel()) |
333 |
(p.GetLabel(), str(p.GetValue())), |
close_el = 'clnull' |
334 |
lambda p: 'clpoint'], |
elif isinstance(g, ClassGroupSingleton): |
335 |
[lambda p: 'clrange label="%s" min="%s" max="%s"' % |
if layer.GetFieldType(field) == FIELDTYPE_STRING: |
336 |
(p.GetLabel(), |
value = self.encode(g.GetValue()) |
337 |
str(p.GetMin()), (str(p.GetMax()))), |
else: |
338 |
lambda p: 'clrange']] |
value = str(g.GetValue()) |
339 |
|
open_el = 'clpoint label="%s" value="%s"' \ |
340 |
def write_class_group(group): |
% (self.encode(g.GetLabel()), value) |
341 |
type = -1 |
close_el = 'clpoint' |
342 |
if isinstance(group, ClassGroupDefault): type = 0 |
elif isinstance(g, ClassGroupRange): |
343 |
elif isinstance(group, ClassGroupSingleton): type = 1 |
open_el = 'clrange label="%s" range="%s"' \ |
344 |
elif isinstance(group, ClassGroupRange): type = 2 |
% (self.encode(g.GetLabel()), str(g.GetRange())) |
345 |
elif isinstance(group, ClassGroupMap): type = 3 |
close_el = 'clrange' |
346 |
assert type >= 0 |
elif isinstance(g, ClassGroupPattern): |
347 |
|
open_el = 'clpattern label="%s" pattern="%s"' \ |
348 |
if type <= 2: |
% (self.encode(g.GetLabel()), str(g.GetPattern())) |
349 |
data = group.GetProperties() |
close_el = 'clpattern' |
350 |
dict = {'stroke' : data.GetLineColor().hex(), |
|
351 |
'stroke_width': str(data.GetLineWidth()), |
else: |
352 |
'fill' : data.GetFill().hex()} |
assert False, _("Unsupported group type in classification") |
353 |
|
continue |
354 |
self.open_element(types[type][0](group)) |
|
355 |
self.write_element("cldata", dict) |
data = g.GetProperties() |
356 |
self.close_element(types[type][1](group)) |
dict = {'stroke' : data.GetLineColor().hex(), |
357 |
else: pass # XXX: we need to handle maps |
'stroke_width': str(data.GetLineWidth()), |
358 |
|
'fill' : data.GetFill().hex()} |
359 |
for i in lc: |
|
360 |
write_class_group(i) |
# only for point layers write the size attribute |
361 |
|
if layer.ShapeType() == SHAPETYPE_POINT: |
362 |
|
dict['size'] = str(data.GetSize()) |
363 |
|
|
364 |
|
self.open_element(open_el) |
365 |
|
self.write_element("cldata", dict) |
366 |
|
self.close_element(close_el) |
367 |
|
|
368 |
self.close_element("classification") |
self.close_element("classification") |
369 |
|
|
376 |
for label in labels: |
for label in labels: |
377 |
self.write_element(('label x="%g" y="%g" text="%s"' |
self.write_element(('label x="%g" y="%g" text="%s"' |
378 |
' halign="%s" valign="%s"') |
' halign="%s" valign="%s"') |
379 |
% (label.x, label.y, label.text, label.halign, |
% (label.x, label.y, |
380 |
|
self.encode(label.text), |
381 |
|
label.halign, |
382 |
label.valign)) |
label.valign)) |
383 |
self.close_element('labellayer') |
self.close_element('labellayer') |
384 |
|
|