/[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 268 by bh, Thu Aug 22 10:25:43 2002 UTC revision 1452 by bh, Fri Jul 18 12:57:59 2003 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001, 2002 by Intevation GmbH  # Copyright (c) 2001, 2002, 2003 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]>
6  #  #
7  # This program is free software under the GPL (>=v2)  # This program is free software under the GPL (>=v2)
8  # Read the file COPYING coming with Thuban for details.  # Read the file COPYING coming with Thuban for details.
# Line 13  Functions to save a session to a file Line 14  Functions to save a session to a file
14  __version__ = "$Revision$"  __version__ = "$Revision$"
15    
16  import os  import os
 import string  
17    
18  import Thuban.Lib.fileutil  import Thuban.Lib.fileutil
19    
20    from Thuban.Model.layer import Layer, RasterLayer
21    
22    from Thuban.Model.classification import \
23        ClassGroupDefault, ClassGroupSingleton, ClassGroupRange, ClassGroupMap
24    from Thuban.Model.transientdb import AutoTransientTable, TransientJoinedTable
25    from Thuban.Model.table import DBFTable, FIELDTYPE_STRING
26    from Thuban.Model.data import DerivedShapeStore, ShapefileStore
27    
28    from Thuban.Model.xmlwriter import XMLWriter
29    
30  def relative_filename(dir, filename):  def relative_filename(dir, filename):
31      """Return a filename relative to dir for the absolute file name absname.      """Return a filename relative to dir for the absolute file name absname.
32    
# Line 29  def relative_filename(dir, filename): Line 39  def relative_filename(dir, filename):
39      else:      else:
40          return filename          return filename
41    
42  def escape(data):  
43      """Escape &, \", ', <, and > in a string of data.  def sort_data_stores(stores):
44        """Return a topologically sorted version of the sequence of data containers
45    
46        The list is sorted so that data containers that depend on other data
47        containers have higher indexes than the containers they depend on.
48      """      """
49      data = string.replace(data, "&", "&amp;")      if not stores:
50      data = string.replace(data, "<", "&lt;")          return []
51      data = string.replace(data, ">", "&gt;")      processed = {}
52      data = string.replace(data, '"', "&quot;")      result = []
53      data = string.replace(data, "'", "&apos;")      todo = stores[:]
54      return data      while todo:
55            # It doesn't really matter which if the items of todo is
56            # processed next, but if we take the first one, the order is
57            # preserved to some degree which makes writing some of the test
58            # cases easier.
59            container = todo.pop(0)
60            if id(container) in processed:
61                continue
62            deps = [dep for dep in container.Dependencies()
63                        if id(dep) not in processed]
64            if deps:
65                todo.append(container)
66                todo.extend(deps)
67            else:
68                result.append(container)
69                processed[id(container)] = 1
70        return result
71    
72  class Saver:  
73    class SessionSaver(XMLWriter):
74    
75      """Class to serialize a session into an XML file.      """Class to serialize a session into an XML file.
76    
77      Applications built on top of Thuban may derive from this class and      Applications built on top of Thuban may derive from this class and
78      override or extend the methods to save additinal information. This      override or extend the methods to save additional information. This
79      additional information should take the form of additional attributes      additional information should take the form of additional attributes
80      or elements whose names are prefixed with a namespace. To define a      or elements whose names are prefixed with a namespace. To define a
81      namespace derived classes should extend the write_session method to      namespace derived classes should extend the write_session method to
82      pass the namespaces to the default implementation.      pass the namespaces to the default implementation.
83      """      """
84    
85    
86      def __init__(self, session):      def __init__(self, session):
87            XMLWriter.__init__(self)
88          self.session = session          self.session = session
89            # Map object ids to the ids used in the thuban files
90            self.idmap = {}
91    
92      def write(self, file_or_filename):      def get_id(self, obj):
93          """Write the session to a file.          """Return the id used in the thuban file for the object obj"""
94            return self.idmap.get(id(obj))
95    
96        def define_id(self, obj, value = None):
97            if value is None:
98                value = "D" + str(id(obj))
99            self.idmap[id(obj)] = value
100            return value
101    
102          The argument may be either a file object or a filename. If it's      def has_id(self, obj):
103          a filename, the file will be opened for writing. Files of          return self.idmap.has_key(id(obj))
         shapefiles will be stored as filenames relative to the directory  
         the file is stored in (as given by os.path.dirname(filename)) if  
         they have a common parent directory other than the root  
         directory.  
   
         If the argument is a file object (which is determined by the  
         presence of a write method) all filenames will be absolut  
         filenames.  
         """  
         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)  
104    
105      def write_element(self, element, attrs, empty = 0, indentation = ""):      def write(self, file_or_filename):
106          # Helper function to write an element open tag with attributes          XMLWriter.write(self, file_or_filename)
         self.file.write("%s<%s" % (indentation, element))  
         for name, value in attrs.items():  
             self.file.write(' %s="%s"' % (escape(name), escape(value)))  
         if empty:  
             self.file.write("/>\n")  
         else:  
             self.file.write(">\n")  
