-
Notifications
You must be signed in to change notification settings - Fork 1
/
highlightFeature.py
158 lines (135 loc) · 7.02 KB
/
highlightFeature.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- 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 Qt, QVariant, QMetaType
from qgis.core import (QgsCoordinateReferenceSystem, QgsWkbTypes, QgsCoordinateTransform, QgsProject, QgsPointXY,
QgsPoint, QgsRectangle)
from qgis.gui import QgsRubberBand
from math import isnan
class HighlightFeature:
def __init__(self, canvas, p_pointsonly, p_closecontour, p_projectcrs):
self.canvas = canvas
# Highliting all conturs and nodes of current contour
self.lineHighlight = list()
self.nodesHighlight = list()
self.projectCrs = p_projectcrs
self.featureCrs = -1
self.pointsOnly = p_pointsonly
self.closeContour = p_closecontour
def createHighlight(self, coords, currentPart, p_featurecrs, currentVertex=0):
"""
coords - list of tuples with coordinates coords matrix type from addFeatureGUI
"""
needTransformation = False
self.featureCrs = p_featurecrs
if self.featureCrs != self.projectCrs:
needTransformation = True
transformation = QgsCoordinateTransform(self.featureCrs, self.projectCrs, QgsProject.instance())
if not self.pointsOnly:
for partNum in range(len(coords)):
self.lineHighlight.append(QgsRubberBand(self.canvas, QgsWkbTypes.LineGeometry))
coordsPart = coords[partNum][1]
for i in range(len(coordsPart)):
if self.isFloat(coordsPart[i][0]) and self.isFloat(coordsPart[i][1]):
if needTransformation:
src_point = QgsPoint(float(coordsPart[i][0]), float(coordsPart[i][1]))
src_point.transform(transformation)
point = QgsPointXY(src_point)
else:
point = QgsPointXY(float(coordsPart[i][0]), float(coordsPart[i][1]))
self.lineHighlight[partNum].addPoint(point, True, 0)
if self.closeContour and self.lineHighlight[partNum].numberOfVertices() > 2:
self.lineHighlight[partNum].closePoints(True)
self.lineHighlight[partNum].setColor(Qt.red)
self.lineHighlight[partNum].setWidth(2)
j = 0
coordsPart = coords[currentPart][1]
for i in range(len(coordsPart)):
if self.isFloat(coordsPart[i][0]) and self.isFloat(coordsPart[i][1]):
self.nodesHighlight.append(QgsRubberBand(self.canvas, QgsWkbTypes.PointGeometry))
if needTransformation:
src_point = QgsPoint(float(coordsPart[i][0]), float(coordsPart[i][1]))
src_point.transform(transformation)
point = QgsPointXY(src_point)
else:
point = QgsPointXY(float(coordsPart[i][0]), float(coordsPart[i][1]))
self.nodesHighlight[j].addPoint(point, True, 0)
if i == currentVertex:
self.nodesHighlight[j].setIcon(QgsRubberBand.ICON_FULL_BOX)
self.nodesHighlight[j].setColor(Qt.darkRed)
else:
self.nodesHighlight[j].setIcon(QgsRubberBand.ICON_FULL_DIAMOND)
self.nodesHighlight[j].setColor(Qt.darkBlue)
self.nodesHighlight[j].setIconSize(10)
j = j + 1
if len(self.nodesHighlight) > 0:
x_list = list()
y_list = list()
for i in range(len(self.nodesHighlight)):
x_list.append(self.nodesHighlight[i].getPoint(0).x())
y_list.append(self.nodesHighlight[i].getPoint(0).y())
featureRect = QgsRectangle(min(x_list), min(y_list), max(x_list), max(y_list))
# If canvas not contains entire feature then set canvas center on center of feature
mapRect = self.canvas.extent()
if not mapRect.contains(featureRect):
centerPoint = QgsPointXY(float((min(x_list) + max(x_list)) / 2), float((min(y_list) + max(y_list)) / 2))
self.canvas.setCenter(centerPoint)
# If now If canvas not contains entire feature then set canvas extent on feature extent
mapRect = self.canvas.extent()
if not mapRect.contains(featureRect):
self.canvas.setExtent(featureRect)
self.canvas.refresh()
def changeCurrentVertex(self, currentVertex=0):
if self.nodesHighlight is not None:
for i in range(len(self.nodesHighlight)):
if i == currentVertex:
self.nodesHighlight[i].setIcon(QgsRubberBand.ICON_FULL_BOX)
self.nodesHighlight[i].setColor(Qt.darkRed)
else:
self.nodesHighlight[i].setIcon(QgsRubberBand.ICON_FULL_DIAMOND)
self.nodesHighlight[i].setColor(Qt.darkBlue)
self.canvas.refresh()
def removeHighlight(self):
if len(self.lineHighlight) > 0:
for partNum in range(len(self.lineHighlight)):
self.canvas.scene().removeItem(self.lineHighlight[partNum])
self.lineHighlight[partNum].reset(QgsWkbTypes.LineGeometry)
self.lineHighlight.clear()
for i in range(len(self.nodesHighlight)):
self.canvas.scene().removeItem(self.nodesHighlight[i])
self.nodesHighlight[i].reset(QgsWkbTypes.PointGeometry)
self.nodesHighlight.clear()
self.canvas.refresh()
@staticmethod
def isFloat(value):
q_value = QVariant(value)
if q_value.isNull():
return False
if q_value.convert(QMetaType.QString):
if str(q_value.value()) == '':
return False
if q_value.convert(QMetaType.Float):
if isnan(float(q_value.value())):
return False
else:
return True
else:
return False