1 |
# Copyright (c) 2002 by Intevation GmbH |
2 |
# 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 |
|
24 |
from Thuban.Model.layer import Layer, SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
25 |
SHAPETYPE_POINT |
26 |
from Thuban.Model.messages import LAYER_LEGEND_CHANGED, \ |
27 |
LAYER_VISIBILITY_CHANGED |
28 |
from Thuban.Model.color import Color |
29 |
|
30 |
class TestLayer(unittest.TestCase, support.FileTestMixin, |
31 |
support.FloatComparisonMixin): |
32 |
|
33 |
"""Test cases for different layer (shape) types""" |
34 |
|
35 |
def assertFloatTuplesEqual(self, test, value): |
36 |
"""Assert equality of two lists of tuples of float""" |
37 |
for i in range(len(test)): |
38 |
self.assertFloatSeqEqual(test[i], value[i]) |
39 |
|
40 |
def test_arc_layer(self): |
41 |
"""Test Layer with arc shapes""" |
42 |
layer = Layer("Test Layer", |
43 |
os.path.join("..", "Data", "iceland", "roads-line.shp")) |
44 |
self.assertEquals(layer.Title(), "Test Layer") |
45 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_ARC) |
46 |
self.assertEquals(layer.NumShapes(), 839) |
47 |
shape = layer.Shape(32) |
48 |
self.assertFloatTuplesEqual(shape.Points(), |
49 |
[(-15.082174301147461, 66.27738189697265), |
50 |
(-15.026350021362305, 66.27339172363281)]) |
51 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
52 |
[-24.450359344482422, 63.426830291748047, |
53 |
-13.55668830871582, 66.520111083984375]) |
54 |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.75, 64.25)), |
55 |
[613, 726, 838]) |
56 |
|
57 |
def test_polygon_layer(self): |
58 |
"""Test Layer with polygon shapes""" |
59 |
layer = Layer("Test Layer", |
60 |
os.path.join("..", "Data", "iceland", "political.shp")) |
61 |
self.assertEquals(layer.Title(), "Test Layer") |
62 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POLYGON) |
63 |
self.assertEquals(layer.NumShapes(), 156) |
64 |
shape = layer.Shape(4) |
65 |
self.assertFloatTuplesEqual(shape.Points(), |
66 |
[(-22.406391143798828, 64.714111328125), |
67 |
(-22.41621208190918, 64.71600341796875), |
68 |
(-22.406051635742188, 64.719200134277344), |
69 |
(-22.406391143798828, 64.714111328125)]) |
70 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
71 |
[-24.546524047851562, 63.286754608154297, |
72 |
-13.495815277099609, 66.563774108886719]) |
73 |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.9, 64.1)), |
74 |
[91, 92, 144, 146, 148, 150, 152, 153]) |
75 |
|
76 |
def test_point_layer(self): |
77 |
"""Test Layer with point shapes""" |
78 |
layer = Layer("Test Layer", |
79 |
os.path.join("..", "Data", "iceland", |
80 |
"cultural_landmark-point.shp")) |
81 |
self.assertEquals(layer.Title(), "Test Layer") |
82 |
self.assertEquals(layer.ShapeType(), SHAPETYPE_POINT) |
83 |
self.assertEquals(layer.NumShapes(), 34) |
84 |
shape = layer.Shape(0) |
85 |
self.assertFloatTuplesEqual(shape.Points(), |
86 |
[(-22.711074829101562, 66.36572265625)]) |
87 |
self.assertFloatSeqEqual(layer.BoundingBox(), |
88 |
[-23.806047439575195, 63.405960083007812, |
89 |
-15.12291431427002, 66.36572265625]) |
90 |
self.assertEquals(layer.ShapesInRegion((-24.0, 64.0, -23.80, 64.1)), |
91 |
[0, 1, 2, 3, 4, 5, 27, 28, 29, 30, 31]) |
92 |
|
93 |
def test_empty_layer(self): |
94 |
"""Test Layer with empty shape file""" |
95 |
# create an empty shape file |
96 |
shapefilename = self.temp_file_name("layer_empty.shp") |
97 |
shp = shapelib.create(shapefilename, shapelib.SHPT_POLYGON) |
98 |
shp.close() |
99 |
|
100 |
layer = Layer("Empty Layer", shapefilename) |
101 |
self.assertEquals(layer.BoundingBox(), None) |
102 |
self.assertEquals(layer.LatLongBoundingBox(), None) |
103 |
self.assertEquals(layer.NumShapes(), 0) |
104 |
|
105 |
|
106 |
class TestLayerLegend(unittest.TestCase, support.SubscriberMixin): |
107 |
|
108 |
"""Test cases for Layer method that modify the layer. |
109 |
""" |
110 |
|
111 |
def setUp(self): |
112 |
"""Clear the list of received messages and create a layer |
113 |
|
114 |
The layer is bound to self.layer. |
115 |
""" |
116 |
self.clear_messages() |
117 |
self.layer = Layer("Test Layer", |
118 |
os.path.join("..", "Data", "iceland", |
119 |
"political.shp")) |
120 |
self.layer.Subscribe(LAYER_LEGEND_CHANGED, self.subscribe_with_params, |
121 |
LAYER_LEGEND_CHANGED) |
122 |
self.layer.Subscribe(LAYER_VISIBILITY_CHANGED, |
123 |
self.subscribe_with_params, |
124 |
LAYER_VISIBILITY_CHANGED) |
125 |
|
126 |
def tearDown(self): |
127 |
"""Clear the list of received messages and explictly destroy self.layer |
128 |
""" |
129 |
self.layer.Destroy() |
130 |
self.clear_messages() |
131 |
|
132 |
def test_initial_settings(self): |
133 |
"""Test Layer's initial legend attributes""" |
134 |
# test default settings |
135 |
self.failIf(self.layer.WasModified()) |
136 |
self.assertEquals(self.layer.fill, None) |
137 |
self.assertEquals(self.layer.stroke.hex(), "#000000") |
138 |
self.assertEquals(self.layer.stroke_width, 1) |
139 |
self.assertEquals(self.layer.Visible(), 1) |
140 |
# no messages should have been produced |
141 |
self.check_messages([]) |
142 |
|
143 |
def test_fill(self): |
144 |
"""Test Layer's fill attribute""" |
145 |
# modify the fill |
146 |
self.layer.SetFill(Color(0.5, 1.0, 0.75)) |
147 |
self.check_messages([(self.layer, LAYER_LEGEND_CHANGED)]) |
148 |
self.assertEquals(self.layer.fill.hex().lower(), "#7fffbf") |
149 |
self.assert_(self.layer.WasModified()) |
150 |
|
151 |
def test_stroke(self): |
152 |
"""Test Layer's stroke attribute""" |
153 |
self.layer.SetStroke(Color(0.5, 1.0, 0.75)) |
154 |
self.assertEquals(self.layer.stroke.hex().lower(), "#7fffbf") |
155 |
self.check_messages([(self.layer, LAYER_LEGEND_CHANGED)]) |
156 |
self.assert_(self.layer.WasModified()) |
157 |
|
158 |
def test_stroke_width(self): |
159 |
"""Test Layer's stroke_width attribute""" |
160 |
self.layer.SetStrokeWidth(3.0) |
161 |
self.assertEquals(self.layer.stroke_width, 3.0) |
162 |
self.check_messages([(self.layer, LAYER_LEGEND_CHANGED)]) |
163 |
self.assert_(self.layer.WasModified()) |
164 |
|
165 |
def test_visibility(self): |
166 |
"""Test Layer visibility""" |
167 |
self.layer.SetVisible(0) |
168 |
self.assertEquals(self.layer.Visible(), 0) |
169 |
self.check_messages([(self.layer, LAYER_VISIBILITY_CHANGED)]) |
170 |
|
171 |
# currently, modifying the visibility doesn't count as changing |
172 |
# the layer. |
173 |
self.failIf(self.layer.WasModified()) |
174 |
|
175 |
def test_tree_info(self): |
176 |
"""Test Layer.TreeInfo""" |
177 |
self.assertEquals(self.layer.TreeInfo(), |
178 |
("Layer 'Test Layer'", |
179 |
['Shown', |
180 |
'Shapes: 156', |
181 |
('Extent (lat-lon):' |
182 |
' (-24.5465, 63.2868, -13.4958, 66.5638)'), |
183 |
'Shapetype: Polygon', |
184 |
'Fill: None', |
185 |
'Outline: (0.000, 0.000, 0.000)'])) |
186 |
|
187 |
|
188 |
if __name__ == "__main__": |
189 |
unittest.main() |