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