1 |
bh |
2644 |
# Copyright (c) 2002, 2003, 2004, 2005 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 |
bh |
1587 |
import mockgeo |
20 |
bh |
331 |
import support |
21 |
|
|
support.initthuban() |
22 |
|
|
|
23 |
|
|
import shapelib |
24 |
bh |
336 |
import dbflib |
25 |
bh |
331 |
|
26 |
bh |
723 |
from Thuban.Model.session import Session |
27 |
bh |
1538 |
from Thuban.Model.layer import BaseLayer, Layer, RasterLayer |
28 |
|
|
from Thuban.Model.data import SHAPETYPE_POLYGON, SHAPETYPE_ARC, SHAPETYPE_POINT |
29 |
bh |
331 |
from Thuban.Model.messages import LAYER_LEGEND_CHANGED, \ |
30 |
bh |
1142 |
LAYER_VISIBILITY_CHANGED, LAYER_SHAPESTORE_REPLACED, LAYER_CHANGED |
31 |
bh |
1088 |
from Thuban.Model.table import FIELDTYPE_DOUBLE, FIELDTYPE_STRING, MemoryTable |
32 |
jonathan |
947 |
from Thuban.Model.proj import Projection |
33 |
bh |
996 |
from Thuban.Model.data import DerivedShapeStore |
34 |
bh |
1452 |
from Thuban.Model.classification import Classification, ClassGroupSingleton, \ |
35 |
frank |
2688 |
ClassGroupRange, ClassGroupPattern |
36 |
jonathan |
2551 |
from Thuban.Model.color import Color |
37 |
bh |
331 |
|
38 |
jonathan |
1174 |
import Thuban.Model.resource |
39 |
|
|
|
40 |
bh |
331 |
class TestLayer(unittest.TestCase, support.FileTestMixin, |
41 |
|
|
support.FloatComparisonMixin): |
42 |
|
|
|
43 |
|
|
"""Test cases for different layer (shape) types""" |
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 |
1681 |
self.session.Destroy() |
53 |
bh |
723 |
self.session = None |
54 |
bh |
996 |
if self.layer is not None: |
55 |
|
|
self.layer.Destroy() |
56 |
|
|
self.layer = None |
57 |
bh |
723 |
|
58 |
jonathan |
947 |
def build_path(self, filename): |
59 |
|
|
return os.path.join("..", "Data", "iceland", filename) |
60 |
bh |
996 |
|
61 |
bh |
839 |
def open_shapefile(self, filename): |
62 |
|
|
"""Open and return a shapestore for filename in the iceland data set""" |
63 |
jonathan |
947 |
return self.session.OpenShapefile(self.build_path(filename)) |
64 |
bh |
839 |
|
65 |
jonathan |
947 |
def test_base_layer(self): |
66 |
bh |
996 |
layer = self.layer = BaseLayer("Test BaseLayer") |
67 |
jonathan |
947 |
self.assertEquals(layer.Title(), "Test BaseLayer") |
68 |
|
|
self.failUnless(layer.Visible()) |
69 |
|
|
|
70 |
|
|
# toggle visibility |
71 |
|
|
layer.SetVisible(False) |
72 |
|
|
self.failIf(layer.Visible()) |
73 |
|
|
|
74 |
|
|
layer.SetVisible(True) |
75 |
|
|
self.failUnless(layer.Visible()) |
76 |
|
|
|
77 |
|
|
self.failIf(layer.HasClassification()) |
78 |
jonathan |
1278 |
self.failIf(layer.HasShapes()) |
79 |
|
|
|
80 |
jonathan |
947 |
self.assertEquals(layer.GetProjection(), None) |
81 |
|
|
|
82 |
|
|
# set/get projection |
83 |
bh |
1687 |
proj = Projection(["proj=utm", "zone=26", "ellps=clrk66"]) |
84 |
jonathan |
947 |
|
85 |
|
|
layer.SetProjection(proj) |
86 |
|
|
self.failUnless(layer.GetProjection() is proj) |
87 |
|
|
|
88 |
|
|
# __init__ with other arguments |
89 |
|
|
layer = BaseLayer("Test BaseLayer", False, proj) |
90 |
|
|
self.failIf(layer.Visible()) |
91 |
|
|
self.failUnless(layer.GetProjection() is proj) |
92 |
|
|
|
93 |
bh |
331 |
def test_arc_layer(self): |
94 |
|
|
"""Test Layer with arc shapes""" |
95 |
bh |
996 |
layer = self.layer = Layer("Test Layer", |
96 |
|
|
self.open_shapefile("roads-line.shp")) |
97 |
jonathan |
1278 |
self.failUnless(layer.HasClassification()) |
98 |
|
|
self.failUnless(layer.HasShapes()) |
99 |
bh |
331 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC) |
100 |
|
|
self.assertEquals(layer.NumShapes(), 839) |
101 |
|
|
shape = layer.Shape(32) |
102 |
bh |
1551 |
self.assertPointListEquals(shape.Points(), |
103 |
|
|
[[(-15.082174301147461, 66.27738189697265), |
104 |
|
|
(-15.026350021362305, 66.27339172363281)]]) |
105 |
bh |
331 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
106 |
|
|
[-24.450359344482422, 63.426830291748047, |
107 |
|
|
-13.55668830871582, 66.520111083984375]) |
108 |
bh |
1593 |
shapes = layer.ShapesInRegion((-24.0, 64.0, -23.75, 64.25)) |
109 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], |
110 |
bh |
331 |
[613, 726, 838]) |
111 |
jonathan |
832 |
|
112 |
|
|
self.assertFloatSeqEqual(layer.ShapesBoundingBox([32]), |
113 |
|
|
[-15.082174301147461, 66.27339172363281, |
114 |
|
|
-15.026350021362305, 66.27738189697265]) |
115 |
|
|
|
116 |
|
|
shape = layer.Shape(33) |
117 |
bh |
1551 |
self.assertPointListEquals(shape.Points(), |
118 |
|
|
[[(-22.24850654602050, 66.30645751953125), |
119 |
|
|
(-22.23273086547851, 66.29407501220703), |
120 |
|
|
(-22.23158073425293, 66.2876892089843), |
121 |
|
|
(-22.24631881713867, 66.27006530761718)]]) |
122 |
jonathan |
832 |
|
123 |
|
|
self.assertFloatSeqEqual(layer.ShapesBoundingBox([32, 33]), |
124 |
|
|
[-22.248506546020508, 66.270065307617188, |
125 |
|
|
-15.026350021362305, 66.30645751953125]) |
126 |
|
|
|
127 |
|
|
self.assertEquals(layer.ShapesBoundingBox([]), None) |
128 |
|
|
self.assertEquals(layer.ShapesBoundingBox(None), None) |
129 |
|
|
|
130 |
bh |
331 |
def test_polygon_layer(self): |
131 |
|
|
"""Test Layer with polygon shapes""" |
132 |
bh |
996 |
layer = self.layer = Layer("Test Layer", |
133 |
|
|
self.open_shapefile("political.shp")) |
134 |
jonathan |
1278 |
self.failUnless(layer.HasClassification()) |
135 |
|
|
self.failUnless(layer.HasShapes()) |
136 |
bh |
331 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POLYGON) |
137 |
|
|
self.assertEquals(layer.NumShapes(), 156) |
138 |
|
|
shape = layer.Shape(4) |
139 |
bh |
1551 |
self.assertPointListEquals(shape.Points(), |
140 |
|
|
[[(-22.40639114379882, 64.714111328125), |
141 |
|
|
(-22.41621208190918, 64.7160034179687), |
142 |
|
|
(-22.40605163574218, 64.719200134277), |
143 |
|
|
(-22.40639114379882, 64.714111328125)]]) |
144 |
bh |
331 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
145 |
|
|
[-24.546524047851562, 63.286754608154297, |
146 |
|
|
-13.495815277099609, 66.563774108886719]) |
147 |
bh |
1593 |
shapes = layer.ShapesInRegion((-24.0, 64.0, -23.9, 64.1)) |
148 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], |
149 |
bh |
331 |
[91, 92, 144, 146, 148, 150, 152, 153]) |
150 |
|
|
|
151 |
|
|
def test_point_layer(self): |
152 |
|
|
"""Test Layer with point shapes""" |
153 |
bh |
996 |
layer = self.layer = Layer("Test Layer", |
154 |
|
|
self.open_shapefile("cultural_landmark-point.shp")) |
155 |
jonathan |
1278 |
self.failUnless(layer.HasClassification()) |
156 |
|
|
self.failUnless(layer.HasShapes()) |
157 |
bh |
331 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POINT) |
158 |
|
|
self.assertEquals(layer.NumShapes(), 34) |
159 |
|
|
shape = layer.Shape(0) |
160 |
bh |
1551 |
self.assertPointListEquals(shape.Points(), |
161 |
|
|
[[(-22.711074829101562, 66.36572265625)]]) |
162 |
bh |
331 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
163 |
|
|
[-23.806047439575195, 63.405960083007812, |
164 |
|
|
-15.12291431427002, 66.36572265625]) |
165 |
bh |
1593 |
shapes = layer.ShapesInRegion((-24.0, 64.0, -23.80, 64.1)) |
166 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], |
167 |
bh |
331 |
[0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31]) |
168 |
|
|
|
169 |
bh |
1983 |
def test_arc_layer_with_projection(self): |
170 |
bh |
1587 |
"""Test Layer with point shapes and a projection""" |
171 |
|
|
# We use mock data here so that we have precise control over the |
172 |
|
|
# values |
173 |
|
|
table = MemoryTable([("FOO", FIELDTYPE_STRING)], [("bla",)]) |
174 |
bh |
1983 |
store = mockgeo.SimpleShapeStore(SHAPETYPE_ARC, |
175 |
bh |
1987 |
[[[(9884828.7209840547, 5607720.9774499247), |
176 |
|
|
(11298336.04640449, 9287823.2044059951)]]], |
177 |
bh |
1983 |
table) |
178 |
bh |
1587 |
layer = self.layer = Layer("Test Layer", store) |
179 |
|
|
|
180 |
bh |
1987 |
proj = Projection(["proj=lcc", "lon_0=0", "lat_1=20n", "lat_2=60n", |
181 |
|
|
"ellps=clrk66"]) |
182 |
bh |
1587 |
layer.SetProjection(proj) |
183 |
|
|
|
184 |
bh |
1983 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
185 |
bh |
1987 |
(9884828.7209840547, 5607720.9774499247, |
186 |
|
|
11298336.04640449, 9287823.2044059951)) |
187 |
bh |
1983 |
self.assertFloatSeqEqual(layer.LatLongBoundingBox(), |
188 |
bh |
1987 |
(90.0, -8.90043373, 120, 11.1616263)) |
189 |
bh |
1983 |
shapes = layer.ShapesInRegion((100, -10, 150, +10)) |
190 |
bh |
1593 |
self.assertEquals([s.ShapeID() for s in shapes], [0]) |
191 |
bh |
1983 |
self.assertFloatSeqEqual(layer.ShapesBoundingBox([0]), |
192 |
bh |
1987 |
(90.0, -8.90043373, 120, 11.1616263)) |
193 |
bh |
1587 |
|
194 |
bh |
2644 |
# Test a very large bounding box in the query. Naive inverse |
195 |
|
|
# projection will create infs instead of proper coordinate |
196 |
|
|
# values and a different result (an empty list instead of [0]) |
197 |
silke |
2339 |
shapes = layer.ShapesInRegion((-180, -170, 200, +120)) |
198 |
|
|
self.assertEquals([s.ShapeID() for s in shapes],[0]) |
199 |
|
|
|
200 |
bh |
331 |
def test_empty_layer(self): |
201 |
|
|
"""Test Layer with empty shape file""" |
202 |
|
|
# create an empty shape file |
203 |
|
|
shapefilename = self.temp_file_name("layer_empty.shp") |
204 |
|
|
shp = shapelib.create(shapefilename, shapelib.SHPT_POLYGON) |
205 |
|
|
shp.close() |
206 |
bh |
336 |
# create an empty DBF file too because Thuban can't cope yet |
207 |
|
|
# with missing DBF file. |
208 |
|
|
dbffilename = self.temp_file_name("layer_empty.dbf") |
209 |
|
|
dbf = dbflib.create(dbffilename) |
210 |
|
|
dbf.add_field("NAME", dbflib.FTString, 20, 0) |
211 |
bh |
1768 |
dbf.close() |
212 |
bh |
331 |
|
213 |
bh |
336 |
# Now try to open it. |
214 |
bh |
996 |
layer = self.layer = Layer("Empty Layer", |
215 |
|
|
self.session.OpenShapefile(shapefilename)) |
216 |
bh |
331 |
self.assertEquals(layer.BoundingBox(), None) |
217 |
|
|
self.assertEquals(layer.LatLongBoundingBox(), None) |
218 |
|
|
self.assertEquals(layer.NumShapes(), 0) |
219 |
|
|
|
220 |
bh |
839 |
def test_get_field_type(self): |
221 |
|
|
"""Test Layer.GetFieldType()""" |
222 |
bh |
996 |
layer = self.layer = Layer("Test Layer", |
223 |
|
|
self.open_shapefile("roads-line.shp")) |
224 |
bh |
839 |
self.assertEquals(layer.GetFieldType("LENGTH"), FIELDTYPE_DOUBLE) |
225 |
|
|
self.assertEquals(layer.GetFieldType("non existing"), None) |
226 |
bh |
331 |
|
227 |
jonathan |
947 |
def test_raster_layer(self): |
228 |
jonathan |
1174 |
if not Thuban.Model.resource.has_gdal_support(): |
229 |
bh |
1555 |
raise support.SkipTest("No gdal support") |
230 |
jonathan |
1174 |
|
231 |
jonathan |
947 |
filename = self.build_path("island.tif") |
232 |
|
|
layer = RasterLayer("Test RasterLayer", filename) |
233 |
jonathan |
1278 |
self.failIf(layer.HasClassification()) |
234 |
|
|
self.failIf(layer.HasShapes()) |
235 |
jonathan |
2562 |
self.assertEquals(layer.MaskType(), layer.MASK_BIT) |
236 |
bh |
1599 |
self.assertEquals(layer.GetImageFilename(), os.path.abspath(filename)) |
237 |
jonathan |
947 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
238 |
|
|
[-24.5500000, 63.2833330, |
239 |
|
|
-13.4916670, 66.5666670]) |
240 |
|
|
self.assertFloatSeqEqual(layer.LatLongBoundingBox(), |
241 |
|
|
[-24.5500000, 63.2833330, |
242 |
|
|
-13.4916670, 66.5666670]) |
243 |
bh |
839 |
|
244 |
jonathan |
2551 |
info = layer.ImageInfo() |
245 |
|
|
self.failIf(info is None) |
246 |
|
|
self.failUnless(info.has_key("nBands")) |
247 |
|
|
self.failUnless(info.has_key("Size")) |
248 |
|
|
self.failUnless(info.has_key("Driver")) |
249 |
|
|
self.failUnless(info.has_key("BandData")) |
250 |
|
|
|
251 |
|
|
self.assertEquals(info["nBands"], 1) |
252 |
|
|
self.assertEquals(info["Size"], (5002, 394)) |
253 |
|
|
self.assertEquals(info["Driver"], "GTiff") |
254 |
|
|
self.assertEquals(info["BandData"], [(0.0, 140.0)]) |
255 |
|
|
|
256 |
bh |
996 |
def test_derived_store(self): |
257 |
|
|
"""Test layer with derived store""" |
258 |
|
|
layer = self.layer = Layer("Test Layer", |
259 |
|
|
self.open_shapefile("roads-line.shp")) |
260 |
|
|
try: |
261 |
|
|
store = layer.ShapeStore() |
262 |
|
|
derived = DerivedShapeStore(store, store.Table()) |
263 |
|
|
layer.SetShapeStore(derived) |
264 |
|
|
self.assert_(layer.ShapeStore() is derived) |
265 |
jonathan |
1262 |
|
266 |
bh |
1538 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC) |
267 |
|
|
self.assertEquals(layer.NumShapes(), 839) |
268 |
|
|
shape = layer.Shape(32) |
269 |
bh |
1551 |
self.assertPointListEquals(shape.Points(), |
270 |
|
|
[[(-15.082174301147, 66.277381896972), |
271 |
|
|
(-15.026350021362, 66.273391723632)]]) |
272 |
bh |
1538 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
273 |
|
|
[-24.450359344482422, 63.426830291748047, |
274 |
|
|
-13.55668830871582, 66.520111083984375]) |
275 |
bh |
1593 |
shapes = layer.ShapesInRegion((-24.0, 64.0, -23.75, 64.25)) |
276 |
|
|
self.assertEquals([s.ShapeID() for s in shapes], |
277 |
bh |
1538 |
[613, 726, 838]) |
278 |
jonathan |
1262 |
|
279 |
bh |
1538 |
self.assertFloatSeqEqual(layer.ShapesBoundingBox([32]), |
280 |
|
|
[-15.082174301147461, 66.27339172363281, |
281 |
|
|
-15.026350021362305, 66.27738189697265]) |
282 |
|
|
|
283 |
bh |
996 |
finally: |
284 |
|
|
store = derived = None |
285 |
jonathan |
947 |
|
286 |
bh |
996 |
|
287 |
bh |
1142 |
class SetShapeStoreTests(unittest.TestCase, support.SubscriberMixin): |
288 |
bh |
1088 |
|
289 |
|
|
def setUp(self): |
290 |
|
|
"""Create a layer with a classification as self.layer""" |
291 |
bh |
1142 |
self.clear_messages() |
292 |
bh |
1088 |
self.session = Session("Test session for %s" % self.__class__) |
293 |
|
|
self.shapefilename = os.path.join("..", "Data", "iceland", |
294 |
|
|
"cultural_landmark-point.dbf") |
295 |
|
|
self.store = self.session.OpenShapefile(self.shapefilename) |
296 |
|
|
self.layer = Layer("test layer", self.store) |
297 |
jonathan |
1438 |
self.classification = Classification() |
298 |
bh |
1088 |
self.classification.AppendGroup(ClassGroupSingleton("FARM")) |
299 |
bh |
1452 |
self.layer.SetClassificationColumn("CLPTLABEL") |
300 |
bh |
1088 |
self.layer.SetClassification(self.classification) |
301 |
bh |
1142 |
self.layer.UnsetModified() |
302 |
|
|
self.layer.Subscribe(LAYER_SHAPESTORE_REPLACED, |
303 |
|
|
self.subscribe_with_params, |
304 |
|
|
LAYER_SHAPESTORE_REPLACED) |
305 |
|
|
self.layer.Subscribe(LAYER_CHANGED, |
306 |
|
|
self.subscribe_with_params, LAYER_CHANGED) |
307 |
bh |
1088 |
|
308 |
|
|
def tearDown(self): |
309 |
bh |
1142 |
self.clear_messages() |
310 |
bh |
1088 |
self.layer.Destroy() |
311 |
|
|
self.session.Destroy() |
312 |
|
|
self.session = self.layer = self.store = self.classification = None |
313 |
|
|
|
314 |
|
|
def test_sanity(self): |
315 |
bh |
1142 |
"""SetShapeStoreTests sanity check |
316 |
|
|
|
317 |
|
|
Test the initial state of the test case instances after setUp. |
318 |
|
|
""" |
319 |
bh |
1088 |
cls = self.layer.GetClassification() |
320 |
|
|
self.assert_(cls is self.classification) |
321 |
bh |
1452 |
field = self.layer.GetClassificationColumn() |
322 |
jonathan |
1438 |
self.assertEquals(field, "CLPTLABEL") |
323 |
|
|
self.assertEquals(self.layer.GetFieldType(field), FIELDTYPE_STRING) |
324 |
bh |
1088 |
self.assertEquals(self.layer.GetClassification().GetNumGroups(), 1) |
325 |
bh |
1142 |
self.failIf(self.layer.WasModified()) |
326 |
bh |
1088 |
|
327 |
bh |
1142 |
def test_set_shape_store_modified_flag(self): |
328 |
|
|
"""Test whether Layer.SetShapeStore() sets the modified flag""" |
329 |
|
|
memtable = MemoryTable([("FOO", FIELDTYPE_STRING)], |
330 |
|
|
[("bla",)] * self.layer.ShapeStore().Table().NumRows()) |
331 |
|
|
self.layer.SetShapeStore(DerivedShapeStore(self.store, memtable)) |
332 |
|
|
|
333 |
|
|
self.assert_(self.layer.WasModified()) |
334 |
|
|
|
335 |
bh |
1088 |
def test_set_shape_store_different_field_name(self): |
336 |
|
|
"""Test Layer.SetShapeStore() with different column name""" |
337 |
|
|
memtable = MemoryTable([("FOO", FIELDTYPE_STRING)], |
338 |
|
|
[("bla",)] * self.layer.ShapeStore().Table().NumRows()) |
339 |
|
|
self.layer.SetShapeStore(DerivedShapeStore(self.store, memtable)) |
340 |
|
|
# The classification should contain only the default group now. |
341 |
|
|
self.assertEquals(self.layer.GetClassification().GetNumGroups(), 0) |
342 |
bh |
1142 |
self.check_messages([(self.layer, LAYER_CHANGED), |
343 |
|
|
(self.layer, LAYER_SHAPESTORE_REPLACED)]) |
344 |
bh |
1088 |
|
345 |
|
|
def test_set_shape_store_same_field(self): |
346 |
|
|
"""Test Layer.SetShapeStore() with same column name and type""" |
347 |
|
|
memtable = MemoryTable([("CLPTLABEL", FIELDTYPE_STRING)], |
348 |
|
|
[("bla",)] * self.layer.ShapeStore().Table().NumRows()) |
349 |
|
|
self.layer.SetShapeStore(DerivedShapeStore(self.store, memtable)) |
350 |
|
|
# The classification should be the same as before |
351 |
|
|
self.assert_(self.layer.GetClassification() is self.classification) |
352 |
bh |
1142 |
self.check_messages([(self.layer, LAYER_SHAPESTORE_REPLACED)]) |
353 |
bh |
1088 |
|
354 |
|
|
def test_set_shape_store_same_field_different_type(self): |
355 |
|
|
"""Test Layer.SetShapeStore() with same column name but different type |
356 |
|
|
""" |
357 |
|
|
memtable = MemoryTable([("CLPTLABEL", FIELDTYPE_DOUBLE)], |
358 |
|
|
[(0.0,)] * self.layer.ShapeStore().Table().NumRows()) |
359 |
|
|
self.layer.SetShapeStore(DerivedShapeStore(self.store, memtable)) |
360 |
|
|
# The classification should contain only the default group now. |
361 |
|
|
self.assertEquals(self.layer.GetClassification().GetNumGroups(), 0) |
362 |
bh |
1142 |
self.check_messages([(self.layer, LAYER_CHANGED), |
363 |
|
|
(self.layer, LAYER_SHAPESTORE_REPLACED)]) |
364 |
bh |
1088 |
|
365 |
|
|
|
366 |
bh |
1452 |
class TestLayerModification(unittest.TestCase, support.SubscriberMixin): |
367 |
bh |
331 |
|
368 |
|
|
"""Test cases for Layer method that modify the layer. |
369 |
|
|
""" |
370 |
|
|
|
371 |
|
|
def setUp(self): |
372 |
bh |
723 |
"""Clear the list of received messages and create a layer and a session |
373 |
bh |
331 |
|
374 |
bh |
723 |
The layer is bound to self.layer and the session to self.session. |
375 |
bh |
331 |
""" |
376 |
|
|
self.clear_messages() |
377 |
bh |
723 |
self.session = Session("Test session for %s" % self.__class__) |
378 |
bh |
2229 |
self.filename = os.path.join("..", "Data", "iceland", "political.shp") |
379 |
bh |
331 |
self.layer = Layer("Test Layer", |
380 |
bh |
2229 |
self.session.OpenShapefile(self.filename)) |
381 |
bh |
331 |
self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params, |
382 |
|
|
LAYER_LEGEND_CHANGED) |
383 |
|
|
self.layer.Subscribe(LAYER_VISIBILITY_CHANGED, |
384 |
|
|
self.subscribe_with_params, |
385 |
|
|
LAYER_VISIBILITY_CHANGED) |
386 |
bh |
1452 |
self.layer.Subscribe(LAYER_CHANGED, self.subscribe_with_params, |
387 |
|
|
LAYER_CHANGED) |
388 |
bh |
331 |
|
389 |
|
|
def tearDown(self): |
390 |
|
|
"""Clear the list of received messages and explictly destroy self.layer |
391 |
|
|
""" |
392 |
|
|
self.layer.Destroy() |
393 |
bh |
723 |
self.layer = None |
394 |
|
|
self.session.Destroy() |
395 |
|
|
self.session = None |
396 |
bh |
331 |
self.clear_messages() |
397 |
|
|
|
398 |
jonathan |
2551 |
def build_path(self, filename): |
399 |
|
|
return os.path.join("..", "Data", "iceland", filename) |
400 |
|
|
|
401 |
bh |
1452 |
def test_sanity(self): |
402 |
|
|
"""TestLayerModification Sanity Checks""" |
403 |
bh |
331 |
# test default settings |
404 |
|
|
self.failIf(self.layer.WasModified()) |
405 |
|
|
self.assertEquals(self.layer.Visible(), 1) |
406 |
|
|
# no messages should have been produced |
407 |
|
|
self.check_messages([]) |
408 |
|
|
|
409 |
|
|
def test_visibility(self): |
410 |
|
|
"""Test Layer visibility""" |
411 |
|
|
self.layer.SetVisible(0) |
412 |
|
|
self.assertEquals(self.layer.Visible(), 0) |
413 |
|
|
self.check_messages([(self.layer, LAYER_VISIBILITY_CHANGED)]) |
414 |
|
|
|
415 |
|
|
# currently, modifying the visibility doesn't count as changing |
416 |
|
|
# the layer. |
417 |
|
|
self.failIf(self.layer.WasModified()) |
418 |
|
|
|
419 |
frank |
2688 |
def test_set_classification_numerical(self): |
420 |
|
|
"""Test Layer.SetClassification numerical""" |
421 |
bh |
1452 |
classification = Classification() |
422 |
|
|
classification.AppendGroup(ClassGroupRange((0.0, 0.1))) |
423 |
|
|
|
424 |
|
|
self.layer.SetClassification(classification) |
425 |
|
|
self.layer.SetClassificationColumn("AREA") |
426 |
|
|
|
427 |
|
|
self.check_messages([(self.layer, LAYER_CHANGED), |
428 |
|
|
(self.layer, LAYER_CHANGED)]) |
429 |
|
|
self.failUnless(self.layer.WasModified()) |
430 |
|
|
|
431 |
|
|
self.clear_messages() |
432 |
|
|
self.layer.UnsetModified() |
433 |
|
|
|
434 |
|
|
# change only the classification column. This should issue a |
435 |
|
|
# LAYER_CHANGED message as well. |
436 |
|
|
self.layer.SetClassificationColumn("PERIMETER") |
437 |
|
|
|
438 |
|
|
self.check_messages([(self.layer, LAYER_CHANGED)]) |
439 |
|
|
self.failUnless(self.layer.WasModified()) |
440 |
|
|
|
441 |
frank |
2688 |
def test_set_classification_textual(self): |
442 |
|
|
"""Test Layer.SetClassification textual""" |
443 |
|
|
classification = Classification() |
444 |
|
|
classification.AppendGroup(ClassGroupPattern("I")) |
445 |
bh |
1452 |
|
446 |
frank |
2688 |
self.layer.SetClassification(classification) |
447 |
|
|
self.layer.SetClassificationColumn("POPYCOUN") |
448 |
|
|
|
449 |
|
|
self.check_messages([(self.layer, LAYER_CHANGED), |
450 |
|
|
(self.layer, LAYER_CHANGED)]) |
451 |
|
|
self.failUnless(self.layer.WasModified()) |
452 |
|
|
|
453 |
|
|
self.clear_messages() |
454 |
|
|
self.layer.UnsetModified() |
455 |
|
|
|
456 |
|
|
# change only the classification column. This should issue a |
457 |
|
|
# LAYER_CHANGED message as well. |
458 |
|
|
self.layer.SetClassificationColumn("POPYREG") |
459 |
|
|
|
460 |
|
|
self.check_messages([(self.layer, LAYER_CHANGED)]) |
461 |
|
|
self.failUnless(self.layer.WasModified()) |
462 |
|
|
|
463 |
|
|
|
464 |
bh |
2229 |
def test_tree_info(self): |
465 |
|
|
"""Test Layer.TreeInfo""" |
466 |
|
|
self.assertEquals(self.layer.TreeInfo(), |
467 |
|
|
("Layer 'Test Layer'", |
468 |
|
|
['Filename: %s' % os.path.abspath(self.filename), |
469 |
|
|
'Shown', |
470 |
|
|
'Shapes: 156', |
471 |
|
|
'Extent (lat-lon): (-24.5465, 63.2868, -13.4958, 66.5638)', |
472 |
|
|
'Shapetype: Polygon', |
473 |
|
|
self.layer.GetClassification()])) |
474 |
bh |
331 |
|
475 |
jonathan |
2551 |
def test_raster_layer(self): |
476 |
|
|
if not Thuban.Model.resource.has_gdal_support(): |
477 |
|
|
raise support.SkipTest("No gdal support") |
478 |
bh |
331 |
|
479 |
jonathan |
2551 |
|
480 |
|
|
filename = self.build_path("island.tif") |
481 |
|
|
layer = RasterLayer("Test RasterLayer", filename) |
482 |
|
|
|
483 |
|
|
layer.Subscribe(LAYER_CHANGED, self.subscribe_with_params, |
484 |
|
|
LAYER_CHANGED) |
485 |
|
|
|
486 |
jonathan |
2562 |
self.assertEquals(layer.MaskType(), layer.MASK_BIT) |
487 |
jonathan |
2552 |
|
488 |
jonathan |
2562 |
layer.SetMaskType(layer.MASK_NONE) |
489 |
|
|
self.failIf(layer.MaskType() != layer.MASK_NONE) |
490 |
jonathan |
2552 |
self.check_messages([(layer, LAYER_CHANGED)]) |
491 |
|
|
self.clear_messages() |
492 |
jonathan |
2551 |
|
493 |
jonathan |
2562 |
layer.SetMaskType(layer.MASK_NONE) |
494 |
|
|
self.failIf(layer.MaskType() != layer.MASK_NONE) |
495 |
jonathan |
2552 |
self.check_messages([]) |
496 |
|
|
self.clear_messages() |
497 |
|
|
|
498 |
jonathan |
2562 |
layer.SetMaskType(layer.MASK_BIT) |
499 |
|
|
self.failIf(layer.MaskType() != layer.MASK_BIT) |
500 |
jonathan |
2551 |
self.check_messages([(layer, LAYER_CHANGED)]) |
501 |
|
|
self.clear_messages() |
502 |
|
|
|
503 |
jonathan |
2562 |
layer.SetMaskType(layer.MASK_BIT) |
504 |
|
|
self.failIf(layer.MaskType() != layer.MASK_BIT) |
505 |
jonathan |
2551 |
self.check_messages([]) |
506 |
|
|
self.clear_messages() |
507 |
|
|
|
508 |
jonathan |
2562 |
layer.SetMaskType(layer.MASK_ALPHA) |
509 |
|
|
self.failIf(layer.MaskType() != layer.MASK_ALPHA) |
510 |
jonathan |
2551 |
|
511 |
jonathan |
2587 |
layer.SetOpacity(0) |
512 |
|
|
self.assertEquals(layer.Opacity(), 0) |
513 |
|
|
layer.SetOpacity(0.5) |
514 |
|
|
self.assertEquals(layer.Opacity(), 0.5) |
515 |
jonathan |
2562 |
|
516 |
jonathan |
2587 |
self.clear_messages() |
517 |
|
|
layer.SetOpacity(1) |
518 |
|
|
self.assertEquals(layer.Opacity(), 1) |
519 |
|
|
self.check_messages([(layer, LAYER_CHANGED)]) |
520 |
|
|
self.clear_messages() |
521 |
jonathan |
2562 |
|
522 |
jonathan |
2587 |
self.assertRaises(ValueError, layer.SetOpacity, -0.1) |
523 |
|
|
self.assertRaises(ValueError, layer.SetOpacity, 1.1) |
524 |
|
|
|
525 |
jonathan |
2562 |
layer.SetMaskType(layer.MASK_NONE) |
526 |
jonathan |
2587 |
self.clear_messages() |
527 |
|
|
self.assertEquals(layer.Opacity(), 1) |
528 |
|
|
self.check_messages([]) |
529 |
|
|
self.clear_messages() |
530 |
jonathan |
2562 |
|
531 |
|
|
self.assertRaises(ValueError, layer.SetMaskType, -1) |
532 |
|
|
self.assertRaises(ValueError, layer.SetMaskType, 4) |
533 |
|
|
|
534 |
|
|
|
535 |
bh |
331 |
if __name__ == "__main__": |
536 |
bh |
599 |
support.run_tests() |