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

Contents of /branches/WIP-pyshapelib-bramz/test/test_classification.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 723 - (show annotations)
Thu Apr 24 15:31:53 2003 UTC (21 years, 10 months ago) by bh
Original Path: trunk/thuban/test/test_classification.py
File MIME type: text/x-python
File size: 10012 byte(s)
First step towards table management. Introduce a simple data
abstraction so that we replace the data a layer uses more easily
in the next step.

* Thuban/Model/data.py: New file with a simple data abstraction
that bundles shapefile and dbffile into one object.

* Thuban/Model/session.py (Session.OpenShapefile): New method to
open shapefiles and return a shape store object

* Thuban/Model/layer.py (Layer.__init__): Pass the data as a store
object instead of a shapefile filename. This introduces a new
instance variable store holding the datastore. For intermediate
backwards compatibility keep the old instance variables.
(open_shapefile): Removed. No longer needed with the shape store.
(Layer.SetShapeStore, Layer.ShapeStore): New methods to set and
get the shape store used by a layer.
(Layer.Destroy): No need to explicitly destroy the shapefile or
table anymore.

* Thuban/UI/mainwindow.py (MainWindow.AddLayer)
(MainWindow.AddLayer): Use the session's OpenShapefile method to
open shapefiles

* Thuban/Model/load.py (ProcessSession.start_layer): Use the
session's OpenShapefile method to open shapefiles

* test/test_classification.py
(TestClassification.test_classification): Use the session's
OpenShapefile method to open shapefiles and build the filename in
a more platform independed way

* test/test_layer.py (TestLayer.setUp, TestLayer.tearDown):
Implement to have a session to use in the tests
(TestLayer.test_arc_layer, TestLayer.test_polygon_layer)
(TestLayer.test_point_layer, TestLayer.test_empty_layer): Use the
session's OpenShapefile method to open shapefiles
(TestLayerLegend.setUp): Instantiate a session so that we can use
it to open shapefiles.
(TestLayerLegend.tearDown): Make sure that all references to
layers and session are removed otherwise we may get a resource
leak

* test/test_map.py (TestMapAddLayer.test_add_layer)
(TestMapWithContents.setUp): Instantiate a session so that we can
use it to open shapefiles.
(TestMapWithContents.tearDown): Make sure that all references to
layers, maps and sessions are removed otherwise we may get a
resource leak
("__main__"): use support.run_tests() so that more info about
uncollected garbage is printed

* test/test_save.py (SaveSessionTest.testSingleLayer): Use the
session's OpenShapefile method to open shapefiles
("__main__"): use support.run_tests() so that more info about
uncollected garbage is printed

* test/test_selection.py (TestSelection.tearDown): Make sure that
all references to the session and the selection are removed
otherwise we may get a resource leak
(TestSelection.get_layer): Instantiate a session so that we can
use it to open shapefiles.
("__main__"): use support.run_tests() so that more info about
uncollected garbage is printed

* test/test_session.py (TestSessionBase.tearDown)
(TestSessionWithContent.tearDown): Make sure that all references
to the session and layers are removed otherwise we may get a
resource leak
(TestSessionWithContent.setUp): Use the session's OpenShapefile
method to open shapefiles

