/[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 1634 - (hide annotations)
Fri Aug 22 16:55:19 2003 UTC (21 years, 6 months ago) by bh
Original Path: trunk/thuban/test/test_viewport.py
File MIME type: text/x-python
File size: 16457 byte(s)
Prepare the test suite for tests with required authentication

* test/postgissupport.py (PostgreSQLServer.__init__): Add instance
variables with two predefined users/passwords, one for the admin
and one for a non-privileged user.
(PostgreSQLServer.createdb): Pass the admin name to initdb and add
the non-privileged user to the database and set the admin password
(PostgreSQLServer.wait_for_postmaster): Use the admin user name.
Better error reporting
(PostgreSQLServer.connection_params)
(PostgreSQLServer.connection_string): New methods to return
information about how to connect to the server
(PostgreSQLServer.execute_sql): New. Convenience method to execute
SQL statements
(PostgreSQLServer.require_authentication): Toggle whether the
server requires authentication
(PostgreSQLServer.create_user, PostgreSQLServer.alter_user): New.
Add or alter users
(PostGISDatabase.initdb): Pass the admin name one the
subprocesses' command lines. Grant select rights on
geometry_columns to everybody.
(upload_shapefile): Use the admin name and password when
connecting. Grant select rights on the new table to everybody.

* test/test_viewport.py (TestViewportWithPostGIS.setUp): Use the
server's new methods to get the connection parameters.

* test/test_postgis_session.py (TestSessionWithPostGIS.setUp)
(TestSessionWithPostGIS.test_remove_dbconn_exception): Use the
server's new methods to get the connection parameters.

* test/test_postgis_db.py
(TestPostGISConnection.test_gis_tables_empty)
(TestPostGISConnection.test_gis_tables_non_empty)
(PostGISStaticTests.setUp): Use the server's new methods to get
the connection parameters.

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

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26