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

Legend:
Removed from v.691  
changed lines
  Added in v.957

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26