-
Notifications
You must be signed in to change notification settings - Fork 2
/
maptool.py
59 lines (47 loc) · 2.02 KB
/
maptool.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
from qgis.gui import QgsMapTool, QgsRubberBand
from qgis.core import QgsGeometry, QgsWkbTypes
from qgis.utils import iface
from qgis.PyQt.QtCore import Qt, QPoint, pyqtSignal
class PolygonTool(QgsMapTool):
sketchFinished = pyqtSignal(QgsGeometry)
def __init__(self):
self.rubberBand = None
self.canvas = iface.mapCanvas()
super().__init__(self.canvas)
def reset(self):
if self.rubberBand:
self.rubberBand.reset()
self.canvas.scene().removeItem(self.rubberBand)
self.rubberBand = None
def canvasPressEvent(self, event):
pass
def canvasReleaseEvent(self, event):
x = event.pos().x()
y = event.pos().y()
thisPoint = QPoint(x, y)
mapToPixel = self.canvas.getCoordinateTransform() # QgsMapToPixel instance
if event.button() == Qt.MouseButton.LeftButton:
if not self.rubberBand:
self.rubberBand = QgsRubberBand(self.canvas, geometryType=QgsWkbTypes.GeometryType.PolygonGeometry)
self.rubberBand.setLineStyle(Qt.PenStyle.DashLine)
self.rubberBand.setWidth(2)
self.rubberBand.addPoint(mapToPixel.toMapCoordinates(thisPoint))
elif event.button() == Qt.MouseButton.RightButton:
if self.rubberBand and self.rubberBand.numberOfVertices() > 3:
# Finish rubberband sketch
self.rubberBand.removeLastPoint()
geometry = self.rubberBand.asGeometry()
self.sketchFinished.emit(geometry)
self.reset()
self.canvas.refresh()
def canvasMoveEvent(self, event):
if not self.rubberBand:
return
x = event.pos().x()
y = event.pos().y()
thisPoint = QPoint(x, y)
mapToPixel = self.canvas.getCoordinateTransform()
self.rubberBand.movePoint(self.rubberBand.numberOfVertices() - 1, mapToPixel.toMapCoordinates(thisPoint))
def deactivate(self):
self.reset()
QgsMapTool.deactivate(self)