1 # Copyright (c) 2002, 2003 by Intevation GmbH
2 # Authors:
3 # Jonathan Coles <[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 Classification class
10 """
11
12 __version__ = "$Revision$"
13 # $Source$
14 # $Id$
15
16 from __future__ import nested_scopes
17
18 import unittest
19
20 import support
21 support.initthuban()
22
23 import os
24 from Thuban.Model.table import *
25 from Thuban.Model.classification import *
26 from Thuban.Model.session import Session
27 from Thuban.Model.layer import Layer
28
29 import copy
30
31
32 class TestClassification(unittest.TestCase):
33
34 def test_ClassGroupProperties(self):
35 """Test ClassGroupProperties"""
36
37 props = ClassGroupProperties()
38 self.assertEqual(props.GetLineColor(), Color.Black)
39 self.assertEqual(props.GetLineWidth(), 1)
40 self.assertEqual(props.GetFill(), Color.Transparent)
41
42 red = Color(1, 0, 0)
43 props.SetLineColor(red)
44 self.assertEqual(props.GetLineColor(), red)
45
46 blue = Color(0, 0, 1)
47 props.SetLineColor(blue)
48 self.assertEqual(props.GetLineColor(), blue)
49
50 props.SetLineWidth(10)
51 self.assertEqual(props.GetLineWidth(), 10)
52
53 self.assertRaises(ValueError, props.SetLineWidth, -10)
54 self.assertEqual(props.GetLineWidth(), 10)
55
56 newProps1 = ClassGroupProperties()
57 newProps2 = ClassGroupProperties()
58 self.assertNotEqual(newProps1, props)
59 self.assertEqual(newProps1, newProps2)
60
61 def test_ClassGroup(self):
62 """Test ClassGroup"""
63
64 # test constructor with no label
65 group = ClassGroup()
66 self.assertEqual(group.GetLabel(), "")
67
68 # test constructor with label
69 group = ClassGroup("hallo")
70 self.assertEqual(group.GetLabel(), "hallo")
71
72 # test SetLabel()/GetLabel()
73 group = ClassGroup("welt")
74 group.SetLabel("hallo")
75 self.assertEqual(group.GetLabel(), "hallo")
76
77 group.SetLabel("")
78 self.assertEqual(group.GetLabel(), "")
79
80 # test Matches
81 # Matches() is a virtual function...can't test it here
82 #
83 #self.assertEqual(group.Matches(None), False)
84 #self.assertEqual(group.Matches(1), False)
85 #self.assertEqual(group.Matches("hallo"), False)
86 #self.assertEqual(group.Matches([]), False)
87
88 # test GetProperties...also a virtual function
89 #self.assertEqual(group.GetProperties(), None)
90
91 def test_ClassGroupDefault(self):
92 """Test ClassGroupDefault"""
93
94 defProps = ClassGroupProperties()
95
96 newProps = ClassGroupProperties()
97 newProps.SetLineColor(Color(.25, .5, .75))
98 newProps.SetLineWidth(5)
99 newProps.SetFill(Color(.12, .24, .36))
100
101 # test constructor
102
103 group = ClassGroupDefault(newProps)
104 self.assertEqual(group.GetProperties(), newProps)
105
106 group = ClassGroupDefault(newProps, "hallo")
107 self.assertEqual(group.GetProperties(), newProps)
108 self.assertEqual(group.GetLabel(), "hallo")
109
110 # test empty constructor
111 group = ClassGroupDefault()
112 props = group.GetProperties()
113
114 self.assertEqual(group.GetLabel(), "")
115 self.assertEqual(defProps, props)
116
117 # test Matches()
118 self.assertEqual(group.Matches(None), True)
119 self.assertEqual(group.Matches(1), True)
120 self.assertEqual(group.Matches("hallo"), True)
121 self.assertEqual(group.Matches([]), True)
122
123 # test SetProperties()/GetProperties()
124 group.SetProperties(newProps)
125 self.assertEqual(group.GetProperties(), newProps)
126
127 # test copy
128 groupCopy = copy.copy(group)
129 self.assertEqual(group, groupCopy)
130
131 def test_ClassGroupRange(self):
132 """Test ClassGroupRange"""
133
134 defProps = ClassGroupProperties()
135 newProps = ClassGroupProperties()
136 newProps.SetLineColor(Color(.25, .5, .75))
137 newProps.SetLineWidth(5)
138 newProps.SetFill(Color(.12, .24, .36))
139
140 # test empty constructor
141 group = ClassGroupRange()
142
143 self.assertEqual(group.GetMin(), 0)
144 self.assertEqual(group.GetMax(), 1)
145 self.assertEqual(group.GetProperties(), defProps)
146 self.assertEqual(group.GetLabel(), "")
147
148 # test SetMax()
149 self.assertRaises(ValueError, group.SetMax, 0)
150 self.assertRaises(ValueError, group.SetMax, -1)
151 self.assertEquals(group.GetMax(), 1)
152 group.SetMax(2)
153 self.assertEquals(group.GetMax(), 2)
154
155 # test SetMin()
156 self.assertRaises(ValueError, group.SetMin, 2)
157 self.assertRaises(ValueError, group.SetMin, 3)
158 self.assertEquals(group.GetMin(), 0)
159 group.SetMin(-5)
160 self.assertEquals(group.GetMin(), -5)
161
162 # test SetProperties()/GetProperties()
163 group.SetProperties(newProps)
164 self.assertEqual(group.GetProperties(), newProps)
165
166 # test SetRange()
167 self.assertRaises(ValueError, group.SetRange, 1, 0)
168 group.SetRange(-5, 5)
169 self.assertEqual(group.GetRange(), (-5, 5))
170
171 # test Matches()
172 self.assertEqual(group.Matches(-6), False)
173 self.assertEqual(group.Matches(-5), True)
174 self.assertEqual(group.Matches(0), True)
175 self.assertEqual(group.Matches(4), True)
176 self.assertEqual(group.Matches(5), False)
177
178 # test copy
179 groupCopy = copy.copy(group)
180 self.assertEqual(group, groupCopy)
181
182 def test_ClassGroupSingleton(self):
183 """Test ClassGroupSingleton"""
184
185 defProps = ClassGroupProperties()
186 newProps = ClassGroupProperties()
187 newProps.SetLineColor(Color(.25, .5, .75))
188 newProps.SetLineWidth(5)
189 newProps.SetFill(Color(.12, .24, .36))
190
191 # test empty constructor
192 group = ClassGroupSingleton()
193
194 self.assertEqual(group.GetValue(), 0)
195 self.assertEqual(group.GetProperties(), defProps)
196 self.assertEqual(group.GetLabel(), "")
197
198 # test SetProperties()/GetProperties()
199 group.SetProperties(newProps)
200 self.assertEqual(group.GetProperties(), newProps)
201
202 # test SetValue()
203 group.SetValue(5)
204 self.assertEqual(group.GetValue(), 5)
205
206 # test Matches()
207 self.assertEqual(group.Matches(0), False)
208 self.assertEqual(group.Matches(5), True)
209
210 group.SetValue("5")
211 self.assertNotEqual(group.GetValue(), 5)
212
213 # test Matches()
214 self.assertEqual(group.Matches(5), False)
215 self.assertEqual(group.Matches("5"), True)
216
217 group.SetValue("hallo")
218 self.assertEqual(group.GetValue(), "hallo")
219
220 # test Matches()
221 self.assertEqual(group.Matches("HALLO"), False)
222 self.assertEqual(group.Matches("hallo"), True)
223
224 # test copy
225 groupCopy = copy.copy(group)
226 self.assertEqual(group, groupCopy)
227
228
229 def test_ClassIterator(self):
230 """Test ClassIterator"""
231
232 groups = [ClassGroupSingleton(5), ClassGroupSingleton(5),
233 ClassGroupRange(-3, 3), ClassGroupSingleton(-5),
234 ClassGroupDefault()]
235
236 clazz = Classification()
237
238 for g in groups:
239 clazz.AppendGroup(g)
240
241 def convert(clazz):
242 if isinstance(clazz, ClassGroupDefault): return 0
243 if isinstance(clazz, ClassGroupSingleton): return 1
244 if isinstance(clazz, ClassGroupRange): return 2
245
246 list = []
247 for g in clazz:
248 list.append(convert(g))
249
250 self.assertEquals(list, [0, 1, 1, 2, 1, 0])
251
252 def test_classification(self):
253 """Test Classification"""
254
255 defProps = ClassGroupProperties()
256 red = Color(1, 0, 0)
257 green = Color(0, 1, 0)
258 blue = Color(0, 0, 1)
259
260 session = Session("Test session")
261 filename = os.path.join("..", "Data", "iceland", "political.dbf")
262 layer = Layer("asdf", session.OpenShapefile(filename))
263
264 #
265 # init with no params
266 #
267 c = Classification()
268 self.assertEqual(c.GetField(), None)
269 self.assertEqual(c.GetFieldType(), None)
270 self.assertEqual(c.FindGroup(-1), c.GetDefaultGroup())
271
272 c.SetDefaultLineColor(red)
273 self.assertEqual(c.GetDefaultLineColor(), red)
274 self.assertEqual(c.GetDefaultFill(), Color.Transparent)
275
276 c.SetDefaultFill(green)
277 self.assertEqual(c.GetDefaultFill(), green)
278 self.assertEqual(c.GetDefaultLineColor(), red)
279
280 c.SetField("hallo")
281 self.assertEqual(c.GetField(), "hallo")
282
283 c.SetFieldType(FIELDTYPE_STRING)
284 self.assertEqual(c.GetFieldType(), FIELDTYPE_STRING)
285
286 # should raise an exception because 'hallo' doesn't
287 # exist in the table
288 self.assertRaises(ValueError, c.SetLayer, layer)
289
290 c.SetField("AREA")
291 c.SetLayer(layer)
292 self.assertEqual(c.GetLayer(), layer)
293 self.assertEqual(c.GetField(), "AREA")
294 self.assertEqual(c.GetFieldType(), FIELDTYPE_DOUBLE)
295
296 c.SetField(None)
297 self.assertEquals(c.GetFieldType(), None)
298 self.assertEquals(c.FindGroup(5), c.GetDefaultGroup())
299
300 c.SetField("AREA")
301 s = ClassGroupSingleton(5)
302 c.AppendGroup(s)
303 self.assertEquals(c.FindGroup(5), s)
304 self.assertEquals(c.FindGroup(0), c.GetDefaultGroup())
305
306 r = ClassGroupRange(-10, 10)
307 c.AppendGroup(r)
308 self.assertEquals(c.FindGroup(-11), c.GetDefaultGroup())
309 self.assertEquals(c.FindGroup(-10), r)
310 self.assertEquals(c.FindGroup(9), r)
311 self.assertEquals(c.FindGroup(5), s)
312 self.assertEquals(c.FindGroup(10), c.GetDefaultGroup())
313
314 clazz = copy.deepcopy(c)
315
316 self.assertEquals(clazz.GetNumGroups(), c.GetNumGroups())
317
318 for i in range(clazz.GetNumGroups()):
319 self.assertEquals(clazz.GetGroup(i), c.GetGroup(i))
320
321 layer.Destroy()
322
323 if __name__ == "__main__":
324 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