File: test_viewport.py

package info (click to toggle)
thuban 1.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,752 kB
  • sloc: python: 30,427; ansic: 6,181; xml: 4,127; cpp: 1,595; makefile: 166
file content (509 lines) | stat: -rw-r--r-- 20,022 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# Copyright (c) 2003, 2004 by Intevation GmbH
# Authors:
# Jonathan Coles <jonathan@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with Thuban for details.

"""
Test the interaction with the view
"""

__version__ = "$Revision: 2297 $"
# $Source$
# $Id: test_viewport.py 2297 2004-07-22 13:07:52Z bh $

import os
import unittest

import postgissupport
import support
support.initthuban()

from Thuban.UI.viewport import ViewPort, ZoomInTool, ZoomOutTool, \
                               PanTool, IdentifyTool, LabelTool

from Thuban.Model.map import Map
from Thuban.Model.proj import Projection
from Thuban.Model.layer import Layer
from Thuban.Model.session import Session
from Thuban.Model.color import Color
from Thuban.Model.postgisdb import PostGISConnection
from Thuban.UI.messages import SCALE_CHANGED, MAP_REPLACED
from Thuban.Model.messages import TITLE_CHANGED

class Event:
    pass


class MockView(ViewPort):

    def GetTextExtent(self, text):
        """Mock implementation so that the test cases work"""
        # arbitrary numbers, really just so the tests pass
        return 40, 20



class SimpleViewPortTest(unittest.TestCase):

    """Simple ViewPort tests"""

    def test_default_size(self):
        """Test ViewPort default size and scale"""
        port = ViewPort()
        try:
            self.assertEquals(port.GetPortSizeTuple(), (400, 300))
            self.assertEquals(port.scale, 1.0)
            self.assertEquals(port.offset, (0, 0))
            self.assertEquals(port.VisibleExtent(), (0.0, -300.0, 400.0, 0.0))
        finally:
            port.Destroy()

    def test_init_with_size(self):
        """Test ViewPort(<size>)"""
        port = ViewPort((1001, 1001))
        try:
            self.assertEquals(port.GetPortSizeTuple(), (1001, 1001))
            self.assertEquals(port.VisibleExtent(), (0.0, -1001.0, 1001.0, 0.0))
        finally:
            port.Destroy()

    def test_visible_extent(self):
        """Test ViewPort.VisibleExtent()"""
        class MockMap:
            def ProjectedBoundingBox(self):
                return (500, 400, 600, 500)
            # noops that the viewport expects but which aren't needed
            # here:
            Subscribe = Unsubscribe = lambda *args: None

        map = MockMap()
        port = ViewPort((1000, 1000))
        try:
            port.SetMap(map)
            # The viewport adjusts automatically to the map.  Since both
            # the map's bounding box and the viewport are square the map
            # fits exactly.
            self.assertEquals(port.VisibleExtent(), (500, 400, 600, 500))

            # Zoom in a bit
            port.ZoomFactor(2)
            self.assertEquals(port.VisibleExtent(), (525, 425, 575, 475))
        finally:
            port.Destroy()


