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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 832 - (hide annotations)
Tue May 6 12:07:36 2003 UTC (21 years, 10 months ago) by jonathan
Original Path: trunk/thuban/test/test_layer.py
File MIME type: text/x-python
File size: 8734 byte(s)
(TestLayer.test_arc_layer): Add some tests for Layer.ShapesBoundingBox().

1 bh 599 # Copyright (c) 2002, 2003 by Intevation GmbH
2 bh 331 # Authors:
3     # Bernhard Herzog <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     """
9     Test the Layer class
10     """
11    
12     __version__ = "$Revision$"
13     # $Source$
14     # $Id$
15    
16     import os
17     import unittest
18    
19     import support
20     support.initthuban()
21    
22     import shapelib
23 bh 336 import dbflib
24 bh 331
25 bh 723 from Thuban.Model.session import Session
26 bh 331 from Thuban.Model.layer import Layer, SHAPETYPE_POLYGON, SHAPETYPE_ARC, \
27     SHAPETYPE_POINT
28     from Thuban.Model.messages import LAYER_LEGEND_CHANGED, \
29     LAYER_VISIBILITY_CHANGED
30     from Thuban.Model.color import Color
31    
32     class TestLayer(unittest.TestCase, support.FileTestMixin,
33     support.FloatComparisonMixin):
34    
35     """Test cases for different layer (shape) types"""
36    
37     def assertFloatTuplesEqual(self, test, value):
38     """Assert equality of two lists of tuples of float"""
39     for i in range(len(test)):
40     self.assertFloatSeqEqual(test[i], value[i])
41    
42 bh 723 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 bh 331 def test_arc_layer(self):
50     """Test Layer with arc shapes"""
51 bh 723 filename = os.path.join("..", "Data", "iceland", "roads-line.shp")
52     layer = Layer("Test Layer", self.session.OpenShapefile(filename))
53 bh 331 self.assertEquals(layer.Title(), "Test Layer")
54     self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC)
55     self.assertEquals(layer.NumShapes(), 839)
56     shape = layer.Shape(32)
57     self.assertFloatTuplesEqual(shape.Points(),
58     [(-15.082174301147461, 66.27738189697265),
59     (-15.026350021362305, 66.27339172363281)])
60     self.assertFloatSeqEqual(layer.BoundingBox(),
61     [-24.450359344482422, 63.426830291748047,
62     -13.55668830871582, 66.520111083984375])
63     self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.75, 64.25)),
64     [613, 726, 838])
65 jonathan 832
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 bh 599 layer.Destroy()
85 bh 331
86     def test_polygon_layer(self):
87     """Test Layer with polygon shapes"""
88 bh 723 filename = os.path.join("..", "Data", "iceland", "political.shp")
89     layer = Layer("Test Layer", self.session.OpenShapefile(filename))
90 bh 331 self.assertEquals(layer.Title(), "Test Layer")
91     self.assertEquals(layer.ShapeType(), SHAPETYPE_POLYGON)
92     self.assertEquals(layer.NumShapes(), 156)
93     shape = layer.Shape(4)
94     self.assertFloatTuplesEqual(shape.Points(),
95     [(-22.406391143798828, 64.714111328125),
96     (-22.41621208190918, 64.71600341796875),
97     (-22.406051635742188, 64.719200134277344),
98     (-22.406391143798828, 64.714111328125)])
99     self.assertFloatSeqEqual(layer.BoundingBox(),
100     [-24.546524047851562, 63.286754608154297,
101     -13.495815277099609, 66.563774108886719])
102     self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.9, 64.1)),
103     [91, 92, 144, 146, 148, 150, 152, 153])
104 bh 599 layer.Destroy()
105 bh 331
106     def test_point_layer(self):
107     """Test Layer with point shapes"""
108 bh 723 filename = os.path.join("..", "Data", "iceland",
109     "cultural_landmark-point.shp")
110     layer = Layer("Test Layer", self.session.OpenShapefile(filename))
111 bh 331 self.assertEquals(layer.Title(), "Test Layer")
112     self.assertEquals(layer.ShapeType(), SHAPETYPE_POINT)
113     self.assertEquals(layer.NumShapes(), 34)
114     shape = layer.Shape(0)
115     self.assertFloatTuplesEqual(shape.Points(),
116     [(-22.711074829101562, 66.36572265625)])
117     self.assertFloatSeqEqual(layer.BoundingBox(),
118     [-23.806047439575195, 63.405960083007812,
119     -15.12291431427002, 66.36572265625])
120     self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.80, 64.1)),
121     [0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31])
122 bh 599 layer.Destroy()
123 bh 331
124     def test_empty_layer(self):
125     """Test Layer with empty shape file"""
126     # create an empty shape file
127     shapefilename = self.temp_file_name("layer_empty.shp")
128     shp = shapelib.create(shapefilename, shapelib.SHPT_POLYGON)
129     shp.close()
130 bh 336 # create an empty DBF file too because Thuban can't cope yet
131     # 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 bh 331
136 bh 336 # Now try to open it.
137 bh 723 layer = Layer("Empty Layer",
138     self.session.OpenShapefile(shapefilename))
139 bh 331 self.assertEquals(layer.BoundingBox(), None)
140     self.assertEquals(layer.LatLongBoundingBox(), None)
141     self.assertEquals(layer.NumShapes(), 0)
142 bh 599 layer.Destroy()
143 bh 331
144    
145     class TestLayerLegend(unittest.TestCase, support.SubscriberMixin):
146    
147     """Test cases for Layer method that modify the layer.
148     """
149    
150     def setUp(self):
151 bh 723 """Clear the list of received messages and create a layer and a session
152 bh 331
153 bh 723 The layer is bound to self.layer and the session to self.session.
154 bh 331 """
155     self.clear_messages()
156 bh 723 self.session = Session("Test session for %s" % self.__class__)
157     filename = os.path.join("..", "Data", "iceland", "political.shp")
158 bh 331 self.layer = Layer("Test Layer",
159 bh 723 self.session.OpenShapefile(filename))
160 bh 331 self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params,
161     LAYER_LEGEND_CHANGED)
162     self.layer.Subscribe(LAYER_VISIBILITY_CHANGED,
163     self.subscribe_with_params,
164     LAYER_VISIBILITY_CHANGED)
165    
166     def tearDown(self):
167     """Clear the list of received messages and explictly destroy self.layer
168     """
169     self.layer.Destroy()
170 bh 723 self.layer = None
171     self.session.Destroy()
172     self.session = None
173 bh 331 self.clear_messages()
174    
175     def test_initial_settings(self):
176     """Test Layer's initial legend attributes"""
177     # test default settings
178     self.failIf(self.layer.WasModified())
179 jonathan 409 #self.assertEquals(self.layer.fill, None)
180     #self.assertEquals(self.layer.stroke.hex(), "#000000")
181     #self.assertEquals(self.layer.stroke_width, 1)
182 bh 331 self.assertEquals(self.layer.Visible(), 1)
183     # no messages should have been produced
184     self.check_messages([])
185    
186     def test_visibility(self):
187     """Test Layer visibility"""
188     self.layer.SetVisible(0)
189     self.assertEquals(self.layer.Visible(), 0)
190     self.check_messages([(self.layer, LAYER_VISIBILITY_CHANGED)])
191    
192     # currently, modifying the visibility doesn't count as changing
193     # the layer.
194     self.failIf(self.layer.WasModified())
195    
196 jonathan 395
197     #
198     # the tree info now contains Color objects which are difficult to test
199     #
200     # def test_tree_info(self):
201     # """Test Layer.TreeInfo"""
202     # self.assertEquals(self.layer.TreeInfo(),
203     # ("Layer 'Test Layer'",
204     # ['Shown',
205     # 'Shapes: 156',
206     # ('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 bh 331
212    
213     if __name__ == "__main__":
214 bh 599 support.run_tests()

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26