/[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 1772 - (hide annotations)
Mon Oct 6 11:28:48 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: 16668 byte(s)
(MockView): New class derived from
ViewPort with a mock implementation of GetTextExtent to be used in
the test cases
(ViewPortTest.setUp): Use MockView instead of ViewPort

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

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26