-
Notifications
You must be signed in to change notification settings - Fork 6
/
MapTools.py
262 lines (198 loc) · 7.07 KB
/
MapTools.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name : Omero RT
Description : Omero plugin map tools
Date : August 15, 2010
copyright : (C) 2010 by Giuseppe Sucameli (Faunalia)
email : [email protected]
***************************************************************************/
Omero plugin
Works done from Faunalia (http://www.faunalia.it) with funding from Regione
Toscana - S.I.T.A. (http://www.regione.toscana.it/territorio/cartografia/index.html)
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
from qgis.PyQt.QtCore import Qt, pyqtSignal
from qgis.PyQt.QtGui import QCursor
from qgis.PyQt.QtWidgets import QApplication
from qgis.core import QgsWkbTypes, QgsFeatureRequest, QgsRectangle, QgsGeometry, QgsFeature, QgsSettings, Qgis
from qgis.gui import QgsMapToolEmitPoint, QgsMapTool, QgsRubberBand
class MapToolEmitPoint(QgsMapToolEmitPoint):
geometryEmitted = pyqtSignal(object)
def __init__(self, canvas):
QgsMapToolEmitPoint.__init__(self, canvas)
self.canvas = canvas
self.action = None
self.canvas.mapToolSet.connect(self._toolChanged)
def deleteLater(self, *args):
self.canvas.mapToolSet.disconnect(self._toolChanged)
return QgsMapToolEmitPoint.deleteLater(self, *args)
def setAction(self, action):
self.action = action
def action(self):
return self.action
def _toolChanged(self, tool):
if self.action:
self.action.setChecked( tool == self )
def startCapture(self):
self.canvas.setMapTool( self )
def stopCapture(self):
self._toolChanged( None )
self.canvas.unsetMapTool( self )
def deactivate(self):
QgsMapTool.deactivate(self)
self.deactivated.emit()
class Drawer(MapToolEmitPoint):
def __init__(self, canvas, isPolygon=False, props=None):
MapToolEmitPoint.__init__(self, canvas)
self.isPolygon = isPolygon
self.props = props if props is not None else {}
self.action = None
self.isEmittingPoints = False
self.rubberBand = qgis.gui.QgsRubberBand( self.canvas, QgsWkbTypes.PolygonGeometry if self.isPolygon else QgsWkbTypes.LineGeometry )
self.rubberBand.setColor( self.props.get('color', Qt.red) )
self.rubberBand.setWidth( self.props.get('border', 1) )
self.snapper = self.canvas.snappingUtils()
def deleteLater(self, *args):
self.reset()
del self.rubberBand
del self.snapper
return MapToolEmitPoint.deleteLater(self, *args)
def setColor(self, color):
self.rubberBand.setColor( color )
def reset(self):
self.isEmittingPoints = False
self.rubberBand.reset( QgsWkbTypes.PolygonGeometry if self.isPolygon else QgsWkbTypes.LineGeometry )
def canvasPressEvent(self, e):
if e.button() == Qt.RightButton:
prevIsEmittingPoints = self.isEmittingPoints
self.isEmittingPoints = False
if not self.isEmittingPoints:
self.onEnd( self.geometry() )
else:
self.onEnd( None )
return
if e.button() != Qt.LeftButton:
return
if not self.isEmittingPoints: # first click
self.reset()
self.isEmittingPoints = True
point = self.toMapCoordinates( e.pos() )
self.rubberBand.addPoint( point, True ) # true to update canvas
self.rubberBand.show()
def canvasMoveEvent(self, e):
if not self.isEmittingPoints:
return
if not self.props.get('enableSnap', True):
point = self.toMapCoordinates( e.pos() )
else:
snapResults = self.snapper.snapToMap( e.pos() )
if snapResults.isValid():
point = snapResults.point()
else:
point = self.toMapCoordinates( e.pos() )
self.rubberBand.movePoint( point )
def canvasReleaseEvent(self, e):
if not self.isEmittingPoints:
return
if self.isPolygon:
return
if self.props.get('mode', None) != 'segment':
return
self.isEmittingPoints = False
self.onEnd( self.geometry() )
def isValid(self):
return self.rubberBand.numberOfVertices() > 0
def geometry(self):
if not self.isValid():
return None
geom = self.rubberBand.asGeometry()
if geom == None:
return
return geom
def onEnd(self, geometry):
#self.stopCapture()
self.geometryEmitted.emit( geometry )
def deactivate(self):
if not self.props.get('keepAfterEnd', False):
self.reset()
return MapToolEmitPoint.deactivate(self)
class PolygonDrawer(Drawer):
def __init__(self, canvas, props=None):
Drawer.__init__(self, canvas, True, props)
class LineDrawer(Drawer):
def __init__(self, canvas, props=None):
Drawer.__init__(self, canvas, False, props)
class SegmentDrawer(Drawer):
def __init__(self, canvas, props=None):
props = props if isinstance(props, dict) else {}
props['mode'] = 'segment'
Drawer.__init__(self, canvas, False, props)
class FeatureFinder(MapToolEmitPoint):
pointEmitted = pyqtSignal(object, object)
def __init__(self, canvas):
MapToolEmitPoint.__init__(self, canvas)
self.canvasClicked.connect(self.onEnd)
def onEnd(self, point, button):
self.stopCapture()
self.pointEmitted.emit(point, button)
@classmethod
def findAtPoint(self, layer, point, canvas, onlyTheClosestOne=True, onlyIds=False):
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
# recupera il valore del raggio di ricerca
settings = QgsSettings()
radius = settings.value( "/Map/searchRadiusMM", Qgis.DEFAULT_SEARCH_RADIUS_MM, type=float)
if radius <= 0:
radius = Qgis.DEFAULT_SEARCH_RADIUS_MM
radius = canvas.extent().width() * radius/100
# crea il rettangolo da usare per la ricerca
rect = QgsRectangle()
rect.setXMinimum(point.x() - radius)
rect.setXMaximum(point.x() + radius)
rect.setYMinimum(point.y() - radius)
rect.setYMaximum(point.y() + radius)
rect = canvas.mapSettings().mapToLayerCoordinates(layer, rect)
# recupera le feature che intersecano il rettangolo
ret = None
if onlyTheClosestOne:
request=QgsFeatureRequest()
request.setFilterRect(rect)
minDist = -1
featureId = None
rect = QgsGeometry.fromRect(rect)
count = 0
for f in layer.getFeatures(request):
if onlyTheClosestOne:
geom = f.geometry()
distance = geom.distance(rect)
if minDist < 0 or distance < minDist:
minDist = distance
featureId = f.id()
if onlyIds:
ret = featureId
elif featureId != None:
f = QgsFeature()
feats = layer.getFeature( QgsFeatureRequest(featureId) )
feats.nextFeature(f)
ret = f
else:
IDs = []
for f in layer.getFeatures():
IDs.append( f.id() )
if onlyIds:
ret = IDs
else:
ret = []
request = QgsFeatureRequest()
QgsFeatureRequest.setFilterFids(IDs)
for f in layer.getFeatures( request ):
ret.append( f )
QApplication.restoreOverrideCursor()
return ret