File: test_map.py

package info (click to toggle)
thuban 1.2.2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,872 kB
  • ctags: 5,853
  • sloc: python: 30,410; ansic: 6,181; xml: 4,234; cpp: 1,595; makefile: 141
file content (308 lines) | stat: -rw-r--r-- 11,774 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Copyright (c) 2002, 2003 by Intevation GmbH
# Authors:
# Bernhard Herzog <bh@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with Thuban for details.

"""
Test the Map class
"""

__version__ = "$Revision: 2575 $"
# $Source$
# $Id: test_map.py 2575 2005-02-22 11:09:32Z jan $

import os
import unittest

import support
support.initthuban()

from Thuban.Model.messages import CHANGED, MAP_PROJECTION_CHANGED, \
     MAP_LAYERS_CHANGED, MAP_LAYERS_ADDED, MAP_LAYERS_REMOVED,\
     MAP_STACKING_CHANGED, LAYER_VISIBILITY_CHANGED, LAYER_LEGEND_CHANGED, \
     LAYER_CHANGED

from Thuban.Model.session import Session
from Thuban.Model.map import Map
from Thuban.Model.layer import Layer
from Thuban.Model.proj import Projection
from Thuban.Model.color import Color


class TestMapSimple(unittest.TestCase):

    """Very simple test cases for Map"""

    def test_initial_state(self):
        """Test Map's initial state"""
        map = Map("Test Map")
        self.assertEquals(map.Title(), "Test Map")
        self.assertEquals(map.Layers(), [])
        label_layer = map.LabelLayer()
        self.assertEquals(label_layer.Title(), "Labels")
        self.assertEquals(label_layer.Labels(), [])
        self.failIf(map.WasModified())
        map.Destroy()

    def test_empty_map(self):
        """Test empty Map"""
        map = Map("Test Map")
        self.assertEquals(map.BoundingBox(), None)
        self.assertEquals(map.ProjectedBoundingBox(), None)
        self.failIf(map.HasLayers())
        map.Destroy()


class TestMapBase(unittest.TestCase, support.SubscriberMixin):

    """Base class for Map test cases that test messages"""

    def setUp(self):
        """
        Clear the message list, create self.map and subscribe to its messages
        """
        self.clear_messages()

        # Create a Map and subscribe to all interesting channels.
        self.map = Map("Test Map")
        for channel in (CHANGED, 
                        MAP_PROJECTION_CHANGED, 
                        MAP_LAYERS_CHANGED,
                        MAP_LAYERS_ADDED,
                        MAP_LAYERS_REMOVED,
                        MAP_STACKING_CHANGED,
                        LAYER_VISIBILITY_CHANGED, 
                        LAYER_LEGEND_CHANGED,
                        LAYER_CHANGED):
            self.map.Subscribe(channel, self.subscribe_with_params, channel)

    def tearDown(self):
        """Destroy self.map and self.session and clear the message list"""
        if hasattr(self, "session"):
            self.session.Destroy()
            self.session = None
        self.map.Destroy()
        self.map = None
        self.clear_messages()


class TestMapAddLayer(TestMapBase):

    """Simple test cases involving messages"""

    def test_add_layer(self):
        """Test Map.AddLayer"""
        # make sure the created Map is unmodified
        session = self.session = Session("Test session for %s" %self.__class__)
        self.failIf(self.map.WasModified())
        self.failIf(self.map.HasLayers())

        # add a layer and check the result
        filename = os.path.join("..", "Data", "iceland", "roads-line.shp")
        roads = Layer("Roads", session.OpenShapefile(filename))
        self.map.AddLayer(roads)
        self.assertEquals(self.map.Layers(), [roads])
        self.check_messages([(self.map, MAP_LAYERS_CHANGED),
                             (self.map, MAP_LAYERS_ADDED)])
        self.assert_(self.map.WasModified())
        self.assert_(self.map.HasLayers())


