/[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 690 by jonathan, Wed Apr 16 13:47:22 2003 UTC revision 1247 by bh, Thu Jun 19 19:53:36 2003 UTC
# Line 1  Line 1 
1  # Copyright (c) 2002 by Intevation GmbH  # Copyright (c) 2002, 2003 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# 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.
21  """  """
22    
23  __version__ = "$Revision$"  __version__ = "$Revision$"
# Line 20  import support Line 31  import support
31  support.initthuban()  support.initthuban()
32    
33  from Thuban.Model.load import load_session, parse_color  from Thuban.Model.load import load_session, parse_color
 from Thuban.Model.session import Session  
 from Thuban.Model.map import Map  
 from Thuban.Model.layer import Layer  
 from Thuban.Model.proj import Projection  
34  from Thuban.Model.color import Color  from Thuban.Model.color import Color
   
 from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, FIELDTYPE_STRING  
   
35  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
36      ClassGroupSingleton, ClassGroupDefault      ClassGroupSingleton, ClassGroupDefault
37    
38    
39  def filenames_equal(name1, name2):  def filenames_equal(name1, name2):
40      """Return true if the filenames name1 and name2 are equal.      """Return true if the filenames name1 and name2 are equal.
41    
# Line 43  def filenames_equal(name1, name2): Line 48  def filenames_equal(name1, name2):
48      return os.path.normpath(name1) == os.path.normpath(name2)      return os.path.normpath(name1) == os.path.normpath(name2)
49    
50    
51  contents_single_map = '''\  
52    class LoadSessionTest(support.FileLoadTestCase):
53    
54        """Base class for .thuban file loading tests
55    
56        Basically the same as the FileLoadTestCase, except that all tests
57        use the '.thuban' extension by default and that setUp and tearDown
58        handle sessions.
59        """
60    
61        file_extension = ".thuban"
62    
63        def setUp(self):
64            """Create the test files"""
65            support.FileLoadTestCase.setUp(self)
66            self.session = None
67    
68        def tearDown(self):
69            if self.session is not None:
70                self.session.Destroy()
71            self.session = None
72    
73    
74    class ClassificationTest(LoadSessionTest):
75    
76        """
77        Base class for tests that do some detailed checking of classifications
78        """
79    
80        def TestLayers(self, layers, expected):
81            TITLE = 0
82            NUM_GROUPS = 1
83            CLASSES = 2
84            GROUP_TYPE = 0
85            GROUP_DATA = 1
86            GROUP_LABEL = 2
87            GROUP_PROPS = 3
88    
89            eq = self.assertEquals
90    
91            eq(len(layers), len(expected))
92    
93            for layer, data in zip(layers, expected):
94                eq(layer.Title(), data[TITLE])
95    
96                clazz = layer.GetClassification()
97                eq(clazz.GetNumGroups(), data[NUM_GROUPS])
98                eq(clazz.GetNumGroups() + 1, len(data[CLASSES]))
99    
100                i = 0
101                for group in clazz:
102                    props = ClassGroupProperties()
103                    props.SetLineColor(
104                        parse_color(data[CLASSES][i][GROUP_PROPS][0]))
105                    props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])
106                    props.SetFill(
107                        parse_color(data[CLASSES][i][GROUP_PROPS][2]))
108    
109                    if data[CLASSES][i][GROUP_TYPE] == "default":
110                        g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])
111                    elif data[CLASSES][i][GROUP_TYPE] == "range":
112                        g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0],
113                                            data[CLASSES][i][GROUP_DATA][1],
114                                            props, data[CLASSES][i][GROUP_LABEL])
115                    elif data[CLASSES][i][GROUP_TYPE] == "single":
116                        g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],
117                                              props, data[CLASSES][i][GROUP_LABEL])
118    
119                    eq(group, g)
120    
121                    i += 1
122    
123    
124    
125    class TestSingleLayer(LoadSessionTest):
126    
127        file_contents = '''\
128  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
129  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban.dtd">
130  <session title="single map&amp;layer">  <session title="single map&amp;layer">
# Line 60  contents_single_map = '''\ Line 141  contents_single_map = '''\
141  </session>  </session>
142  '''  '''
143    
144  contents_classified_map_v0_2 = '''\      def test(self):
145            """Load a session with a single map with a single layer"""
146            eq = self.assertEquals
147            session = load_session(self.filename())
148            self.session = session
149    
150            # Check the title
151            eq(session.Title(), "single map&layer")
152    
153            # the session has one map.
154            maps = session.Maps()
155            eq(len(maps), 1)
156    
157            # Check the map's attributes
158            map = maps[0]
159            eq(map.Title(), "Test Map")
160    
161            # the map has a single layer
162            layers = map.Layers()
163            eq(len(layers), 1)
164    
165            # Check the layer attributes
166            layer = layers[0]
167            eq(layer.Title(), "My Layer")
168            self.failUnless(filenames_equal(layer.ShapeStore().FileName(),
169                                            os.path.join(self.temp_dir(),
170                                                         os.pardir, os.pardir,
171                                                         "Data", "iceland",
172                                                         "political.shp")))
173            eq(layer.GetClassification().GetDefaultFill(), Color.Transparent)
174            eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")
175            eq(layer.Visible(), True)
176    
177            self.session.Destroy()
178            self.session = None
179    
180    
181    class TestLayerVisibility(LoadSessionTest):
182    
183        file_contents = '''\
184    <?xml version="1.0" encoding="UTF-8"?>
185    <!DOCTYPE session SYSTEM "thuban.dtd">
186    <session title="single map&amp;layer">
187            <map title="Test Map">
188                    <projection>
189                            <parameter value="zone=26"/>
190                            <parameter value="proj=utm"/>
191                            <parameter value="ellps=clrk66"/>
192                    </projection>
193                    <layer title="My Layer" stroke_width="1" fill="None"
194                        filename="../../Data/iceland/political.shp"
195                        stroke="#000000" visible="false">
196            </layer>
197        </map>
198    </session>
199    '''
200    
201        def test(self):
202            """Test that the visible flag is correctly loaded for a layer."""
203            eq = self.assertEquals
204            session = load_session(self.filename())
205            self.session = session
206            maps = session.Maps()
207            eq(len(maps), 1)
208            map = maps[0]
209            layers = map.Layers()
210            eq(len(layers), 1)
211            layer = layers[0]
212    
213            eq(layer.Visible(), False)
214    
215    
216    
217    
218    class TestClassification(ClassificationTest):
219    
220        file_contents = '''\
221  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
222  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban.dtd">
223  <session title="single map&amp;layer">  <session title="single map&amp;layer">
# Line 90  contents_classified_map_v0_2 = '''\ Line 247  contents_classified_map_v0_2 = '''\
247                      stroke="#000000">                      stroke="#000000">
248              <classification field="AREA" field_type="double">              <classification field="AREA" field_type="double">
249                  <clnull>                  <clnull>
                     <cldata stroke="#000000" stroke_width="1" fill="None"/>  
                 </clnull>  
                 <clnull>  
250                      <cldata stroke="#000000" stroke_width="2" fill="None"/>                      <cldata stroke="#000000" stroke_width="2" fill="None"/>
251                  </clnull>                  </clnull>
252                  <clrange min="0" max="1">                  <clrange min="0" max="1">
253                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#111111" stroke_width="1" fill="None"/>
254                  </clrange>                  </clrange>
255                  <clpoint value=".5">                  <clpoint value=".5">
256                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="#111111"/>
257                  </clpoint>                  </clpoint>
258                  <clrange min="-1" max="0">                  <clrange min="-1" max="0">
259                      <cldata stroke="#000000" stroke_width="1" fill="None"/>                      <cldata stroke="#000000" stroke_width="1" fill="None"/>
# Line 113  contents_classified_map_v0_2 = '''\ Line 267  contents_classified_map_v0_2 = '''\
267  </session>  </session>
268  '''  '''
269    
270  contents_test_labels = '''\      def test(self):
271            """Load a Thuban session with a map and classified layers."""
272            session = load_session(self.filename())
273            self.session = session
274    
275            map = self.session.Maps()[0] # only one map in the sample
276    
277            expected = [("My Layer", 2,
278                            [("default", (), "",
279                                ("#000000", 1, "None")),
280                             ("single", "1", "",
281                                ("#000000", 2, "None")),
282                             ("single", "1", "",
283                                ("#000000", 10, "None"))]),
284                         ("My Layer 2", 4,
285                             [("default", (), "",
286                                ("#000000", 2, "None")),
287                              ("range", (0, 1), "",
288                                ("#111111", 1, "None")),
289                              ("single", .5, "",
290                                ("#000000", 1, "#111111")),
291                              ("range", (-1, 0), "",
292                                ("#000000", 1, "None")),
293                              ("single", -.5, "",
294                                ("#000000", 1, "None"))])]
295    
296            self.TestLayers(map.Layers(), expected)
297    
298    
299    class TestLabels(ClassificationTest):
300    
301        file_contents = '''\
302  <?xml version="1.0" encoding="UTF-8"?>  <?xml version="1.0" encoding="UTF-8"?>
303  <!DOCTYPE session SYSTEM "thuban.dtd">  <!DOCTYPE session SYSTEM "thuban.dtd">
304  <session title="single map&amp;layer">  <session title="single map&amp;layer">
# Line 139  contents_test_labels = '''\ Line 324  contents_test_labels = '''\
324  </session>  </session>
325  '''  '''
326    
327  class LoadSessionTest(unittest.TestCase, support.FileTestMixin):      def test(self):
328            """Load a session and test for reading the group labels."""
     def setUp(self):  
         """Create the test files"""  
         file = open(self.temp_file_name("load_singlelayer.thuban"), "w")  
         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()  
         self.session = None  
   
     def tearDown(self):  
         if self.session is not None:  
             self.session.Destroy()  
   
     def testSingleLayer(self):  
         """Load a session with a single map with a single layer"""  
329          eq = self.assertEquals          eq = self.assertEquals
330          session = load_session(self.temp_file_name("load_singlelayer.thuban"))          session = load_session(self.filename())
         self.session = session  
   
         # Check the title  
         eq(session.Title(), "single map&layer")  
   
         # the session has one map.  
         maps = session.Maps()  
         eq(len(maps), 1)  
   
         # Check the map's attributes  
         map = maps[0]  
         eq(map.Title(), "Test Map")  
   
         # the map has a single layer  
         layers = map.Layers()  
         eq(len(layers), 1)  
   
         # Check the layer attributes  
         layer = layers[0]  
         eq(layer.Title(), "My Layer")  
         self.failUnless(filenames_equal(layer.filename,  
                                         os.path.join(self.temp_dir(),  
                                                      os.pardir, os.pardir,  
                                                      "Data", "iceland",  
                                                      "political.shp")))  
         eq(layer.GetClassification().GetDefaultFill(), Color.Transparent)  
         eq(layer.GetClassification().GetDefaultLineColor().hex(), "#000000")  
   
         self.session.Destroy()  
         self.session = None  
   
     def testClassification(self):  
         """Load a session with a map and classified layers."""  
   
         session = load_session(self.temp_file_name("load_classified_v0_2.thuban"))  
331          self.session = session          self.session = session
332    
333          map = self.session.Maps()[0] # only one map in the sample          map = self.session.Maps()[0] # only one map in the sample
334    
335          expected = [("My Layer", 2,          expected = [("My Layer", 1,
336                          [("default", (), "",                          [("default", (), "hallo",
                             ("#000000", 1, "None")),  
                          ("single", "1", "",  
                             ("#000000", 2, "None")),  
                          ("single", "1", "",  
                             ("#000000", 10, "None"))]),  
                      ("My Layer 2", 4,  
                          [("default", (), "",  
                             ("#000000", 2, "None")),  
                           ("range", (0, 1), "",  
                             ("#000000", 1, "None")),  
                           ("single", .5, "",  
                             ("#000000", 1, "None")),  
                           ("range", (-1, 0), "",  
337                              ("#000000", 1, "None")),                              ("#000000", 1, "None")),
338                            ("single", -.5, "",                           ("single", "1", "welt",
339                              ("#000000", 1, "None"))])]                              ("#000000", 2, "None"))])]
340    
341          self.TestLayers(map.Layers(), expected)          self.TestLayers(map.Layers(), expected)
342    
     def TestLayers(self, layers, expected):  
343    
344          TITLE = 0  class TestLayerProjection(LoadSessionTest):
         NUM_GROUPS = 1  
         CLASSES = 2  
         GROUP_TYPE = 0  
         GROUP_DATA = 1  
         GROUP_LABEL = 2  
         GROUP_PROPS = 3  
345    
346          eq = self.assertEquals      file_contents = '''\
347    <?xml version="1.0" encoding="UTF-8"?>
348    <!DOCTYPE session SYSTEM "thuban.dtd">
349    <session title="single map&amp;layer">
350            <map title="Test Map">
351                    <projection>
352                            <parameter value="zone=26"/>
353                            <parameter value="proj=utm"/>
354                            <parameter value="ellps=clrk66"/>
355                    </projection>
356                    <layer title="My Layer" stroke_width="1" fill="None"
357                        filename="../../Data/iceland/political.shp"
358                        stroke="#000000">
359                        <projection name="hello">
360                            <parameter value="zone=13"/>
361                            <parameter value="proj=tmerc"/>
362                            <parameter value="ellps=clrk66"/>
363                        </projection>
364                <classification field="POPYREG" field_type="string">
365                    <clnull label="hallo">
366                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
367                    </clnull>
368                    <clpoint label="welt" value="1">
369                        <cldata stroke="#000000" stroke_width="2" fill="None"/>
370                    </clpoint>
371                </classification>
372            </layer>
373                    <layer title="My Layer" stroke_width="1" fill="None"
374                        filename="../../Data/iceland/political.shp"
375                        stroke="#000000">
376                        <projection>
377                            <parameter value="proj=lcc"/>
378                            <parameter value="ellps=clrk66"/>
379                        </projection>
380            </layer>
381        </map>
382    </session>
383    '''
384    
385          eq(len(layers), len(expected))      def test(self):
386            """Test loading layers with projections"""
387            eq = self.assertEquals
388            neq = self.assertNotEqual
389    
390          for layer, data in zip(layers, expected):          session = load_session(self.filename())
391              eq(layer.Title(), data[TITLE])          self.session = session
392    
393              clazz = layer.GetClassification()          map = self.session.Maps()[0] # only one map in the sample
             eq(clazz.GetNumGroups(), data[NUM_GROUPS])  
             eq(clazz.GetNumGroups() + 1, len(data[CLASSES]))  
394    
395              i = 0          layers = map.Layers() # two layers in the sample
             for group in clazz:  
                   
                 props = ClassGroupProperties()  
                 props.SetLineColor(  
                     parse_color(data[CLASSES][i][GROUP_PROPS][0]))  
                 props.SetLineWidth(data[CLASSES][i][GROUP_PROPS][1])  
                 props.SetFill(  
                     parse_color(data[CLASSES][i][GROUP_PROPS][2]))  
396    
397                  if data[CLASSES][i][GROUP_TYPE] == "default":          # test layer with a named projection
398                      g = ClassGroupDefault(props, data[CLASSES][i][GROUP_LABEL])          proj = layers[0].GetProjection()
399                  elif data[CLASSES][i][GROUP_TYPE] == "range":          neq(proj, None)
400                      g = ClassGroupRange(data[CLASSES][i][GROUP_DATA][0],          eq(proj.GetName(), "hello")
401                                          data[CLASSES][i][GROUP_DATA][1],          eq(proj.GetParameter("proj"), "tmerc")
402                                          props, data[CLASSES][i][GROUP_LABEL])          eq(proj.GetParameter("zone"), "13")
403                  elif data[CLASSES][i][GROUP_TYPE] == "single":          eq(proj.GetParameter("ellps"), "clrk66")
404                      g = ClassGroupSingleton(data[CLASSES][i][GROUP_DATA],  
405                                            props, data[CLASSES][i][GROUP_LABEL])          # test layer with an unnamed projection
406            proj = layers[1].GetProjection()
407            neq(proj, None)
408            eq(proj.GetName(), "Unknown")
409            eq(proj.GetParameter("proj"), "lcc")
410            eq(proj.GetParameter("ellps"), "clrk66")
411    
                 eq(group, g)  
412    
413                  i += 1  class TestRasterLayer(LoadSessionTest):
414    
415      def testLabels(self):      file_contents = '''\
416          """Load a session and test for reading the group labels."""  <?xml version="1.0" encoding="UTF-8"?>
417    <!DOCTYPE session SYSTEM "thuban.dtd">
418    <session title="single map&amp;layer">
419            <map title="Test Map">
420                    <rasterlayer title="My RasterLayer"
421                         filename="../../Data/iceland/island.tif"
422                         visible="false">
423            </rasterlayer>
424        </map>
425    </session>
426    '''
427    
428        def test(self):
429          eq = self.assertEquals          eq = self.assertEquals
430          session = load_session(self.temp_file_name("load_labels.thuban"))          neq = self.assertNotEqual
431    
432            session = load_session(self.filename())
433          self.session = session          self.session = session
434    
435          map = self.session.Maps()[0] # only one map in the sample          map = self.session.Maps()[0] # only one map in the sample
436    
437          expected = [("My Layer", 1,          layer = map.Layers()[0] # one layer in the sample
                         [("default", (), "hallo",  
                             ("#000000", 1, "None")),  
                          ("single", "1", "welt",  
                             ("#000000", 2, "None"))])]  
438    
439          self.TestLayers(map.Layers(), expected)          eq(layer.Title(), "My RasterLayer")
440            self.failIf(layer.Visible())
441            self.failUnless(filenames_equal(layer.GetImageFilename(),
442                                            os.path.join(self.temp_dir(),
443                                                         os.pardir, os.pardir,
444                                                         "Data", "iceland",
445                                                         "island.tif")))
446    
447  if __name__ == "__main__":  if __name__ == "__main__":
448      unittest.main()      unittest.main()
   
   
   

Legend:
Removed from v.690  
changed lines
  Added in v.1247

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26