107    
108      def write_header(self):          self.write_header("session", "thuban-0.9.dtd")
109          """Write the XML header"""          self.write_session(self.session)
110          write = self.file.write          self.close()
         write('<?xml version="1.0" encoding="UTF-8"?>\n')  
         write('<!DOCTYPE session SYSTEM "thuban.dtd">\n')  
111    
112      def write_session(self, session, attrs = None, namespaces = ()):      def write_session(self, session, attrs = None, namespaces = ()):
113          """Write the session and its contents          """Write the session and its contents
# Line 116  class Saver: Line 130  class Saver:
130          attrs["title"] = session.title          attrs["title"] = session.title
131          for name, uri in namespaces:          for name, uri in namespaces:
132              attrs["xmlns:" + name] = uri              attrs["xmlns:" + name] = uri
133          self.write_element("session", attrs)          # default name space
134            attrs["xmlns"] = \
135                   "http://thuban.intevation.org/dtds/thuban-0.9-dev.dtd"
136            self.open_element("session", attrs)
137            self.write_data_containers(session)
138          for map in session.Maps():          for map in session.Maps():
139              self.write_map(map)              self.write_map(map)
140          self.file.write('</session>\n')          self.close_element("session")
141    
142        def write_data_containers(self, session):
143            containers = sort_data_stores(session.DataContainers())
144            for container in containers:
145                if isinstance(container, AutoTransientTable):
146                    # AutoTransientTable instances are invisible in the
147                    # thuban files. They're only used internally. To make
148                    # sure that containers depending on AutoTransientTable
149                    # instances refer to the right real containers we give
150                    # the AutoTransientTable instances the same id as the
151                    # source they depend on.
152                    self.define_id(container,
153                                   self.get_id(container.Dependencies()[0]))
154                    continue
155    
156                idvalue = self.define_id(container)
157                if isinstance(container, ShapefileStore):
158                    self.define_id(container.Table(), idvalue)
159                    filename = relative_filename(self.dir, container.FileName())
160                    self.write_element("fileshapesource",
161                                       {"id": idvalue, "filename": filename,
162                                        "filetype": "shapefile"})
163                elif isinstance(container, DerivedShapeStore):
164                    shapesource, table = container.Dependencies()
165                    self.write_element("derivedshapesource",
166                                       {"id": idvalue,
167                                        "shapesource": self.get_id(shapesource),
168                                        "table": self.get_id(table)})
169                elif isinstance(container, DBFTable):
170                    filename = relative_filename(self.dir, container.FileName())
171                    self.write_element("filetable",
172                                       {"id": idvalue,
173                                        "title": container.Title(),
174                                        "filename": filename,
175                                        "filetype": "DBF"})
176                elif isinstance(container, TransientJoinedTable):
177                    left, right = container.Dependencies()
178                    left_field = container.left_field
179                    right_field = container.right_field
180                    self.write_element("jointable",
181                                       {"id": idvalue,
182                                        "title": container.Title(),
183                                        "right": self.get_id(right),
184                                        "rightcolumn": right_field,
185                                        "left": self.get_id(left),
186                                        "leftcolumn": left_field,
187                                        "jointype": container.JoinType()})
188                else:
189                    raise ValueError("Can't handle container %r" % container)
190    
191    
192      def write_map(self, map):      def write_map(self, map):
193          """Write the map and its contents.          """Write the map and its contents.
# Line 129  class Saver: Line 197  class Saver:
197          element, call write_layer for each layer contained in the map          element, call write_layer for each layer contained in the map
198          and finally call write_label_layer to write the label layer.          and finally call write_label_layer to write the label layer.
199          """          """
200          write = self.file.write          self.open_element('map title="%s"' % self.encode(map.title))
         write('\t<map title="%s">\n' % escape(map.title))  
