/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/load.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/Model/load.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2034 by bh, Mon Dec 22 16:35:16 2003 UTC revision 2446 by frank, Mon Dec 13 11:52:34 2004 UTC
# Line 1  Line 1 
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.
# Line 100  class AttrDesc: Line 101  class AttrDesc:
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
# Line 144  class SessionLoader(XMLReader): Line 146  class SessionLoader(XMLReader):
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"):                        "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    
# Line 256  class SessionLoader(XMLReader): Line 260  class SessionLoader(XMLReader):
260              normalized[d.name] = value              normalized[d.name] = value
261          return normalized          return normalized
262    
263        def open_shapefile(self, filename):
264            """Open shapefile, eventually with alternative path."""
265            from_list = 0
266            while 1:
267                try:
268                    store = self.theSession.OpenShapefile(filename)
269                    if from_list:
270                        # The correct? path has been guessed from a list
271                        # Let the user confirm - or select an alternative.
272                        filename, from_list = self.shapefile_callback(
273                                                filename, "check")
274                        if filename is None:
275                            # Selection cancelled
276                            raise LoadCancelled
277                        elif store.FileName() == filename:
278                            # Proposed file has been accepted
279                            break
280                        else:
281                            # the filename has been changed, try the new file
282                            pass
283                    else:
284                        break
285                except IOError:
286                    if self.shapefile_callback is not None:
287                        filename, from_list = self.shapefile_callback(
288                                                filename,
289                                                mode = "search",
290                                                second_try = from_list)
291                        if filename is None:
292                            raise LoadCancelled
293                        print filename
294                    else:
295                        raise
296            return store
297    
298      def start_dbconnection(self, name, qname, attrs):      def start_dbconnection(self, name, qname, attrs):
299          attrs = self.check_attrs(name, attrs,          attrs = self.check_attrs(name, attrs,
300                                   [AttrDesc("id", True),                                   [AttrDesc("id", True),
# Line 299  class SessionLoader(XMLReader): Line 338  class SessionLoader(XMLReader):
338                                    AttrDesc("dbconn", True,                                    AttrDesc("dbconn", True,
339                                             conversion = "idref"),                                             conversion = "idref"),
340                                    AttrDesc("tablename", True,                                    AttrDesc("tablename", True,
341                                               conversion = "ascii"),
342                                      # id_column and geometry_column were
343                                      # newly introduced with thuban-1.1.dtd
344                                      # where they're required.  Since we
345                                      # support the older formats too we
346                                      # have them optional here.
347                                      AttrDesc("id_column", False, "gid",
348                                               conversion = "ascii"),
349                                      AttrDesc("geometry_column", False,
350                                             conversion = "ascii")])                                             conversion = "ascii")])
351          ID = attrs["id"]          # The default value of geometry_column to use when instantiating
352          db = attrs["dbconn"]          # the db shapestore is None which we currently can't easily use
353          tablename = attrs["tablename"]          # in check_attrs
354          self.idmap[ID] = self.theSession.OpenDBShapeStore(db, tablename)          geometry_column = attrs["geometry_column"]
355            if not geometry_column:
356                geometry_column = None
357            dbopen = self.theSession.OpenDBShapeStore
358            self.idmap[attrs["id"]] = dbopen(attrs["dbconn"], attrs["tablename"],
359                                             id_column = attrs["id_column"],
360                                             geometry_column=geometry_column)
361    
362      def start_fileshapesource(self, name, qname, attrs):      def start_fileshapesource(self, name, qname, attrs):
363          attrs = self.check_attrs(name, attrs,          attrs = self.check_attrs(name, attrs,
# Line 316  class SessionLoader(XMLReader): Line 370  class SessionLoader(XMLReader):
370          filetype = attrs["filetype"]          filetype = attrs["filetype"]
371          if filetype != "shapefile":          if filetype != "shapefile":
372              raise LoadError("shapesource filetype %r not supported" % filetype)              raise LoadError("shapesource filetype %r not supported" % filetype)
373          self.idmap[ID] = self.theSession.OpenShapefile(filename)          self.idmap[ID] = self.open_shapefile(filename)
374    
375      def start_derivedshapesource(self, name, qname, attrs):      def start_derivedshapesource(self, name, qname, attrs):
376          attrs = self.check_attrs(name, attrs,          attrs = self.check_attrs(name, attrs,
# Line 427  class SessionLoader(XMLReader): Line 481  class SessionLoader(XMLReader):
481          if attrs.has_key((None, "shapestore")):          if attrs.has_key((None, "shapestore")):
482              store = self.idmap[attrs[(None, "shapestore")]]              store = self.idmap[attrs[(None, "shapestore")]]
483          else:          else:
484              store = self.theSession.OpenShapefile(filename)              store = self.open_shapefile(filename)
485    
486          self.aLayer = layer_class(title, store,          self.aLayer = layer_class(title, store,
487                                    fill = fill, stroke = stroke,                                    fill = fill, stroke = stroke,
488                                    lineWidth = stroke_width,                                    lineWidth = stroke_width,
# Line 537  class SessionLoader(XMLReader): Line 592  class SessionLoader(XMLReader):
592              parse_color(attrs.get((None, 'stroke'), "None")))              parse_color(attrs.get((None, 'stroke'), "None")))
593          self.cl_prop.SetLineWidth(          self.cl_prop.SetLineWidth(
594              int(attrs.get((None, 'stroke_width'), "0")))              int(attrs.get((None, 'stroke_width'), "0")))
595            self.cl_prop.SetSize(int(attrs.get((None, 'size'), "5")))
596          self.cl_prop.SetFill(parse_color(attrs.get((None, 'fill'), "None")))          self.cl_prop.SetFill(parse_color(attrs.get((None, 'fill'), "None")))
597    
598      def end_cldata(self, name, qname):      def end_cldata(self, name, qname):
# Line 569  class SessionLoader(XMLReader): Line 625  class SessionLoader(XMLReader):
625          pass          pass
626    
627    
628  def load_session(filename, db_connection_callback = None):  def load_session(filename, db_connection_callback = None,
629                               shapefile_callback = None):
630      """Load a Thuban session from the file object file      """Load a Thuban session from the file object file
631    
632      The db_connection_callback, if given should be a callable object      The db_connection_callback, if given should be a callable object
# Line 582  def load_session(filename, db_connection Line 639  def load_session(filename, db_connection
639      corrected and perhaps additional parameters like a password or None      corrected and perhaps additional parameters like a password or None
640      to indicate that the user cancelled.      to indicate that the user cancelled.
641      """      """
642      handler = SessionLoader(db_connection_callback)      handler = SessionLoader(db_connection_callback, shapefile_callback)
643      handler.read(filename)      handler.read(filename)
644    
645      session = handler.theSession      session = handler.theSession

Legend:
Removed from v.2034  
changed lines
  Added in v.2446

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26