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

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

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

revision 331 by bh, Fri Sep 20 14:34:23 2002 UTC revision 832 by jonathan, Tue May 6 12:07: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 20  import support Line 20  import support
20  support.initthuban()  support.initthuban()
21    
22  import shapelib  import shapelib
23    import dbflib
24    
25    from Thuban.Model.session import Session
26  from Thuban.Model.layer import Layer, SHAPETYPE_POLYGON, SHAPETYPE_ARC, \  from Thuban.Model.layer import Layer, SHAPETYPE_POLYGON, SHAPETYPE_ARC, \
27       SHAPETYPE_POINT       SHAPETYPE_POINT
28  from Thuban.Model.messages import LAYER_LEGEND_CHANGED, \  from Thuban.Model.messages import LAYER_LEGEND_CHANGED, \
# Line 37  class TestLayer(unittest.TestCase, suppo Line 39  class TestLayer(unittest.TestCase, suppo
39          for i in range(len(test)):          for i in range(len(test)):
40              self.assertFloatSeqEqual(test[i], value[i])              self.assertFloatSeqEqual(test[i], value[i])
41    
42        def setUp(self):
43            """Create a session"""
44            self.session = Session("Test session for %s" % self.__class__)
45    
46        def tearDown(self):
47            self.session = None
48    
49      def test_arc_layer(self):      def test_arc_layer(self):
50          """Test Layer with arc shapes"""          """Test Layer with arc shapes"""
51          layer = Layer("Test Layer",          filename = os.path.join("..", "Data", "iceland", "roads-line.shp")
52                        os.path.join("..", "Data", "iceland", "roads-line.shp"))          layer = Layer("Test Layer", self.session.OpenShapefile(filename))
53          self.assertEquals(layer.Title(), "Test Layer")          self.assertEquals(layer.Title(), "Test Layer")
54          self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC)          self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC)
55          self.assertEquals(layer.NumShapes(), 839)          self.assertEquals(layer.NumShapes(), 839)
# Line 54  class TestLayer(unittest.TestCase, suppo Line 63  class TestLayer(unittest.TestCase, suppo
63          self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.75, 64.25)),          self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.75, 64.25)),
64                            [613, 726, 838])                            [613, 726, 838])
65    
66            self.assertFloatSeqEqual(layer.ShapesBoundingBox([32]),
67                              [-15.082174301147461, 66.27339172363281,
68                               -15.026350021362305, 66.27738189697265])
69    
70            shape = layer.Shape(33)
71            self.assertFloatTuplesEqual(shape.Points(),
72                                        [(-22.248506546020508, 66.30645751953125),
73                                         (-22.232730865478516, 66.294075012207031),
74                                         (-22.23158073425293,  66.287689208984375),
75                                         (-22.246318817138672, 66.270065307617188)])
76    
77            self.assertFloatSeqEqual(layer.ShapesBoundingBox([32, 33]),
78                                     [-22.248506546020508, 66.270065307617188,
79                                      -15.026350021362305, 66.30645751953125])
80    
81            self.assertEquals(layer.ShapesBoundingBox([]), None)
82            self.assertEquals(layer.ShapesBoundingBox(None), None)
83    
84            layer.Destroy()
85    
86      def test_polygon_layer(self):      def test_polygon_layer(self):
87          """Test Layer with polygon shapes"""          """Test Layer with polygon shapes"""
88          layer = Layer("Test Layer",          filename = os.path.join("..", "Data", "iceland", "political.shp")
89                        os.path.join("..", "Data", "iceland", "political.shp"))          layer = Layer("Test Layer", self.session.OpenShapefile(filename))
90          self.assertEquals(layer.Title(), "Test Layer")          self.assertEquals(layer.Title(), "Test Layer")
91          self.assertEquals(layer.ShapeType(), SHAPETYPE_POLYGON)          self.assertEquals(layer.ShapeType(), SHAPETYPE_POLYGON)
92          self.assertEquals(layer.NumShapes(), 156)          self.assertEquals(layer.NumShapes(), 156)
# Line 72  class TestLayer(unittest.TestCase, suppo Line 101  class TestLayer(unittest.TestCase, suppo
101                                    -13.495815277099609, 66.563774108886719])                                    -13.495815277099609, 66.563774108886719])
102          self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.9, 64.1)),          self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.9, 64.1)),
103                            [91, 92, 144, 146, 148, 150, 152, 153])                            [91, 92, 144, 146, 148, 150, 152, 153])
104            layer.Destroy()
105    
106      def test_point_layer(self):      def test_point_layer(self):
107          """Test Layer with point shapes"""          """Test Layer with point shapes"""
108          layer = Layer("Test Layer",          filename = os.path.join("..", "Data", "iceland",
109                        os.path.join("..", "Data", "iceland",                                  "cultural_landmark-point.shp")
110                                     "cultural_landmark-point.shp"))          layer = Layer("Test Layer", self.session.OpenShapefile(filename))
111          self.assertEquals(layer.Title(), "Test Layer")          self.assertEquals(layer.Title(), "Test Layer")
112          self.assertEquals(layer.ShapeType(), SHAPETYPE_POINT)          self.assertEquals(layer.ShapeType(), SHAPETYPE_POINT)
113          self.assertEquals(layer.NumShapes(), 34)          self.assertEquals(layer.NumShapes(), 34)
# Line 89  class TestLayer(unittest.TestCase, suppo Line 119  class TestLayer(unittest.TestCase, suppo
119                                    -15.12291431427002, 66.36572265625])                                    -15.12291431427002, 66.36572265625])
120          self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.80, 64.1)),          self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.80, 64.1)),
121                            [0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31])                            [0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31])
122            layer.Destroy()
123    
124      def test_empty_layer(self):      def test_empty_layer(self):
125          """Test Layer with empty shape file"""          """Test Layer with empty shape file"""
# Line 96  class TestLayer(unittest.TestCase, suppo Line 127  class TestLayer(unittest.TestCase, suppo
127          shapefilename = self.temp_file_name("layer_empty.shp")          shapefilename = self.temp_file_name("layer_empty.shp")
128          shp = shapelib.create(shapefilename, shapelib.SHPT_POLYGON)          shp = shapelib.create(shapefilename, shapelib.SHPT_POLYGON)
129          shp.close()          shp.close()
130            # create an empty DBF file too because Thuban can't cope yet
131          layer = Layer("Empty Layer", shapefilename)          # with missing DBF file.
132            dbffilename = self.temp_file_name("layer_empty.dbf")
133            dbf = dbflib.create(dbffilename)
134            dbf.add_field("NAME", dbflib.FTString, 20, 0)
135    
136            # Now try to open it.
137            layer = Layer("Empty Layer",
138                          self.session.OpenShapefile(shapefilename))
139          self.assertEquals(layer.BoundingBox(), None)          self.assertEquals(layer.BoundingBox(), None)
140          self.assertEquals(layer.LatLongBoundingBox(), None)          self.assertEquals(layer.LatLongBoundingBox(), None)
141          self.assertEquals(layer.NumShapes(), 0)          self.assertEquals(layer.NumShapes(), 0)
142            layer.Destroy()
143    
144    
145  class TestLayerLegend(unittest.TestCase, support.SubscriberMixin):  class TestLayerLegend(unittest.TestCase, support.SubscriberMixin):
# Line 109  class TestLayerLegend(unittest.TestCase, Line 148  class TestLayerLegend(unittest.TestCase,
148      """      """
149    
150      def setUp(self):      def setUp(self):
151          """Clear the list of received messages and create a layer          """Clear the list of received messages and create a layer and a session
152    
153          The layer is bound to self.layer.          The layer is bound to self.layer and the session to self.session.
154          """          """
155          self.clear_messages()          self.clear_messages()
156            self.session = Session("Test session for %s" % self.__class__)
157            filename = os.path.join("..", "Data", "iceland", "political.shp")
158          self.layer = Layer("Test Layer",          self.layer = Layer("Test Layer",
159                             os.path.join("..", "Data", "iceland",                             self.session.OpenShapefile(filename))
                                         "political.shp"))  
