-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertexfindertool.py
173 lines (141 loc) · 6.07 KB
/
vertexfindertool.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
# -*- coding: latin1 -*-
#---------------------------------------------------------------------
#
# Vertex Finder - A QGIS tool to get a vertex and marking it
# by clicking in the map window
#
# Copyright (C) 2008 Cédric Möri
#
# EMAIL: cedric.moeri (at) bd.so.ch
# WEB : www.sogis.ch
#
#---------------------------------------------------------------------
#
# licensed under the terms of GNU GPL 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#---------------------------------------------------------------------
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
# Vertex Finder Tool class
class VertexFinderTool(QgsMapTool):
def __init__(self, canvas):
QgsMapTool.__init__(self,canvas)
self.canvas=canvas
#our own fancy cursor
self.cursor = QCursor(QPixmap(["16 16 3 1",
" c None",
". c #FF0000",
"+ c #FFFFFF",
" ",
" +.+ ",
" ++.++ ",
" +.....+ ",
" +. .+ ",
" +. . .+ ",
" +. . .+ ",
" ++. . .++",
" ... ...+... ...",
" ++. . .++",
" +. . .+ ",
" +. . .+ ",
" ++. .+ ",
" ++.....+ ",
" ++.++ ",
" +.+ "]))
def canvasPressEvent(self,event):
pass
def canvasMoveEvent(self,event):
pass
def canvasReleaseEvent(self,event):
#Get the click
x = event.pos().x()
y = event.pos().y()
layer = self.canvas.currentLayer()
if layer <> None:
#something to store the result
snapResult = None
#the clicked point is our starting point
startingPoint = QPoint(x,y)
#we need a snapper, so we use the MapCanvas snapper
snapper = QgsMapCanvasSnapper(self.canvas)
#we snap to the current layer (we don't have exclude points and use the tolerances from the qgis properties)
(retval,result) = snapper.snapToCurrentLayer (startingPoint,QgsSnapper.SnapToVertex)
#so if we have found a vertex
if result <> []:
snapResult = result[0]
else:
#fix/workaround for broken snapper (Bug in 2.8 see ticket #12631)
try:
utils = self.canvas.snappingUtils()
SegmentLocator = utils.snapToCurrentLayer(startingPoint, 1)
if SegmentLocator.isValid():
snapResult = QgsSnappingResult()
vertexCoords = snapResult.snappedVertex = SegmentLocator.point()#vertexCoord are given in crs of the project
vertexNr = snapResult.snappedVertexNr = SegmentLocator.vertexIndex()
geometry = snapResult.snappedAtGeometry = SegmentLocator.featureId()
layer = snapResult.layer = SegmentLocator.layer()
except AttributeError:
self.showSettingsWarning()
return
if snapResult <> None:
#we like to mark the vertex that is choosen, so we build a vertex marker...
self.marker = QgsVertexMarker(self.canvas)
#we have to know about the standard vertex marker, so we may use an other one
settings = QSettings()
settingsLabel = "/Qgis/digitizing/marker_style"
markerType = settings.value(settingsLabel)
if markerType == "Cross":
self.marker.setIconType(3)
self.marker.setColor(QColor(255,255,0))
else:
self.marker.setIconType(2)
self.marker.setColor(QColor(255,0,0))
self.marker.setIconSize(10)
self.marker.setPenWidth (2)
#mark the vertex
self.marker.setCenter(snapResult.snappedVertex)
#tell the world about the vertex and the marker
self.emit(SIGNAL("vertexFound(PyQt_PyObject)"), [snapResult,self.marker])
else:
#warn about missing snapping tolerance if appropriate
self.showSettingsWarning()
def showSettingsWarning(self):
#get the setting for displaySnapWarning
settings = QSettings()
settingsLabel = "/UI/displaySnapWarning"
displaySnapWarning = settings.value(settingsLabel)
#only show the warning if the setting is true
if displaySnapWarning == 'true' or displaySnapWarning is None:
m = QgsMessageViewer()
m.setWindowTitle("Snap tolerance")
m.setCheckBoxText("Don't show this message again")
m.setCheckBoxVisible(True)
m.setCheckBoxQSettingsLabel(settingsLabel)
m.setMessageAsHtml( "<p>Could not snap vertex.</p><p>Have you set the tolerance in Settings > Project Properties > General?</p>")
m.showMessage()
def activate(self):
self.canvas.setCursor(self.cursor)
def deactivate(self):
pass
def isZoomTool(self):
return False
def isTransient(self):
return False
def isEditTool(self):
return True