/[thuban]/branches/WIP-pyshapelib-bramz/test/test_load.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/test/test_load.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 947 by jonathan, Tue May 20 15:27:19 2003 UTC revision 1268 by bh, Fri Jun 20 16:10:12 2003 UTC
# Line 7  Line 7 
7    
8  """  """
9  Test loading a thuban session from a file  Test loading a thuban session from a file
10    
11    The tests in this file (test_load.py) are always be the tests for the
12    current version of the thuban file format. Tests for older versions can
13    be found in the version specific test modules, e.g. test_load_0_2 for
14    files created by Thuban 0.2.
15    
16    Maintenance of the test cases:
17    
18    When during a development period the file format is changed with respect
19    to the last released version for the first, the tests here should be
20    copied to the version specific test file. The round-trip tests which
21    save the session again and compare the XML files should not be copied
22    over as they only make sense here to make sure th that the files checked
23    here are actually ones that may have been written by the current thuban
24    version.
25  """  """
26    
27  __version__ = "$Revision$"  __version__ = "$Revision$"
# Line 19  import unittest Line 34  import unittest
34  import support  import support
35  support.initthuban()  support.initthuban()
36    
37  from Thuban.Model.load import load_session, parse_color  from xmlsupport import sax_eventlist
 from Thuban.Model.session import Session  
 from Thuban.Model.map import Map  
 from Thuban.Model.layer import Layer  
 from Thuban.Model.proj import Projection  
 from Thuban.Model.color import Color  
38    
39  from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, FIELDTYPE_STRING  import dbflib
40    
41    from Thuban.Model.save import save_session
42    from Thuban.Model.load import load_session, parse_color, LoadError
43    from Thuban.Model.color import Color
44  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
45      ClassGroupSingleton, ClassGroupDefault      ClassGroupSingleton, ClassGroupDefault
46    
47    
48  def filenames_equal(name1, name2):  def filenames_equal(name1, name2):
49      """Return true if the filenames name1 and name2 are equal.      """Return true if the filenames name1 and name2 are equal.
50    
# Line 43  def filenames_equal(name1, name2): Line 57  def filenames_equal(name1, name2):
57      return os.path.normpath(name1) == os.path.normpath(name2)      return os.path.normpath(name1) == os.path.normpath(name2)
58    
59    
 contents_single_map = '''\  
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE session SYSTEM "thuban.dtd">  
 <session title="single map&amp;layer">  
         <map title="Test Map">  
                 <projection>  
                         <parameter value="zone=26"/>  
                         <parameter value="proj=utm"/>  
                         <parameter value="ellps=clrk66"/>  
                 </projection>  
                 <layer title="My Layer" stroke_width="1" fill="None"  
                     filename="../../Data/iceland/political.shp"  
                     stroke="#000000"/>  
         </map>  
 </session>  
 '''  
   
 contents_classified_map_v0_2 = '''\  
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE session SYSTEM "thuban.dtd">  
 <session title="single map&amp;layer">  
         <map title="Test Map">  
                 <projection>  
                         <parameter value="zone=26"/>  
                         <parameter value="proj=utm"/>  
                         <parameter value="ellps=clrk66"/>  
                 </projection>  
                 <layer title="My Layer" stroke_width="1" fill="None"  
                     filename="../../Data/iceland/political.shp"  
                     stroke="#000000">  
             <classification field="POPYREG" field_type="string">  
                 <clnull>  
                     <cldata stroke="#000000" stroke_width="1" fill="None"/>  
                 </clnull>  
                 <clpoint value="1">  
                     <cldata stroke="#000000" stroke_width="2" fill="None"/>  
                 </clpoint>  
                 <clpoint value="1">  
                     <cldata stroke="#000000" stroke_width="10" fill="None"/>  
                 </clpoint>  
             </classification>  
         </layer>  
                 <layer title="My Layer 2" stroke_width="1" fill="None"  
                     filename="../../Data/iceland/political.shp"  
                     stroke="#000000">  
             <classification field="AREA" field_type="double">  
                 <clnull>  
                     <cldata stroke="#000000" stroke_width="2" fill="None"/>  
                 </clnull>  
                 <clrange min="0" max="1">  
                     <cldata stroke="#111111" stroke_width="1" fill="None"/>  
                 </clrange>  
                 <clpoint value=".5">  
                     <cldata stroke="#000000" stroke_width="1" fill="#111111"/>  
                 </clpoint>  
                 <clrange min="-1" max="0">  
                     <cldata stroke="#000000" stroke_width="1" fill="None"/>  
                 </clrange>  
                 <clpoint value="-.5">  
                     <cldata stroke="#000000" stroke_width="1" fill="None"/>  
                 </clpoint>  
             </classification>  
         </layer>  
         </map>  
 </session>  
 '''  
   
 contents_test_labels = '''\  
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE session SYSTEM "thuban.dtd">  
 <session title="single map&amp;layer">  
         <map title="Test Map">  
                 <projection>  
                         <parameter value="zone=26"/>  
                         <parameter value="proj=utm"/>  
                         <parameter value="ellps=clrk66"/>  
                 </projection>  
                 <layer title="My Layer" stroke_width="1" fill="None"  
                     filename="../../Data/iceland/political.shp"  
                     stroke="#000000">  
             <classification field="POPYREG" field_type="string">  
                 <clnull label="hallo">  
                     <cldata stroke="#000000" stroke_width="1" fill="None"/>  
                 </clnull>  
                 <clpoint label="welt" value="1">  
                     <cldata stroke="#000000" stroke_width="2" fill="None"/>  
                 </clpoint>  
             </classification>  
         </layer>  
     </map>  
 </session>  
 '''  