160          self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params,          self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params,
161                               LAYER_LEGEND_CHANGED)                               LAYER_LEGEND_CHANGED)
162          self.layer.Subscribe(LAYER_VISIBILITY_CHANGED,          self.layer.Subscribe(LAYER_VISIBILITY_CHANGED,
# Line 127  class TestLayerLegend(unittest.TestCase, Line 167  class TestLayerLegend(unittest.TestCase,
167          """Clear the list of received messages and explictly destroy self.layer          """Clear the list of received messages and explictly destroy self.layer
168          """          """
169          self.layer.Destroy()          self.layer.Destroy()
170            self.layer = None
171            self.session.Destroy()
172            self.session = None
173          self.clear_messages()          self.clear_messages()
174    
175      def test_initial_settings(self):      def test_initial_settings(self):
176          """Test Layer's initial legend attributes"""          """Test Layer's initial legend attributes"""
177          # test default settings          # test default settings
178          self.failIf(self.layer.WasModified())          self.failIf(self.layer.WasModified())
179          self.assertEquals(self.layer.fill, None)          #self.assertEquals(self.layer.fill, None)
180          self.assertEquals(self.layer.stroke.hex(), "#000000")          #self.assertEquals(self.layer.stroke.hex(), "#000000")
181          self.assertEquals(self.layer.stroke_width, 1)          #self.assertEquals(self.layer.stroke_width, 1)
182          self.assertEquals(self.layer.Visible(), 1)          self.assertEquals(self.layer.Visible(), 1)
183          # no messages should have been produced          # no messages should have been produced
184          self.check_messages([])          self.check_messages([])
185    
     def test_fill(self):  
         """Test Layer's fill attribute"""  
         # modify the fill  
         self.layer.SetFill(Color(0.5, 1.0, 0.75))  
         self.check_messages([(self.layer, LAYER_LEGEND_CHANGED)])  
         self.assertEquals(self.layer.fill.hex().lower(), "#7fffbf")  
         self.assert_(self.layer.WasModified())  
   
     def test_stroke(self):  
         """Test Layer's stroke attribute"""  
         self.layer.SetStroke(Color(0.5, 1.0, 0.75))  
         self.assertEquals(self.layer.stroke.hex().lower(), "#7fffbf")  
         self.check_messages([(self.layer, LAYER_LEGEND_CHANGED)])  
         self.assert_(self.layer.WasModified())  
   
     def test_stroke_width(self):  
         """Test Layer's stroke_width attribute"""  
         self.layer.SetStrokeWidth(3.0)  
         self.assertEquals(self.layer.stroke_width, 3.0)  
         self.check_messages([(self.layer, LAYER_LEGEND_CHANGED)])  
         self.assert_(self.layer.WasModified())  
   
186      def test_visibility(self):      def test_visibility(self):
187          """Test Layer visibility"""          """Test Layer visibility"""
188          self.layer.SetVisible(0)          self.layer.SetVisible(0)
# Line 172  class TestLayerLegend(unittest.TestCase, Line 193  class TestLayerLegend(unittest.TestCase,
193          # the layer.          # the layer.
194          self.failIf(self.layer.WasModified())          self.failIf(self.layer.WasModified())
195    
196      def test_tree_info(self):      
197          """Test Layer.TreeInfo"""  #
198          self.assertEquals(self.layer.TreeInfo(),  # the tree info now contains Color objects which are difficult to test
199                            ("Layer 'Test Layer'",  #
200                             ['Shown',  #   def test_tree_info(self):
201                              'Shapes: 156',  #       """Test Layer.TreeInfo"""
202                              ('Extent (lat-lon):'  #       self.assertEquals(self.layer.TreeInfo(),
203                               ' (-24.5465, 63.2868, -13.4958, 66.5638)'),  #                         ("Layer 'Test Layer'",
204                              'Shapetype: Polygon',  #                          ['Shown',
205                              'Fill: None',  #                           'Shapes: 156',
206                              'Outline: (0.000, 0.000, 0.000)']))  #                           ('Extent (lat-lon):'
207    #                            ' (-24.5465, 63.2868, -13.4958, 66.5638)'),
208    #                           'Shapetype: Polygon',
209    #                           'Fill: None',
210    #                           'Outline: (0.000, 0.000, 0.000)']))
211    
212    
213  if __name__ == "__main__":  if __name__ == "__main__":
214      unittest.main()      support.run_tests()

Legend:
Removed from v.331  
changed lines
  Added in v.832

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26