201          self.write_projection(map.projection)          self.write_projection(map.projection)
202          for layer in map.Layers():          for layer in map.Layers():
203              self.write_layer(layer)              self.write_layer(layer)
204          self.write_label_layer(map.LabelLayer())          self.write_label_layer(map.LabelLayer())
205          write('\t</map>\n')          self.close_element('map')
206    
207      def write_projection(self, projection):      def write_projection(self, projection):
208          """Write the projection.          """Write the projection.
209          """          """
210          if projection and len(projection.params) > 0:          if projection and len(projection.params) > 0:
211              self.file.write('\t\t<projection>\n')              self.open_element("projection", {"name": projection.GetName()})
212              for param in projection.params:              for param in projection.params:
213                  self.file.write('\t\t\t<parameter value="%s"/>\n'                  self.write_element('parameter value="%s"' %
214                                  % escape(param))                                     self.encode(param))
215              self.file.write('\t\t</projection>\n')              self.close_element("projection")
216    
217      def write_layer(self, layer, attrs = None):      def write_layer(self, layer, attrs = None):
218          """Write the layer.          """Write the layer.
# Line 154  class Saver: Line 221  class Saver:
221          given, should be a mapping from attribute names to attribute          given, should be a mapping from attribute names to attribute
222          values. The values should not be XML-escaped yet.          values. The values should not be XML-escaped yet.
223          """          """
224    
225          if attrs is None:          if attrs is None:
226              attrs = {}              attrs = {}
227          attrs["title"] = layer.title  
228          attrs["filename"] = relative_filename(self.dir, layer.filename)          attrs["title"]   = layer.title
229          attrs["stroke_width"] = str(layer.stroke_width)          attrs["visible"] = ("false", "true")[int(layer.Visible())]
230          fill = layer.fill  
231          if fill is None:          if isinstance(layer, Layer):
232              attrs["fill"] = "None"              attrs["shapestore"]   = self.get_id(layer.ShapeStore())
233          else:  
234              attrs["fill"] = fill.hex()              lc = layer.GetClassification()
235          stroke = layer.stroke              attrs["stroke"] = lc.GetDefaultLineColor().hex()
236          if stroke is None:              attrs["stroke_width"] = str(lc.GetDefaultLineWidth())
237              attrs["stroke"] = "None"              attrs["fill"] = lc.GetDefaultFill().hex()
238          else:  
239              attrs["stroke"] = stroke.hex()              self.open_element("layer", attrs)
240          self.write_element("layer", attrs, empty = 1, indentation = "\t\t")              self.write_projection(layer.GetProjection())
241                self.write_classification(layer)
242                self.close_element("layer")
243            elif isinstance(layer, RasterLayer):
244                attrs["filename"] = relative_filename(self.dir, layer.filename)
245                self.open_element("rasterlayer", attrs)
246                self.write_projection(layer.GetProjection())
247                self.close_element("rasterlayer")
248    
249        def write_classification(self, layer, attrs = None):
250            """Write Classification information."""
251    
252            if attrs is None:
253                attrs = {}
254    
255            lc = layer.GetClassification()
256    
257            field = layer.GetClassificationColumn()
258    
259            #
260            # there isn't a classification of anything so do nothing
261            #
262            if field is None: return
263    
264            attrs["field"] = field
265            attrs["field_type"] = str(layer.GetFieldType(field))
266            self.open_element("classification", attrs)
267    
268            for g in lc:
269                if isinstance(g, ClassGroupDefault):
270                    open_el  = 'clnull label="%s"' % self.encode(g.GetLabel())
271                    close_el = 'clnull'
272                elif isinstance(g, ClassGroupSingleton):
273                    if layer.GetFieldType(field) == FIELDTYPE_STRING:
274                        value = self.encode(g.GetValue())
275                    else:
276                        value = str(g.GetValue())
277                    open_el  = 'clpoint label="%s" value="%s"' \
278                               % (self.encode(g.GetLabel()), value)
279                    close_el = 'clpoint'
280                elif isinstance(g, ClassGroupRange):
281                    open_el  = 'clrange label="%s" range="%s"' \
282                              % (self.encode(g.GetLabel()), str(g.GetRange()))
283                    close_el = 'clrange'
284                else:
285                    assert False, _("Unsupported group type in classification")
286                    continue
287    
288                data = g.GetProperties()
289                dict = {'stroke'      : data.GetLineColor().hex(),
290                        'stroke_width': str(data.GetLineWidth()),
291                        'fill'        : data.GetFill().hex()}
292    
293                self.open_element(open_el)
294                self.write_element("cldata", dict)
295                self.close_element(close_el)
296    
297            self.close_element("classification")
298    
299      def write_label_layer(self, layer):      def write_label_layer(self, layer):
300          """Write the label layer.          """Write the label layer.
301          """          """
302          labels = layer.Labels()          labels = layer.Labels()
303          if labels:          if labels:
304              self.file.write('\t\t<labellayer>\n')              self.open_element('labellayer')
305              for label in labels:              for label in labels:
306                  self.file.write(('\t\t\t<label x="%g" y="%g" text="%s"'                  self.write_element(('label x="%g" y="%g" text="%s"'
307                                   ' halign="%s" valign="%s"/>\n')                                      ' halign="%s" valign="%s"')
308                                  % (label.x, label.y, label.text, label.halign,                                  % (label.x, label.y,
309                                       self.encode(label.text),
310                                       label.halign,
311                                     label.valign))                                     label.valign))
312              self.file.write('\t\t</labellayer>\n')              self.close_element('labellayer')
313    
314    
315    
# Line 193  def save_session(session, file, saver_cl Line 320  def save_session(session, file, saver_cl
320    
321      The optional argument saver_class is the class to use to serialize      The optional argument saver_class is the class to use to serialize
322      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
323      Saver.      SessionSaver.
324    
325      If writing the session is successful call the session's      If writing the session is successful call the session's
326      UnsetModified method      UnsetModified method
327      """      """
328      if saver_class is None:      if saver_class is None:
329          saver_class = Saver          saver_class = SessionSaver
330      saver = saver_class(session)      saver = saver_class(session)
331      saver.write(file)      saver.write(file)
332    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26