1 |
# Copyright (C) 2001, 2002, 2003 by Intevation GmbH |
# Copyright (C) 2001, 2002, 2003, 2004 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> |
4 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
5 |
# Jonathan Coles <[email protected]> |
# Jonathan Coles <[email protected]> |
6 |
|
# Frank Koormann <[email protected]> |
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 GRASS for details. |
# Read the file COPYING coming with GRASS for details. |
101 |
|
|
102 |
class SessionLoader(XMLReader): |
class SessionLoader(XMLReader): |
103 |
|
|
104 |
def __init__(self, db_connection_callback = None): |
def __init__(self, db_connection_callback = None, |
105 |
|
shapefile_callback = None): |
106 |
"""Inititialize the Sax handler.""" |
"""Inititialize the Sax handler.""" |
107 |
XMLReader.__init__(self) |
XMLReader.__init__(self) |
108 |
|
|
109 |
self.db_connection_callback = db_connection_callback |
self.db_connection_callback = db_connection_callback |
110 |
|
self.shapefile_callback = shapefile_callback |
111 |
self.theSession = None |
self.theSession = None |
112 |
self.aMap = None |
self.aMap = None |
113 |
self.aLayer = None |
self.aLayer = None |
145 |
for xmlns in ("http://thuban.intevation.org/dtds/thuban-0.8.dtd", |
for xmlns in ("http://thuban.intevation.org/dtds/thuban-0.8.dtd", |
146 |
"http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd", |
"http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd", |
147 |
"http://thuban.intevation.org/dtds/thuban-0.9.dtd", |
"http://thuban.intevation.org/dtds/thuban-0.9.dtd", |
148 |
"http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd"): |
"http://thuban.intevation.org/dtds/thuban-1.0-dev.dtd", |
149 |
|
"http://thuban.intevation.org/dtds/thuban-1.0rc1.dtd", |
150 |
|
"http://thuban.intevation.org/dtds/thuban-1.0.0.dtd", |
151 |
|
"http://thuban.intevation.org/dtds/thuban-1.1-dev.dtd"): |
152 |
for key, value in dispatchers.items(): |
for key, value in dispatchers.items(): |
153 |
dispatchers[(xmlns, key)] = value |
dispatchers[(xmlns, key)] = value |
154 |
|
|
155 |
XMLReader.AddDispatchers(self, dispatchers) |
XMLReader.AddDispatchers(self, dispatchers) |
156 |
|
|
157 |
|
def Destroy(self): |
158 |
|
"""Clear all instance variables to cut cyclic references. |
159 |
|
|
160 |
|
The GC would have collected the loader eventually but it can |
161 |
|
happen that it doesn't run at all until Thuban is closed (2.3 |
162 |
|
but not 2.2 tries a bit harder and forces a collection when the |
163 |
|
interpreter terminates) |
164 |
|
""" |
165 |
|
self.__dict__.clear() |
166 |
|
|
167 |
def start_session(self, name, qname, attrs): |
def start_session(self, name, qname, attrs): |
168 |
self.theSession = Session(self.encode(attrs.get((None, 'title'), |
self.theSession = Session(self.encode(attrs.get((None, 'title'), |
169 |
None))) |
None))) |
191 |
If the attribute has a default value and it is not present in |
If the attribute has a default value and it is not present in |
192 |
attrs, use that default value as the value in the returned dict. |
attrs, use that default value as the value in the returned dict. |
193 |
|
|
194 |
If a conversion is specified, convert the value before putting |
The value is converted before putting it into the returned dict. |
195 |
it into the returned dict. The following conversions are |
The following conversions are available: |
|
available: |
|
196 |
|
|
197 |
'filename' -- The attribute is a filename. |
'filename' -- The attribute is a filename. |
198 |
|
|
216 |
ascii encoding. |
ascii encoding. |
217 |
|
|
218 |
a callable -- The attribute value is passed to the callable |
a callable -- The attribute value is passed to the callable |
219 |
and the return value is used a as the converted |
and the return value is used as the converted |
220 |
value |
value |
221 |
|
|
222 |
|
If no conversion is specified for an attribute it is converted |
223 |
|
with self.encode. |
224 |
""" |
""" |
225 |
normalized = {} |
normalized = {} |
226 |
|
|
254 |
elif d.conversion: |
elif d.conversion: |
255 |
# Assume it's a callable |
# Assume it's a callable |
256 |
value = d.conversion(value) |
value = d.conversion(value) |
257 |
|
else: |
258 |
|
value = self.encode(value) |
259 |
|
|
260 |
normalized[d.name] = value |
normalized[d.name] = value |
261 |
return normalized |
return normalized |
262 |
|
|
263 |
|
def open_shapefile(self, filename): |
264 |
|
"""Open shapefile, with alternative path handling. |
265 |
|
|
266 |
|
If a shapefile cannot be opened and an IOError is raised, check for |
267 |
|
an alternative. This alternative can be specified interactively by |
268 |
|
the user or taken from a list of (potential) locations, depending on |
269 |
|
the callback implementation. |
270 |
|
|
271 |
|
The alternative is rechecked. If taken from a list the user |
272 |
|
has to confirm the alternative. |
273 |
|
""" |
274 |
|
|
275 |
|
# Flag if the alternative path was specified interactively / from list. |
276 |
|
from_list = 0 |
277 |
|
while 1: |
278 |
|
try: |
279 |
|
store = self.theSession.OpenShapefile(filename) |
280 |
|
if from_list: |
281 |
|
# A valid path has been guessed from a list |
282 |
|
# Let the user confirm - or select an alternative. |
283 |
|
filename, from_list = self.shapefile_callback( |
284 |
|
filename, "check") |
285 |
|
if filename is None: |
286 |
|
# Selection cancelled |
287 |
|
raise LoadCancelled |
288 |
|
elif store.FileName() == filename: |
289 |
|
# Proposed file has been accepted |
290 |
|
break |
291 |
|
else: |
292 |
|
# the filename has been changed, try the new file |
293 |
|
pass |
294 |
|
else: |
295 |
|
break |
296 |
|
except IOError: |
297 |
|
if self.shapefile_callback is not None: |
298 |
|
filename, from_list = self.shapefile_callback( |
299 |
|
filename, |
300 |
|
mode = "search", |
301 |
|
second_try = from_list) |
302 |
|
if filename is None: |
303 |
|
raise LoadCancelled |
304 |
|
print filename |
305 |
|
else: |
306 |
|
raise |
307 |
|
return store |
308 |
|
|
309 |
def start_dbconnection(self, name, qname, attrs): |
def start_dbconnection(self, name, qname, attrs): |
310 |
attrs = self.check_attrs(name, attrs, |
attrs = self.check_attrs(name, attrs, |
311 |
[AttrDesc("id", True), |
[AttrDesc("id", True), |
349 |
AttrDesc("dbconn", True, |
AttrDesc("dbconn", True, |
350 |
conversion = "idref"), |
conversion = "idref"), |
351 |
AttrDesc("tablename", True, |
AttrDesc("tablename", True, |
352 |
|
conversion = "ascii"), |
353 |
|
# id_column and geometry_column were |
354 |
|
# newly introduced with thuban-1.1.dtd |
355 |
|
# where they're required. Since we |
356 |
|
# support the older formats too we |
357 |
|
# have them optional here. |
358 |
|
AttrDesc("id_column", False, "gid", |
359 |
|
conversion = "ascii"), |
360 |
|
AttrDesc("geometry_column", False, |
361 |
conversion = "ascii")]) |
conversion = "ascii")]) |
362 |
ID = attrs["id"] |
# The default value of geometry_column to use when instantiating |
363 |
db = attrs["dbconn"] |
# the db shapestore is None which we currently can't easily use |
364 |
tablename = attrs["tablename"] |
# in check_attrs |
365 |
self.idmap[ID] = self.theSession.OpenDBShapeStore(db, tablename) |
geometry_column = attrs["geometry_column"] |
366 |
|
if not geometry_column: |
367 |
|
geometry_column = None |
368 |
|
dbopen = self.theSession.OpenDBShapeStore |
369 |
|
self.idmap[attrs["id"]] = dbopen(attrs["dbconn"], attrs["tablename"], |
370 |
|
id_column = attrs["id_column"], |
371 |
|
geometry_column=geometry_column) |
372 |
|
|
373 |
def start_fileshapesource(self, name, qname, attrs): |
def start_fileshapesource(self, name, qname, attrs): |
374 |
attrs = self.check_attrs(name, attrs, |
attrs = self.check_attrs(name, attrs, |
381 |
filetype = attrs["filetype"] |
filetype = attrs["filetype"] |
382 |
if filetype != "shapefile": |
if filetype != "shapefile": |
383 |
raise LoadError("shapesource filetype %r not supported" % filetype) |
raise LoadError("shapesource filetype %r not supported" % filetype) |
384 |
self.idmap[ID] = self.theSession.OpenShapefile(filename) |
self.idmap[ID] = self.open_shapefile(filename) |
385 |
|
|
386 |
def start_derivedshapesource(self, name, qname, attrs): |
def start_derivedshapesource(self, name, qname, attrs): |
387 |
attrs = self.check_attrs(name, attrs, |
attrs = self.check_attrs(name, attrs, |
492 |
if attrs.has_key((None, "shapestore")): |
if attrs.has_key((None, "shapestore")): |
493 |
store = self.idmap[attrs[(None, "shapestore")]] |
store = self.idmap[attrs[(None, "shapestore")]] |
494 |
else: |
else: |
495 |
store = self.theSession.OpenShapefile(filename) |
store = self.open_shapefile(filename) |
496 |
|
|
497 |
self.aLayer = layer_class(title, store, |
self.aLayer = layer_class(title, store, |
498 |
fill = fill, stroke = stroke, |
fill = fill, stroke = stroke, |
499 |
lineWidth = stroke_width, |
lineWidth = stroke_width, |
517 |
self.aLayer = None |
self.aLayer = None |
518 |
|
|
519 |
def start_classification(self, name, qname, attrs): |
def start_classification(self, name, qname, attrs): |
520 |
field = attrs.get((None, 'field'), None) |
attrs = self.check_attrs(name, attrs, |
521 |
|
[AttrDesc("field", True), |
522 |
|
AttrDesc("field_type", True)]) |
523 |
|
field = attrs["field"] |
524 |
|
fieldType = attrs["field_type"] |
525 |
|
|
|
fieldType = attrs.get((None, 'field_type'), None) |
|
526 |
dbFieldType = self.aLayer.GetFieldType(field) |
dbFieldType = self.aLayer.GetFieldType(field) |
527 |
|
|
528 |
if fieldType != dbFieldType: |
if fieldType != dbFieldType: |
603 |
parse_color(attrs.get((None, 'stroke'), "None"))) |
parse_color(attrs.get((None, 'stroke'), "None"))) |
604 |
self.cl_prop.SetLineWidth( |
self.cl_prop.SetLineWidth( |
605 |
int(attrs.get((None, 'stroke_width'), "0"))) |
int(attrs.get((None, 'stroke_width'), "0"))) |
606 |
|
self.cl_prop.SetSize(int(attrs.get((None, 'size'), "5"))) |
607 |
self.cl_prop.SetFill(parse_color(attrs.get((None, 'fill'), "None"))) |
self.cl_prop.SetFill(parse_color(attrs.get((None, 'fill'), "None"))) |
608 |
|
|
609 |
def end_cldata(self, name, qname): |
def end_cldata(self, name, qname): |
613 |
self.aLayer = self.aMap.LabelLayer() |
self.aLayer = self.aMap.LabelLayer() |
614 |
|
|
615 |
def start_label(self, name, qname, attrs): |
def start_label(self, name, qname, attrs): |
616 |
x = float(attrs[(None, 'x')]) |
attrs = self.check_attrs(name, attrs, |
617 |
y = float(attrs[(None, 'y')]) |
[AttrDesc("x", True, conversion = float), |
618 |
text = self.encode(attrs[(None, 'text')]) |
AttrDesc("y", True, conversion = float), |
619 |
halign = attrs[(None, 'halign')] |
AttrDesc("text", True), |
620 |
valign = attrs[(None, 'valign')] |
AttrDesc("halign", True, |
621 |
|
conversion = "ascii"), |
622 |
|
AttrDesc("valign", True, |
623 |
|
conversion = "ascii")]) |
624 |
|
x = attrs['x'] |
625 |
|
y = attrs['y'] |
626 |
|
text = attrs['text'] |
627 |
|
halign = attrs['halign'] |
628 |
|
valign = attrs['valign'] |
629 |
|
if halign not in ("left", "center", "right"): |
630 |
|
raise LoadError("Unsupported halign value %r" % halign) |
631 |
|
if valign not in ("top", "center", "bottom"): |
632 |
|
raise LoadError("Unsupported valign value %r" % valign) |
633 |
self.aLayer.AddLabel(x, y, text, halign = halign, valign = valign) |
self.aLayer.AddLabel(x, y, text, halign = halign, valign = valign) |
634 |
|
|
635 |
def characters(self, chars): |
def characters(self, chars): |
636 |
pass |
pass |
637 |
|
|
638 |
|
|
639 |
def load_session(filename, db_connection_callback = None): |
def load_session(filename, db_connection_callback = None, |
640 |
|
shapefile_callback = None): |
641 |
"""Load a Thuban session from the file object file |
"""Load a Thuban session from the file object file |
642 |
|
|
643 |
The db_connection_callback, if given should be a callable object |
The db_connection_callback, if given should be a callable object |
650 |
corrected and perhaps additional parameters like a password or None |
corrected and perhaps additional parameters like a password or None |
651 |
to indicate that the user cancelled. |
to indicate that the user cancelled. |
652 |
""" |
""" |
653 |
handler = SessionLoader(db_connection_callback) |
handler = SessionLoader(db_connection_callback, shapefile_callback) |
654 |
handler.read(filename) |
handler.read(filename) |
655 |
|
|
656 |
session = handler.theSession |
session = handler.theSession |
657 |
# Newly loaded session aren't modified |
# Newly loaded session aren't modified |
658 |
session.UnsetModified() |
session.UnsetModified() |
659 |
|
|
660 |
|
handler.Destroy() |
661 |
|
|
662 |
return session |
return session |
663 |
|
|