/[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 996 - (hide annotations)
Thu May 22 18:21:10 2003 UTC (21 years, 9 months ago) by bh
Original Path: trunk/thuban/test/test_layer.py
File MIME type: text/x-python
File size: 11268 byte(s)
* test/test_layer.py (TestLayer.setUp, TestLayer.tearDown):
Provide a better way to destroy the layers
(TestLayer.test_base_layer, TestLayer.test_arc_layer)
(TestLayer.test_point_layer, TestLayer.test_empty_layer)
(TestLayer.test_polygon_layer, TestLayer.test_get_field_type): Use
the new way to destroy the layers.
(TestLayer.test_derived_store): New. Test for using a layer with a
DerivedShapeStore

* Thuban/Model/layer.py (Layer.SetShapeStore): Only set the
filename if the shape store actually has one.

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 jonathan 947 from Thuban.Model.layer import BaseLayer, Layer, RasterLayer, \
27     SHAPETYPE_POLYGON, SHAPETYPE_ARC, SHAPETYPE_POINT
28 bh 331 from Thuban.Model.messages import LAYER_LEGEND_CHANGED, \
29     LAYER_VISIBILITY_CHANGED
30     from Thuban.Model.color import Color
31 bh 839 from Thuban.Model.table import FIELDTYPE_DOUBLE
32 jonathan 947 from Thuban.Model.proj import Projection
33 bh 996 from Thuban.Model.data import DerivedShapeStore
34 bh 331
35     class TestLayer(unittest.TestCase, support.FileTestMixin,
36     support.FloatComparisonMixin):
37    
38     """Test cases for different layer (shape) types"""
39    
40     def assertFloatTuplesEqual(self, test, value):
41     """Assert equality of two lists of tuples of float"""
42     for i in range(len(test)):
43     self.assertFloatSeqEqual(test[i], value[i])
44    
45 bh 723 def setUp(self):
46 bh 996 """Create a session self.session and initialize self.layer to None"""
47 bh 723 self.session = Session("Test session for %s" % self.__class__)
48 bh 996 self.layer = None
49 bh 723
50     def tearDown(self):
51 bh 996 """Call the layer's Destroy method and set session and layer to None"""
52 bh 723 self.session = None
53 bh 996 if self.layer is not None:
54     self.layer.Destroy()
55     self.layer = None
56 bh 723
57 jonathan 947 def build_path(self, filename):
58     return os.path.join("..", "Data", "iceland", filename)
59 bh 996
60 bh 839 def open_shapefile(self, filename):
61     """Open and return a shapestore for filename in the iceland data set"""
62 jonathan 947 return self.session.OpenShapefile(self.build_path(filename))
63 bh 839
64 jonathan 947 def test_base_layer(self):
65 bh 996 layer = self.layer = BaseLayer("Test BaseLayer")
66 jonathan 947 self.assertEquals(layer.Title(), "Test BaseLayer")
67     self.failUnless(layer.Visible())
68    
69     # toggle visibility
70     layer.SetVisible(False)
71     self.failIf(layer.Visible())
72    
73     layer.SetVisible(True)
74     self.failUnless(layer.Visible())
75    
76     self.failIf(layer.HasClassification())
77     self.assertEquals(layer.GetProjection(), None)
78    
79     # set/get projection
80     proj = Projection(["proj=utm", "zone=26"])
81    
82     layer.SetProjection(proj)
83     self.failUnless(layer.GetProjection() is proj)
84    
85     # __init__ with other arguments
86     layer = BaseLayer("Test BaseLayer", False, proj)
87     self.failIf(layer.Visible())
88     self.failUnless(layer.GetProjection() is proj)
89    
90 bh 331 def test_arc_layer(self):
91     """Test Layer with arc shapes"""
92 bh 996 layer = self.layer = Layer("Test Layer",
93     self.open_shapefile("roads-line.shp"))
94 bh 331 self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC)
95     self.assertEquals(layer.NumShapes(), 839)
96     shape = layer.Shape(32)
97     self.assertFloatTuplesEqual(shape.Points(),
98     [(-15.082174301147461, 66.27738189697265),
99     (-15.026350021362305, 66.27339172363281)])
100     self.assertFloatSeqEqual(layer.BoundingBox(),
101     [-24.450359344482422, 63.426830291748047,
102     -13.55668830871582, 66.520111083984375])
103     self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.75, 64.25)),
104     [613, 726, 838])
105 jonathan 832
106     self.assertFloatSeqEqual(layer.ShapesBoundingBox([32]),
107     [-15.082174301147461, 66.27339172363281,
108     -15.026350021362305, 66.27738189697265])
109    
110     shape = layer.Shape(33)
111     self.assertFloatTuplesEqual(shape.Points(),
112     [(-22.248506546020508, 66.30645751953125),
113     (-22.232730865478516, 66.294075012207031),
114     (-22.23158073425293, 66.287689208984375),
115     (-22.246318817138672, 66.270065307617188)])
116    
117     self.assertFloatSeqEqual(layer.ShapesBoundingBox([32, 33]),
118     [-22.248506546020508, 66.270065307617188,
119     -15.026350021362305, 66.30645751953125])
120    
121     self.assertEquals(layer.ShapesBoundingBox([]), None)
122     self.assertEquals(layer.ShapesBoundingBox(None), None)
123    
124 bh 331 def test_polygon_layer(self):
125     """Test Layer with polygon shapes"""
126 bh 996 layer = self.layer = Layer("Test Layer",
127     self.open_shapefile("political.shp"))
128 bh 331 self.assertEquals(layer.ShapeType(), SHAPETYPE_POLYGON)
129     self.assertEquals(layer.NumShapes(), 156)
130     shape = layer.Shape(4)
131     self.assertFloatTuplesEqual(shape.Points(),
132     [(-22.406391143798828, 64.714111328125),
133     (-22.41621208190918, 64.71600341796875),
134     (-22.406051635742188, 64.719200134277344),
135     (-22.406391143798828, 64.714111328125)])
136     self.assertFloatSeqEqual(layer.BoundingBox(),
137     [-24.546524047851562, 63.286754608154297,
138     -13.495815277099609, 66.563774108886719])
139     self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.9, 64.1)),
140     [91, 92, 144, 146, 148, 150, 152, 153])
141    
142     def test_point_layer(self):
143     """Test Layer with point shapes"""
144 bh 996 layer = self.layer = Layer("Test Layer",
145     self.open_shapefile("cultural_landmark-point.shp"))
146 bh 331 self.assertEquals(layer.ShapeType(), SHAPETYPE_POINT)
147     self.assertEquals(layer.NumShapes(), 34)
148     shape = layer.Shape(0)
149     self.assertFloatTuplesEqual(shape.Points(),
150     [(-22.711074829101562, 66.36572265625)])
151     self.assertFloatSeqEqual(layer.BoundingBox(),
152     [-23.806047439575195, 63.405960083007812,
153     -15.12291431427002, 66.36572265625])
154     self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.80, 64.1)),
155     [0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31])
156    
157     def test_empty_layer(self):
158     """Test Layer with empty shape file"""
159     # create an empty shape file
160     shapefilename = self.temp_file_name("layer_empty.shp")
161     shp = shapelib.create(shapefilename, shapelib.SHPT_POLYGON)
162     shp.close()
163 bh 336 # create an empty DBF file too because Thuban can't cope yet
164     # with missing DBF file.
165     dbffilename = self.temp_file_name("layer_empty.dbf")
166     dbf = dbflib.create(dbffilename)
167     dbf.add_field("NAME", dbflib.FTString, 20, 0)
168 bh 331
169 bh 336 # Now try to open it.
170 bh 996 layer = self.layer = Layer("Empty Layer",
171     self.session.OpenShapefile(shapefilename))
172 bh 331 self.assertEquals(layer.BoundingBox(), None)
173     self.assertEquals(layer.LatLongBoundingBox(), None)
174     self.assertEquals(layer.NumShapes(), 0)
175    
176 bh 839 def test_get_field_type(self):
177     """Test Layer.GetFieldType()"""
178 bh 996 layer = self.layer = Layer("Test Layer",
179     self.open_shapefile("roads-line.shp"))
180 bh 839 self.assertEquals(layer.GetFieldType("LENGTH"), FIELDTYPE_DOUBLE)
181     self.assertEquals(layer.GetFieldType("non existing"), None)
182 bh 331
183 jonathan 947 def test_raster_layer(self):
184     filename = self.build_path("island.tif")
185     layer = RasterLayer("Test RasterLayer", filename)
186     self.assertEquals(layer.GetImageFilename(), filename)
187     self.assertFloatSeqEqual(layer.BoundingBox(),
188     [-24.5500000, 63.2833330,
189     -13.4916670, 66.5666670])
190     self.assertFloatSeqEqual(layer.LatLongBoundingBox(),
191     [-24.5500000, 63.2833330,
192     -13.4916670, 66.5666670])
193 bh 839
194 bh 996 def test_derived_store(self):
195     """Test layer with derived store"""
196     layer = self.layer = Layer("Test Layer",
197     self.open_shapefile("roads-line.shp"))
198     try:
199     store = layer.ShapeStore()
200     derived = DerivedShapeStore(store, store.Table())
201     layer.SetShapeStore(derived)
202     self.assert_(layer.ShapeStore() is derived)
203     finally:
204     store = derived = None
205 jonathan 947
206 bh 996
207 bh 331 class TestLayerLegend(unittest.TestCase, support.SubscriberMixin):
208    
209     """Test cases for Layer method that modify the layer.
210     """
211    
212     def setUp(self):
213 bh 723 """Clear the list of received messages and create a layer and a session
214 bh 331
215 bh 723 The layer is bound to self.layer and the session to self.session.
216 bh 331 """
217     self.clear_messages()
218 bh 723 self.session = Session("Test session for %s" % self.__class__)
219     filename = os.path.join("..", "Data", "iceland", "political.shp")
220 bh 331 self.layer = Layer("Test Layer",
221 bh 723 self.session.OpenShapefile(filename))
222 bh 331 self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params,
223     LAYER_LEGEND_CHANGED)
224     self.layer.Subscribe(LAYER_VISIBILITY_CHANGED,
225     self.subscribe_with_params,
226     LAYER_VISIBILITY_CHANGED)
227    
228     def tearDown(self):
229     """Clear the list of received messages and explictly destroy self.layer
230     """
231     self.layer.Destroy()
232 bh 723 self.layer = None
233     self.session.Destroy()
234     self.session = None
235 bh 331 self.clear_messages()
236    
237     def test_initial_settings(self):
238     """Test Layer's initial legend attributes"""
239     # test default settings
240     self.failIf(self.layer.WasModified())
241 jonathan 409 #self.assertEquals(self.layer.fill, None)
242     #self.assertEquals(self.layer.stroke.hex(), "#000000")
243     #self.assertEquals(self.layer.stroke_width, 1)
244 bh 331 self.assertEquals(self.layer.Visible(), 1)
245     # no messages should have been produced
246     self.check_messages([])
247    
248     def test_visibility(self):
249     """Test Layer visibility"""
250     self.layer.SetVisible(0)
251     self.assertEquals(self.layer.Visible(), 0)
252     self.check_messages([(self.layer, LAYER_VISIBILITY_CHANGED)])
253    
254     # currently, modifying the visibility doesn't count as changing
255     # the layer.
256     self.failIf(self.layer.WasModified())
257    
258 jonathan 395
259     #
260     # the tree info now contains Color objects which are difficult to test
261     #
262     # def test_tree_info(self):
263     # """Test Layer.TreeInfo"""
264     # self.assertEquals(self.layer.TreeInfo(),
265     # ("Layer 'Test Layer'",
266     # ['Shown',
267     # 'Shapes: 156',
268     # ('Extent (lat-lon):'
269     # ' (-24.5465, 63.2868, -13.4958, 66.5638)'),
270     # 'Shapetype: Polygon',
271     # 'Fill: None',
272     # 'Outline: (0.000, 0.000, 0.000)']))
273 bh 331
274    
275     if __name__ == "__main__":
276 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