class ViewPortTest(unittest.TestCase, support.SubscriberMixin,
                   support.FloatComparisonMixin):

    def build_path(self, filename):
        return os.path.join("..", "Data", "iceland", filename)

    def open_shapefile(self, filename):
        """Open and return a shapestore for filename in the iceland data set"""
        return self.session.OpenShapefile(self.build_path(filename))

    def setUp(self):
        self.session = Session("Test session for %s" % self.__class__)

        # make view port 1001x1001 so we have an exact center
        self.port = MockView((1001, 1001))

        proj = Projection(["proj=latlong", 
                           "to_meter=.017453292519943",
                           "ellps=clrk66"])

        self.map = map = Map("title", proj)
        layer = Layer("Polygon", self.open_shapefile("political.shp"))
        layer.GetClassification().GetDefaultGroup()\
                        .GetProperties().SetFill(Color(0,0,0))
        map.AddLayer(layer)
        layer = Layer("Point",
                      self.open_shapefile("cultural_landmark-point.shp"))
        layer.GetClassification().GetDefaultGroup()\
                        .GetProperties().SetFill(Color(0,0,0))
        map.AddLayer(layer)
        layer = Layer("Arc", self.open_shapefile("roads-line.shp"))
        layer.GetClassification().GetDefaultGroup()\
                        .GetProperties().SetFill(Color(0,0,0))
        map.AddLayer(layer)
        self.session.AddMap(map)

        self.layer = layer

        self.port.SetMap(map)
        for msg in (SCALE_CHANGED, MAP_REPLACED, TITLE_CHANGED):
            self.port.Subscribe(msg, self.subscribe_with_params, msg)
        self.clear_messages()

    def tearDown(self):
        self.port.Destroy()
        self.session.Destroy()
        self.map = self.session = self.port = self.layer = None

    def test_inital_settings(self):
        self.failIf(self.port.HasSelectedLayer())
        self.failIf(self.port.HasSelectedShapes())

    def test_win_to_proj(self):
        self.assertFloatSeqEqual(self.port.win_to_proj(0, 0),
                                 (-24.546524047851978, 70.450618743897664))
        self.assertFloatSeqEqual(self.port.win_to_proj(100, 0),
                                 (-23.442557137686929, 70.450618743897664))
        self.assertFloatSeqEqual(self.port.win_to_proj(0, 100),
                                 (-24.546524047851978, 69.346651833732622))

    def test_proj_to_win(self):
        self.assertFloatSeqEqual(self.port.proj_to_win(-24.546524047851978,
                                                       70.450618743897664),
                                 (0, 0))
        self.assertFloatSeqEqual(self.port.proj_to_win(-23.442557137686929,
                                                       70.450618743897664),
                                 (100, 0))
        self.assertFloatSeqEqual(self.port.proj_to_win(-24.546524047851978,
                                                       69.346651833732622),
                                 (0, 100))

    def test_set_map(self):
        """Test ViewPort.SetMap()"""
        # The port already has a map. So we set it to None before we set
        # it to self.map again.

        # Setting the map to None does not change the scale, but it will
        # issue a MAP_REPLACED message.
        self.port.SetMap(None)
        self.check_messages([(MAP_REPLACED,)])

        self.clear_messages()

        self.port.SetMap(self.map)
        self.check_messages([(90.582425142660739, SCALE_CHANGED),
                             (MAP_REPLACED,)])

    def test_changing_map_projection(self):
        """Test ViewPort behavior when changing the map's projection

        The viewport subscribe's to the map's MAP_PROJECTION_CHANGED
        messages and tries to adjust the viewport when the projection
        changes to make sure the map is still visible in the window.
        There was a bug at one point where the viewport couldn't cope
        with the map not having a meaningful bounding box in this
        situation.
        """
        # Create a projection and an empty map.  We can't use self.map
        # here because we do need an empty one.
        themap = Map("title", Projection(["proj=latlong",
                                          "to_meter=.017453292519943",
                                          "ellps=clrk66"]))
        # Add the map to self.session so that it's properly destroyed in
        # tearDown()
        self.session.AddMap(themap)

        # Add the map to the view port and clear the messages.  Then
        # we're set for the actual test.
        self.port.SetMap(themap)
        self.clear_messages()

        # The test: set another projection.  The viewport tries to
        # adjust the view so that the currently visible region stays
        # visible.  The viewport has to take into account that the map
        # is empty, which it didn't in Thuban/UI/viewport.py rev <= 1.16.
        # This part of the test is OK when the SetProjection call does
        # not lead to an exception.
        themap.SetProjection(Projection(["proj=latlong",
                                         "to_meter=.017453292519943",
                                         "ellps=clrk66"]))

        # If the map weren't empty the viewport might send SCALE_CHANGED
        # messages, but it must no do so in this case because the scale
        # doesn't change.
        self.check_messages([])

    def testFitRectToWindow(self):
        rect = self.port.win_to_proj(9, 990) + self.port.win_to_proj(990, 9)
        self.port.FitRectToWindow(rect)
        self.assertFloatSeqEqual(rect, self.port.win_to_proj(0, 1000)
                                 + self.port.win_to_proj(1000, 0), 1e-1)

    def testZoomFactor(self):
        self.port.FitMapToWindow()
        rect = self.port.win_to_proj(9, 990) + self.port.win_to_proj(990, 9)
        proj_rect = self.port.win_to_proj(0,1000)+self.port.win_to_proj(1000,0)
        self.port.ZoomFactor(2)
        self.port.ZoomFactor(.5)
        self.assertFloatSeqEqual(rect,
                                 self.port.win_to_proj(0, 1000)
                                 + self.port.win_to_proj(1000, 0), 1)

        point = self.port.win_to_proj(600, 600)
        self.port.ZoomFactor(2, (600, 600))
        self.assertFloatSeqEqual(point, self.port.win_to_proj(500, 500), 1e-3)
        self.port.FitMapToWindow()

        proj_rect = self.port.win_to_proj(-499, 1499)\
                    + self.port.win_to_proj(1499, -499)
        self.port.ZoomFactor(.5)
        self.assertFloatSeqEqual(proj_rect,
                                 self.port.win_to_proj(0, 1000)
                                 + self.port.win_to_proj(1000, 0), 1)

    def testZoomOutToRect(self):
        self.port.FitMapToWindow()
        rect   = self.port.win_to_proj(9, 990) + self.port.win_to_proj(990, 9)
        rectTo = self.port.win_to_proj(0, 1000) + self.port.win_to_proj(1000,
                                                                        0)
        self.port.ZoomOutToRect(rect)
        self.assertFloatSeqEqual(rect, rectTo, 1)

    def testTranslate(self):
        self.port.FitMapToWindow()
        orig_rect = self.port.win_to_proj(0,1000)+self.port.win_to_proj(1000,0)
        for delta in [(0, 0), (5, 0), (0, 5), (5,5),
                      (-5, 0), (0, -5), (-5, -5)]:
            rect = self.port.win_to_proj(0 + delta[0], 1000 + delta[1])  \
                   + self.port.win_to_proj(1000 + delta[0], 0 + delta[1])
            self.port.Translate(delta[0], delta[1])
            self.assertFloatSeqEqual(rect,
                                     self.port.win_to_proj(0, 1000)
                                     + self.port.win_to_proj(1000, 0), 1)
            self.port.Translate(-delta[0], -delta[1])
            self.assertFloatSeqEqual(rect, orig_rect, 1)

    def test_unprojected_rect_around_point(self):
        rect = self.port.unprojected_rect_around_point(500, 500, 5)
        self.assertFloatSeqEqual(rect,
                                 (-19.063379161960469, 64.924498140752377, 
                                  -18.95455127948528, 65.033326023227573),
                                 1e-1)

    def test_find_shape_at(self):
        eq = self.assertEquals
        x, y = self.port.proj_to_win(-18, 64.81418571)
        eq(self.port.find_shape_at(x, y, searched_layer=self.layer),
           (None, None))

        x, y = self.port.proj_to_win(-18.18776318, 64.81418571)
        eq(self.port.find_shape_at(x, y, searched_layer=self.layer),
           (self.layer, 610))

    def testLabelShapeAt(self):
        eq = self.assertEquals

        # select a road
        x, y = self.port.proj_to_win(-18.18776318, 64.81418571)
        eq(self.port.LabelShapeAt(x, y), False) # nothing to do
        eq(self.port.LabelShapeAt(x, y, "Hello world"), True) # add
        eq(self.port.LabelShapeAt(x, y), True) # remove

        # select a point
        x, y = self.port.proj_to_win(-19.140, 63.4055717)
        eq(self.port.LabelShapeAt(x, y), False) # nothing to do
        eq(self.port.LabelShapeAt(x, y, "Hello world"), True) # add
        eq(self.port.LabelShapeAt(x, y), True) # remove

        # select a polygon
        x, y = self.port.proj_to_win(-16.75286628, 64.67807745)
        eq(self.port.LabelShapeAt(x, y), False) # nothing to do
        eq(self.port.LabelShapeAt(x, y, "Hello world"), True) # add
        # for polygons the coordinates will be different, so
        # these numbers were copied
        x, y = self.port.proj_to_win(-18.5939850348, 64.990607973)
        eq(self.port.LabelShapeAt(x, y), True) # remove


    def test_set_pos(self):
        eq = self.assertEquals
        # set_current_position / CurrentPosition
        event = Event()
        event.m_x, event.m_y = 5, 5
        self.port.set_current_position(event)
        eq(self.port.current_position, (5, 5))
        eq(self.port.CurrentPosition(), self.port.win_to_proj(5, 5))
        self.port.set_current_position(None)
        eq(self.port.current_position, None)
        eq(self.port.CurrentPosition(), None)

        event.m_x, event.m_y = 15, 15
        self.port.MouseMove(event)
        eq(self.port.current_position, (15, 15))
        event.m_x, event.m_y = 25, 15
        self.port.MouseLeftDown(event)
        eq(self.port.current_position, (25, 15))
        event.m_x, event.m_y = 15, 25
        self.port.MouseLeftUp(event)
        eq(self.port.current_position, (15, 25))

    def testTools(self):
        eq = self.assertEquals
        event = Event()
        def test_tools(tool, shortcut):
            self.port.SelectTool(tool)
            eq(self.port.CurrentTool(), tool.Name())
            self.port.SelectTool(None)
            eq(self.port.CurrentTool(), None)
            shortcut()
            eq(self.port.CurrentTool(), tool.Name())

        test_tools(ZoomInTool(self.port), self.port.ZoomInTool)

        point = self.port.win_to_proj(600, 600)

        # one click zoom
        event.m_x, event.m_y = 600, 600
        self.port.MouseMove(event)
        self.port.MouseLeftDown(event)
        self.port.MouseLeftUp(event)
        self.assertFloatSeqEqual(point, self.port.win_to_proj(500, 500), 1e-3)
        self.port.FitMapToWindow()

        # zoom rectangle
        rect = self.port.win_to_proj(29, 970) + self.port.win_to_proj(970, 29)
        event.m_x, event.m_y = 29, 29
        self.port.MouseMove(event)
        self.port.MouseLeftDown(event)
        event.m_x, event.m_y = 970, 970
        self.port.MouseMove(event)
        self.port.MouseLeftUp(event)
        self.assertFloatSeqEqual(rect,
                                 self.port.win_to_proj(0, 1000)
                                 + self.port.win_to_proj(1000, 0), 1e-1)
        self.port.FitMapToWindow()

        test_tools(ZoomOutTool(self.port), self.port.ZoomOutTool)

        # one click zoom out
        proj_rect = self.port.win_to_proj(-499, 1499) \
                    + self.port.win_to_proj(1499, -499)
        event.m_x, event.m_y = 500, 500
        self.port.MouseMove(event)
        self.port.MouseLeftDown(event)
        self.port.MouseLeftUp(event)
        self.assertFloatSeqEqual(proj_rect,
                                 self.port.win_to_proj(0, 1000)
                                 + self.port.win_to_proj(1000, 0),1e-1)
        self.port.FitMapToWindow()

        # zoom out rectangle
        rect = self.port.win_to_proj(0, 1000) + self.port.win_to_proj(1000, 0)
        event.m_x, event.m_y = 29, 29
        self.port.MouseMove(event)
        self.port.MouseLeftDown(event)
        event.m_x, event.m_y = 970, 970
        self.port.MouseMove(event)
        self.port.MouseLeftUp(event)
        self.assertFloatSeqEqual(rect,
                                 self.port.win_to_proj(29, 970)
                                 + self.port.win_to_proj(970, 29))
        self.port.FitMapToWindow()

        test_tools(PanTool(self.port), self.port.PanTool)

        rect = self.port.win_to_proj(-25, 975) + self.port.win_to_proj(975,-25)
        event.m_x, event.m_y = 50, 50
        self.port.MouseMove(event)
        self.port.MouseLeftDown(event)
        event.m_x, event.m_y = 75, 75
        self.port.MouseMove(event)
        self.port.MouseLeftUp(event)
        self.assertFloatSeqEqual(rect,
                                 self.port.win_to_proj(0, 1000)
                                 + self.port.win_to_proj(1000, 0))

        test_tools(IdentifyTool(self.port), self.port.IdentifyTool)

        event.m_x, event.m_y = self.port.proj_to_win(-18.18776318, 64.81418571)
        self.port.MouseMove(event)
        self.port.MouseLeftDown(event)
        self.port.MouseLeftUp(event)
        eq(self.port.SelectedShapes(), [610])

        test_tools(LabelTool(self.port), self.port.LabelTool)

        # since adding a label requires use interaction with a dialog
        # we will insert a label and then only test whether clicking
        # removes the label

        x, y = self.port.proj_to_win(-19.140, 63.4055717)
        self.port.LabelShapeAt(x, y, "Hello world")
        event.m_x, event.m_y = x, y
        self.port.MouseMove(event)
        self.port.MouseLeftDown(event)
        self.port.MouseLeftUp(event)
        eq(self.port.LabelShapeAt(x, y), False) # should have done nothing

    def test_forwarding_title_changed(self):
        """Test whether ViewPort forwards the TITLE_CHANGED message of the map
        """
        self.map.SetTitle(self.map.Title() + " something to make it different")
        self.check_messages([(self.map, TITLE_CHANGED)])


