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 |
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 |
|
else: |
305 |
|
raise |
306 |
|
return store |
307 |
|
|
308 |
def start_dbconnection(self, name, qname, attrs): |
def start_dbconnection(self, name, qname, attrs): |
309 |
attrs = self.check_attrs(name, attrs, |
attrs = self.check_attrs(name, attrs, |
310 |
[AttrDesc("id", True), |
[AttrDesc("id", True), |
380 |
filetype = attrs["filetype"] |
filetype = attrs["filetype"] |
381 |
if filetype != "shapefile": |
if filetype != "shapefile": |
382 |
raise LoadError("shapesource filetype %r not supported" % filetype) |
raise LoadError("shapesource filetype %r not supported" % filetype) |
383 |
self.idmap[ID] = self.theSession.OpenShapefile(filename) |
self.idmap[ID] = self.open_shapefile(filename) |
384 |
|
|
385 |
def start_derivedshapesource(self, name, qname, attrs): |
def start_derivedshapesource(self, name, qname, attrs): |
386 |
attrs = self.check_attrs(name, attrs, |
attrs = self.check_attrs(name, attrs, |
491 |
if attrs.has_key((None, "shapestore")): |
if attrs.has_key((None, "shapestore")): |
492 |
store = self.idmap[attrs[(None, "shapestore")]] |
store = self.idmap[attrs[(None, "shapestore")]] |
493 |
else: |
else: |
494 |
store = self.theSession.OpenShapefile(filename) |
store = self.open_shapefile(filename) |
495 |
|
|
496 |
self.aLayer = layer_class(title, store, |
self.aLayer = layer_class(title, store, |
497 |
fill = fill, stroke = stroke, |
fill = fill, stroke = stroke, |
498 |
lineWidth = stroke_width, |
lineWidth = stroke_width, |
635 |
pass |
pass |
636 |
|
|
637 |
|
|
638 |
def load_session(filename, db_connection_callback = None): |
def load_session(filename, db_connection_callback = None, |
639 |
|
shapefile_callback = None): |
640 |
"""Load a Thuban session from the file object file |
"""Load a Thuban session from the file object file |
641 |
|
|
642 |
The db_connection_callback, if given should be a callable object |
The db_connection_callback, if given should be a callable object |
649 |
corrected and perhaps additional parameters like a password or None |
corrected and perhaps additional parameters like a password or None |
650 |
to indicate that the user cancelled. |
to indicate that the user cancelled. |
651 |
""" |
""" |
652 |
handler = SessionLoader(db_connection_callback) |
handler = SessionLoader(db_connection_callback, shapefile_callback) |
653 |
handler.read(filename) |
handler.read(filename) |
654 |
|
|
655 |
session = handler.theSession |
session = handler.theSession |