/[thuban]/branches/WIP-pyshapelib-bramz/test/test_viewport.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/test/test_viewport.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1774 - (hide annotations)
Mon Oct 6 13:09:18 2003 UTC (21 years, 5 months ago) by bh
Original Path: trunk/thuban/test/test_viewport.py
File MIME type: text/x-python
File size: 16817 byte(s)
(ViewPortTest.setUp)
(SimpleViewPortTest.test_init_with_size): Move the test for the
initial size as a constructor parameter from ViewPortTest.setUp
method to a new separate test in SimpleViewPortTest.

1 jonathan 1440 # Copyright (c) 2003 by Intevation GmbH
2     # Authors:
3     # Jonathan Coles <[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 interaction with the view
10     """
11    
12     __version__ = "$Revision$"
13     # $Source$
14     # $Id$
15    
16     import os
17     import unittest
18    
19 bh 1608 import postgissupport
20 jonathan 1440 import support
21     support.initthuban()
22    
23     from Thuban.UI.viewport import ViewPort, ZoomInTool, ZoomOutTool, \
24     PanTool, IdentifyTool, LabelTool
25    
26     from Thuban.Model.map import Map
27     from Thuban.Model.proj import Projection
28     from Thuban.Model.layer import Layer
29     from Thuban.Model.session import Session
30     from Thuban.Model.color import Color
31 bh 1608 from Thuban.Model.postgisdb import PostGISConnection
32 bh 1464 from Thuban.UI.messages import SCALE_CHANGED, MAP_REPLACED
33 jonathan 1440
34 bh 1462 class Event:
35     pass
36 jonathan 1440
37 bh 1462
38 bh 1772 class MockView(ViewPort):
39    
40     def GetTextExtent(self, text):
41     """Mock implementation so that the test cases work"""
42     # arbitrary numbers, really just so the tests pass
43     return 40, 20
44    
45    
46    
47 bh 1462 class SimpleViewPortTest(unittest.TestCase):
48    
49 bh 1464 """Simple ViewPort tests"""
50    
51 bh 1462 def test_default_size(self):
52 bh 1464 """Test ViewPort default size and scale"""
53 bh 1462 port = ViewPort()
54     try:
55     self.assertEquals(port.GetPortSizeTuple(), (400, 300))
56     self.assertEquals(port.scale, 1.0)
57     self.assertEquals(port.offset, (0, 0))
58     finally:
59     port.Destroy()
60    
61 bh 1774 def test_init_with_size(self):
62     """Test ViewPort(<size>)"""
63     port = ViewPort((1001, 1001))
64     try:
65     self.assertEquals(port.GetPortSizeTuple(), (1001, 1001))
66     finally:
67     port.Destroy()
68 bh 1464
69 bh 1774
70 jonathan 1440 class ViewPortTest(unittest.TestCase, support.SubscriberMixin,
71 bh 1608 support.FloatComparisonMixin):
72 jonathan 1440
73     def build_path(self, filename):
74     return os.path.join("..", "Data", "iceland", filename)
75 bh 1462
76 jonathan 1440 def open_shapefile(self, filename):
77     """Open and return a shapestore for filename in the iceland data set"""
78     return self.session.OpenShapefile(self.build_path(filename))
79    
80     def setUp(self):
81     self.session = Session("Test session for %s" % self.__class__)
82    
83     # make view port 1001x1001 so we have an exact center
84 bh 1772 self.port = MockView((1001, 1001))
85 jonathan 1440
86     proj = Projection(["proj=latlong",
87     "to_meter=.017453292519943",
88     "ellps=clrk66"])
89    
90 bh 1464 self.map = map = Map("title", proj)
91 jonathan 1440 layer = Layer("Polygon", self.open_shapefile("political.shp"))
92 bh 1462 layer.GetClassification().GetDefaultGroup()\
93     .GetProperties().SetFill(Color(0,0,0))
94 jonathan 1440 map.AddLayer(layer)
95 bh 1462 layer = Layer("Point",
96     self.open_shapefile("cultural_landmark-point.shp"))
97     layer.GetClassification().GetDefaultGroup()\
98     .GetProperties().SetFill(Color(0,0,0))
99 jonathan 1440 map.AddLayer(layer)
100     layer = Layer("Arc", self.open_shapefile("roads-line.shp"))
101 bh 1462 layer.GetClassification().GetDefaultGroup()\
102     .GetProperties().SetFill(Color(0,0,0))
103 jonathan 1440 map.AddLayer(layer)
104     self.session.AddMap(map)
105    
106     self.layer = layer
107    
108 bh 1464 self.port.SetMap(map)
109 bh 1462 self.port.Subscribe(SCALE_CHANGED, self.subscribe_with_params,
110     SCALE_CHANGED)
111 bh 1464 self.port.Subscribe(MAP_REPLACED, self.subscribe_with_params,
112     MAP_REPLACED)
113     self.clear_messages()
114 jonathan 1440
115     def tearDown(self):
116     self.port.Destroy()
117     self.session.Destroy()
118 bh 1464 self.map = self.session = self.port = self.layer = None
119 jonathan 1440
120 bh 1462 def test_inital_settings(self):
121     self.failIf(self.port.HasSelectedLayer())
122     self.failIf(self.port.HasSelectedShapes())
123 jonathan 1440
124 bh 1462 def test_win_to_proj(self):
125     self.assertFloatSeqEqual(self.port.win_to_proj(0, 0),
126     (-24.546524047851978, 70.450618743897664))
127     self.assertFloatSeqEqual(self.port.win_to_proj(100, 0),
128     (-23.442557137686929, 70.450618743897664))
129     self.assertFloatSeqEqual(self.port.win_to_proj(0, 100),
130     (-24.546524047851978, 69.346651833732622))
131 jonathan 1440
132 bh 1462 def test_proj_to_win(self):
133     self.assertFloatSeqEqual(self.port.proj_to_win(-24.546524047851978,
134     70.450618743897664),
135     (0, 0))
136     self.assertFloatSeqEqual(self.port.proj_to_win(-23.442557137686929,
137     70.450618743897664),
138     (100, 0))
139     self.assertFloatSeqEqual(self.port.proj_to_win(-24.546524047851978,
140     69.346651833732622),
141     (0, 100))
142    
143 bh 1464 def test_set_map(self):
144     """Test ViewPort.SetMap()"""
145     # The port already has a map. So we set it to None before we set
146     # it to self.map again.
147    
148     # Setting the map to None does not change the scale, but it will
149     # issue a MAP_REPLACED message.
150     self.port.SetMap(None)
151     self.check_messages([(MAP_REPLACED,)])
152    
153     self.clear_messages()
154    
155     self.port.SetMap(self.map)
156     self.check_messages([(90.582425142660739, SCALE_CHANGED),
157     (MAP_REPLACED,)])
158    
159 jonathan 1440 def testFitRectToWindow(self):
160     rect = self.port.win_to_proj(9, 990) + self.port.win_to_proj(990, 9)
161     self.port.FitRectToWindow(rect)
162 bh 1462 self.assertFloatSeqEqual(rect, self.port.win_to_proj(0, 1000)
163     + self.port.win_to_proj(1000, 0), 1e-1)
164 jonathan 1440
165     def testZoomFactor(self):
166     self.port.FitMapToWindow()
167     rect = self.port.win_to_proj(9, 990) + self.port.win_to_proj(990, 9)
168 bh 1462 proj_rect = self.port.win_to_proj(0,1000)+self.port.win_to_proj(1000,0)
169 jonathan 1440 self.port.ZoomFactor(2)
170     self.port.ZoomFactor(.5)
171 bh 1462 self.assertFloatSeqEqual(rect,
172     self.port.win_to_proj(0, 1000)
173     + self.port.win_to_proj(1000, 0), 1)
174 jonathan 1440
175     point = self.port.win_to_proj(600, 600)
176     self.port.ZoomFactor(2, (600, 600))
177 bh 1462 self.assertFloatSeqEqual(point, self.port.win_to_proj(500, 500), 1e-3)
178     self.port.FitMapToWindow()
179 jonathan 1440
180 bh 1462 proj_rect = self.port.win_to_proj(-499, 1499)\
181     + self.port.win_to_proj(1499, -499)
182 jonathan 1440 self.port.ZoomFactor(.5)
183 bh 1462 self.assertFloatSeqEqual(proj_rect,
184     self.port.win_to_proj(0, 1000)
185     + self.port.win_to_proj(1000, 0), 1)
186 jonathan 1440
187     def testZoomOutToRect(self):
188     self.port.FitMapToWindow()
189     rect = self.port.win_to_proj(9, 990) + self.port.win_to_proj(990, 9)
190 bh 1462 rectTo = self.port.win_to_proj(0, 1000) + self.port.win_to_proj(1000,
191     0)
192 jonathan 1440 self.port.ZoomOutToRect(rect)
193     self.assertFloatSeqEqual(rect, rectTo, 1)
194    
195     def testTranslate(self):
196     self.port.FitMapToWindow()
197 bh 1462 orig_rect = self.port.win_to_proj(0,1000)+self.port.win_to_proj(1000,0)
198 jonathan 1440 for delta in [(0, 0), (5, 0), (0, 5), (5,5),
199     (-5, 0), (0, -5), (-5, -5)]:
200     rect = self.port.win_to_proj(0 + delta[0], 1000 + delta[1]) \
201     + self.port.win_to_proj(1000 + delta[0], 0 + delta[1])
202     self.port.Translate(delta[0], delta[1])
203 bh 1462 self.assertFloatSeqEqual(rect,
204     self.port.win_to_proj(0, 1000)
205     + self.port.win_to_proj(1000, 0), 1)
206 jonathan 1440 self.port.Translate(-delta[0], -delta[1])
207     self.assertFloatSeqEqual(rect, orig_rect, 1)
208    
209     def test_unprojected_rect_around_point(self):
210     rect = self.port.unprojected_rect_around_point(500, 500, 5)
211 bh 1462 self.assertFloatSeqEqual(rect,
212     (-19.063379161960469, 64.924498140752377,
213     -18.95455127948528, 65.033326023227573),
214     1e-1)
215 jonathan 1440
216     def test_find_shape_at(self):
217     eq = self.assertEquals
218     x, y = self.port.proj_to_win(-18, 64.81418571)
219 bh 1462 eq(self.port.find_shape_at(x, y, searched_layer=self.layer),
220     (None, None))
221 jonathan 1440
222     x, y = self.port.proj_to_win(-18.18776318, 64.81418571)
223 bh 1462 eq(self.port.find_shape_at(x, y, searched_layer=self.layer),
224     (self.layer, 610))
225 jonathan 1440
226     def testLabelShapeAt(self):
227     eq = self.assertEquals
228    
229     # select a road
230     x, y = self.port.proj_to_win(-18.18776318, 64.81418571)
231     eq(self.port.LabelShapeAt(x, y), False) # nothing to do
232     eq(self.port.LabelShapeAt(x, y, "Hello world"), True) # add
233     eq(self.port.LabelShapeAt(x, y), True) # remove
234    
235     # select a point
236     x, y = self.port.proj_to_win(-19.140, 63.4055717)
237     eq(self.port.LabelShapeAt(x, y), False) # nothing to do
238     eq(self.port.LabelShapeAt(x, y, "Hello world"), True) # add
239     eq(self.port.LabelShapeAt(x, y), True) # remove
240    
241     # select a polygon
242     x, y = self.port.proj_to_win(-16.75286628, 64.67807745)
243     eq(self.port.LabelShapeAt(x, y), False) # nothing to do
244     eq(self.port.LabelShapeAt(x, y, "Hello world"), True) # add
245     # for polygons the coordinates will be different, so
246     # these numbers were copied
247     x, y = self.port.proj_to_win(-18.5939850348, 64.990607973)
248     eq(self.port.LabelShapeAt(x, y), True) # remove
249    
250    
251     def test_set_pos(self):
252     eq = self.assertEquals
253     # set_current_position / CurrentPosition
254     event = Event()
255     event.m_x, event.m_y = 5, 5
256     self.port.set_current_position(event)
257     eq(self.port.current_position, (5, 5))
258     eq(self.port.CurrentPosition(), self.port.win_to_proj(5, 5))
259     self.port.set_current_position(None)
260     eq(self.port.current_position, None)
261     eq(self.port.CurrentPosition(), None)
262    
263     event.m_x, event.m_y = 15, 15
264     self.port.MouseMove(event)
265     eq(self.port.current_position, (15, 15))
266     event.m_x, event.m_y = 25, 15
267     self.port.MouseLeftDown(event)
268     eq(self.port.current_position, (25, 15))
269     event.m_x, event.m_y = 15, 25
270     self.port.MouseLeftUp(event)
271     eq(self.port.current_position, (15, 25))
272    
273     def testTools(self):
274     eq = self.assertEquals
275     event = Event()
276     def test_tools(tool, shortcut):
277     self.port.SelectTool(tool)
278     eq(self.port.CurrentTool(), tool.Name())
279     self.port.SelectTool(None)
280     eq(self.port.CurrentTool(), None)
281     shortcut()
282     eq(self.port.CurrentTool(), tool.Name())
283    
284     test_tools(ZoomInTool(self.port), self.port.ZoomInTool)
285    
286     point = self.port.win_to_proj(600, 600)
287    
288     # one click zoom
289     event.m_x, event.m_y = 600, 600
290     self.port.MouseMove(event)
291     self.port.MouseLeftDown(event)
292     self.port.MouseLeftUp(event)
293 bh 1462 self.assertFloatSeqEqual(point, self.port.win_to_proj(500, 500), 1e-3)
294     self.port.FitMapToWindow()
295 jonathan 1440
296     # zoom rectangle
297     rect = self.port.win_to_proj(29, 970) + self.port.win_to_proj(970, 29)
298     event.m_x, event.m_y = 29, 29
299     self.port.MouseMove(event)
300     self.port.MouseLeftDown(event)
301     event.m_x, event.m_y = 970, 970
302     self.port.MouseMove(event)
303     self.port.MouseLeftUp(event)
304 bh 1462 self.assertFloatSeqEqual(rect,
305     self.port.win_to_proj(0, 1000)
306     + self.port.win_to_proj(1000, 0), 1e-1)
307     self.port.FitMapToWindow()
308 jonathan 1440
309     test_tools(ZoomOutTool(self.port), self.port.ZoomOutTool)
310    
311     # one click zoom out
312 bh 1462 proj_rect = self.port.win_to_proj(-499, 1499) \
313     + self.port.win_to_proj(1499, -499)
314 jonathan 1440 event.m_x, event.m_y = 500, 500
315     self.port.MouseMove(event)
316     self.port.MouseLeftDown(event)
317     self.port.MouseLeftUp(event)
318 bh 1462 self.assertFloatSeqEqual(proj_rect,
319     self.port.win_to_proj(0, 1000)
320     + self.port.win_to_proj(1000, 0),1e-1)
321     self.port.FitMapToWindow()
322 jonathan 1440
323     # zoom out rectangle
324     rect = self.port.win_to_proj(0, 1000) + self.port.win_to_proj(1000, 0)
325     event.m_x, event.m_y = 29, 29
326     self.port.MouseMove(event)
327     self.port.MouseLeftDown(event)
328     event.m_x, event.m_y = 970, 970
329     self.port.MouseMove(event)
330     self.port.MouseLeftUp(event)
331 bh 1462 self.assertFloatSeqEqual(rect,
332     self.port.win_to_proj(29, 970)
333     + self.port.win_to_proj(970, 29))
334     self.port.FitMapToWindow()
335 jonathan 1440
336     test_tools(PanTool(self.port), self.port.PanTool)
337    
338 bh 1462 rect = self.port.win_to_proj(-25, 975) + self.port.win_to_proj(975,-25)
339 jonathan 1440 event.m_x, event.m_y = 50, 50
340     self.port.MouseMove(event)
341     self.port.MouseLeftDown(event)
342     event.m_x, event.m_y = 75, 75
343     self.port.MouseMove(event)
344     self.port.MouseLeftUp(event)
345 bh 1462 self.assertFloatSeqEqual(rect,
346     self.port.win_to_proj(0, 1000)
347     + self.port.win_to_proj(1000, 0))
348 jonathan 1440
349     test_tools(IdentifyTool(self.port), self.port.IdentifyTool)
350    
351     event.m_x, event.m_y = self.port.proj_to_win(-18.18776318, 64.81418571)
352     self.port.MouseMove(event)
353     self.port.MouseLeftDown(event)
354     self.port.MouseLeftUp(event)
355     eq(self.port.SelectedShapes(), [610])
356 bh 1462
357 jonathan 1440 test_tools(LabelTool(self.port), self.port.LabelTool)
358    
359     # since adding a label requires use interaction with a dialog
360     # we will insert a label and then only test whether clicking
361     # removes the label
362    
363     x, y = self.port.proj_to_win(-19.140, 63.4055717)
364     self.port.LabelShapeAt(x, y, "Hello world")
365     event.m_x, event.m_y = x, y
366     self.port.MouseMove(event)
367     self.port.MouseLeftDown(event)
368     self.port.MouseLeftUp(event)
369     eq(self.port.LabelShapeAt(x, y), False) # should have done nothing
370    
371    
372 bh 1608 class TestViewportWithPostGIS(unittest.TestCase):
373    
374     def setUp(self):
375     """Start the server and create a database.
376    
377     The database name will be stored in self.dbname, the server
378     object in self.server and the db object in self.db.
379     """
380     postgissupport.skip_if_no_postgis()
381     self.server = postgissupport.get_test_server()
382     self.dbref = self.server.get_default_static_data_db()
383     self.dbname = self.dbref.dbname
384     self.session = Session("PostGIS Session")
385     self.db = PostGISConnection(dbname = self.dbname,
386 bh 1634 **self.server.connection_params("user"))
387 bh 1608
388     proj = Projection(["proj=latlong",
389     "to_meter=.017453292519943",
390     "ellps=clrk66"])
391     self.map = Map("title", proj)
392    
393     self.port = ViewPort((1001, 1001))
394    
395     def tearDown(self):
396     self.session.Destroy()
397     self.map.Destroy()
398     self.port.Destroy()
399     self.map = self.port = None
400    
401     def test_find_shape_at_point(self):
402     """Test ViewPort.find_shape_at() with postgis point layer"""
403     layer = Layer("Point",
404     self.session.OpenDBShapeStore(self.db, "landmarks"))
405     prop = layer.GetClassification().GetDefaultGroup().GetProperties()
406     prop.SetFill(Color(0,0,0))
407     self.map.AddLayer(layer)
408    
409     self.port.SetMap(self.map)
410    
411     x, y = self.port.proj_to_win(-22.54335021, 66.30889129)
412 bh 1662 self.assertEquals(self.port.find_shape_at(x, y), (layer, 1001))
413 bh 1608
414     def test_find_shape_at_arc(self):
415     """Test ViewPort.find_shape_at() with postgis arc layer"""
416     layer = Layer("Arc", self.session.OpenDBShapeStore(self.db, "roads"))
417     self.map.AddLayer(layer)
418    
419     self.port.SetMap(self.map)
420    
421     x, y = self.port.proj_to_win(-18.18776318, 64.81418571)
422     self.assertEquals(self.port.find_shape_at(x, y), (layer, 610))
423    
424     def test_find_shape_at_polygon(self):
425     """Test ViewPort.find_shape_at() with postgis polygon layer"""
426     layer = Layer("Poly",
427     self.session.OpenDBShapeStore(self.db, "political"))
428     prop = layer.GetClassification().GetDefaultGroup().GetProperties()
429     prop.SetFill(Color(0,0,0))
430     self.map.AddLayer(layer)
431    
432     self.port.SetMap(self.map)
433    
434     x, y = self.port.proj_to_win(-19.78369, 65.1649143)
435     self.assertEquals(self.port.find_shape_at(x, y), (layer, 144))
436    
437    
438 jonathan 1440 if __name__ == "__main__":
439     unittest.main()
440    

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26