60    
61  contents_test_layer_projection = '''\  class LoadSessionTest(support.FileLoadTestCase):
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE session SYSTEM "thuban.dtd">  
 <session title="single map&amp;layer">  
         <map title="Test Map">  
                 <projection>  
                         <parameter value="zone=26"/>  
                         <parameter value="proj=utm"/>  
                         <parameter value="ellps=clrk66"/>  
                 </projection>  
                 <layer title="My Layer" stroke_width="1" fill="None"  
                     filename="../../Data/iceland/political.shp"  
                     stroke="#000000">  
                     <projection name="hello">  
                         <parameter value="zone=13"/>  
                         <parameter value="proj=tmerc"/>  
                         <parameter value="ellps=clrk66"/>  
                     </projection>  
             <classification field="POPYREG" field_type="string">  
                 <clnull label="hallo">  
                     <cldata stroke="#000000" stroke_width="1" fill="None"/>  
                 </clnull>  
                 <clpoint label="welt" value="1">  
                     <cldata stroke="#000000" stroke_width="2" fill="None"/>  
                 </clpoint>  
             </classification>  
         </layer>  
                 <layer title="My Layer" stroke_width="1" fill="None"  
                     filename="../../Data/iceland/political.shp"  
                     stroke="#000000">  
                     <projection>  
                         <parameter value="proj=lcc"/>  
                         <parameter value="ellps=clrk66"/>  
                     </projection>  
         </layer>  
     </map>  
 </session>  
 '''  
62    
63  contents_test_visible = '''\      """Base class for .thuban file loading tests
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE session SYSTEM "thuban.dtd">  
 <session title="single map&amp;layer">  
         <map title="Test Map">  
                 <projection>  
                         <parameter value="zone=26"/>  
                         <parameter value="proj=utm"/>  
                         <parameter value="ellps=clrk66"/>  
                 </projection>  
                 <layer title="My Layer" stroke_width="1" fill="None"  
                     filename="../../Data/iceland/political.shp"  
                     stroke="#000000" visible="false">  
         </layer>  
     </map>  
 </session>  
 '''  
64    
65  contents_test_rasterlayer = '''\      Basically the same as the FileLoadTestCase, except that all tests
66  <?xml version="1.0" encoding="UTF-8"?>      use the '.thuban' extension by default and that setUp and tearDown
67  <!DOCTYPE session SYSTEM "thuban.dtd">      handle sessions.
68  <session title="single map&amp;layer">      """
         <map title="Test Map">  
                 <rasterlayer title="My RasterLayer"  
                      filename="../../Data/iceland/island.tif"  
                      visible="false">  
         </rasterlayer>  
     </map>  
 </session>  
 '''  
