forked from pw0300/Qgis_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectfindertool.py
More file actions
161 lines (124 loc) · 5.3 KB
/
objectfindertool.py
File metadata and controls
161 lines (124 loc) · 5.3 KB
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
# -*- coding: latin1 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
class VertexAndObjectFinderTool(QgsMapTool):
vertexAndObjectFound = pyqtSignal(object)
def __init__(self, canvas):
QgsMapTool.__init__(self, canvas)
self.canvas = canvas
self.count = 0
self.rb = None
self.m1 = None
# self.p1 = QgsPoint()
self.cursor = QCursor(QPixmap(["16 16 3 1",
" c None",
". c #FF0000",
"+ c #FFFFFF",
" ",
" +.+ ",
" ++.++ ",
" +.....+ ",
" +. .+ ",
" +. . .+ ",
" +. . .+ ",
" ++. . .++",
" ... ...+... ...",
" ++. . .++",
" +. . .+ ",
" +. . .+ ",
" ++. .+ ",
" ++.....+ ",
" ++.++ ",
" +.+ "]))
def canvasPressEvent(self, event):
## First we need to select the object.
if self.count % 1 == 0:
vlayer = self.canvas.currentLayer()
if vlayer is None:
return
self.type = vlayer.geometryType()
self.isPolygon = True
if self.type == 1:
self.isPolygon = False
else:
self.isPolygon = True
if self.rb is not None:
if self.isPolygon:
self.rb.reset(QGis.Polygon)
else:
self.rb.reset()
layerCoords = self.toLayerCoordinates( vlayer, event.pos() )
searchRadius = QgsTolerance.vertexSearchRadius( vlayer, self.canvas.mapRenderer() )
pointGeometry = QgsGeometry.fromPoint( layerCoords )
if pointGeometry is None:
return
try:
minDistance = float('inf')
except ValueError:
minDistance = 1e100000
# # minDistance = 1e100000
self.cf = QgsFeature()
for f in vlayer.getFeatures():
if f.geometry() is not None:
currentDistance = pointGeometry.distance( f.geometry() )
if currentDistance < minDistance:
minDistance = currentDistance
self.cf.setGeometry(f.geometry())
try:
if minDistance == float('inf'):
return
except ValueError:
if minDistance == 1e100000:
return
# if minDistance == 1e100000:
# return
self.rb = self.createRubberBand(self.isPolygon)
self.rb.setToGeometry( self.cf.geometry(), vlayer )
self.count += 1
if self.count > 1:
self.vertexAndObjectFound.emit([self.cf, self.m1, self.rb])
def canvasMoveEvent(self,event):
pass
def canvasReleaseEvent(self,event):
pass
def createRubberBand( self, isPolygon ):
if isPolygon:
rb = QgsRubberBand( self.canvas, QGis.Polygon )
else:
rb = QgsRubberBand( self.canvas )
color = QColor(255,0,0,100)
rb.setColor(color)
rb.setWidth(2)
rb.show()
return rb
def showSettingsWarning(self):
#get the setting for displaySnapWarning
settings = QSettings()
settingsLabel = "/UI/displaySnapWarning"
displaySnapWarning = bool(settings.value(settingsLabel))
#only show the warning if the setting is true
if displaySnapWarning:
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):
self.canvas.scene().removeItem(self.m1)
self.m1 = None
if self.rb is not None:
self.rb.reset()
self.count = 0
pass
def isZoomTool(self):
return False
def isTransient(self):
return False
def isEditTool(self):
return True