-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute_intervals_dockwidget.py
More file actions
131 lines (107 loc) · 5.38 KB
/
attribute_intervals_dockwidget.py
File metadata and controls
131 lines (107 loc) · 5.38 KB
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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
AttributeIntervalsDockWidget
A QGIS plugin
This plugin detects intervals of consecutive values
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2021-07-22
git sha : $Format:%H$
copyright : (C) 2021 by Georgii Savelev
email : bassetranquille@gmail.com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
import os
from qgis.PyQt import QtWidgets, uic
from PyQt5.QtWidgets import QMessageBox
from qgis.PyQt.QtCore import pyqtSignal
from qgis.core import QgsMapLayerProxyModel
from qgis.utils import iface
from .list_to_interval import intervals
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'attribute_intervals_dockwidget_base.ui'))
class AttributeIntervalsDockWidget(QtWidgets.QDockWidget, FORM_CLASS):
closingPlugin = pyqtSignal()
def __init__(self, parent=None):
"""Constructor."""
super(AttributeIntervalsDockWidget, self).__init__(parent)
# Set up the user interface from Designer.
# After setupUI you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://doc.qt.io/qt-5/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
#input layer and field setting up
self.inputMapLayerComboBox.setFilters(QgsMapLayerProxyModel.VectorLayer)
self.inputMapLayerComboBox.setShowCrs(True)
self.inputMapLayerComboBox.currentIndexChanged.connect(lambda:
self.inputFieldComboBox.setLayer(self.inputMapLayerComboBox.currentLayer())
)
self.getValuePushButton.clicked.connect(self.calculatePrefix)
#tie up active layer with the input layer for the tool
self.inputMapLayerComboBox.setLayer(iface.activeLayer())
iface.layerTreeView().currentLayerChanged.connect(lambda:
self.inputMapLayerComboBox.setLayer(iface.activeLayer())
)
self.clearPushButton.clicked.connect(lambda:
self.prefixLineEdit.clear()
)
self.calculatePushButton.clicked.connect(self.calculateIntervals)
def calculatePrefix(self):
mainlayer = self.inputMapLayerComboBox.currentLayer()
attr = self.inputFieldComboBox.currentField()
feats = mainlayer.getFeatures()
self.prefixLineEdit.setText(str([feat.attribute(attr) for feat in feats][0]))
def calculateIntervals(self):
attr = self.inputFieldComboBox.currentField()
mainlayer = self.inputMapLayerComboBox.currentLayer()
#if some features are selected, work with them
if mainlayer.selectedFeatureCount() == 0:
selection = mainlayer.getFeatures()
else:
selection = mainlayer.selectedFeatures()
num_data = []
text_data = []
prefix = self.prefixLineEdit.text()
for feat in selection:
feat_attr = feat[attr]
if (prefix or prefix == 0) and prefix in str(feat_attr):
feat_attr = feat_attr.split(prefix)[1]
if feat_attr or feat_attr == 0:
if str(feat_attr).isdigit():
num_data.append(int(feat_attr))
else:
text_data.append(feat_attr)
if len(num_data) > 0:
data_intervals = intervals(num_data)
else:
data_intervals = ''
data = data_intervals
if len(data) > 0 and len(text_data) > 0:
data += ', '
if len(text_data) > 0:
data += ', '.join(sorted(list(set(text_data))))
intervals_number = len(data.split(","))
if len(data) > 0:
msgbox = QtWidgets.QMessageBox(
QMessageBox.Information, 'Intervals',
f'{intervals_number} interval{"s" if intervals_number > 1 else ""} in {attr}s:' \
+ f'\n{data}')
msgbox.exec_()
else:
msgbox = QtWidgets.QMessageBox(
QMessageBox.Information, 'Intervals',
'No intervals found')
msgbox.exec_()
def closeEvent(self, event):
self.closingPlugin.emit()
event.accept()