class TestMapWithContents(TestMapBase, support.FloatComparisonMixin):

    """More complex Map test cases with messages that.

    All test cases here start with a non-empty map.
    """

    def setUp(self):
        """Extend the inherited method to also fill the Map.

        Put some layers into the map created by the inherited method and
        reset its modified flag. Make also sure that the list of
        received messages is empty.
        """
        TestMapBase.setUp(self)
        self.session = Session("Test session for %s" % self.__class__)
        open_shp = self.session.OpenShapefile
        self.arc_layer = Layer("Roads",
                               open_shp(os.path.join("..", "Data", "iceland",
                                                     "roads-line.shp")))
        self.poly_layer = Layer("Political",
                                open_shp(os.path.join("..", "Data", "iceland",
                                                      "political.shp")))
        self.map.AddLayer(self.arc_layer)
        self.map.AddLayer(self.poly_layer)
        self.map.UnsetModified()
        self.clear_messages()

    def tearDown(self):
        TestMapBase.tearDown(self)
        self.session = None
        self.map = None
        self.poly_layer = self.arc_layer = None

    def test_remove_layer(self):
        """Test Map.RemoveLayer"""
        self.map.RemoveLayer(self.arc_layer)
        self.assert_(self.map.WasModified())
        self.assertEquals(self.map.Layers(), [self.poly_layer])
        self.map.UnsetModified()
        self.check_messages([(self.map, MAP_LAYERS_CHANGED),
                             (self.map, MAP_LAYERS_REMOVED),
                             (CHANGED,)])

    def test_clear_layers(self):
        """Test Map.ClearLayers"""
        self.map.ClearLayers()
        self.assertEquals(self.map.Layers(), [])
        self.assertEquals(self.map.LabelLayer().Labels(), [])
        self.check_messages([(MAP_LAYERS_CHANGED,),
                             (self.map, MAP_LAYERS_CHANGED),
                             (self.map, MAP_LAYERS_REMOVED)])
        self.assert_(self.map.WasModified())
        self.failIf(self.map.HasLayers())

    def test_raise_layer(self):
        """Test Map.RaiseLayer"""
        self.map.RaiseLayer(self.arc_layer)
        self.assertEquals(self.map.Layers(), [self.poly_layer, self.arc_layer])
        self.check_messages([(self.map, MAP_LAYERS_CHANGED),
                             (self.map, MAP_STACKING_CHANGED)])
        self.assert_(self.map.WasModified())

    def test_raise_layer_top(self):
        """Test Map.MoveLayerToTop"""
        open_shp = self.session.OpenShapefile
        dummy = Layer("Roads",
                      open_shp(os.path.join("..", "Data", "iceland",
                                            "roads-line.shp")))
        self.map.AddLayer(dummy)
        self.clear_messages()

        self.map.MoveLayerToTop(self.poly_layer)
        self.assertEquals(self.map.Layers(), 
                          [self.arc_layer, dummy, self.poly_layer])
        self.check_messages([(self.map, MAP_LAYERS_CHANGED),
                             (self.map, MAP_STACKING_CHANGED)])
        self.assert_(self.map.WasModified())

        self.map.RemoveLayer(dummy)

    def test_lower_layer_bottom(self):
        """Test Map.MoveLayerToBottom"""
        open_shp = self.session.OpenShapefile
        dummy = Layer("Roads",
                      open_shp(os.path.join("..", "Data", "iceland",
                                            "roads-line.shp")))
        self.map.AddLayer(dummy)
        self.clear_messages()

        self.map.MoveLayerToBottom(dummy)
        self.assertEquals(self.map.Layers(), 
                          [dummy, self.arc_layer, self.poly_layer])
        self.check_messages([(self.map, MAP_LAYERS_CHANGED),
                             (self.map, MAP_STACKING_CHANGED)])
        self.assert_(self.map.WasModified())

        self.map.RemoveLayer(dummy)

    def test_raise_highest_layer(self):
        """Test Map.RaiseLayer with highest layer

        Attempting to raise the highest layer should not modify the map.
        In particular it should not send any messages.
        """
        self.map.RaiseLayer(self.poly_layer)
        self.assertEquals(self.map.Layers(), [self.arc_layer, self.poly_layer])
        self.check_messages([])
        self.failIf(self.map.WasModified())

    def test_lower_layer(self):
        """Test Map.LowerLayer"""
        self.map.LowerLayer(self.poly_layer)
        self.assertEquals(self.map.Layers(), [self.poly_layer, self.arc_layer])
        self.check_messages([(self.map, MAP_LAYERS_CHANGED),
                             (self.map, MAP_STACKING_CHANGED)])
        self.assert_(self.map.WasModified())

    def test_lower_lowest_layer(self):
        """Test Map.LowerLayer with lowest layer.

        Attempting to lower the lowest layer should not modify the map.
        In particular it should not send any messages.
        """
        self.map.LowerLayer(self.arc_layer)
        self.assertEquals(self.map.Layers(), [self.arc_layer, self.poly_layer])
        self.check_messages([])
        self.failIf(self.map.WasModified())

    def test_bounding_box(self):
        """Test Map.BoundingBox"""
        self.assertFloatSeqEqual(self.map.BoundingBox(),
                                 (-24.546524047851562, 63.286754608154297,
                                  -13.495815277099609, 66.563774108886719))

    def test_projected_bounding_box(self):
        """Test Map.ProjectedBoundingBox"""
        proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
        self.map.SetProjection(proj)
        self.assertFloatSeqEqual(self.map.ProjectedBoundingBox(),
                                 (608873.03380603762, 7019694.6517963577,
                                  1173560.0288053728, 7447353.2203218574),
                                 epsilon = 1e-5)

    def test_set_projection(self):
        """Test Map.SetProjection"""
        proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
        self.map.SetProjection(proj)
        self.check_messages([(self.map, None, MAP_PROJECTION_CHANGED)])
        self.assert_(self.map.WasModified())

    def test_tree_info(self):
        """Test Map.TreeInfo"""
        proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"])
        self.map.SetProjection(proj)
        # compute the extent string because there are platform
        # differences in the way %g is handled:
        # glibc: "%g" % 7.01969e+06 == "7.01969e+06"
        # w32/VC: "%g" % 7.01969e+06 == "7.01969e+006"
        extent = 'Extent (projected): (%g, %g, %g, %g)'\
                 % (608873, 7.01969e+06, 1.17356e+06, 7.44735e+06)
        self.assertEquals(self.map.TreeInfo(),
                          ('Map: Test Map',
                           [('Extent (lat-lon):'
                             ' (-24.5465, 63.2868, -13.4958, 66.5638)'),
                            extent,
                            ('Projection',
                             ['zone=26', 'proj=utm', 'ellps=clrk66']),
                            self.poly_layer,
                            self.arc_layer,
                            self.map.LabelLayer()]))

    def test_forwarding_visibility(self):
        """Test Map's forwarding of Layer.SetVisible messages"""
        self.poly_layer.SetVisible(0)
        self.check_messages([(self.poly_layer, LAYER_VISIBILITY_CHANGED)])

    def test_unset_modified(self):
        """Test Map.UnsetModified.

        Test whether a change to a layer results in the map being
        considered modified and test whether then calling the map's
        UnsetModified clears the changed flag in the layer as well.
        """
        self.failIf(self.map.WasModified())
        self.poly_layer.GetClassification().SetDefaultFill(Color(0.0, 0.5, 1.0))
        self.assert_(self.map.WasModified())
        self.map.UnsetModified()
        self.failIf(self.map.WasModified())
        self.failIf(self.poly_layer.WasModified())
        self.check_messages([(self.poly_layer, LAYER_CHANGED),
                             (CHANGED,)])

if __name__ == "__main__":
    support.run_tests()