-
Notifications
You must be signed in to change notification settings - Fork 3
/
MultiLayerRectangleSelection.py
executable file
·89 lines (73 loc) · 2.98 KB
/
MultiLayerRectangleSelection.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
'''
Created on 12/10/2014
@author: ferrari
'''
from qgis.core import QgsMapLayer, QgsPointXY, QgsRectangle, QgsWkbTypes
from qgis.gui import QgsMapTool, QgsRubberBand
from qgis.PyQt.QtGui import QColor
class MultiLayerRectangleSelection(QgsMapTool):
def __init__(self, canvas, action):
self.canvas = canvas
self.active = False
QgsMapTool.__init__(self, self.canvas)
self.setAction(action)
self.rubberBand = QgsRubberBand(self.canvas, QgsWkbTypes.PolygonGeometry)
mFillColor = QColor( 254, 178, 76, 63 );
self.rubberBand.setColor(mFillColor)
self.rubberBand.setWidth(1)
self.reset()
def reset(self):
self.startPoint = self.endPoint = None
self.isEmittingPoint = False
self.rubberBand.reset(QgsWkbTypes.PolygonGeometry)
def canvasPressEvent(self, e):
self.startPoint = self.toMapCoordinates(e.pos())
self.endPoint = self.startPoint
self.isEmittingPoint = True
self.showRect(self.startPoint, self.endPoint)
def canvasReleaseEvent(self, e):
self.isEmittingPoint = False
r = self.rectangle()
layers = self.canvas.layers()
for layer in layers:
if layer.type() == QgsMapLayer.RasterLayer:
continue
if r is not None:
lRect = self.canvas.mapSettings().mapToLayerCoordinates(layer, r)
layer.selectByRect(lRect, layer.SelectBehavior.SetSelection)
self.rubberBand.hide()
def canvasMoveEvent(self, e):
if not self.isEmittingPoint:
return
self.endPoint = self.toMapCoordinates( e.pos() )
self.showRect(self.startPoint, self.endPoint)
def showRect(self, startPoint, endPoint):
self.rubberBand.reset(QgsWkbTypes.PolygonGeometry)
if startPoint.x() == endPoint.x() or startPoint.y() == endPoint.y():
return
point1 = QgsPointXY(startPoint.x(), startPoint.y())
point2 = QgsPointXY(startPoint.x(), endPoint.y())
point3 = QgsPointXY(endPoint.x(), endPoint.y())
point4 = QgsPointXY(endPoint.x(), startPoint.y())
self.rubberBand.addPoint(point1, False)
self.rubberBand.addPoint(point2, False)
self.rubberBand.addPoint(point3, False)
self.rubberBand.addPoint(point4, True) # true to update canvas
self.rubberBand.show()
def rectangle(self):
if self.startPoint is None or self.endPoint is None:
return None
elif self.startPoint.x() == self.endPoint.x() or self.startPoint.y() == self.endPoint.y():
return None
return QgsRectangle(self.startPoint, self.endPoint)
def deactivate(self):
self.rubberBand.reset()
try:
if self is not None:
QgsMapTool.deactivate(self)
except:
pass
def activate(self):
QgsMapTool.activate(self)
def unload(self):
self.deactivate()