/[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 292 by bh, Fri Aug 30 09:44:12 2002 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 19  import unittest Line 19  import unittest
19  import support  import support
20  support.initthuban()  support.initthuban()
21    
22  from Thuban.Model.load import load_session  from Thuban.Model.load import load_session, parse_color
23  from Thuban.Model.session import Session  from Thuban.Model.color import Color
24  from Thuban.Model.map import Map  from Thuban.Model.classification import ClassGroupProperties, ClassGroupRange,\
25  from Thuban.Model.layer import Layer      ClassGroupSingleton, ClassGroupDefault
26  from Thuban.Model.proj import Projection  
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.
# Line 37  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 54  contents_single_map = '''\ Line 130  contents_single_map = '''\
130  </session>  </session>
131  '''  '''
132    
133        def test(self):
 class LoadSessionTest(unittest.TestCase, support.FileTestMixin):  
   
     def setUp(self):  
         """Create the test files"""  
         file = open(self.temp_file_name("load_singlelayer.thuban"), "w")  
         file.write(contents_single_map)  
         file.close()  
         self.session = None  
   
     def tearDown(self):  
         if self.session is not None:  
             self.session.Destroy()  
   
     def testSingleLayer(self):  
134          """Load a session with a single map with a single layer"""          """Load a session with a single map with a single layer"""
135          eq = self.assertEquals          eq = self.assertEquals
136          session = load_session(self.temp_file_name("load_singlelayer.thuban"))          session = load_session(self.filename())
137          self.session = session          self.session = session
138    
139          # Check the title          # Check the title
# Line 97  class LoadSessionTest(unittest.TestCase, Line 159  class LoadSessionTest(unittest.TestCase,
159                                                       os.pardir, os.pardir,                                                       os.pardir, os.pardir,
160                                                       "Data", "iceland",                                                       "Data", "iceland",
161                                                       "political.shp")))                                                       "political.shp")))
162          eq(layer.fill, None)          eq(layer.GetClassification().GetDefaultFill(), Color.Transparent)
163          eq(layer.stroke.hex(), "#000000")          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"?>
211    <!DOCTYPE session SYSTEM "thuban.dtd">
212    <session title="single map&amp;layer">
213            <map title="Test Map">
214                    <projection>
215                            <parameter value="zone=26"/>
216                            <parameter value="proj=utm"/>
217                            <parameter value="ellps=clrk66"/>
218                    </projection>
219                    <layer title="My Layer" stroke_width="1" fill="None"
220                        filename="../../Data/iceland/political.shp"
221                        stroke="#000000">
222                <classification field="POPYREG" field_type="string">
223                    <clnull>
224                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
225                    </clnull>
226                    <clpoint value="1">
227                        <cldata stroke="#000000" stroke_width="2" fill="None"/>
228                    </clpoint>
229                    <clpoint value="1">
230                        <cldata stroke="#000000" stroke_width="10" fill="None"/>
231                    </clpoint>
232                </classification>
233            </layer>
234                    <layer title="My Layer 2" stroke_width="1" fill="None"
235                        filename="../../Data/iceland/political.shp"
236                        stroke="#000000">
237                <classification field="AREA" field_type="double">
238                    <clnull>
239                        <cldata stroke="#000000" stroke_width="2" fill="None"/>
240                    </clnull>
241                    <clrange min="0" max="1">
242                        <cldata stroke="#111111" stroke_width="1" fill="None"/>
243                    </clrange>
244                    <clpoint value=".5">
245                        <cldata stroke="#000000" stroke_width="1" fill="#111111"/>
246                    </clpoint>
247                    <clrange min="-1" max="0">
248                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
249                    </clrange>
250                    <clpoint value="-.5">
251                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
252                    </clpoint>
253                </classification>
254            </layer>
255            </map>
256    </session>
257    '''
258    
259        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"?>
292    <!DOCTYPE session SYSTEM "thuban.dtd">
293    <session title="single map&amp;layer">
294            <map title="Test Map">
295                    <projection>
296                            <parameter value="zone=26"/>
297                            <parameter value="proj=utm"/>
298                            <parameter value="ellps=clrk66"/>
299                    </projection>
300                    <layer title="My Layer" stroke_width="1" fill="None"
301                        filename="../../Data/iceland/political.shp"
302                        stroke="#000000">
303                <classification field="POPYREG" field_type="string">
304                    <clnull label="hallo">
305                        <cldata stroke="#000000" stroke_width="1" fill="None"/>
306                    </clnull>
307                    <clpoint label="welt" value="1">
308                        <cldata stroke="#000000" stroke_width="2" fill="None"/>
309                    </clpoint>
310                </classification>
311            </layer>
312        </map>
313    </session>
314    '''
315    
316        def test(self):
317            """Load a session and test for reading the group labels."""
318            eq = self.assertEquals
319            session = load_session(self.filename())
320            self.session = session
321    
322            map = self.session.Maps()[0] # only one map in the sample
323    
324            expected = [("My Layer", 1,
325                            [("default", (), "hallo",
326                                ("#000000", 1, "None")),
327                             ("single", "1", "welt",
328                                ("#000000", 2, "None"))])]
329    
330            self.TestLayers(map.Layers(), expected)
331    
332    
333    class TestLayerProjection(LoadSessionTest):
334    
335        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        def test(self):
375            """Test loading layers with projections"""
376            eq = self.assertEquals
377            neq = self.assertNotEqual
378    
379            session = load_session(self.filename())
380            self.session = session
381    
382            map = self.session.Maps()[0] # only one map in the sample
383    
384            layers = map.Layers() # two layers in the sample
385    
386            # test layer with a named projection
387            proj = layers[0].GetProjection()
388            neq(proj, None)
389            eq(proj.GetName(), "hello")
390            eq(proj.GetParameter("proj"), "tmerc")
391            eq(proj.GetParameter("zone"), "13")
392            eq(proj.GetParameter("ellps"), "clrk66")
393    
394            # 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    
401    
402    class TestRasterLayer(LoadSessionTest):
403    
404        file_contents = '''\
405    <?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
419            neq = self.assertNotEqual
420    
421            session = load_session(self.filename())
422            self.session = session
423    
424            map = self.session.Maps()[0] # only one map in the sample
425    
426            layer = map.Layers()[0] # one layer in the sample
427    
428            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.292  
changed lines
  Added in v.957

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26