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