forked from desertwolf7/NumericalDigitize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
featureFinderTool.py
126 lines (104 loc) · 5.29 KB
/
featureFinderTool.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Numerical digitize - sets up a Qgis actions for append and edit features
by inserting or changing numerical values of vertex's coordinates
A QGIS plugin
-------------------
begin : 2019 year
git sha : $Format:%H$
copyright (C) 2019 Igor Chumichev
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.PyQt.QtCore import pyqtSignal, Qt
from qgis.PyQt.QtGui import QCursor, QPixmap
from qgis.core import QgsWkbTypes, QgsGeometry, QgsProject, QgsCoordinateTransform, QgsPointXY, QgsRectangle
from qgis.gui import QgsMapToolEmitPoint, QgsRubberBand
# Feature Finder Tool class
class FeatureFinderTool(QgsMapToolEmitPoint):
Clicked = pyqtSignal("QgsGeometry")
def __init__(self, canvas):
self.canvas = canvas
self.feature_id = None
self.startPoint = self.endPoint = None
self.isEmittingPoint = False
QgsMapToolEmitPoint.__init__(self, canvas)
# our own fancy cursor
self.cursor = QCursor(QPixmap(["16 16 3 1",
" c None",
". c #FF0000",
"+ c #FFFFFF",
" ",
" +.+ ",
" ++.++ ",
" +.....+ ",
" +. .+ ",
" +. . .+ ",
" +. . .+ ",
" ++. . .++",
" ... ...+... ...",
" ++. . .++",
" +. . .+ ",
" +. . .+ ",
" ++. .+ ",
" ++.....+ ",
" ++.++ ",
" +.+ "]))
self.rubberBand = QgsRubberBand(self.canvas, QgsWkbTypes.PolygonGeometry)
self.rubberBand.setColor(Qt.red)
self.rubberBand.setFillColor(Qt.transparent)
self.rubberBand.setWidth(1)
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, event):
self.isEmittingPoint = False
rect = self.rectangle()
if rect is not None:
rect_geom = QgsGeometry.fromRect(rect)
crs_canvas = self.canvas.mapSettings().destinationCrs()
layer_crs = self.canvas.currentLayer().crs()
xformer = QgsCoordinateTransform(crs_canvas, layer_crs, QgsProject.instance())
rect_geom.transform(xformer, QgsCoordinateTransform.ForwardTransform)
self.canvas.scene().removeItem(self.rubberBand)
self.Clicked.emit(rect_geom)
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 activate(self):
self.canvas.setCursor(self.cursor)
def deactivate(self):
super(FeatureFinderTool, self).deactivate()
self.deactivated.emit()