-
Notifications
You must be signed in to change notification settings - Fork 9
/
pointtool_states.py
226 lines (165 loc) · 6.58 KB
/
pointtool_states.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'''
Module contains States for pointtool.
'''
from math import atan2, cos, sin, radians
from qgis.core import QgsApplication
from .autotrace import AutotraceSubTask
class State:
'''
Abstract class for the state
'''
def __init__(self, pointtool):
self.pointtool = pointtool
def click_rmb(self, mouseEvent, vlayer):
'''
Event when the user clicks on the map with the right button
'''
# finish point path if it was last point
self.pointtool.anchors = []
# hide all markers
while self.pointtool.markers:
marker = self.pointtool.markers.pop()
self.pointtool.canvas().scene().removeItem(marker)
# hide rubber_band
self.pointtool.rubber_band.hide()
# change state
self.pointtool.change_state(WaitingFirstPointState)
def click_lmb(self, mouseEvent, vlayer):
'''
Event when the user clicks on the map with the left button
'''
# self.pointtool.last_mouse_event_pos = mouseEvent.pos()
# hide rubber_band
self.pointtool.rubber_band.hide()
# check if he haven't any new tasks yet
if self.pointtool.tracking_is_active:
self.pointtool.display_message(
" ",
"Please wait till the last segment is finished" +
" or terminate tracing by hitting Esc",
level='Critical',
duration=1,
)
return False
# acquire point coordinates from mouseEvent
qgsPoint = self.pointtool.toMapCoordinates(mouseEvent.pos())
x1, y1 = qgsPoint.x(), qgsPoint.y()
if self.pointtool.to_indexes is None:
self.pointtool.display_message(
"Missing Layer",
"Please select correct raster layer",
level='Critical',
duration=2,
)
return False
if self.pointtool.snap2_tolerance:
x1, y1 = self.pointtool.snap_to_itself(x1, y1, self.pointtool.snap2_tolerance)
i1, j1 = self.pointtool.to_indexes(x1, y1)
self.pointtool.add_anchor_points(x1, y1, i1, j1)
return True
class WaitingFirstPointState(State):
'''
State of waiting the user to click on the first point in the line.
Is active when the user is about to begin tracing new line.
After the user clicks on the left mouse button
it changes the state to WaitingMiddlePointState.
'''
def click_lmb(self, mouseEvent, vlayer):
if super().click_lmb(mouseEvent, vlayer) is False:
return
# change state
self.pointtool.change_state(WaitingMiddlePointState)
# self.pointtool.change_state(AutoFollowingLineState)
def click_rmb(self, mouseEvent, vlayer):
pass
class WaitingMiddlePointState(State):
'''
State of waiting the user to click on the next point in the line.
Is active when the user is already clicked on at least one point.
After the user clicks on the left mouse button it keeps the state.
After the user clicks on the right mouse button it finishes the line and
switches the state to WaitingFirstPointState.
'''
def click_lmb(self, mouseEvent, vlayer):
if super().click_lmb(mouseEvent, vlayer) is False:
return
x1, y1, i1, j1 = self.pointtool.anchors[-1]
if self.pointtool.tracing_mode.is_auto():
# perform autotrace
self.autotrace_task = AutotraceSubTask(
self.pointtool,
vlayer,
clicked_point=self.pointtool.anchors[-1],
)
# self.pointtool.remove_last_anchor_point(undo_edit=False, redraw=False)
QgsApplication.taskManager().addTask(
self.autotrace_task,
)
else:
self.pointtool.trace(x1, y1, i1, j1, vlayer)
def click_rmb(self, mouseEvent, vlayer):
super().click_rmb(mouseEvent, vlayer)
# # add last feature to spatial index to perform fast search of closet points
# self.pointtool.add_last_feature_to_spindex(vlayer)
class AutoFollowingLineState(State):
'''
This state is active when raster_tracer is trying to
perform auto-following of the line.
'''
def click_lmb(self, mouseEvent, vlayer):
if super().click_lmb(mouseEvent, vlayer) is False:
return
self.follow_next_segment(vlayer, initial=True)
for _ in range(25):
self.follow_next_segment(vlayer)
# while True:
# if self.pointtool.ready is True:
# break
self.pointtool.redraw()
self.pointtool.update_rubber_band()
# print('a')
def click_rmb(self, mouseEvent, vlayer):
super().click_rmb(mouseEvent, vlayer)
def follow_next_segment(self, vlayer, initial=False):
_, _, i0, j0 = self.pointtool.anchors[-2]
_, _, i1, j1 = self.pointtool.anchors[-1]
direction = atan2(j1 - j0, i1 - i0)
distance = 5
if initial:
self.pointtool.remove_last_anchor_point(undo_edit=False)
i1, j1 = i0, j0
points = self.search_near_points((i1, j1), direction, distance)
costs = []
paths = []
for point in points:
i2, j2 = point
x2, y2 = self.pointtool.to_coords(i2, j2)
path, cost = self.pointtool.trace_over_image((i1, j1), (i2, j2))
costs.append(cost)
paths.append(path)
min_cost = min(costs)
min_cost_index = costs.index(min_cost)
best_point = points[min_cost_index]
best_path = paths[min_cost_index]
i, j = best_point
x, y = self.pointtool.to_coords(i, j)
if len(self.pointtool.anchors)>1:
self.pointtool.draw_path(best_path, vlayer, was_tracing=True)
self.pointtool.add_anchor_points(x, y, i, j)
else:
self.pointtool.add_anchor_points(x, y, i, j)
self.pointtool.draw_path(best_path, vlayer, was_tracing=True)
self.pointtool.pan(x, y)
def search_near_points(self, point, direction, distance):
'''
Returns list of points near last point in the given direction,
at a given distance with given space between points.
'''
points = []
i1, j1 = point
angles = [direction + radians(i) for i in range(-60, 60, 10)]
for angle in angles:
i2 = i1 + distance * cos(angle)
j2 = j1 + distance * sin(angle)
points.append((int(i2), int(j2)))
return points