/[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 414 by jonathan, Wed Feb 19 16:52:23 2003 UTC revision 605 by jonathan, Fri Apr 4 12:16:13 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]>
# Line 13  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 20  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 52  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 94  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 127  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 181  class Saver: Line 180  class Saver:
180          element, call write_layer for each layer contained in the map          element, call write_layer for each layer contained in the map
181          and finally call write_label_layer to write the label layer.          and finally call write_label_layer to write the label layer.
182          """          """
183          write = self.file.write          #write = self.file.write
184          self.open_element('map title="%s"' % escape(map.title))          self.open_element('map title="%s"' % escape(map.title))
185          self.write_projection(map.projection)          self.write_projection(map.projection)
186          for layer in map.Layers():          for layer in map.Layers():
# Line 209  class Saver: Line 208  class Saver:
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["stroke"] = lc.GetDefaultStroke().hex()          attrs["filename"]     = relative_filename(self.dir, layer.filename)
214          attrs["stroke_width"] = str(lc.GetDefaultStrokeWidth())          attrs["stroke"]       = lc.GetDefaultLineColor().hex()
215          attrs["fill"] = lc.GetDefaultFill().hex()          attrs["stroke_width"] = str(lc.GetDefaultLineWidth())
216            attrs["fill"]         = lc.GetDefaultFill().hex()
         #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()  
217    
218          self.open_element("layer", attrs)          self.open_element("layer", attrs)
219          self.write_classification(layer)          self.write_classification(layer)
# Line 238  class Saver: Line 225  class Saver:
225    
226          lc = layer.GetClassification()          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 247  class Saver: Line 234  class Saver:
234          if field is None: return          if field is None: return
235    
236          attrs["field"] = field          attrs["field"] = field
237            attrs["field_type"] = str(lc.GetFieldType())
238          self.open_element("classification", attrs)          self.open_element("classification", attrs)
239    
240          def write_class_data(data):  
241              dict = {'stroke'      : data.GetStroke().hex(),  #       self.open_element("clnull")
242                      'stroke_width': str(data.GetStrokeWidth()),  #       write_class_data(lc.GetDefaultData())
243                      'fill'        : data.GetFill().hex()}  #       self.close_element("clnull")
244              self.write_element("cldata", dict)              
245            # just playing now with lambdas and dictionaries
246          self.open_element("clnull")  
247          write_class_data(lc.GetDefaultData())          types = [[lambda p: 'clnull',
248          self.close_element("clnull")                    lambda p: 'clnull'],
249                     [lambda p: 'clpoint value="%s"' %
250                                 str(p.GetValue()),
251                      lambda p: 'clpoint'],
252                     [lambda p: 'clrange min="%s" max="%s"' %
253                                 (str(p.GetMin()),
254                                  (str(p.GetMax()))),
255                      lambda p: 'clrange']]
256    
257            def write_class_group(group):
258                type = -1
259                if isinstance(group, ClassGroupDefault): type = 0
260                elif isinstance(group, ClassGroupSingleton): type = 1
261                elif isinstance(group, ClassGroupRange): type = 2
262                elif isinstance(group, ClassGroupMap):   type = 3
263                assert type >= 0
264    
265                if type <= 2:
266                    data = group.GetProperties()
267                    dict = {'stroke'      : data.GetLineColor().hex(),
268                            'stroke_width': str(data.GetLineWidth()),
269                            'fill'        : data.GetFill().hex()}
270    
271                    self.open_element(types[type][0](group))
272                    self.write_element("cldata", dict)
273                    self.close_element(types[type][1](group))
274                else: pass # XXX: we need to handle maps
275    
276            for i in lc:
277                write_class_group(i)
278    
279    #       for i in lc:
280    #           t = i.GetType()
281    #           self.open_element(types[t][0](i))
282    #           write_class_data(i)
283    #           self.close_element(types[t][1](i))
284    
285    #       for p in lc:
286    #           type = p.GetType()
287    #           if p == ClassData.DEFAULT:
288    #               lopen = lclose = 'clnull'
289    #           elif p == ClassData.POINTS:
290    #               lopen = 'clpoint value="%s"' % escape(str(p.GetValue()))
291    #               lclose = 'clpoint'
292    #           elif p == ClassData.RANGES:
293    #               lopen = 'clrange min="%s" max="%s"'
294    #                   % (escape(str(p.GetMin())), escape(str(p.GetMax()))))
295    #               lclose = 'clrange'
296    
297    #           self.open_element(lopen)
298    #           write_class_data(p)
299    #           self.close_element(lclose)
300                            
301          if lc.points != {}:  #       if lc.points != {}:
302              for p in lc.points.values():  #           for p in lc.points.values():
303                  self.open_element('clpoint value="%s"' %  #               self.open_element('clpoint value="%s"' %
304                      (escape(str(p.GetValue()))))  #                   (escape(str(p.GetValue()))))
305                  write_class_data(p)  #               write_class_data(p)
306                  self.close_element('clpoint')  #               self.close_element('clpoint')
307              #          
308          if lc.ranges != []:  #       if lc.ranges != []:
309              for p in lc.ranges:  #           for p in lc.ranges:
310                  self.open_element('clrange min="%s" max="%s"'  #               self.open_element('clrange min="%s" max="%s"'
311                      % (escape(str(p.GetMin())), escape(str(p.GetMax()))))  #                   % (escape(str(p.GetMin())), escape(str(p.GetMax()))))
312                  write_class_data(p)  #               write_class_data(p)
313                  self.close_element('clrange')  #               self.close_element('clrange')
314    
315          self.close_element("classification")          self.close_element("classification")
316    

Legend:
Removed from v.414  
changed lines
  Added in v.605

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26