/[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 947 - (hide annotations)
Tue May 20 15:27:19 2003 UTC (21 years, 9 months ago) by jonathan
Original Path: trunk/thuban/test/test_layer.py
File MIME type: text/x-python
File size: 10432 byte(s)
Added tests associated with the raster layer code.

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