1 |
# Copyright (c) 2002, 2003 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 Map 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 |
from Thuban.Model.messages import CHANGED, MAP_PROJECTION_CHANGED, \ |
23 |
MAP_LAYERS_CHANGED, MAP_LAYERS_ADDED, MAP_LAYERS_REMOVED,\ |
24 |
MAP_STACKING_CHANGED, LAYER_VISIBILITY_CHANGED, LAYER_LEGEND_CHANGED, \ |
25 |
LAYER_CHANGED |
26 |
|
27 |
from Thuban.Model.session import Session |
28 |
from Thuban.Model.map import Map |
29 |
from Thuban.Model.layer import Layer |
30 |
from Thuban.Model.proj import Projection |
31 |
from Thuban.Model.color import Color |
32 |
|
33 |
|
34 |
class TestMapSimple(unittest.TestCase): |
35 |
|
36 |
"""Very simple test cases for Map""" |
37 |
|
38 |
def test_initial_state(self): |
39 |
"""Test Map's initial state""" |
40 |
map = Map("Test Map") |
41 |
self.assertEquals(map.Title(), "Test Map") |
42 |
self.assertEquals(map.Layers(), []) |
43 |
label_layer = map.LabelLayer() |
44 |
self.assertEquals(label_layer.Title(), "Labels") |
45 |
self.assertEquals(label_layer.Labels(), []) |
46 |
self.failIf(map.WasModified()) |
47 |
map.Destroy() |
48 |
|
49 |
def test_empty_map(self): |
50 |
"""Test empty Map""" |
51 |
map = Map("Test Map") |
52 |
self.assertEquals(map.BoundingBox(), None) |
53 |
self.assertEquals(map.ProjectedBoundingBox(), None) |
54 |
self.failIf(map.HasLayers()) |
55 |
map.Destroy() |
56 |
|
57 |
|
58 |
class TestMapBase(unittest.TestCase, support.SubscriberMixin): |
59 |
|
60 |
"""Base class for Map test cases that test messages""" |
61 |
|
62 |
def setUp(self): |
63 |
""" |
64 |
Clear the message list, create self.map and subscribe to its messages |
65 |
""" |
66 |
self.clear_messages() |
67 |
|
68 |
# Create a Map and subscribe to all interesting channels. |
69 |
self.map = Map("Test Map") |
70 |
for channel in (CHANGED, |
71 |
MAP_PROJECTION_CHANGED, |
72 |
MAP_LAYERS_CHANGED, |
73 |
MAP_LAYERS_ADDED, |
74 |
MAP_LAYERS_REMOVED, |
75 |
MAP_STACKING_CHANGED, |
76 |
LAYER_VISIBILITY_CHANGED, |
77 |
LAYER_LEGEND_CHANGED, |
78 |
LAYER_CHANGED): |
79 |
self.map.Subscribe(channel, self.subscribe_with_params, channel) |
80 |
|
81 |
def tearDown(self): |
82 |
"""Destroy self.map and clear the message list""" |
83 |
self.map.Destroy() |
84 |
self.clear_messages() |
85 |
|
86 |
|
87 |
class TestMapAddLayer(TestMapBase): |
88 |
|
89 |
"""Simple test cases involving messages""" |
90 |
|
91 |
def test_add_layer(self): |
92 |
"""Test Map.AddLayer""" |
93 |
# make sure the created Map is unmodified |
94 |
session = Session("Test session for %s" % self.__class__) |
95 |
self.failIf(self.map.WasModified()) |
96 |
self.failIf(self.map.HasLayers()) |
97 |
|
98 |
# add a layer and check the result |
99 |
filename = os.path.join("..", "Data", "iceland", "roads-line.shp") |
100 |
roads = Layer("Roads", session.OpenShapefile(filename)) |
101 |
self.map.AddLayer(roads) |
102 |
self.assertEquals(self.map.Layers(), [roads]) |
103 |
self.check_messages([(self.map, MAP_LAYERS_CHANGED), |
104 |
(self.map, MAP_LAYERS_ADDED)]) |
105 |
self.assert_(self.map.WasModified()) |
106 |
self.assert_(self.map.HasLayers()) |
107 |
|
108 |
|
109 |
class TestMapWithContents(TestMapBase, support.FloatComparisonMixin): |
110 |
|
111 |
"""More complex Map test cases with messages that. |
112 |
|
113 |
All test cases here start with a non-empty map. |
114 |
""" |
115 |
|
116 |
def setUp(self): |
117 |
"""Extend the inherited method to also fill the Map. |
118 |
|
119 |
Put some layers into the map created by the inherited method and |
120 |
reset its modified flag. Make also sure that the list of |
121 |
received messages is empty. |
122 |
""" |
123 |
TestMapBase.setUp(self) |
124 |
self.session = Session("Test session for %s" % self.__class__) |
125 |
open_shp = self.session.OpenShapefile |
126 |
self.arc_layer = Layer("Roads", |
127 |
open_shp(os.path.join("..", "Data", "iceland", |
128 |
"roads-line.shp"))) |
129 |
self.poly_layer = Layer("Political", |
130 |
open_shp(os.path.join("..", "Data", "iceland", |
131 |
"political.shp"))) |
132 |
self.map.AddLayer(self.arc_layer) |
133 |
self.map.AddLayer(self.poly_layer) |
134 |
self.map.UnsetModified() |
135 |
self.clear_messages() |
136 |
|
137 |
def tearDown(self): |
138 |
TestMapBase.tearDown(self) |
139 |
self.session = None |
140 |
self.map = None |
141 |
self.poly_layer = self.arc_layer = None |
142 |
|
143 |
def test_remove_layer(self): |
144 |
"""Test Map.RemoveLayer""" |
145 |
self.map.RemoveLayer(self.arc_layer) |
146 |
self.assert_(self.map.WasModified()) |
147 |
self.assertEquals(self.map.Layers(), [self.poly_layer]) |
148 |
self.map.UnsetModified() |
149 |
self.check_messages([(self.map, MAP_LAYERS_CHANGED), |
150 |
(self.map, MAP_LAYERS_REMOVED), |
151 |
(CHANGED,)]) |
152 |
|
153 |
def test_clear_layers(self): |
154 |
"""Test Map.ClearLayers""" |
155 |
self.map.ClearLayers() |
156 |
self.assertEquals(self.map.Layers(), []) |
157 |
self.assertEquals(self.map.LabelLayer().Labels(), []) |
158 |
self.check_messages([(MAP_LAYERS_CHANGED,), |
159 |
(self.map, MAP_LAYERS_CHANGED), |
160 |
(self.map, MAP_LAYERS_REMOVED)]) |
161 |
self.assert_(self.map.WasModified()) |
162 |
self.failIf(self.map.HasLayers()) |
163 |
|
164 |
def test_raise_layer(self): |
165 |
"""Test Map.RaiseLayer""" |
166 |
self.map.RaiseLayer(self.arc_layer) |
167 |
self.assertEquals(self.map.Layers(), [self.poly_layer, self.arc_layer]) |
168 |
self.check_messages([(self.map, MAP_LAYERS_CHANGED), |
169 |
(self.map, MAP_STACKING_CHANGED)]) |
170 |
self.assert_(self.map.WasModified()) |
171 |
|
172 |
def test_raise_layer_top(self): |
173 |
"""Test Map.MoveLayerToTop""" |
174 |
open_shp = self.session.OpenShapefile |
175 |
dummy = Layer("Roads", |
176 |
open_shp(os.path.join("..", "Data", "iceland", |
177 |
"roads-line.shp"))) |
178 |
self.map.AddLayer(dummy) |
179 |
self.clear_messages() |
180 |
|
181 |
self.map.MoveLayerToTop(self.poly_layer) |
182 |
self.assertEquals(self.map.Layers(), |
183 |
[self.arc_layer, dummy, self.poly_layer]) |
184 |
self.check_messages([(self.map, MAP_LAYERS_CHANGED), |
185 |
(self.map, MAP_STACKING_CHANGED)]) |
186 |
self.assert_(self.map.WasModified()) |
187 |
|
188 |
self.map.RemoveLayer(dummy) |
189 |
|
190 |
def test_lower_layer_bottom(self): |
191 |
"""Test Map.MoveLayerToBottom""" |
192 |
open_shp = self.session.OpenShapefile |
193 |
dummy = Layer("Roads", |
194 |
open_shp(os.path.join("..", "Data", "iceland", |
195 |
"roads-line.shp"))) |
196 |
self.map.AddLayer(dummy) |
197 |
self.clear_messages() |
198 |
|
199 |
self.map.MoveLayerToBottom(dummy) |
200 |
self.assertEquals(self.map.Layers(), |
201 |
[dummy, self.arc_layer, self.poly_layer]) |
202 |
self.check_messages([(self.map, MAP_LAYERS_CHANGED), |
203 |
(self.map, MAP_STACKING_CHANGED)]) |
204 |
self.assert_(self.map.WasModified()) |
205 |
|
206 |
self.map.RemoveLayer(dummy) |
207 |
|
208 |
def test_raise_highest_layer(self): |
209 |
"""Test Map.RaiseLayer with highest layer |
210 |
|
211 |
Attempting to raise the highest layer should not modify the map. |
212 |
In particular it should not send any messages. |
213 |
""" |
214 |
self.map.RaiseLayer(self.poly_layer) |
215 |
self.assertEquals(self.map.Layers(), [self.arc_layer, self.poly_layer]) |
216 |
self.check_messages([]) |
217 |
self.failIf(self.map.WasModified()) |
218 |
|
219 |
def test_lower_layer(self): |
220 |
"""Test Map.LowerLayer""" |
221 |
self.map.LowerLayer(self.poly_layer) |
222 |
self.assertEquals(self.map.Layers(), [self.poly_layer, self.arc_layer]) |
223 |
self.check_messages([(self.map, MAP_LAYERS_CHANGED), |
224 |
(self.map, MAP_STACKING_CHANGED)]) |
225 |
self.assert_(self.map.WasModified()) |
226 |
|
227 |
def test_lower_lowest_layer(self): |
228 |
"""Test Map.LowerLayer with lowest layer. |
229 |
|
230 |
Attempting to lower the lowest layer should not modify the map. |
231 |
In particular it should not send any messages. |
232 |
""" |
233 |
self.map.LowerLayer(self.arc_layer) |
234 |
self.assertEquals(self.map.Layers(), [self.arc_layer, self.poly_layer]) |
235 |
self.check_messages([]) |
236 |
self.failIf(self.map.WasModified()) |
237 |
|
238 |
def test_bounding_box(self): |
239 |
"""Test Map.BoundingBox""" |
240 |
self.assertFloatSeqEqual(self.map.BoundingBox(), |
241 |
(-24.546524047851562, 63.286754608154297, |
242 |
-13.495815277099609, 66.563774108886719)) |
243 |
|
244 |
def test_projected_bounding_box(self): |
245 |
"""Test Map.ProjectedBoundingBox""" |
246 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
247 |
self.map.SetProjection(proj) |
248 |
self.assertFloatSeqEqual(self.map.ProjectedBoundingBox(), |
249 |
(608873.03380603762, 7019694.6517963577, |
250 |
1173560.0288053728, 7447353.2203218574), |
251 |
epsilon = 1e-5) |
252 |
|
253 |
def test_set_projection(self): |
254 |
"""Test Map.SetProjection""" |
255 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
256 |
self.map.SetProjection(proj) |
257 |
self.check_messages([(self.map, MAP_PROJECTION_CHANGED)]) |
258 |
self.assert_(self.map.WasModified()) |
259 |
|
260 |
def test_tree_info(self): |
261 |
"""Test Map.TreeInfo""" |
262 |
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
263 |
self.map.SetProjection(proj) |
264 |
# compute the extent string because there are platform |
265 |
# differences in the way %g is handled: |
266 |
# glibc: "%g" % 7.01969e+06 == "7.01969e+06" |
267 |
# w32/VC: "%g" % 7.01969e+06 == "7.01969e+006" |
268 |
extent = 'Extent (projected): (%g, %g, %g, %g)'\ |
269 |
% (608873, 7.01969e+06, 1.17356e+06, 7.44735e+06) |
270 |
self.assertEquals(self.map.TreeInfo(), |
271 |
('Map: Test Map', |
272 |
[('Extent (lat-lon):' |
273 |
' (-24.5465, 63.2868, -13.4958, 66.5638)'), |
274 |
extent, |
275 |
('Projection', |
276 |
['zone=26', 'proj=utm', 'ellps=clrk66']), |
277 |
self.poly_layer, |
278 |
self.arc_layer])) |
279 |
|
280 |
def test_forwarding_visibility(self): |
281 |
"""Test Map's forwarding of Layer.SetVisible messages""" |
282 |
self.poly_layer.SetVisible(0) |
283 |
self.check_messages([(self.poly_layer, LAYER_VISIBILITY_CHANGED)]) |
284 |
|
285 |
def test_unset_modified(self): |
286 |
"""Test Map.UnsetModified. |
287 |
|
288 |
Test whether a change to a layer results in the map being |
289 |
considered modified and test whether then calling the map's |
290 |
UnsetModified clears the changed flag in the layer as well. |
291 |
""" |
292 |
self.failIf(self.map.WasModified()) |
293 |
self.poly_layer.GetClassification().SetDefaultFill(Color(0.0, 0.5, 1.0)) |
294 |
self.assert_(self.map.WasModified()) |
295 |
self.map.UnsetModified() |
296 |
self.failIf(self.map.WasModified()) |
297 |
self.failIf(self.poly_layer.WasModified()) |
298 |
self.check_messages([(self.poly_layer, LAYER_CHANGED), |
299 |
(CHANGED,)]) |
300 |
|
301 |
if __name__ == "__main__": |
302 |
support.run_tests() |
303 |
|