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