/[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 1452 by bh, Fri Jul 18 12:57:59 2003 UTC revision 2658 by jan, Wed Jul 27 21:50:30 2005 UTC
# Line 1  Line 1 
1  # Copyright (C) 2001, 2002, 2003 by Intevation GmbH  # Copyright (C) 2001, 2002, 2003, 2004, 2005 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 38  from Thuban.Model.data import DerivedSha Line 39  from Thuban.Model.data import DerivedSha
39  from Thuban.Model.table import DBFTable  from Thuban.Model.table import DBFTable
40  from Thuban.Model.transientdb import TransientJoinedTable  from Thuban.Model.transientdb import TransientJoinedTable
41    
42    from Thuban.Model.xmlreader import XMLReader
43    import resource
44    
45    import postgisdb
46    
47  class LoadError(Exception):  class LoadError(Exception):
     pass  
48    
49  from Thuban.Model.xmlreader import XMLReader      """Exception raised when the thuban file is corrupted
50  import resource  
51        Not all cases of corrupted thuban files will lead to this exception
52        but those that are found by checks in the loading code itself are.
53        """
54    
55    
56    class LoadCancelled(Exception):
57    
58        """Exception raised to indicate that loading was interrupted by the user"""
59    
60    
61  def parse_color(color):  def parse_color(color):
62      """Return the color object for the string color.      """Return the color object for the string color.
# Line 87  class AttrDesc: Line 101  class AttrDesc:
101    
102  class SessionLoader(XMLReader):  class SessionLoader(XMLReader):
103    
104      def __init__(self):      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
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 101  class SessionLoader(XMLReader): Line 118  class SessionLoader(XMLReader):
118    
119          dispatchers = {          dispatchers = {
120              'session'       : ("start_session",        "end_session"),              'session'       : ("start_session",        "end_session"),
121    
122                'dbconnection': ("start_dbconnection", None),
123    
124                'dbshapesource': ("start_dbshapesource", None),
125              'fileshapesource': ("start_fileshapesource", None),              'fileshapesource': ("start_fileshapesource", None),
126              'derivedshapesource': ("start_derivedshapesource", None),              'derivedshapesource': ("start_derivedshapesource", None),
127              'filetable': ("start_filetable", None),              'filetable': ("start_filetable", None),
# Line 122  class SessionLoader(XMLReader): Line 143  class SessionLoader(XMLReader):
143    
144          # all dispatchers should be used for the 0.8 and 0.9 namespaces too          # all dispatchers should be used for the 0.8 and 0.9 namespaces too
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",
148                          "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)))
# Line 155  class SessionLoader(XMLReader): Line 191  class SessionLoader(XMLReader):
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    
# Line 173  class SessionLoader(XMLReader): Line 208  class SessionLoader(XMLReader):
208                        defined earlier in the .thuban file. Look it up                        defined earlier in the .thuban file. Look it up
209                        self.idmap. If it's the ID of a shapestore the                        self.idmap. If it's the ID of a shapestore the
210                        value will be the table of the shapestore.                        value will be the table of the shapestore.
211    
212               'idref' -- The attribute is the id of an object defined
213                          earlier in the .thuban file. Look it up self.idmap
214    
215               'ascii' -- The attribute is converted to a bytestring with
216                          ascii encoding.
217    
218               a callable -- The attribute value is passed to the callable
219                             and the return value is used as the converted
220                             value
221    
222            If no conversion is specified for an attribute it is converted
223            with self.encode.
224          """          """
225          normalized = {}          normalized = {}
226    
227          for d in descr:          for d in descr:
228              if d.required and not attrs.has_key(d.fullname):              if d.required and not attrs.has_key(d.fullname):
229                  pass                  raise LoadError("Element %s requires an attribute %r"
230              #raise LoadError("Element %s requires an attribute %r"                                  % (element, d.name))
             #                    % (element, d.name))  
231              value = attrs.get(d.fullname, d.default)              value = attrs.get(d.fullname, d.default)
232    
233              if d.conversion == "shapesource":              if d.conversion in ("idref", "shapesource"):
234                  if value in self.idmap:                  if value in self.idmap:
235                      value = self.idmap[value]                      value = self.idmap[value]
236                  else:                  else:
# Line 201  class SessionLoader(XMLReader): Line 248  class SessionLoader(XMLReader):
248                                      % (element, d.name))                                      % (element, d.name))
249              elif d.conversion == "filename":              elif d.conversion == "filename":
250                  value = os.path.abspath(os.path.join(self.GetDirectory(),                  value = os.path.abspath(os.path.join(self.GetDirectory(),
251                                                       value))                                                       self.encode(value)))
252                elif d.conversion == "ascii":
253                    value = value.encode("ascii")
254                elif d.conversion:
255                    # Assume it's a callable
256                    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                    else:
305                        raise
306            return store
307    
308        def start_dbconnection(self, name, qname, attrs):
309            attrs = self.check_attrs(name, attrs,
310                                     [AttrDesc("id", True),
311                                      AttrDesc("dbtype", True),
312                                      AttrDesc("host", False, ""),
313                                      AttrDesc("port", False, ""),
314                                      AttrDesc("user", False, ""),
315                                      AttrDesc("dbname", True)])
316            ID = attrs["id"]
317            dbtype = attrs["dbtype"]
318            if dbtype != "postgis":
319                raise LoadError("dbtype %r not supported" % filetype)
320    
321            del attrs["id"]
322            del attrs["dbtype"]
323    
324            # Try to open the connection and if it fails ask the user for
325            # the correct parameters repeatedly.
326            # FIXME: it would be better not to insist on getting a
327            # connection here. We should handle this more like the raster
328            # images where the layers etc still are created but are not
329            # drawn in case Thuban can't use the data for various reasons
330            while 1:
331                try:
332                    conn = postgisdb.PostGISConnection(**attrs)
333                    break
334                except postgisdb.ConnectionError, val:
335                    if self.db_connection_callback is not None:
336                        attrs = self.db_connection_callback(attrs, str(val))
337                        if attrs is None:
338                            raise LoadCancelled
339                    else:
340                        raise
341    
342            self.idmap[ID] = conn
343            self.theSession.AddDBConnection(conn)
344    
345        def start_dbshapesource(self, name, qname, attrs):
346            attrs = self.check_attrs(name, attrs,
347                                     [AttrDesc("id", True),
348                                      AttrDesc("dbconn", True,
349                                               conversion = "idref"),
350                                      AttrDesc("tablename", True,
351                                               conversion = "ascii"),
352                                      # id_column and geometry_column were
353                                      # newly introduced with thuban-1.1.dtd
354                                      # where they're required.  Since we
355                                      # support the older formats too we
356                                      # have them optional here.
357                                      AttrDesc("id_column", False, "gid",
358                                               conversion = "ascii"),
359                                      AttrDesc("geometry_column", False,
360                                               conversion = "ascii")])
361            # The default value of geometry_column to use when instantiating
362            # the db shapestore is None which we currently can't easily use
363            # in check_attrs
364            geometry_column = attrs["geometry_column"]
365            if not geometry_column:
366                geometry_column = None
367            dbopen = self.theSession.OpenDBShapeStore
368            self.idmap[attrs["id"]] = dbopen(attrs["dbconn"], attrs["tablename"],
369                                             id_column = attrs["id_column"],
370                                             geometry_column=geometry_column)
371    
372      def start_fileshapesource(self, name, qname, attrs):      def start_fileshapesource(self, name, qname, attrs):
373          attrs = self.check_attrs(name, attrs,          attrs = self.check_attrs(name, attrs,
374                                    [AttrDesc("id", True),                                    [AttrDesc("id", True),
# Line 217  class SessionLoader(XMLReader): Line 380  class SessionLoader(XMLReader):
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,
# Line 284  class SessionLoader(XMLReader): Line 447  class SessionLoader(XMLReader):
447          self.aMap = None          self.aMap = None
448    
449      def start_projection(self, name, qname, attrs):      def start_projection(self, name, qname, attrs):
450          self.ProjectionName = self.encode(attrs.get((None, 'name'), None))          attrs = self.check_attrs(name, attrs,
451          self.ProjectionParams = [ ]                                   [AttrDesc("name", conversion=self.encode),
452                                      AttrDesc("epsg", default=None,
453                                               conversion=self.encode)])
454            self.projection_name = attrs["name"]
455            self.projection_epsg = attrs["epsg"]
456            self.projection_params = [ ]
457    
458      def end_projection(self, name, qname):      def end_projection(self, name, qname):
459          if self.aLayer is not None:          if self.aLayer is not None:
# Line 296  class SessionLoader(XMLReader): Line 464  class SessionLoader(XMLReader):
464              assert False, "projection tag out of context"              assert False, "projection tag out of context"
465              pass              pass
466    
467          obj.SetProjection(          obj.SetProjection(Projection(self.projection_params,
468              Projection(self.ProjectionParams, self.ProjectionName))                                       self.projection_name,
469                                         epsg = self.projection_epsg))
470    
471      def start_parameter(self, name, qname, attrs):      def start_parameter(self, name, qname, attrs):
472          s = attrs.get((None, 'value'))          s = attrs.get((None, 'value'))
473          s = str(s) # we can't handle unicode in proj          s = str(s) # we can't handle unicode in proj
474          self.ProjectionParams.append(s)          self.projection_params.append(s)
475    
476      def start_layer(self, name, qname, attrs, layer_class = Layer):      def start_layer(self, name, qname, attrs, layer_class = Layer):
477          """Start a layer          """Start a layer
# Line 322  class SessionLoader(XMLReader): Line 491  class SessionLoader(XMLReader):
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,
# Line 338  class SessionLoader(XMLReader): Line 508  class SessionLoader(XMLReader):
508          filename = os.path.join(self.GetDirectory(), filename)          filename = os.path.join(self.GetDirectory(), filename)
509          filename = self.encode(filename)          filename = self.encode(filename)
510          visible  = self.encode(attrs.get((None, 'visible'), "true")) != "false"          visible  = self.encode(attrs.get((None, 'visible'), "true")) != "false"
511            opacity  = float(attrs.get((None, 'opacity'), "1"))
512            masktype = str(attrs.get((None, 'masktype'), "bit"))
513    
514          self.aLayer = layer_class(title, filename, visible = visible)          masktypes = {"none": layer_class.MASK_NONE,
515                         "bit":  layer_class.MASK_BIT,
516                         "alpha": layer_class.MASK_ALPHA}
517    
518            self.aLayer = layer_class(title, filename,
519                                      visible = visible,
520                                      opacity = opacity,
521                                      masktype = masktypes[masktype])
522    
523      def end_rasterlayer(self, name, qname):      def end_rasterlayer(self, name, qname):
524          self.aMap.AddLayer(self.aLayer)          self.aMap.AddLayer(self.aLayer)
525          self.aLayer = None          self.aLayer = None
526    
527      def start_classification(self, name, qname, attrs):      def start_classification(self, name, qname, attrs):
528          field = attrs.get((None, 'field'), None)          # field and field_type are optional because the classification
529            # can also be empty, ie. have only a default.
530            attrs = self.check_attrs(name, attrs,
531                                     [AttrDesc("field", False),
532                                      AttrDesc("field_type", False)])
533    
534            field = attrs["field"]
535            fieldType = attrs["field_type"]
536    
537            if field == "": return # no need to set classification column.
538    
         fieldType = attrs.get((None, 'field_type'), None)  
539          dbFieldType = self.aLayer.GetFieldType(field)          dbFieldType = self.aLayer.GetFieldType(field)
540    
541          if fieldType != dbFieldType:          if fieldType != dbFieldType:
# Line 397  class SessionLoader(XMLReader): Line 584  class SessionLoader(XMLReader):
584          del self.cl_group, self.cl_prop          del self.cl_group, self.cl_prop
585    
586      def start_clrange(self, name, qname, attrs):      def start_clrange(self, name, qname, attrs):
587            attrs = self.check_attrs(name, attrs,
588                                     [AttrDesc("range", False, None),
589                                      AttrDesc("min", False, None),
590                                      AttrDesc("max", False, None)])
591    
592          range = attrs.get((None, 'range'), None)          range = attrs['range']
593          # for backward compatibility (min/max are not saved)          # for backward compatibility (min/max are not saved)
594          min   = attrs.get((None, 'min'), None)          min   = attrs['min']
595          max   = attrs.get((None, 'max'), None)          max   = attrs['max']
596    
597          try:          try:
598              if range is not None:              if range is not None:
# Line 429  class SessionLoader(XMLReader): Line 620  class SessionLoader(XMLReader):
620              parse_color(attrs.get((None, 'stroke'), "None")))              parse_color(attrs.get((None, 'stroke'), "None")))
621          self.cl_prop.SetLineWidth(          self.cl_prop.SetLineWidth(
622              int(attrs.get((None, 'stroke_width'), "0")))              int(attrs.get((None, 'stroke_width'), "0")))
623            self.cl_prop.SetSize(int(attrs.get((None, 'size'), "5")))
624          self.cl_prop.SetFill(parse_color(attrs.get((None, 'fill'), "None")))          self.cl_prop.SetFill(parse_color(attrs.get((None, 'fill'), "None")))
625    
626      def end_cldata(self, name, qname):      def end_cldata(self, name, qname):
# Line 438  class SessionLoader(XMLReader): Line 630  class SessionLoader(XMLReader):
630          self.aLayer = self.aMap.LabelLayer()          self.aLayer = self.aMap.LabelLayer()
631    
632      def start_label(self, name, qname, attrs):      def start_label(self, name, qname, attrs):
633          x = float(attrs[(None, 'x')])          attrs = self.check_attrs(name, attrs,
634          y = float(attrs[(None, 'y')])                                   [AttrDesc("x", True, conversion = float),
635          text = self.encode(attrs[(None, 'text')])                                    AttrDesc("y", True, conversion = float),
636          halign = attrs[(None, 'halign')]                                    AttrDesc("text", True),
637          valign = attrs[(None, 'valign')]                                    AttrDesc("halign", True,
638                                               conversion = "ascii"),
639                                      AttrDesc("valign", True,
640                                               conversion = "ascii")])
641            x = attrs['x']
642            y = attrs['y']
643            text = attrs['text']
644            halign = attrs['halign']
645            valign = attrs['valign']
646            if halign not in ("left", "center", "right"):
647                raise LoadError("Unsupported halign value %r" % halign)
648            if valign not in ("top", "center", "bottom"):
649                raise LoadError("Unsupported valign value %r" % valign)
650          self.aLayer.AddLabel(x, y, text, halign = halign, valign = valign)          self.aLayer.AddLabel(x, y, text, halign = halign, valign = valign)
651    
652      def characters(self, chars):      def characters(self, chars):
653          pass          pass
654    
655    
656  def load_session(filename):  def load_session(filename, db_connection_callback = None,
657      """Load a Thuban session from the file object file"""                             shapefile_callback = None):
658        """Load a Thuban session from the file object file
659      handler = SessionLoader()  
660        The db_connection_callback, if given should be a callable object
661        that can be called like this:
662           db_connection_callback(params, message)
663    
664        where params is a dictionary containing the known connection
665        parameters and message is a string with a message why the connection
666        failed. db_connection_callback should return a new dictionary with
667        corrected and perhaps additional parameters like a password or None
668        to indicate that the user cancelled.
669        """
670        handler = SessionLoader(db_connection_callback, shapefile_callback)
671      handler.read(filename)      handler.read(filename)
672    
673      session = handler.theSession      session = handler.theSession
674      # Newly loaded session aren't modified      # Newly loaded session aren't modified
675      session.UnsetModified()      session.UnsetModified()
676    
677        handler.Destroy()
678    
679      return session      return session
680    

Legend:
Removed from v.1452  
changed lines
  Added in v.2658

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26