class TestViewportWithPostGIS(unittest.TestCase):

    def setUp(self):
        """Start the server and create a database.

        The database name will be stored in self.dbname, the server
        object in self.server and the db object in self.db.
        """
        postgissupport.skip_if_no_postgis()
        self.server = postgissupport.get_test_server()
        self.dbref = self.server.get_default_static_data_db()
        self.dbname = self.dbref.dbname
        self.session = Session("PostGIS Session")
        self.db = PostGISConnection(dbname = self.dbname,
                                    **self.server.connection_params("user"))

        proj = Projection(["proj=latlong",
                           "to_meter=.017453292519943",
                           "ellps=clrk66"])
        self.map = Map("title", proj)

        self.port = ViewPort((1001, 1001))

    def tearDown(self):
        self.session.Destroy()
        self.port.Destroy()
        self.map.Destroy()
        self.map = self.port = None

    def test_find_shape_at_point(self):
        """Test ViewPort.find_shape_at() with postgis point layer"""
        layer = Layer("Point",
                      self.session.OpenDBShapeStore(self.db, "landmarks"))
        prop = layer.GetClassification().GetDefaultGroup().GetProperties()
        prop.SetFill(Color(0,0,0))
        self.map.AddLayer(layer)

        self.port.SetMap(self.map)

        x, y = self.port.proj_to_win(-22.54335021, 66.30889129)
        self.assertEquals(self.port.find_shape_at(x, y), (layer, 1001))

    def test_find_shape_at_arc(self):
        """Test ViewPort.find_shape_at() with postgis arc layer"""
        layer = Layer("Arc", self.session.OpenDBShapeStore(self.db, "roads"))
        self.map.AddLayer(layer)

        self.port.SetMap(self.map)

        x, y = self.port.proj_to_win(-18.18776318, 64.81418571)
        self.assertEquals(self.port.find_shape_at(x, y), (layer, 610))

    def test_find_shape_at_polygon(self):
        """Test ViewPort.find_shape_at() with postgis polygon layer"""
        layer = Layer("Poly",
                      self.session.OpenDBShapeStore(self.db, "political"))
        prop = layer.GetClassification().GetDefaultGroup().GetProperties()
        prop.SetFill(Color(0,0,0))
        self.map.AddLayer(layer)

        self.port.SetMap(self.map)

        x, y = self.port.proj_to_win(-19.78369, 65.1649143)
        self.assertEquals(self.port.find_shape_at(x, y), (layer, 144))


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