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