69    
70  class LoadSessionTest(unittest.TestCase, support.FileTestMixin):      file_extension = ".thuban"
71    
72      def setUp(self):      def setUp(self):
73          """Create the test files"""          """Create the test files"""
74          file = open(self.temp_file_name("load_singlelayer.thuban"), "w")          support.FileLoadTestCase.setUp(self)
         file.write(contents_single_map)  
         file.close()  
   
         file = open(self.temp_file_name("load_classified_v0_2.thuban"), "w")  
         file.write(contents_classified_map_v0_2)  
         file.close()  
   
         file = open(self.temp_file_name("load_labels.thuban"), "w")  
         file.write(contents_test_labels)  
         file.close()  
   
         file = open(self.temp_file_name("load_layerproj.thuban"), "w")  
         file.write(contents_test_layer_projection)  
         file.close()  
   
         file = open(self.temp_file_name("load_visible.thuban"), "w")  
         file.write(contents_test_visible)  
         file.close()  
   
         file = open(self.temp_file_name("load_rasterlayer.thuban"), "w")  
         file.write(contents_test_rasterlayer)  
         file.close()  
   
75          self.session = None          self.session = None
76    
77      def tearDown(self):      def tearDown(self):
# Line 241  class LoadSessionTest(unittest.TestCase, Line 79  class LoadSessionTest(unittest.TestCase,
79              self.session.Destroy()              self.session.Destroy()
80          self.session = None          self.session = None
81    
82      def testSingleLayer(self):  
83        dtd = "http://thuban.intevation.org/dtds/thuban-0.8.dtd"
84        thubanids = [((dtd, n), (None, "id")) for n in
85                     ["fileshapesource", "filetable", "jointable",
86                      "derivedshapesource"]]
87        thubanidrefs = [((dtd, n), (None, m)) for n, m in
88                        [("layer", "shapestore"),
89                         ("jointable", "left"),
90                         ("jointable", "right"),
91                         ("derivedshapesource", "table"),
92                         ("derivedshapesource", "shapesource")]]
93        del n, m, dtd
94    
95        def check_format(self):
96            """Check whether the file we loaded from matches the one that
97            would be written. Call this from each test case after loading
98            the session
99            """
100            filename = self.temp_file_name(self.id() + ".roundtrip.thuban")
101            save_session(self.session, filename)
102            el1 = sax_eventlist(filename = filename, ids = self.thubanids,
103                                idrefs = self.thubanidrefs)
104            el2 = sax_eventlist(filename = self.filename(), ids = self.thubanids,
105                                idrefs = self.thubanidrefs)
106            if 0:
107                for a, b in zip(el1, el2):
108                    print a != b and "***************" or ""
109                    print a
110                    print b
111            self.assertEquals(el1, el2,
112                              "loaded file not equivalent to the saved file")
113    
114    
115    class ClassificationTest(LoadSessionTest):
116    
117        """
118        Base class for tests that do some detailed checking of classifications
119        """
120    
121        def TestLayers(self, layers, expected):
122            TITLE = 0
123            NUM_GROUPS = 1
124            CLASSES = 2
125            GROUP_TYPE = 0
126            GROUP_DATA = 1
127            GROUP_LABEL = 2
128            GROUP_PROPS = 3
129    
130            eq = self.assertEquals
131    
132            eq(len(layers), len(expected))
133    
134            for layer, data in zip(layers, expected):
135                eq(layer.Title(), data[TITLE])
136    
137                clazz = layer.GetClassification()
138                eq(clazz.GetNumGroups(), data[NUM_GROUPS])
139                eq(clazz.GetNumGroups() + 1, len(data[CLASSES]))
140    
141                i = 0
142                for group in clazz:
143                    props = ClassGroupProperties()
144                    props.SetLineColor(
145                        parse_color(data[CLASSES][i][GROUP_PROPS][0]))
146                    props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])
147                    props.SetFill(
148                        parse_color(data[CLASSES][i][GROUP_PROPS][2]))
149    
150                    if data[CLASSES][i][GROUP_TYPE] == "default":
151                        g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])
152                    elif data[CLASSES][i][GROUP_TYPE] == "range":
153                        g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0],
154                                            data[CLASSES][i][GROUP_DATA][1],
155                                            props, data[CLASSES][i][GROUP_LABEL])
156                    elif data[CLASSES][i][GROUP_TYPE] == "single":
157                        g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],
158                                              props, data[CLASSES][i][GROUP_LABEL])
159    
160                    eq(group, g)
161    
162                    i += 1
163    
164    
165    
166    class TestSingleLayer(LoadSessionTest):
167    
168        file_contents = '''\
169    <?xml version="1.0" encoding="UTF-8"?>
170    <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
171    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
172            title="single map&amp;layer">
173        <fileshapesource filetype="shapefile" id="D1"
174            filename="../../Data/iceland/political.shp"/>
175        <map title="Test Map">
176            <projection name="Unknown">
177                <parameter value="zone=26"/>
178                <parameter value="proj=utm"/>
179                <parameter value="ellps=clrk66"/>
180            </projection>
181            <layer shapestore="D1" visible="true"
182                    stroke="#000000" title="My Layer" stroke_width="1"
183                    fill="None"/>
184        </map>
185    </session>
186    '''
187    
188        def test(self):
189          """Load a session with a single map with a single layer"""          """Load a session with a single map with a single layer"""
190          eq = self.assertEquals          eq = self.assertEquals
191          session = load_session(self.temp_file_name("load_singlelayer.thuban"))          session = load_session(self.filename())
192          self.session = session          self.session = session
193    
194          # Check the title          # Check the title
# Line 265  class LoadSessionTest(unittest.TestCase, Line 209  class LoadSessionTest(unittest.TestCase,
209          # Check the layer attributes          # Check the layer attributes
210          layer = layers[0]          layer = layers[0]
211          eq(layer.Title(), "My Layer")          eq(layer.Title(), "My Layer")
212          self.failUnless(filenames_equal(layer.filename,          self.failUnless(filenames_equal(layer.ShapeStore().FileName(),
213                                          os.path.join(self.temp_dir(),                                          os.path.join(self.temp_dir(),
214                                                       os.pardir, os.pardir,                                                       os.pardir, os.pardir,
215                                                       "Data", "iceland",                                                       "Data", "iceland",
# Line 274  class LoadSessionTest(unittest.TestCase, Line 218  class LoadSessionTest(unittest.TestCase,
218          eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")          eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")
219          eq(layer.Visible(), True)          eq(layer.Visible(), True)
220    
221            self.check_format()
222    
223          self.session.Destroy()          self.session.Destroy()
224          self.session = None          self.session = None
225    
     def testLayerVisibility(self):  
         """Test that the visible flag is correctly loaded for a layer."""  
226    
227    class TestLayerVisibility(LoadSessionTest):
228    
229        file_contents = '''\
230    <?xml version="1.0" encoding="UTF-8"?>
231    <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
232    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
233            title="single map&amp;layer">
234        <fileshapesource filetype="shapefile" id="D1"
235            filename="../../Data/iceland/political.shp"/>
236        <map title="Test Map">
237            <projection name="Unknown">
238                <parameter value="zone=26"/>
239                <parameter value="proj=utm"/>
240                <parameter value="ellps=clrk66"/>
241            </projection>
242            <layer shapestore="D1" visible="false" stroke="#000000"
243                    title="My Layer" stroke_width="1" fill="None"/>
244        </map>
245    </session>
246    '''
247    
248        def test(self):
249            """Test that the visible flag is correctly loaded for a layer."""
250          eq = self.assertEquals          eq = self.assertEquals
251          session = load_session(self.temp_file_name("load_visible.thuban"))          session = load_session(self.filename())
252          self.session = session          self.session = session
253          maps = session.Maps()          maps = session.Maps()
254          eq(len(maps), 1)          eq(len(maps), 1)
# Line 292  class LoadSessionTest(unittest.TestCase, Line 259  class LoadSessionTest(unittest.TestCase,
259    
260          eq(layer.Visible(), False)          eq(layer.Visible(), False)
261    
262      def testClassification(self):          self.check_format()
263          """Load a session with a map and classified layers."""  
264    
265    class TestClassification(ClassificationTest):
266    
267        file_contents = '''\
268    <?xml version="1.0" encoding="UTF-8"?>
269    <!DOCTYPE session SYSTEM "thuban.dtd">
270    <session title="single map&amp;layer">
271            <map title="Test Map">
272                    <projection>
273                            <parameter value="zone=26"/>
274                            <parameter value="proj=utm"/>
275                            <parameter value="ellps=clrk66"/>
276                    </projection>
277                    <layer title="My Layer" stroke_width="1" fill="None"
278                        filename="../../Data/iceland/political.shp"
279                        stroke="#000000">
280                <classification field="POPYREG" field_type="string">
281                    <clnull>
282                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
283                    </clnull>
284                    <clpoint value="1">
285                        <cldata stroke="#000000" stroke_width="2" fill="None"/>
286                    </clpoint>
287                    <clpoint value="1">
288                        <cldata stroke="#000000" stroke_width="10" fill="None"/>
289                    </clpoint>
290                </classification>
291            </layer>
292                    <layer title="My Layer 2" stroke_width="1" fill="None"
293                        filename="../../Data/iceland/political.shp"
294                        stroke="#000000">
295                <classification field="AREA" field_type="double">
296                    <clnull>
297                        <cldata stroke="#000000" stroke_width="2" fill="None"/>
298                    </clnull>
299                    <clrange min="0" max="1">
300                        <cldata stroke="#111111" stroke_width="1" fill="None"/>
301                    </clrange>
302                    <clpoint value=".5">
303                        <cldata stroke="#000000" stroke_width="1" fill="#111111"/>
304                    </clpoint>
305                    <clrange min="-1" max="0">
306                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
307                    </clrange>
308                    <clpoint value="-.5">
309                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
310                    </clpoint>
311                </classification>
312            </layer>
313            </map>
314    </session>
315    '''
316    
317          session = load_session(self.temp_file_name("load_classified_v0_2.thuban"))      def test(self):
318            """Load a Thuban session with a map and classified layers."""
319            session = load_session(self.filename())
320          self.session = session          self.session = session
321    
322          map = self.session.Maps()[0] # only one map in the sample          map = self.session.Maps()[0] # only one map in the sample
323    
324          expected = [("My Layer", 2,          expected = [("My Layer", 2,
325                          [("default", (), "",                          [("default", (), "",
326                              ("#000000", 1, "None")),                              ("#000000", 1, "None")),
327                           ("single", "1", "",                           ("single", "1", "",
# Line 321  class LoadSessionTest(unittest.TestCase, Line 342  class LoadSessionTest(unittest.TestCase,
342    
343          self.TestLayers(map.Layers(), expected)          self.TestLayers(map.Layers(), expected)
344    
     def TestLayers(self, layers, expected):  
345    
346          TITLE = 0  class TestLabels(ClassificationTest):
         NUM_GROUPS = 1  
         CLASSES = 2  
         GROUP_TYPE = 0  
         GROUP_DATA = 1  
         GROUP_LABEL = 2  
         GROUP_PROPS = 3  
347    
348          eq = self.assertEquals      file_contents = '''\
349    <?xml version="1.0" encoding="UTF-8"?>
350          eq(len(layers), len(expected))  <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
351    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
352          for layer, data in zip(layers, expected):          title="single map&amp;layer">
353              eq(layer.Title(), data[TITLE])      <fileshapesource filetype="shapefile" id="D1"
354            filename="../../Data/iceland/political.shp"/>
355              clazz = layer.GetClassification()      <map title="Test Map">
356              eq(clazz.GetNumGroups(), data[NUM_GROUPS])          <projection name="Unknown">
357              eq(clazz.GetNumGroups() + 1, len(data[CLASSES]))              <parameter value="zone=26"/>
358                <parameter value="proj=utm"/>
359              i = 0              <parameter value="ellps=clrk66"/>
360              for group in clazz:          </projection>
361                            <layer shapestore="D1" visible="true" stroke="#000000"
362                  props = ClassGroupProperties()                  title="My Layer" stroke_width="1" fill="None">
363                  props.SetLineColor(              <classification field="POPYREG" field_type="string">
364                      parse_color(data[CLASSES][i][GROUP_PROPS][0]))                  <clnull label="hallo">
365                  props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
366                  props.SetFill(                  </clnull>
367                      parse_color(data[CLASSES][i][GROUP_PROPS][2]))                  <clpoint label="welt" value="1">
368                        <cldata stroke="#000000" stroke_width="2" fill="None"/>
369                  if data[CLASSES][i][GROUP_TYPE] == "default":                  </clpoint>
370                      g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])              </classification>
371                  elif data[CLASSES][i][GROUP_TYPE] == "range":          </layer>
372                      g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0],      </map>
373                                          data[CLASSES][i][GROUP_DATA][1],  </session>
374                                          props, data[CLASSES][i][GROUP_LABEL])  '''
                 elif data[CLASSES][i][GROUP_TYPE] == "single":  
                     g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],  
                                           props, data[CLASSES][i][GROUP_LABEL])  
   
                 eq(group, g)  
   
                 i += 1  
375    
376      def testLabels(self):      def test(self):
377          """Load a session and test for reading the group labels."""          """Load a session and test for reading the group labels."""
   
378          eq = self.assertEquals          eq = self.assertEquals
379          session = load_session(self.temp_file_name("load_labels.thuban"))          session = load_session(self.filename())
380          self.session = session          self.session = session
381    
382          map = self.session.Maps()[0] # only one map in the sample          map = self.session.Maps()[0] # only one map in the sample
# Line 382  class LoadSessionTest(unittest.TestCase, Line 388  class LoadSessionTest(unittest.TestCase,
388                              ("#000000", 2, "None"))])]                              ("#000000", 2, "None"))])]
389    
390          self.TestLayers(map.Layers(), expected)          self.TestLayers(map.Layers(), expected)
391            self.check_format()
392    
393    
394    class TestLayerProjection(LoadSessionTest):
395    
396        file_contents = '''\
397    <?xml version="1.0" encoding="UTF-8"?>
398    <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
399    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
400            title="single map&amp;layer">
401        <fileshapesource filetype="shapefile" id="D2"
402            filename="../../Data/iceland/roads-line.shp"/>
403        <fileshapesource filetype="shapefile" id="D4"
404            filename="../../Data/iceland/political.shp"/>
405        <map title="Test Map">
406            <projection name="Unknown">
407                <parameter value="zone=26"/>
408                <parameter value="proj=utm"/>
409                <parameter value="ellps=clrk66"/>
410            </projection>
411            <layer shapestore="D4" visible="true" stroke="#000000"
412                    title="My Layer" stroke_width="1" fill="None">
413                <projection name="hello">
414                    <parameter value="zone=13"/>
415                    <parameter value="proj=tmerc"/>
416                    <parameter value="ellps=clrk66"/>
417                </projection>
418                <classification field="POPYREG" field_type="string">
419                    <clnull label="hallo">
420                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
421                    </clnull>
422                    <clpoint label="welt" value="1">
423                        <cldata stroke="#000000" stroke_width="2" fill="None"/>
424                    </clpoint>
425                </classification>
426            </layer>
427            <layer shapestore="D2" visible="true" stroke="#000000"
428                    title="My Layer" stroke_width="1" fill="None">
429                <projection name="Unknown">
430                    <parameter value="proj=lcc"/>
431                    <parameter value="ellps=clrk66"/>
432                </projection>
433            </layer>
434        </map>
435    </session>
436    '''
437    
438      def testLayerProjection(self):      def test(self):
439            """Test loading layers with projections"""
440          eq = self.assertEquals          eq = self.assertEquals
441          neq = self.assertNotEqual          neq = self.assertNotEqual
442    
443          session = load_session(self.temp_file_name("load_layerproj.thuban"))          session = load_session(self.filename())
444          self.session = session          self.session = session
445    
446          map = self.session.Maps()[0] # only one map in the sample          map = self.session.Maps()[0] # only one map in the sample
# Line 409  class LoadSessionTest(unittest.TestCase, Line 462  class LoadSessionTest(unittest.TestCase,
462          eq(proj.GetParameter("proj"), "lcc")          eq(proj.GetParameter("proj"), "lcc")
463          eq(proj.GetParameter("ellps"), "clrk66")          eq(proj.GetParameter("ellps"), "clrk66")
464    
465      def testRasterLayer(self):          self.check_format()
466    
467    
468    class TestRasterLayer(LoadSessionTest):
469    
470        file_contents = '''\
471    <?xml version="1.0" encoding="UTF-8"?>
472    <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
473    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
474            title="single map&amp;layer">
475        <map title="Test Map">
476            <rasterlayer visible="false" filename="../../Data/iceland/island.tif"
477                    title="My RasterLayer"/>
478        </map>
479    </session>
480    '''
481    
482        def test(self):
483          eq = self.assertEquals          eq = self.assertEquals
484          neq = self.assertNotEqual          neq = self.assertNotEqual
485    
486          session = load_session(self.temp_file_name("load_rasterlayer.thuban"))          session = load_session(self.filename())
487          self.session = session          self.session = session
488    
489          map = self.session.Maps()[0] # only one map in the sample          map = self.session.Maps()[0] # only one map in the sample
# Line 427  class LoadSessionTest(unittest.TestCase, Line 497  class LoadSessionTest(unittest.TestCase,
497                                                       os.pardir, os.pardir,                                                       os.pardir, os.pardir,
498                                                       "Data", "iceland",                                                       "Data", "iceland",
499                                                       "island.tif")))                                                       "island.tif")))
500            self.check_format()
501    
502  if __name__ == "__main__":  
503      unittest.main()  class TestJoinedTable(LoadSessionTest):
504    
505        file_contents = '''<?xml version="1.0" encoding="UTF-8"?>
506    <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
507    <session title="A Joined Table session">
508        <filetable filename="load_joinedtable.dbf"
509                   title="Some Title"
510                   filetype="DBF" id="D141881756"/>
511        <fileshapesource filename="../../Data/iceland/roads-line.shp"
512                         filetype="shapefile" id="D142197204"/>
513        <jointable id="D142180284"
514                   title="Joined"
515                   leftcolumn="RDLNTYPE" left="D142197204"
516                   rightcolumn="RDTYPE" right="D141881756"/>
517        <derivedshapesource id="D141915644"
518                            table="D142180284" shapesource="D142197204"/>
519        <map title="Test Map">
520            <layer title="My Layer"
521                   shapestore="D141915644" visible="true"
522                   stroke="#000000" stroke_width="1" fill="None"/>
523        </map>
524    </session>'''
525    
526        def setUp(self):
527            """Extend inherited method to create the dbffile for the join"""
528            LoadSessionTest.setUp(self)
529            dbffile = self.temp_file_name("load_joinedtable.dbf")
530            dbf = dbflib.create(dbffile)
531            dbf.add_field("RDTYPE", dbflib.FTInteger, 10, 0)
532            dbf.add_field("TEXT", dbflib.FTString, 10, 0)
533            dbf.write_record(0, {'RDTYPE': 8, "TEXT": "foo"})
534            dbf.write_record(1, {'RDTYPE': 2, "TEXT": "bar"})
535            dbf.write_record(2, {'RDTYPE': 3, "TEXT": "baz"})
536            dbf.close()
537    
538        def test(self):
539            """Test loading a session containing a joined table"""
540            session = load_session(self.filename())
541            self.session = session
542    
543            tables = session.Tables()
544            self.assertEquals(len(tables), 3)
545            # FIXME: The tests shouldn't assume a certain order of the tables
546            self.assertEquals(tables[0].Title(), "Some Title")
547            self.assertEquals(tables[1].Title(), "Joined")
548    
549    
550    class TestLoadError(LoadSessionTest):
551    
552        file_contents = '''\
553    <?xml version="1.0" encoding="UTF-8"?>
554    <!DOCTYPE session SYSTEM "thuban-0.8.dtd">
555    <session xmlns="http://thuban.intevation.org/dtds/thuban-0.8.dtd"
556            title="single map&amp;layer">
557        <fileshapesource id="D1" filename="../../Data/iceland/political.shp"/>
558        <map title="Test Map">
559            <projection name="Unknown">
560                <parameter value="zone=26"/>
561                <parameter value="proj=utm"/>
562                <parameter value="ellps=clrk66"/>
563            </projection>
564            <layer shapestore="D1" visible="true"
565                    stroke="#000000" title="My Layer" stroke_width="1"
566                    fill="None"/>
567        </map>
568    </session>
569    '''
570    
571        def test(self):
572            """Test loading a session missing a required attribute"""
573            # Don't use assertRaises to make sure that if a session is
574            # actually returned it gets destroyed properly.
575            try:
576                self.session = load_session(self.filename())
577            except LoadError, value:
578                pass
579            else:
580                self.fail("Missing filetype attribute doesn't raise LoadError")
581    
582    if __name__ == "__main__":
583        unittest.main()

Legend:
Removed from v.947  
changed lines
  Added in v.1268

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26