1 |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
# Copyright (c) 2001-2004 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> (2004) |
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 |
|
# $Id$ |
18 |
|
|
19 |
import os |
import os |
20 |
|
|
26 |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap |
ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap |
27 |
from Thuban.Model.transientdb import AutoTransientTable, TransientJoinedTable |
from Thuban.Model.transientdb import AutoTransientTable, TransientJoinedTable |
28 |
from Thuban.Model.table import DBFTable, FIELDTYPE_STRING |
from Thuban.Model.table import DBFTable, FIELDTYPE_STRING |
29 |
from Thuban.Model.data import DerivedShapeStore, ShapefileStore |
from Thuban.Model.data import DerivedShapeStore, ShapefileStore, \ |
30 |
|
SHAPETYPE_POINT |
31 |
|
|
32 |
from Thuban.Model.xmlwriter import XMLWriter |
from Thuban.Model.xmlwriter import XMLWriter |
33 |
|
from postgisdb import PostGISConnection, PostGISShapeStore |
34 |
|
|
35 |
def relative_filename(dir, filename): |
def relative_filename(dir, filename): |
36 |
"""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 |
return filename |
return filename |
46 |
|
|
47 |
|
|
48 |
|
def unify_filename(filename): |
49 |
|
"""Return a 'unified' version of filename |
50 |
|
|
51 |
|
The .thuban files should be as platform independent as possible. |
52 |
|
Since they must contain filenames the filenames have to unified. We |
53 |
|
unify on unix-like filenames for now, which means we do nothing on a |
54 |
|
posix system and simply replace backslashes with slashes on windows |
55 |
|
""" |
56 |
|
if os.name == "posix": |
57 |
|
return filename |
58 |
|
elif os.name == "nt": |
59 |
|
return "/".join(filename.split("\\")) |
60 |
|
else: |
61 |
|
raise RuntimeError("Unsupported platform for unify_filename: %s" |
62 |
|
% os.name) |
63 |
|
|
64 |
def sort_data_stores(stores): |
def sort_data_stores(stores): |
65 |
"""Return a topologically sorted version of the sequence of data containers |
"""Return a topologically sorted version of the sequence of data containers |
66 |
|
|
123 |
def has_id(self, obj): |
def has_id(self, obj): |
124 |
return self.idmap.has_key(id(obj)) |
return self.idmap.has_key(id(obj)) |
125 |
|
|
126 |
|
def prepare_filename(self, filename): |
127 |
|
"""Return the string to use when writing filename to the thuban file |
128 |
|
|
129 |
|
The returned string is a unified version (only slashes as |
130 |
|
directory separators, see unify_filename) of filename expressed |
131 |
|
relative to the directory the .thuban file is written to. |
132 |
|
""" |
133 |
|
return unify_filename(relative_filename(self.dir, filename)) |
134 |
|
|
135 |
def write(self, file_or_filename): |
def write(self, file_or_filename): |
136 |
XMLWriter.write(self, file_or_filename) |
XMLWriter.write(self, file_or_filename) |
137 |
|
|
138 |
self.write_header("session", "thuban-0.9.dtd") |
self.write_header("session", "thuban-1.1.dtd") |
139 |
self.write_session(self.session) |
self.write_session(self.session) |
140 |
self.close() |
self.close() |
141 |
|
|
162 |
attrs["xmlns:" + name] = uri |
attrs["xmlns:" + name] = uri |
163 |
# default name space |
# default name space |
164 |
attrs["xmlns"] = \ |
attrs["xmlns"] = \ |
165 |
"http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd" |
"http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd" |
166 |
self.open_element("session", attrs) |
self.open_element("session", attrs) |
167 |
|
self.write_db_connections(session) |
168 |
self.write_data_containers(session) |
self.write_data_containers(session) |
169 |
for map in session.Maps(): |
for map in session.Maps(): |
170 |
self.write_map(map) |
self.write_map(map) |
171 |
self.close_element("session") |
self.close_element("session") |
172 |
|
|
173 |
|
def write_db_connections(self, session): |
174 |
|
for conn in session.DBConnections(): |
175 |
|
if isinstance(conn, PostGISConnection): |
176 |
|
self.write_element("dbconnection", |
177 |
|
{"id": self.define_id(conn), |
178 |
|
"dbtype": "postgis", |
179 |
|
"host": conn.host, |
180 |
|
"port": conn.port, |
181 |
|
"user": conn.user, |
182 |
|
"dbname": conn.dbname}) |
183 |
|
else: |
184 |
|
raise ValueError("Can't handle db connection %r" % conn) |
185 |
|
|
186 |
def write_data_containers(self, session): |
def write_data_containers(self, session): |
187 |
containers = sort_data_stores(session.DataContainers()) |
containers = sort_data_stores(session.DataContainers()) |
188 |
for container in containers: |
for container in containers: |
200 |
idvalue = self.define_id(container) |
idvalue = self.define_id(container) |
201 |
if isinstance(container, ShapefileStore): |
if isinstance(container, ShapefileStore): |
202 |
self.define_id(container.Table(), idvalue) |
self.define_id(container.Table(), idvalue) |
203 |
filename = relative_filename(self.dir, container.FileName()) |
filename = self.prepare_filename(container.FileName()) |
204 |
self.write_element("fileshapesource", |
self.write_element("fileshapesource", |
205 |
{"id": idvalue, "filename": filename, |
{"id": idvalue, "filename": filename, |
206 |
"filetype": "shapefile"}) |
"filetype": "shapefile"}) |
210 |
{"id": idvalue, |
{"id": idvalue, |
211 |
"shapesource": self.get_id(shapesource), |
"shapesource": self.get_id(shapesource), |
212 |
"table": self.get_id(table)}) |
"table": self.get_id(table)}) |
213 |
|
elif isinstance(container, PostGISShapeStore): |
214 |
|
conn = container.DBConnection() |
215 |
|
self.write_element("dbshapesource", |
216 |
|
{"id": idvalue, |
217 |
|
"dbconn": self.get_id(conn), |
218 |
|
"tablename": container.TableName(), |
219 |
|
"id_column": container.IDColumn().name, |
220 |
|
"geometry_column": |
221 |
|
container.GeometryColumn().name, |
222 |
|
}) |
223 |
elif isinstance(container, DBFTable): |
elif isinstance(container, DBFTable): |
224 |
filename = relative_filename(self.dir, container.FileName()) |
filename = self.prepare_filename(container.FileName()) |
225 |
self.write_element("filetable", |
self.write_element("filetable", |
226 |
{"id": idvalue, |
{"id": idvalue, |
227 |
"title": container.Title(), |
"title": container.Title(), |
262 |
"""Write the projection. |
"""Write the projection. |
263 |
""" |
""" |
264 |
if projection and len(projection.params) > 0: |
if projection and len(projection.params) > 0: |
265 |
self.open_element("projection", {"name": projection.GetName()}) |
attrs = {"name": projection.GetName()} |
266 |
|
epsg = projection.EPSGCode() |
267 |
|
if epsg is not None: |
268 |
|
attrs["epsg"] = epsg |
269 |
|
self.open_element("projection", attrs) |
270 |
for param in projection.params: |
for param in projection.params: |
271 |
self.write_element('parameter value="%s"' % |
self.write_element('parameter value="%s"' % |
272 |
self.encode(param)) |
self.encode(param)) |
299 |
self.write_classification(layer) |
self.write_classification(layer) |
300 |
self.close_element("layer") |
self.close_element("layer") |
301 |
elif isinstance(layer, RasterLayer): |
elif isinstance(layer, RasterLayer): |
302 |
attrs["filename"] = relative_filename(self.dir, layer.filename) |
attrs["filename"] = self.prepare_filename(layer.filename) |
303 |
self.open_element("rasterlayer", attrs) |
self.open_element("rasterlayer", attrs) |
304 |
self.write_projection(layer.GetProjection()) |
self.write_projection(layer.GetProjection()) |
305 |
self.close_element("rasterlayer") |
self.close_element("rasterlayer") |
312 |
|
|
313 |
lc = layer.GetClassification() |
lc = layer.GetClassification() |
314 |
|
|
315 |
field = layer.GetClassificationField() |
field = layer.GetClassificationColumn() |
316 |
|
|
317 |
# |
# |
318 |
# there isn't a classification of anything so do nothing |
# there isn't a classification of anything so do nothing |
319 |
# |
# |
348 |
'stroke_width': str(data.GetLineWidth()), |
'stroke_width': str(data.GetLineWidth()), |
349 |
'fill' : data.GetFill().hex()} |
'fill' : data.GetFill().hex()} |
350 |
|
|
351 |
|
# only for point layers write the size attribute |
352 |
|
if layer.ShapeType() == SHAPETYPE_POINT: |
353 |
|
dict['size'] = str(data.GetSize()) |
354 |
|
|
355 |
self.open_element(open_el) |
self.open_element(open_el) |
356 |
self.write_element("cldata", dict) |
self.write_element("cldata", dict) |
357 |
self.close_element(close_el) |
self.close_element(close_el) |