forked from mkondratyev85/raster_tracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointtool.py
342 lines (278 loc) · 11.8 KB
/
pointtool.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
from qgis.core import QgsPointXY, QgsPoint, QgsGeometry, QgsFeature, \
QgsVectorLayer, QgsProject
from qgis.gui import QgsMapToolEmitPoint, QgsMapToolEdit, \
QgsRubberBand, QgsVertexMarker, QgsMapTool
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QColor
from qgis.core import Qgis
import numpy as np
#import matplotlib.pyplot as plt
from .astar import find_path
from .line_simplification import smooth, simplify
from .utils import get_whole_raster, PossiblyIndexedImageError
class OutsideMapError(Exception):
pass
class PointTool(QgsMapToolEdit):
def deactivate(self):
QgsMapTool.deactivate(self)
self.deactivated.emit()
def __init__(self, canvas, iface, turn_off_snap):
self.last_mouse_event_pos = None
self.iface = iface
self.anchor_points = []
self.anchor_points_ij = []
self.is_tracing = True
self.turn_off_snap = turn_off_snap
# possible variants: gray_diff, as_is, color_diff (using v from hsv)
self.grid_conversion = "gray_diff"
#QApplication.restoreOverrideCursor()
#QApplication.setOverrideCursor(Qt.CrossCursor)
QgsMapToolEmitPoint.__init__(self, canvas)
self.rlayer = None
self.grid_changed = None
self.snap_tolerance = None
self.vlayer = None
self.rubber_band = QgsRubberBand(self.canvas(), False) # False = not a polygon
self.markers = []
self.marker_snap = QgsVertexMarker(self.canvas())
self.marker_snap.setColor(QColor(255, 0, 255))
def snap_tolerance_changed(self, snap_tolerance):
self.snap_tolerance = snap_tolerance
if snap_tolerance is None:
self.marker_snap.hide()
else:
self.marker_snap.show()
def trace_color_changed(self, color):
r,g,b = self.sample
if color is False:
self.grid_changed = None
else:
r0,g0,b0,t = color.getRgb()
self.grid_changed = np.abs( (r0-r)**2 + (g0-g)**2 + (b0-b)**2 )
def get_current_vector_layer(self):
try:
vlayer = self.iface.layerTreeView().selectedLayers()[0]
if isinstance(vlayer, QgsVectorLayer):
return vlayer
else:
self.iface.messageBar().pushMessage("Missing Layer",
"Please select vector layer to draw",
level=Qgis.Warning, duration=2)
return None
except IndexError:
self.iface.messageBar().pushMessage("Missing Layer",
"Please select vector layer to draw",
level=Qgis.Warning, duration=2)
return None
def raster_layer_has_changed(self, raster_layer):
self.rlayer = raster_layer
if self.rlayer is None:
self.iface.messageBar().pushMessage("Missing Layer",
"Please select raster layer to trace",
level=Qgis.Warning, duration=2)
return
try:
sample, to_indexes, to_coords, to_coords_provider, \
to_coords_provider2 = \
get_whole_raster(self.rlayer,
QgsProject.instance())
except PossiblyIndexedImageError:
self.iface.messageBar().pushMessage("Missing Layer",
"Can't trace indexed or gray image",
level=Qgis.Critical, duration=2)
return
r = sample[0].astype(float)
g = sample[1].astype(float)
b = sample[2].astype(float)
where_are_NaNs = np.isnan(r)
r[where_are_NaNs] = 0
where_are_NaNs = np.isnan(g)
g[where_are_NaNs] = 0
where_are_NaNs = np.isnan(b)
b[where_are_NaNs] = 0
self.sample = (r,g,b)
self.grid = r+g+b
self.to_indexes = to_indexes
self.to_coords = to_coords
self.to_coords_provider = to_coords_provider
self.to_coords_provider2 = to_coords_provider2
def keyPressEvent(self, e):
# delete last segment if backspace is pressed
if e.key() == Qt.Key_Backspace or e.key() == Qt.Key_B:
# check if we have at least one feature to delete
vlayer = self.get_current_vector_layer()
if vlayer is None: return
if vlayer.featureCount() < 1: return
#it's a very ugly way of triggering single undo event
self.iface.editMenu().actions()[0].trigger()
# remove last marker
if len(self.markers)>0:
last_marker = self.markers.pop()
self.canvas().scene().removeItem(last_marker)
# remove last anchor
if len(self.anchor_points)>0:
self.anchor_points.pop()
self.anchor_points_ij.pop()
self.update_rubber_band()
self.redraw()
elif e.key() == Qt.Key_A:
self.is_tracing = not self.is_tracing
self.update_rubber_band()
elif e.key() == Qt.Key_S:
self.turn_off_snap()
def snap(self, i, j):
if self.snap_tolerance is None: return i,j
if not self.is_tracing: return i,j
if self.grid_changed is None: return i,j
size_i, size_j = self.grid.shape
size = self.snap_tolerance
if i<size or j<size or i+size>size_i or j+size>size_j:
raise OutsideMapError
grid_small = self.grid_changed
grid_small = grid_small[ i-size : i+size, j-size : j+size ]
smallest_cells = np.where(grid_small == np.amin(grid_small))
coordinates = list(zip(smallest_cells[0], smallest_cells[1]))
if len(coordinates) == 1:
delta_i, delta_j = coordinates[0]
delta_i -= size
delta_j -= size
else:
# find the closest to the center
deltas = [(i-size, j-size) for i,j in coordinates]
lengths = [(i**2 + j**2) for i,j in deltas]
i = lengths.index(min(lengths))
delta_i, delta_j = deltas[i]
return i+delta_i, j+delta_j
def canvasReleaseEvent(self, mouseEvent):
vlayer = self.get_current_vector_layer()
if vlayer is None: return
if not vlayer.isEditable():
self.iface.messageBar().pushMessage("Edit mode",
"Please begin editing vector layer to trace",
level=Qgis.Warning, duration=2)
return
if self.rlayer is None:
self.iface.messageBar().pushMessage("Missing Layer",
"Please select raster layer to trace",
level=Qgis.Warning, duration=2)
self.last_mouse_event_pos = mouseEvent.pos()
# hide rubber_band
self.rubber_band.hide()
if (mouseEvent.button()==Qt.RightButton):
# finish point path if it was last point
self.anchor_points = []
# hide all markers
while len(self.markers)>0:
marker = self.markers.pop()
self.canvas().scene().removeItem(marker)
return
qgsPoint = self.toMapCoordinates(mouseEvent.pos())
x1, y1 = qgsPoint.x(), qgsPoint.y()
i1, j1 = self.to_indexes(x1, y1)
if self.snap_tolerance is not None:
try:
i1, j1 = self.snap(i1, j1)
except OutsideMapError:
return
x1, y1 = self.to_coords(i1, j1)
self.anchor_points.append((x1,y1))
self.anchor_points_ij.append((i1,j1))
marker = QgsVertexMarker(self.canvas())
marker.setCenter(QgsPointXY(x1,y1))
self.markers.append(marker)
# we need at least two points to draw
if len(self.anchor_points)<2:
return
if self.is_tracing:
if self.grid_changed is None:
r, g, b, = self.sample
r0 = r[i1,j1]
g0 = g[i1,j1]
b0 = b[i1,j1]
grid = np.abs( (r0-r)**2 + (g0-g)**2 + (b0-b)**2 )
else:
grid = self.grid_changed
i0, j0 = self.anchor_points_ij[-2]
start_point = i0, j0
end_point = i1, j1
path = find_path(grid.astype(np.dtype('l')), start_point, end_point)
path = smooth(path, size=5)
path = simplify(path)
current_last_point = self.to_coords(*path[-1])
path_ref = [self.to_coords_provider(i,j) for i,j in path]
else:
x0, y0 = self.anchor_points[-2]
path_ref = [self.to_coords_provider2(x0,y0),
self.to_coords_provider2(x1,y1)]
current_last_point = (x1,y1)
if len(self.anchor_points) == 2:
vlayer.beginEditCommand("Adding new line")
add_feature_to_vlayer(vlayer, path_ref)
vlayer.endEditCommand()
else:
last_point = self.to_coords_provider2(*self.anchor_points[-2])
path_ref = [last_point] + path_ref[1:]
vlayer.beginEditCommand("Adding new segment to the line")
add_to_last_feature(vlayer, path_ref)
vlayer.endEditCommand()
self.anchor_points[-1] = current_last_point
self.redraw()
def update_rubber_band(self):
# this is very ugly but I can't make another way
if self.last_mouse_event_pos is None: return
if len(self.anchor_points)<1: return
x0, y0 = self.anchor_points[-1]
qgsPoint = self.toMapCoordinates(self.last_mouse_event_pos)
x1, y1 = qgsPoint.x(), qgsPoint.y()
points = [QgsPoint(x0, y0), QgsPoint(x1,y1)]
self.rubber_band.setColor(QColor(255, 0, 0))
self.rubber_band.setWidth(3)
if self.is_tracing:
self.rubber_band.setLineStyle(Qt.DotLine)
else:
self.rubber_band.setLineStyle(Qt.SolidLine)
vlayer = self.get_current_vector_layer()
if vlayer is None: return
self.rubber_band.setToGeometry(QgsGeometry.fromPolyline(points), self.vlayer)
def canvasMoveEvent(self, mouseEvent):
self.last_mouse_event_pos = mouseEvent.pos()
if self.snap_tolerance is not None and self.is_tracing:
qgsPoint = self.toMapCoordinates(mouseEvent.pos())
x1, y1 = qgsPoint.x(), qgsPoint.y()
#i, j = get_indxs_from_raster_coords(self.geo_ref, x1, y1)
i, j = self.to_indexes(x1, y1)
try:
i1, j1 = self.snap(i, j)
except OutsideMapError:
return
#x1, y1 = get_coords_from_raster_indxs(self.geo_ref, i1, j1)
x1, y1 = self.to_coords(i1, j1)
self.marker_snap.setCenter(QgsPointXY(x1,y1))
# we need at least one point to draw
if len(self.anchor_points) < 1:
self.redraw()
return
self.update_rubber_band()
self.redraw()
def redraw(self):
# If caching is enabled, a simple canvas refresh might not be sufficient
# to trigger a redraw and you must clear the cached image for the layer
if self.iface.mapCanvas().isCachingEnabled():
vlayer = self.get_current_vector_layer()
if vlayer is None: return
vlayer.triggerRepaint()
else:
self.iface.mapCanvas().refresh()
def add_to_last_feature(vlayer, points):
features = [f for f in vlayer.getFeatures()]
last_feature = features[-1]
fid = last_feature.id()
geom = last_feature.geometry()
points = [QgsPointXY(x,y) for x,y in points]
geom.addPointsXY(points)
vlayer.changeGeometry(fid, geom)
def add_feature_to_vlayer(vlayer, points):
feat = QgsFeature(vlayer.fields())
polyline = [QgsPoint(x,y) for x,y in points]
feat.setGeometry(QgsGeometry.fromPolyline(polyline))
vlayer.addFeature(feat)