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

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

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

revision 391 by jonathan, Mon Feb 10 15:26:11 2003 UTC revision 2384 by jan, Thu Oct 7 14:24:24 2004 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001, 2002 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]> (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.
# Line 11  Functions to save a session to a file Line 13  Functions to save a session to a file
13  """  """
14    
15  __version__ = "$Revision$"  __version__ = "$Revision$"
16    # $Source$
17    # $Id$
18    
19  import os  import os
 import string  
20    
21  import Thuban.Lib.fileutil  import Thuban.Lib.fileutil
22    
23  from Thuban.Model.color import Color  from Thuban.Model.layer import Layer, RasterLayer
24    
25  #  from Thuban.Model.classification import \
26  # one level of indention      ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap
27  #  from Thuban.Model.transientdb import AutoTransientTable, TransientJoinedTable
28  TAB = "    "  from Thuban.Model.table import DBFTable, FIELDTYPE_STRING
29    from Thuban.Model.data import DerivedShapeStore, ShapefileStore, \
30                                  SHAPETYPE_POINT
31    
32    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.
# Line 36  def relative_filename(dir, filename): Line 44  def relative_filename(dir, filename):
44      else:      else:
45          return filename          return filename
46    
47  def escape(data):  
48      """Escape &, \", ', <, and > in a string of data.  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      data = string.replace(data, "&", "&amp;")      if os.name == "posix":
57      data = string.replace(data, "<", "&lt;")          return filename
58      data = string.replace(data, ">", "&gt;")      elif os.name == "nt":
59      data = string.replace(data, '"', "&quot;")          return "/".join(filename.split("\\"))
60      data = string.replace(data, "'", "&apos;")      else:
61      return data          raise RuntimeError("Unsupported platform for unify_filename: %s"
62                               % os.name)
63    
64    def sort_data_stores(stores):
65        """Return a topologically sorted version of the sequence of data containers
66    
67        The list is sorted so that data containers that depend on other data
68        containers have higher indexes than the containers they depend on.
69        """
70        if not stores:
71            return []
72        processed = {}
73        result = []
74        todo = stores[:]
75        while todo:
76            # It doesn't really matter which if the items of todo is
77            # processed next, but if we take the first one, the order is
78            # preserved to some degree which makes writing some of the test
79            # cases easier.
80            container = todo.pop(0)
81            if id(container) in processed:
82                continue
83            deps = [dep for dep in container.Dependencies()
84                        if id(dep) not in processed]
85            if deps:
86                todo.append(container)
87                todo.extend(deps)
88            else:
89                result.append(container)
90                processed[id(container)] = 1
91        return result
92    
93  class Saver:  
94    class SessionSaver(XMLWriter):
95    
96      """Class to serialize a session into an XML file.      """Class to serialize a session into an XML file.
97    
98      Applications built on top of Thuban may derive from this class and      Applications built on top of Thuban may derive from this class and
99      override or extend the methods to save additinal information. This      override or extend the methods to save additional information. This
100      additional information should take the form of additional attributes      additional information should take the form of additional attributes
101      or elements whose names are prefixed with a namespace. To define a      or elements whose names are prefixed with a namespace. To define a
102      namespace derived classes should extend the write_session method to      namespace derived classes should extend the write_session method to
# Line 60  class Saver: Line 105  class Saver:
105    
106    
107      def __init__(self, session):      def __init__(self, session):
108            XMLWriter.__init__(self)
109          self.session = session          self.session = session
110            # Map object ids to the ids used in the thuban files
111            self.idmap = {}
112    
113      def write(self, file_or_filename):      def get_id(self, obj):
114          """Write the session to a file.          """Return the id used in the thuban file for the object obj"""
115            return self.idmap.get(id(obj))
116          The argument may be either a file object or a filename. If it's  
117          a filename, the file will be opened for writing. Files of      def define_id(self, obj, value = None):
118          shapefiles will be stored as filenames relative to the directory          if value is None:
119          the file is stored in (as given by os.path.dirname(filename)) if              value = "D" + str(id(obj))
120          they have a common parent directory other than the root          self.idmap[id(obj)] = value
121          directory.          return value
122    
123          If the argument is a file object (which is determined by the      def has_id(self, obj):
124          presence of a write method) all filenames will be absolut          return self.idmap.has_key(id(obj))
125          filenames.  
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          # keep track of how many levels of indentation to write      def write(self, file_or_filename):
136          self.indent_level = 0          XMLWriter.write(self, file_or_filename)
         # track whether an element is currently open. see open_element().  
         self.element_open = 0  
   
         if hasattr(file_or_filename, "write"):  
             # it's a file object  
             self.file = file_or_filename  
             self.dir = ""  
         else:  
             filename = file_or_filename  
             self.dir = os.path.dirname(filename)  
             self.file = open(filename, 'w')  
         self.write_header()  
         self.write_session(self.session)  
   
         if self.indent_level != 0:  
             raise ValueError("indent_level still positive!")  
   
     def write_attribs(self, attrs):  
         for name, value in attrs.items():  
             if isinstance(value, Color):  
                 value = value.hex()  
             else:  
                 value = str(value)  
             self.file.write(' %s="%s"' % (escape(name), escape(value)))  
       
     def open_element(self, element, attrs = {}):  
   
         #  
         # we note when an element is opened so that if two open_element()  
         # calls are made successively we can end the currently open  
         # tag and will later write a proper close tag. otherwise,  
         # if a close_element() call is made directly after an open_element()  
         # call we will close the tag with a />  
         #  
         if self.element_open == 1:  
             self.file.write(">\n")  
   
         self.element_open = 1  
   
         # Helper function to write an element open tag with attributes  
         self.file.write("%s<%s" % (TAB*self.indent_level, element))  
         self.write_attribs(attrs)  
   
         self.indent_level += 1  
   
     def close_element(self, element):  
         self.indent_level -= 1  
         if self.indent_level < 0:  
             raise ValueError("close_element called too many times!")  
   
         # see open_element() for an explanation  
         if self.element_open == 1:  
             self.element_open = 0  
             self.file.write("/>\n")  
         else:  
             self.file.write("%s</%s>\n" % (TAB*self.indent_level, element))  
137    
138      def write_element(self, element, attrs = {}):          self.write_header("session", "thuban-1.1.dtd")
139          """write an element that won't need a closing tag"""          self.write_session(self.session)
140          self.open_element(element, attrs)          self.close()
         self.close_element(element)  
   
     def write_header(self):  
         """Write the XML header"""  
         self.file.write('<?xml version="1.0" encoding="UTF-8"?>\n')  
         self.file.write('<!DOCTYPE session SYSTEM "thuban.dtd">\n')  
141    
142      def write_session(self, session, attrs = None, namespaces = ()):      def write_session(self, session, attrs = None, namespaces = ()):
143          """Write the session and its contents          """Write the session and its contents
# Line 167  class Saver: Line 160  class Saver:
160          attrs["title"] = session.title          attrs["title"] = session.title
161          for name, uri in namespaces:          for name, uri in namespaces:
162              attrs["xmlns:" + name] = uri              attrs["xmlns:" + name] = uri
163            # default name space
164            attrs["xmlns"] = \
165                   "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)
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):
187            containers = sort_data_stores(session.DataContainers())
188            for container in containers:
189                if isinstance(container, AutoTransientTable):
190                    # AutoTransientTable instances are invisible in the
191                    # thuban files. They're only used internally. To make
192                    # sure that containers depending on AutoTransientTable
193                    # instances refer to the right real containers we give
194                    # the AutoTransientTable instances the same id as the
195                    # source they depend on.
196                    self.define_id(container,
197                                   self.get_id(container.Dependencies()[0]))
198                    continue
199    
200                idvalue = self.define_id(container)
201                if isinstance(container, ShapefileStore):
202                    self.define_id(container.Table(), idvalue)
203                    filename = self.prepare_filename(container.FileName())
204                    self.write_element("fileshapesource",
205                                       {"id": idvalue, "filename": filename,
206                                        "filetype": "shapefile"})
207                elif isinstance(container, DerivedShapeStore):
208                    shapesource, table = container.Dependencies()
209                    self.write_element("derivedshapesource",
210                                       {"id": idvalue,
211                                        "shapesource": self.get_id(shapesource),
212                                        "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):
224                    filename = self.prepare_filename(container.FileName())
225                    self.write_element("filetable",
226                                       {"id": idvalue,
227                                        "title": container.Title(),
228                                        "filename": filename,
229                                        "filetype": "DBF"})
230                elif isinstance(container, TransientJoinedTable):
231                    left, right = container.Dependencies()
232                    left_field = container.left_field
233                    right_field = container.right_field
234                    self.write_element("jointable",
235                                       {"id": idvalue,
236                                        "title": container.Title(),
237                                        "right": self.get_id(right),
238                                        "rightcolumn": right_field,
239                                        "left": self.get_id(left),
240                                        "leftcolumn": left_field,
241                                        "jointype": container.JoinType()})
242                else:
243                    raise ValueError("Can't handle container %r" % container)
244    
245    
246      def write_map(self, map):      def write_map(self, map):
247          """Write the map and its contents.          """Write the map and its contents.
248    
# Line 180  class Saver: Line 251  class Saver:
251          element, call write_layer for each layer contained in the map          element, call write_layer for each layer contained in the map
252          and finally call write_label_layer to write the label layer.          and finally call write_label_layer to write the label layer.
253          """          """
254          write = self.file.write          self.open_element('map title="%s"' % self.encode(map.title))
         self.open_element('map title="%s"' % escape(map.title))  
255          self.write_projection(map.projection)          self.write_projection(map.projection)
256          for layer in map.Layers():          for layer in map.Layers():
257              self.write_layer(layer)              self.write_layer(layer)
# Line 192  class Saver: Line 262  class Saver:
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")              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"' % escape(param))                  self.write_element('parameter value="%s"' %
272                                       self.encode(param))
273              self.close_element("projection")              self.close_element("projection")
274    
275      def write_layer(self, layer, attrs = None):      def write_layer(self, layer, attrs = None):
# Line 204  class Saver: Line 279  class Saver:
279          given, should be a mapping from attribute names to attribute          given, should be a mapping from attribute names to attribute
280          values. The values should not be XML-escaped yet.          values. The values should not be XML-escaped yet.
281          """          """
         lc = layer.classification  
282    
283          if attrs is None:          if attrs is None:
284              attrs = {}              attrs = {}
         attrs["title"] = layer.title  
         attrs["filename"] = relative_filename(self.dir, layer.filename)  
         attrs["stroke_width"] = str(lc.GetDefaultStrokeWidth())  
         fill = lc.GetDefaultFill()  
         if fill is None:  
             attrs["fill"] = "None"  
         else:  
             attrs["fill"] = fill.hex()  
         stroke = lc.GetDefaultStroke()  
         if stroke is None:  
             attrs["stroke"] = "None"  
         else:  
             attrs["stroke"] = stroke.hex()  
285    
286          self.open_element("layer", attrs)          attrs["title"]   = layer.title
287          self.write_classification(layer)          attrs["visible"] = ("false", "true")[int(layer.Visible())]
288          self.close_element("layer")  
289            if isinstance(layer, Layer):
290                attrs["shapestore"]   = self.get_id(layer.ShapeStore())
291    
292                lc = layer.GetClassification()
293                attrs["stroke"] = lc.GetDefaultLineColor().hex()
294                attrs["stroke_width"] = str(lc.GetDefaultLineWidth())
295                attrs["fill"] = lc.GetDefaultFill().hex()
296    
297                self.open_element("layer", attrs)
298                self.write_projection(layer.GetProjection())
299                self.write_classification(layer)
300                self.close_element("layer")
301            elif isinstance(layer, RasterLayer):
302                attrs["filename"] = self.prepare_filename(layer.filename)
303                self.open_element("rasterlayer", attrs)
304                self.write_projection(layer.GetProjection())
305                self.close_element("rasterlayer")
306    
307      def write_classification(self, layer, attrs = None):      def write_classification(self, layer, attrs = None):
308            """Write Classification information."""
309    
310          if attrs is None:          if attrs is None:
311              attrs = {}              attrs = {}
312    
313          lc = layer.classification          lc = layer.GetClassification()
314    
315            field = layer.GetClassificationColumn()
316    
         field = lc.field  
   
317          #          #
318          # there isn't a classification of anything          # there isn't a classification of anything so do nothing
         # so don't do anything  
319          #          #
320          if field is None: return          if field is None: return
321    
322          attrs["field"] = field          attrs["field"] = field
323            attrs["field_type"] = str(layer.GetFieldType(field))
324          self.open_element("classification", attrs)          self.open_element("classification", attrs)
325    
326          def write_class_data(data):          for g in lc:
327              dict = {'stroke'      : data.GetStroke(),              if isinstance(g, ClassGroupDefault):
328                      'stroke_width': data.GetStrokeWidth(),                  open_el  = 'clnull label="%s"' % self.encode(g.GetLabel())
329                      'fill'        : data.GetFill()}                  close_el = 'clnull'
330              self.write_element("cldata", dict)              elif isinstance(g, ClassGroupSingleton):
331                    if layer.GetFieldType(field) == FIELDTYPE_STRING:
332                        value = self.encode(g.GetValue())
333                    else:
334                        value = str(g.GetValue())
335                    open_el  = 'clpoint label="%s" value="%s"' \
336                               % (self.encode(g.GetLabel()), value)
337                    close_el = 'clpoint'
338                elif isinstance(g, ClassGroupRange):
339                    open_el  = 'clrange label="%s" range="%s"' \
340                              % (self.encode(g.GetLabel()), str(g.GetRange()))
341                    close_el = 'clrange'
342                else:
343                    assert False, _("Unsupported group type in classification")
344                    continue
345    
346          self.open_element("clnull")              data = g.GetProperties()
347          write_class_data(lc.GetDefaultData())              dict = {'stroke'      : data.GetLineColor().hex(),
348          self.close_element("clnull")                      'stroke_width': str(data.GetLineWidth()),
349                                    'fill'        : data.GetFill().hex()}
350          if lc.points != {}:  
351              for value, data in lc.points.items():              # only for point layers write the size attribute
352                  self.open_element('clpoint value="%s"' % (escape(str(value))))              if layer.ShapeType() == SHAPETYPE_POINT:
353                  write_class_data(data)                  dict['size'] =  str(data.GetSize())
354                  self.close_element('clpoint')  
355                          self.open_element(open_el)
356          if lc.ranges != []:              self.write_element("cldata", dict)
357              for p in lc.ranges:              self.close_element(close_el)
                 self.open_element('clrange min="%s" max="%s"'  
                     % (escape(str(p[0])), escape(str(p[1]))))  
                 write_class_data(p[2])  
                 self.close_element('clrange')  
358    
359          self.close_element("classification")          self.close_element("classification")
360    
# Line 277  class Saver: Line 367  class Saver:
367              for label in labels:              for label in labels:
368                  self.write_element(('label x="%g" y="%g" text="%s"'                  self.write_element(('label x="%g" y="%g" text="%s"'
369                                      ' halign="%s" valign="%s"')                                      ' halign="%s" valign="%s"')
370                                  % (label.x, label.y, label.text, label.halign,                                  % (label.x, label.y,
371                                       self.encode(label.text),
372                                       label.halign,
373                                     label.valign))                                     label.valign))
374              self.close_element('labellayer')              self.close_element('labellayer')
375    
# Line 290  def save_session(session, file, saver_cl Line 382  def save_session(session, file, saver_cl
382    
383      The optional argument saver_class is the class to use to serialize      The optional argument saver_class is the class to use to serialize
384      the session. By default or if it's None, the saver class will be      the session. By default or if it's None, the saver class will be
385      Saver.      SessionSaver.
386    
387      If writing the session is successful call the session's      If writing the session is successful call the session's
388      UnsetModified method      UnsetModified method
389      """      """
390      if saver_class is None:      if saver_class is None:
391          saver_class = Saver          saver_class = SessionSaver
392      saver = saver_class(session)      saver = saver_class(session)
393      saver.write(file)      saver.write(file)
394    

Legend:
Removed from v.391  
changed lines
  Added in v.2384

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26