/[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 454 by bh, Tue Mar 4 11:31:47 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 12  Functions to save a session to a file Line 13  Functions to save a session to a file
13    
14  __version__ = "$Revision$"  __version__ = "$Revision$"
15    
16    # fix for people using python2.1
17    from __future__ import nested_scopes
18    
19  import os  import os
20  import string  import string
21    
# Line 19  import Thuban.Lib.fileutil Line 23  import Thuban.Lib.fileutil
23    
24  from Thuban.Model.color import Color  from Thuban.Model.color import Color
25    
26    from Thuban.Model.classification import *
27    
28  #  #
29  # one level of indention  # one level of indention
30  #  #
# Line 51  class Saver: Line 57  class Saver:
57      """Class to serialize a session into an XML file.      """Class to serialize a session into an XML file.
58    
59      Applications built on top of Thuban may derive from this class and      Applications built on top of Thuban may derive from this class and
60      override or extend the methods to save additinal information. This      override or extend the methods to save additional information. This
61      additional information should take the form of additional attributes      additional information should take the form of additional attributes
62      or elements whose names are prefixed with a namespace. To define a      or elements whose names are prefixed with a namespace. To define a
63      namespace derived classes should extend the write_session method to      namespace derived classes should extend the write_session method to
# Line 93  class Saver: Line 99  class Saver:
99          self.write_header()          self.write_header()
100          self.write_session(self.session)          self.write_session(self.session)
101    
102          if self.indent_level != 0:          assert(self.indent_level == 0)
             raise ValueError("indent_level still positive!")  
103    
104      def write_attribs(self, attrs):      def write_attribs(self, attrs):
105          for name, value in attrs.items():          for name, value in attrs.items():
             if isinstance(value, Color):  
                 value = value.hex()  
             else:  
                 value = str(value)  
106              self.file.write(' %s="%s"' % (escape(name), escape(value)))              self.file.write(' %s="%s"' % (escape(name), escape(value)))
107            
108      def open_element(self, element, attrs = {}):      def open_element(self, element, attrs = {}):
# Line 126  class Saver: Line 127  class Saver:
127    
128      def close_element(self, element):      def close_element(self, element):
129          self.indent_level -= 1          self.indent_level -= 1
130          if self.indent_level < 0:          assert(self.indent_level >= 0)
             raise ValueError("close_element called too many times!")  
131    
132          # see open_element() for an explanation          # see open_element() for an explanation
133          if self.element_open == 1:          if self.element_open == 1:
# Line 204  class Saver: Line 204  class Saver:
204          given, should be a mapping from attribute names to attribute          given, should be a mapping from attribute names to attribute
205          values. The values should not be XML-escaped yet.          values. The values should not be XML-escaped yet.
206          """          """
207          lc = layer.classification          lc = layer.GetClassification()
208    
209          if attrs is None:          if attrs is None:
210              attrs = {}              attrs = {}
211          attrs["title"] = layer.title  
212          attrs["filename"] = relative_filename(self.dir, layer.filename)          attrs["title"]        = layer.title
213            attrs["filename"]     = relative_filename(self.dir, layer.filename)
214            attrs["stroke"]       = lc.GetDefaultStroke().hex()
215          attrs["stroke_width"] = str(lc.GetDefaultStrokeWidth())          attrs["stroke_width"] = str(lc.GetDefaultStrokeWidth())
216          fill = lc.GetDefaultFill()          attrs["fill"]         = lc.GetDefaultFill().hex()
         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()  
217    
218          self.open_element("layer", attrs)          self.open_element("layer", attrs)
219          self.write_classification(layer)          self.write_classification(layer)
# Line 230  class Saver: Line 223  class Saver:
223          if attrs is None:          if attrs is None:
224              attrs = {}              attrs = {}
225    
226          lc = layer.classification          lc = layer.GetClassification()
227    
228          field = lc.field          field = lc.GetField()
229    
230          #          #
231          # there isn't a classification of anything          # there isn't a classification of anything
# Line 243  class Saver: Line 236  class Saver:
236          attrs["field"] = field          attrs["field"] = field
237          self.open_element("classification", attrs)          self.open_element("classification", attrs)
238    
239          def write_class_data(data):  
240              dict = {'stroke'      : data.GetStroke(),  #       self.open_element("clnull")
241                      'stroke_width': data.GetStrokeWidth(),  #       write_class_data(lc.GetDefaultData())
242                      'fill'        : data.GetFill()}  #       self.close_element("clnull")
243              self.write_element("cldata", dict)              
244            # just playing now with lambdas and dictionaries
245          self.open_element("clnull")  
246          write_class_data(lc.GetDefaultData())          types = [[lambda p: 'clnull',
247          self.close_element("clnull")                    lambda p: 'clnull'],
248                     [lambda p: 'clpoint value="%s"' %
249                                 str(p.GetValue()),
250                      lambda p: 'clpoint'],
251                     [lambda p: 'clrange min="%s" max="%s"' %
252                                 (str(p.GetMin()),
253                                  (str(p.GetMax()))),
254                      lambda p: 'clrange']]
255    
256            def write_class_group(group):
257                type = -1
258                if isinstance(group, ClassGroupDefault): type = 0
259                elif isinstance(group, ClassGroupSingleton): type = 1
260                elif isinstance(group, ClassGroupRange): type = 2
261                elif isinstance(group, ClassGroupMap):   type = 3
262                assert(type >= 0)
263    
264                if type <= 2:
265                    data = group.GetProperties()
266                    dict = {'stroke'      : data.GetStroke().hex(),
267                            'stroke_width': str(data.GetStrokeWidth()),
268                            'fill'        : data.GetFill().hex()}
269    
270                    self.open_element(types[type][0](group))
271                    self.write_element("cldata", dict)
272                    self.close_element(types[type][1](group))
273                else: pass # XXX: we need to handle maps
274    
275            for i in lc:
276                write_class_group(i)
277    
278    #       for i in lc:
279    #           t = i.GetType()
280    #           self.open_element(types[t][0](i))
281    #           write_class_data(i)
282    #           self.close_element(types[t][1](i))
283    
284    #       for p in lc:
285    #           type = p.GetType()
286    #           if p == ClassData.DEFAULT:
287    #               lopen = lclose = 'clnull'
288    #           elif p == ClassData.POINTS:
289    #               lopen = 'clpoint value="%s"' % escape(str(p.GetValue()))
290    #               lclose = 'clpoint'
291    #           elif p == ClassData.RANGES:
292    #               lopen = 'clrange min="%s" max="%s"'
293    #                   % (escape(str(p.GetMin())), escape(str(p.GetMax()))))
294    #               lclose = 'clrange'
295    
296    #           self.open_element(lopen)
297    #           write_class_data(p)
298    #           self.close_element(lclose)
299                            
300          if lc.points != {}:  #       if lc.points != {}:
301              for value, data in lc.points.items():  #           for p in lc.points.values():
302                  self.open_element('clpoint value="%s"' % (escape(str(value))))  #               self.open_element('clpoint value="%s"' %
303                  write_class_data(data)  #                   (escape(str(p.GetValue()))))
304                  self.close_element('clpoint')  #               write_class_data(p)
305              #               self.close_element('clpoint')
306          if lc.ranges != []:  #          
307              for p in lc.ranges:  #       if lc.ranges != []:
308                  self.open_element('clrange min="%s" max="%s"'  #           for p in lc.ranges:
309                      % (escape(str(p[0])), escape(str(p[1]))))  #               self.open_element('clrange min="%s" max="%s"'
310                  write_class_data(p[2])  #                   % (escape(str(p.GetMin())), escape(str(p.GetMax()))))
311                  self.close_element('clrange')  #               write_class_data(p)
312    #               self.close_element('clrange')
313    
314          self.close_element("classification")          self.close_element("classification")
315    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26