-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdrag_box_tool.py
98 lines (82 loc) · 3.6 KB
/
drag_box_tool.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
drag_box_tool
A class for dragging a box on the map
FSC QGIS plugin for biological recorders
-------------------
begin : 2014-02-17
copyright : (C) 2014 by Rich Burkmar, Field Studies Council
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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.core import *
from qgis.gui import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class RectangleMapTool(QgsMapToolEmitPoint):
boxDragged = pyqtSignal()
def __init__(self, canvas, iface):
self.canvas = canvas
self.iface = iface
QgsMapToolEmitPoint.__init__(self, self.canvas)
self.rubberBand = QgsRubberBand(self.canvas)
self.rubberBand.setColor(Qt.black)
self.rubberBand.setWidth(1)
self.reset()
def reset(self):
self.startPoint = self.endPoint = None
self.isEmittingPoint = False
self.rubberBand.reset()
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()
if r is not None:
#strMessage = str(r.xMinimum()) + ":" + str(r.yMinimum()) + ":" + str(r.xMaximum()) + ":" + str(r.yMaximum())
#self.iface.messageBar().pushMessage("Output", strMessage, level=Qgis.Info)
self.xMinimum = r.xMinimum()
self.xMaximum = r.xMaximum()
self.yMinimum = r.yMinimum()
self.yMaximum = r.yMaximum()
self.rubberBand.reset()
self.boxDragged.emit()
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()
if startPoint.x() == endPoint.x() or startPoint.y() == endPoint.y():
return
point1 = QgsPoint(startPoint.x(), startPoint.y())
point2 = QgsPoint(startPoint.x(), endPoint.y())
point3 = QgsPoint(endPoint.x(), endPoint.y())
point4 = QgsPoint(endPoint.x(), startPoint.y())
points = [point1, point2, point3, point4, point1]
self.rubberBand.setToGeometry(QgsGeometry.fromPolyline(points), None)
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):
#AttributeError: 'NoneType' object has no attribute 'deactivate'
#QgsMapTool.deactivate(self)
#self.emit(SIGNAL("deactivated()"))
pass