-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMapSelectionTool.py
100 lines (77 loc) · 3.91 KB
/
MapSelectionTool.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
"""MapSelectionTool to draw a polygon and get the coordinates of it.
This script was based on https://github.com/lcoandrade/OSMDownloader/blob/master/rectangleAreaTool.py
Notes:
begin : 2019-02-09
git sha : $Format:%H$
development : 2019, Ivan Ivanov @ ITC, University of Twente
email : ivan.ivanov@suricactus.com
copyright : (C) 2019 by Ivan Ivanov
License:
MIT License
Copyright (c) 2020 "its4land project", "ITC, University of Twente"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from qgis.gui import QgsMapTool, QgsRubberBand, QgsMapMouseEvent, QgsMapCanvas
from qgis.core import QgsWkbTypes, QgsPointXY
from qgis.PyQt.QtCore import pyqtSignal, Qt
from qgis.PyQt.QtGui import QColor
from qgis.PyQt.QtWidgets import QApplication
class MapSelectionTool(QgsMapTool):
polygonCreated = pyqtSignal(QgsPointXY, QgsPointXY, Qt.KeyboardModifiers)
def __init__(self, canvas: QgsMapCanvas) -> None:
QgsMapTool.__init__(self, canvas)
mFillColor = QColor(254, 178, 76, 63)
self.canvas = canvas
self.active = True
self.rubberBand = QgsRubberBand(self.canvas, QgsWkbTypes.PolygonGeometry)
self.rubberBand.setColor(mFillColor)
self.rubberBand.setWidth(1)
self.reset()
def reset(self) -> None:
self.startPoint = self.endPoint = None
self.isEmittingPoint = False
self.rubberBand.reset(QgsWkbTypes.PolygonGeometry)
def canvasPressEvent(self, e: QgsMapMouseEvent) -> None:
self.startPoint = self.toMapCoordinates(e.pos())
self.endPoint = self.startPoint
self.isEmittingPoint = True
self.showRect(self.startPoint, self.endPoint)
def canvasReleaseEvent(self, e: QgsMapMouseEvent) -> None:
self.isEmittingPoint = False
self.rubberBand.hide()
self.polygonCreated.emit(self.startPoint, self.endPoint, QApplication.keyboardModifiers())
def canvasMoveEvent(self, e: QgsMapMouseEvent) -> None:
if not self.isEmittingPoint:
return
self.endPoint = self.toMapCoordinates(e.pos())
self.showRect(self.startPoint, self.endPoint)
def showRect(self, startPoint: QgsPointXY, endPoint: QgsPointXY) -> None:
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 deactivate(self) -> None:
self.rubberBand.hide()
QgsMapTool.deactivate(self)