/[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 440 by jonathan, Thu Feb 27 15:54:27 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 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    
22  import Thuban.Lib.fileutil  import Thuban.Lib.fileutil
23    
24    from Thuban.Model.color import Color
25    
26    from Thuban.Model.classification import *
27    
28    #
29    # one level of indention
30    #
31    TAB = "    "
32    
33  def relative_filename(dir, filename):  def relative_filename(dir, filename):
34      """Return a filename relative to dir for the absolute file name absname.      """Return a filename relative to dir for the absolute file name absname.
35    
# Line 51  class Saver: Line 64  class Saver:
64      pass the namespaces to the default implementation.      pass the namespaces to the default implementation.
65      """      """
66    
67    
68      def __init__(self, session):      def __init__(self, session):
69          self.session = session          self.session = session
70    
# Line 68  class Saver: Line 82  class Saver:
82          presence of a write method) all filenames will be absolut          presence of a write method) all filenames will be absolut
83          filenames.          filenames.
84          """          """
85    
86            # keep track of how many levels of indentation to write
87            self.indent_level = 0
88            # track whether an element is currently open. see open_element().
89            self.element_open = 0
90    
91          if hasattr(file_or_filename, "write"):          if hasattr(file_or_filename, "write"):
92              # it's a file object              # it's a file object
93              self.file = file_or_filename              self.file = file_or_filename
# Line 79  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      def write_element(self, element, attrs, empty = 0, indentation = ""):          assert(self.indent_level == 0)
103          # Helper function to write an element open tag with attributes  
104          self.file.write("%s<%s" % (indentation, element))      def write_attribs(self, attrs):
105          for name, value in attrs.items():          for name, value in attrs.items():
106              self.file.write(' %s="%s"' % (escape(name), escape(value)))              self.file.write(' %s="%s"' % (escape(name), escape(value)))
107          if empty:      
108        def open_element(self, element, attrs = {}):
109    
110            #
111            # we note when an element is opened so that if two open_element()
112            # calls are made successively we can end the currently open
113            # tag and will later write a proper close tag. otherwise,
114            # if a close_element() call is made directly after an open_element()
115            # call we will close the tag with a />
116            #
117            if self.element_open == 1:
118                self.file.write(">\n")
119    
120            self.element_open = 1
121    
122            # Helper function to write an element open tag with attributes
123            self.file.write("%s<%s" % (TAB*self.indent_level, element))
124            self.write_attribs(attrs)
125    
126            self.indent_level += 1
127    
128        def close_element(self, element):
129            self.indent_level -= 1
130            assert(self.indent_level >= 0)
131    
132            # see open_element() for an explanation
133            if self.element_open == 1:
134                self.element_open = 0
135              self.file.write("/>\n")              self.file.write("/>\n")
136          else:          else:
137              self.file.write(">\n")              self.file.write("%s</%s>\n" % (TAB*self.indent_level, element))
138    
139        def write_element(self, element, attrs = {}):
140            """write an element that won't need a closing tag"""
141            self.open_element(element, attrs)
142            self.close_element(element)
143    
144      def write_header(self):      def write_header(self):
145          """Write the XML header"""          """Write the XML header"""
146          write = self.file.write          self.file.write('<?xml version="1.0" encoding="UTF-8"?>\n')
147          write('<?xml version="1.0" encoding="UTF-8"?>\n')          self.file.write('<!DOCTYPE session SYSTEM "thuban.dtd">\n')
         write('<!DOCTYPE session SYSTEM "thuban.dtd">\n')  
