1 |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
# $Id$ |
2 |
|
# Copyright (c) 2001-2004 by Intevation GmbH |
3 |
# Authors: |
# Authors: |
4 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> (2004) |
5 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> (2001-2004) |
6 |
# Jonathan Coles <[email protected]> |
# Jonathan Coles <[email protected]> (2003) |
7 |
|
# Frank Koormann <[email protected]> (2003) |
8 |
# |
# |
9 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
10 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
24 |
from Thuban.Model.classification import \ |
from Thuban.Model.classification import \ |
25 |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap |
26 |
from Thuban.Model.transientdb import AutoTransientTable, TransientJoinedTable |
from Thuban.Model.transientdb import AutoTransientTable, TransientJoinedTable |
27 |
from Thuban.Model.table import DBFTable |
from Thuban.Model.table import DBFTable, FIELDTYPE_STRING |
28 |
from Thuban.Model.data import DerivedShapeStore, ShapefileStore |
from Thuban.Model.data import DerivedShapeStore, ShapefileStore, \ |
29 |
|
SHAPETYPE_POINT |
30 |
|
|
31 |
from Thuban.Model.xmlwriter import XMLWriter |
from Thuban.Model.xmlwriter import XMLWriter |
32 |
|
from postgisdb import PostGISConnection, PostGISShapeStore |
33 |
|
|
34 |
def relative_filename(dir, filename): |
def relative_filename(dir, filename): |
35 |
"""Return a filename relative to dir for the absolute file name absname. |
"""Return a filename relative to dir for the absolute file name absname. |
44 |
return filename |
return filename |
45 |
|
|
46 |
|
|
47 |
|
def unify_filename(filename): |
48 |
|
"""Return a 'unified' version of filename |
49 |
|
|
50 |
|
The .thuban files should be as platform independent as possible. |
51 |
|
Since they must contain filenames the filenames have to unified. We |
52 |
|
unify on unix-like filenames for now, which means we do nothing on a |
53 |
|
posix system and simply replace backslashes with slashes on windows |
54 |
|
""" |
55 |
|
if os.name == "posix": |
56 |
|
return filename |
57 |
|
elif os.name == "nt": |
58 |
|
return "/".join(filename.split("\\")) |
59 |
|
else: |
60 |
|
raise RuntimeError("Unsupported platform for unify_filename: %s" |
61 |
|
% os.name) |
62 |
|
|
63 |
def sort_data_stores(stores): |
def sort_data_stores(stores): |
64 |
"""Return a topologically sorted version of the sequence of data containers |
"""Return a topologically sorted version of the sequence of data containers |
65 |
|
|
122 |
def has_id(self, obj): |
def has_id(self, obj): |
123 |
return self.idmap.has_key(id(obj)) |
return self.idmap.has_key(id(obj)) |
124 |
|
|
125 |
|
def prepare_filename(self, filename): |
126 |
|
"""Return the string to use when writing filename to the thuban file |
127 |
|
|
128 |
|
The returned string is a unified version (only slashes as |
129 |
|
directory separators, see unify_filename) of filename expressed |
130 |
|
relative to the directory the .thuban file is written to. |
131 |
|
""" |
132 |
|
return unify_filename(relative_filename(self.dir, filename)) |
133 |
|
|
134 |
def write(self, file_or_filename): |
def write(self, file_or_filename): |
135 |
XMLWriter.write(self, file_or_filename) |
XMLWriter.write(self, file_or_filename) |
136 |
|
|
137 |
self.write_header("session", "thuban-0.8.dtd") |
self.write_header("session", "thuban-1.1.dtd") |
138 |
self.write_session(self.session) |
self.write_session(self.session) |
139 |
self.close() |
self.close() |
140 |
|
|
161 |
attrs["xmlns:" + name] = uri |
attrs["xmlns:" + name] = uri |
162 |
# default name space |
# default name space |
163 |
attrs["xmlns"] = \ |
attrs["xmlns"] = \ |
164 |
"http://thuban.intevation.org/dtds/thuban-0.8.dtd" |
"http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
165 |
self.open_element("session", attrs) |
self.open_element("session", attrs) |
166 |
|
self.write_db_connections(session) |
167 |
self.write_data_containers(session) |
self.write_data_containers(session) |
168 |
for map in session.Maps(): |
for map in session.Maps(): |
169 |
self.write_map(map) |
self.write_map(map) |
170 |
self.close_element("session") |
self.close_element("session") |
171 |
|
|
172 |
|
def write_db_connections(self, session): |
173 |
|
for conn in session.DBConnections(): |
174 |
|
if isinstance(conn, PostGISConnection): |
175 |
|
self.write_element("dbconnection", |
176 |
|
{"id": self.define_id(conn), |
177 |
|
"dbtype": "postgis", |
178 |
|
"host": conn.host, |
179 |
|
"port": conn.port, |
180 |
|
"user": conn.user, |
181 |
|
"dbname": conn.dbname}) |
182 |
|
else: |
183 |
|
raise ValueError("Can't handle db connection %r" % conn) |
184 |
|
|
185 |
def write_data_containers(self, session): |
def write_data_containers(self, session): |
186 |
containers = sort_data_stores(session.DataContainers()) |
containers = sort_data_stores(session.DataContainers()) |
187 |
for container in containers: |
for container in containers: |
199 |
idvalue = self.define_id(container) |
idvalue = self.define_id(container) |
200 |
if isinstance(container, ShapefileStore): |
if isinstance(container, ShapefileStore): |
201 |
self.define_id(container.Table(), idvalue) |
self.define_id(container.Table(), idvalue) |
202 |
filename = relative_filename(self.dir, container.FileName()) |
filename = self.prepare_filename(container.FileName()) |
203 |
self.write_element("fileshapesource", |
self.write_element("fileshapesource", |
204 |
{"id": idvalue, "filename": filename, |
{"id": idvalue, "filename": filename, |
205 |
"filetype": "shapefile"}) |
"filetype": "shapefile"}) |
209 |
{"id": idvalue, |
{"id": idvalue, |
210 |
"shapesource": self.get_id(shapesource), |
"shapesource": self.get_id(shapesource), |
211 |
"table": self.get_id(table)}) |
"table": self.get_id(table)}) |
212 |
|
elif isinstance(container, PostGISShapeStore): |
213 |
|
conn = container.DBConnection() |
214 |
|
self.write_element("dbshapesource", |
215 |
|
{"id": idvalue, |
216 |
|
"dbconn": self.get_id(conn), |
217 |
|
"tablename": container.TableName(), |
218 |
|
"id_column": container.IDColumn().name, |
219 |
|
"geometry_column": |
220 |
|
container.GeometryColumn().name, |
221 |
|
}) |
222 |
elif isinstance(container, DBFTable): |
elif isinstance(container, DBFTable): |
223 |
filename = relative_filename(self.dir, container.FileName()) |
filename = self.prepare_filename(container.FileName()) |
224 |
self.write_element("filetable", |
self.write_element("filetable", |
225 |
{"id": idvalue, |
{"id": idvalue, |
226 |
"title": container.Title(), |
"title": container.Title(), |
236 |
"right": self.get_id(right), |
"right": self.get_id(right), |
237 |
"rightcolumn": right_field, |
"rightcolumn": right_field, |
238 |
"left": self.get_id(left), |
"left": self.get_id(left), |
239 |
"leftcolumn": left_field}) |
"leftcolumn": left_field, |
240 |
|
"jointype": container.JoinType()}) |
241 |
else: |
else: |
242 |
raise ValueError("Can't handle container %r" % container) |
raise ValueError("Can't handle container %r" % container) |
243 |
|
|
261 |
"""Write the projection. |
"""Write the projection. |
262 |
""" |
""" |
263 |
if projection and len(projection.params) > 0: |
if projection and len(projection.params) > 0: |
264 |
self.open_element("projection", {"name": projection.GetName()}) |
attrs = {"name": projection.GetName()} |
265 |
|
epsg = projection.EPSGCode() |
266 |
|
if epsg is not None: |
267 |
|
attrs["epsg"] = epsg |
268 |
|
self.open_element("projection", attrs) |
269 |
for param in projection.params: |
for param in projection.params: |
270 |
self.write_element('parameter value="%s"' % |
self.write_element('parameter value="%s"' % |
271 |
self.encode(param)) |
self.encode(param)) |
298 |
self.write_classification(layer) |
self.write_classification(layer) |
299 |
self.close_element("layer") |
self.close_element("layer") |
300 |
elif isinstance(layer, RasterLayer): |
elif isinstance(layer, RasterLayer): |
301 |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
attrs["filename"] = self.prepare_filename(layer.filename) |
302 |
self.open_element("rasterlayer", attrs) |
self.open_element("rasterlayer", attrs) |
303 |
self.write_projection(layer.GetProjection()) |
self.write_projection(layer.GetProjection()) |
304 |
self.close_element("rasterlayer") |
self.close_element("rasterlayer") |
311 |
|
|
312 |
lc = layer.GetClassification() |
lc = layer.GetClassification() |
313 |
|
|
314 |
field = lc.GetField() |
field = layer.GetClassificationColumn() |
315 |
|
|
316 |
# |
# |
317 |
# there isn't a classification of anything so do nothing |
# there isn't a classification of anything so do nothing |
318 |
# |
# |
319 |
if field is None: return |
if field is None: return |
320 |
|
|
321 |
attrs["field"] = field |
attrs["field"] = field |
322 |
attrs["field_type"] = str(lc.GetFieldType()) |
attrs["field_type"] = str(layer.GetFieldType(field)) |
323 |
self.open_element("classification", attrs) |
self.open_element("classification", attrs) |
324 |
|
|
325 |
for g in lc: |
for g in lc: |
326 |
if isinstance(g, ClassGroupDefault): |
if isinstance(g, ClassGroupDefault): |
327 |
open_el = 'clnull label="%s"' % self.encode(g.GetLabel()) |
open_el = 'clnull label="%s"' % self.encode(g.GetLabel()) |
328 |
close_el = 'clnull' |
close_el = 'clnull' |
329 |
elif isinstance(g, ClassGroupSingleton): |
elif isinstance(g, ClassGroupSingleton): |
330 |
|
if layer.GetFieldType(field) == FIELDTYPE_STRING: |
331 |
|
value = self.encode(g.GetValue()) |
332 |
|
else: |
333 |
|
value = str(g.GetValue()) |
334 |
open_el = 'clpoint label="%s" value="%s"' \ |
open_el = 'clpoint label="%s" value="%s"' \ |
335 |
% (self.encode(g.GetLabel()), str(g.GetValue())) |
% (self.encode(g.GetLabel()), value) |
336 |
close_el = 'clpoint' |
close_el = 'clpoint' |
337 |
elif isinstance(g, ClassGroupRange): |
elif isinstance(g, ClassGroupRange): |
338 |
open_el = 'clrange label="%s" range="%s"' \ |
open_el = 'clrange label="%s" range="%s"' \ |
347 |
'stroke_width': str(data.GetLineWidth()), |
'stroke_width': str(data.GetLineWidth()), |
348 |
'fill' : data.GetFill().hex()} |
'fill' : data.GetFill().hex()} |
349 |
|
|
350 |
|
# only for point layers write the size attribute |
351 |
|
if layer.ShapeType() == SHAPETYPE_POINT: |
352 |
|
dict['size'] = str(data.GetSize()) |
353 |
|
|
354 |
self.open_element(open_el) |
self.open_element(open_el) |
355 |
self.write_element("cldata", dict) |
self.write_element("cldata", dict) |
356 |
self.close_element(close_el) |
self.close_element(close_el) |