/[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 366 by jonathan, Mon Jan 27 11:48:17 2003 UTC revision 429 by jonathan, Mon Feb 24 18:46:51 2003 UTC
# Line 2  Line 2 
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 19  import Thuban.Lib.fileutil Line 20  import Thuban.Lib.fileutil
20    
21  from Thuban.Model.color import Color  from Thuban.Model.color import Color
22    
23    from Thuban.Model.classification import *
24    
25  #  #
26  # one level of indention  # one level of indention
27  #  #
# Line 77  class Saver: Line 80  class Saver:
80          filenames.          filenames.
81          """          """
82    
83          self.indent_level = 0          # keep track of how many levels of indentation to write
84            self.indent_level = 0
85            # track whether an element is currently open. see open_element().
86            self.element_open = 0
87    
88          if hasattr(file_or_filename, "write"):          if hasattr(file_or_filename, "write"):
89              # it's a file object              # it's a file object
# Line 90  class Saver: Line 96  class Saver:
96          self.write_header()          self.write_header()
97          self.write_session(self.session)          self.write_session(self.session)
98    
99          if self.indent_level != 0:          assert(self.indent_level == 0)
             raise ValueError("indent_level still positive!")  
100    
101      def write_attribs(self, attrs):      def write_attribs(self, attrs):
102          for name, value in attrs.items():          for name, value in attrs.items():
103              if isinstance(value, Color):              self.file.write(' %s="%s"' % (escape(name), value))
                 value = value.hex()  
             else:  
                 value = str(value)  
             self.file.write(' %s="%s"' % (escape(name), escape(value)))  
104            
105      def open_element(self, element, attrs = {}):      def open_element(self, element, attrs = {}):
106    
107            #
108            # we note when an element is opened so that if two open_element()
109            # calls are made successively we can end the currently open
110            # tag and will later write a proper close tag. otherwise,
111            # if a close_element() call is made directly after an open_element()
112            # call we will close the tag with a />
113            #
114            if self.element_open == 1:
115                self.file.write(">\n")
116    
117            self.element_open = 1
118    
119          # Helper function to write an element open tag with attributes          # Helper function to write an element open tag with attributes
120          self.file.write("%s<%s" % (TAB*self.indent_level, element))          self.file.write("%s<%s" % (TAB*self.indent_level, element))
121          self.write_attribs(attrs)          self.write_attribs(attrs)
         self.file.write(">\n")  
122    
123          self.indent_level += 1          self.indent_level += 1
124    
125      def close_element(self, element):      def close_element(self, element):
126          self.indent_level -= 1          self.indent_level -= 1
127          if self.indent_level < 0:          assert(self.indent_level >= 0)
128              raise ValueError("close_element called too many times!")  
129          self.file.write("%s</%s>\n" % (TAB*self.indent_level, element))          # see open_element() for an explanation
130            if self.element_open == 1:
131                self.element_open = 0
132                self.file.write("/>\n")
133            else:
134                self.file.write("%s</%s>\n" % (TAB*self.indent_level, element))
135    
136      def write_element(self, element, attrs = {}):      def write_element(self, element, attrs = {}):
137          # Helper function to write an element open tag with attributes          """write an element that won't need a closing tag"""
138          self.file.write("%s<%s" % (TAB*self.indent_level, element))          self.open_element(element, attrs)
139          self.write_attribs(attrs)          self.close_element(element)
         self.file.write("/>\n")  
140    
141      def write_header(self):      def write_header(self):
142          """Write the XML header"""          """Write the XML header"""
# Line 184  class Saver: Line 201  class Saver:
201          given, should be a mapping from attribute names to attribute          given, should be a mapping from attribute names to attribute
202          values. The values should not be XML-escaped yet.          values. The values should not be XML-escaped yet.
203          """          """
204            lc = layer.GetClassification()
205    
206          if attrs is None:          if attrs is None:
207              attrs = {}              attrs = {}
208          attrs["title"] = layer.title  
209          attrs["filename"] = relative_filename(self.dir, layer.filename)          attrs["title"]        = layer.title
210          attrs["stroke_width"] = str(layer.stroke_width)          attrs["filename"]     = relative_filename(self.dir, layer.filename)
211          fill = layer.fill          attrs["stroke"]       = lc.GetDefaultStroke().hex()
212          if fill is None:          attrs["stroke_width"] = str(lc.GetDefaultStrokeWidth())
213              attrs["fill"] = "None"          attrs["fill"]         = lc.GetDefaultFill().hex()
         else:  
             attrs["fill"] = fill.hex()  
         stroke = layer.stroke  
         if stroke is None:  
             attrs["stroke"] = "None"  
         else:  
             attrs["stroke"] = stroke.hex()  
214    
215          self.open_element("layer", attrs)          self.open_element("layer", attrs)
216          self.write_classification(layer)          self.write_classification(layer)
# Line 208  class Saver: Line 220  class Saver:
220          if attrs is None:          if attrs is None:
221              attrs = {}              attrs = {}
222    
223          lc = layer.classification          lc = layer.GetClassification()
224    
225          field = lc.field          field = lc.GetField()
226    
227          #          #
228          # there isn't a classification of anything          # there isn't a classification of anything
# Line 221  class Saver: Line 233  class Saver:
233          attrs["field"] = field          attrs["field"] = field
234          self.open_element("classification", attrs)          self.open_element("classification", attrs)
235    
236          if lc.NullData is not None:  
237              self.open_element("clnull")  #       self.open_element("clnull")
238              self.write_element("cldata", lc.NullData)  #       write_class_data(lc.GetDefaultData())
239              self.close_element("clnull")  #       self.close_element("clnull")
240                
241          if lc.points != {}:          # just playing now with lambdas and dictionaries
242              for value, data in lc.points.items():  
243                  self.open_element('clpoint value="%s"' % (escape(str(value))))          types = {ClassData.DEFAULT:
244                  self.write_element("cldata", data)                       [lambda p: 'clnull',
245                  self.close_element('clpoint')                        lambda p: 'clnull'],
246                               ClassData.POINT:
247          if lc.ranges != []:                       [lambda p: 'clpoint value="%s"' %
248              for p in lc.ranges:                                   str(p.GetValue()),
249                  self.open_element('clrange min="%s" max="%s"'                        lambda p: 'clpoint'],
250                      % (escape(str(p[0])), escape(str(p[1]))))                   ClassData.RANGE:
251                  self.write_element("cldata", p[2])                       [lambda p: 'clrange min="%s" max="%s"' %
252                  self.close_element('clrange')                                   (str(p.GetMin()),
253                                      (str(p.GetMax()))),
254                          lambda p: 'clrange']}
255    
256            def write_class_data(data):
257                dict = {'stroke'      : data.GetStroke().hex(),
258                        'stroke_width': str(data.GetStrokeWidth()),
259                        'fill'        : data.GetFill().hex()}
260                t = data.GetType()
261                self.open_element(types[t][0](data))
262                self.write_element("cldata", dict)
263                self.close_element(types[t][1](data))
264    
265            for i in lc:
266                write_class_data(i)
267    
268    #       for i in lc:
269    #           t = i.GetType()
270    #           self.open_element(types[t][0](i))
271    #           write_class_data(i)
272    #           self.close_element(types[t][1](i))
273    
274    #       for p in lc:
275    #           type = p.GetType()
276    #           if p == ClassData.DEFAULT:
277    #               lopen = lclose = 'clnull'
278    #           elif p == ClassData.POINTS:
279    #               lopen = 'clpoint value="%s"' % escape(str(p.GetValue()))
280    #               lclose = 'clpoint'
281    #           elif p == ClassData.RANGES:
282    #               lopen = 'clrange min="%s" max="%s"'
283    #                   % (escape(str(p.GetMin())), escape(str(p.GetMax()))))
284    #               lclose = 'clrange'
285    
286    #           self.open_element(lopen)
287    #           write_class_data(p)
288    #           self.close_element(lclose)
289                
290    #       if lc.points != {}:
291    #           for p in lc.points.values():
292    #               self.open_element('clpoint value="%s"' %
293    #                   (escape(str(p.GetValue()))))
294    #               write_class_data(p)
295    #               self.close_element('clpoint')
296    #          
297    #       if lc.ranges != []:
298    #           for p in lc.ranges:
299    #               self.open_element('clrange min="%s" max="%s"'
300    #                   % (escape(str(p.GetMin())), escape(str(p.GetMax()))))
301    #               write_class_data(p)
302    #               self.close_element('clrange')
303    
304          self.close_element("classification")          self.close_element("classification")
305    

Legend:
Removed from v.366  
changed lines
  Added in v.429

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26