148    
149      def write_session(self, session, attrs = None, namespaces = ()):      def write_session(self, session, attrs = None, namespaces = ()):
150          """Write the session and its contents          """Write the session and its contents
# Line 116  class Saver: Line 167  class Saver:
167          attrs["title"] = session.title          attrs["title"] = session.title
168          for name, uri in namespaces:          for name, uri in namespaces:
169              attrs["xmlns:" + name] = uri              attrs["xmlns:" + name] = uri
170          self.write_element("session", attrs)          self.open_element("session", attrs)
171          for map in session.Maps():          for map in session.Maps():
172              self.write_map(map)              self.write_map(map)
173          self.file.write('</session>\n')          self.close_element("session")
174    
175      def write_map(self, map):      def write_map(self, map):
176          """Write the map and its contents.          """Write the map and its contents.
# Line 130  class Saver: Line 181  class Saver:
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          write('\t<map title="%s">\n' % 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():
187              self.write_layer(layer)              self.write_layer(layer)
188          self.write_label_layer(map.LabelLayer())          self.write_label_layer(map.LabelLayer())
189          write('\t</map>\n')          self.close_element('map')
190    
191      def write_projection(self, projection):      def write_projection(self, projection):
192          """Write the projection.          """Write the projection.
193          """          """
194          if projection and len(projection.params) > 0:          if projection and len(projection.params) > 0:
195              self.file.write('\t\t<projection>\n')              self.open_element("projection")
196              for param in projection.params:              for param in projection.params:
197                  self.file.write('\t\t\t<parameter value="%s"/>\n'                  self.write_element('parameter value="%s"' % escape(param))
198                                  % escape(param))              self.close_element("projection")
             self.file.write('\t\t</projection>\n')  
199    
200      def write_layer(self, layer, attrs = None):      def write_layer(self, layer, attrs = None):
201          """Write the layer.          """Write the layer.
# Line 154  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.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["stroke_width"] = str(layer.stroke_width)          attrs["filename"]     = relative_filename(self.dir, layer.filename)
214          fill = layer.fill          attrs["stroke"]       = lc.GetDefaultStroke().hex()
215          if fill is None:          attrs["stroke_width"] = str(lc.GetDefaultStrokeWidth())
216              attrs["fill"] = "None"          attrs["fill"]         = lc.GetDefaultFill().hex()
217          else:  
218              attrs["fill"] = fill.hex()          self.open_element("layer", attrs)
219          stroke = layer.stroke          self.write_classification(layer)
220          if stroke is None:          self.close_element("layer")
221              attrs["stroke"] = "None"  
222          else:      def write_classification(self, layer, attrs = None):
223              attrs["stroke"] = stroke.hex()          if attrs is None:
224          self.write_element("layer", attrs, empty = 1, indentation = "\t\t")              attrs = {}
225    
226            lc = layer.GetClassification()
227    
228            field = lc.GetField()
229    
230            #
231            # there isn't a classification of anything
232            # so don't do anything
233            #
234            if field is None: return
235    
236            attrs["field"] = field
237            self.open_element("classification", attrs)
238    
239    
240    #       self.open_element("clnull")
241    #       write_class_data(lc.GetDefaultData())
242    #       self.close_element("clnull")
243                
244            # just playing now with lambdas and dictionaries
245    
246            types = [[lambda p: 'clnull',
247                      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 != {}:
301    #           for p in lc.points.values():
302    #               self.open_element('clpoint value="%s"' %
303    #                   (escape(str(p.GetValue()))))
304    #               write_class_data(p)
305    #               self.close_element('clpoint')
306    #          
307    #       if lc.ranges != []:
308    #           for p in lc.ranges:
309    #               self.open_element('clrange min="%s" max="%s"'
310    #                   % (escape(str(p.GetMin())), escape(str(p.GetMax()))))
311    #               write_class_data(p)
312    #               self.close_element('clrange')
313    
314            self.close_element("classification")
315    
316      def write_label_layer(self, layer):      def write_label_layer(self, layer):
317          """Write the label layer.          """Write the label layer.
318          """          """
319          labels = layer.Labels()          labels = layer.Labels()
320          if labels:          if labels:
321              self.file.write('\t\t<labellayer>\n')              self.open_element('labellayer')
322              for label in labels:              for label in labels:
323                  self.file.write(('\t\t\t<label x="%g" y="%g" text="%s"'                  self.write_element(('label x="%g" y="%g" text="%s"'
324                                   ' halign="%s" valign="%s"/>\n')                                      ' halign="%s" valign="%s"')
325                                  % (label.x, label.y, label.text, label.halign,                                  % (label.x, label.y, label.text, label.halign,
326                                     label.valign))                                     label.valign))
327              self.file.write('\t\t</labellayer>\n')              self.close_element('labellayer')
328    
329    
330    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26