This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
data_selector.py
216 lines (181 loc) · 8.23 KB
/
data_selector.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 Oslandia <[email protected]>
#
# This file is a piece of free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, see <http://www.gnu.org/licenses/>.
#
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QDialog, QVBoxLayout, QDialogButtonBox, QAbstractItemView
from qgis.PyQt.QtWidgets import (QListWidget, QListWidgetItem, QHBoxLayout, QLabel, QComboBox,
QPushButton)
from qgis.core import QgsProject, QgsFeatureRequest
from .config import PlotConfig
from .qgeologis.data_interface import LayerData, FeatureData
class DataSelector(QDialog):
def __init__(self, viewer, features, config_list, config):
QDialog.__init__(self)
self.__viewer = viewer
self.__features = features
self.__config_list = config_list
self.__config = config
vbox = QVBoxLayout()
btn = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
btn.accepted.connect(self.accept)
btn.rejected.connect(self.reject)
self.__list = QListWidget()
self.__list.setSelectionMode(QAbstractItemView.ExtendedSelection)
hbox = QHBoxLayout()
lbl = QLabel("Sub selection")
self.__sub_selection_combo = QComboBox()
self.__sub_selection_combo.setEnabled(False)
hbox.addWidget(lbl)
hbox.addWidget(self.__sub_selection_combo)
self.__title_label = QLabel()
hbox2 = QHBoxLayout()
hbox2.addWidget(self.__title_label)
vbox.addLayout(hbox2)
vbox.addWidget(self.__list)
vbox.addLayout(hbox)
vbox.addWidget(btn)
self.__list.itemSelectionChanged.connect(self.on_selection_changed)
self.__sub_selection_combo.currentIndexChanged[str].connect(self.on_combo_changed)
self.setLayout(vbox)
self.setWindowTitle("Choose the data to add")
self.resize(400, 200)
self._populate_list()
self.set_title(",".join([feature[self.__config["name_column"]]
for feature in self.__features]))
def set_title(self, title):
self.__title_label.setText("Station(s): {}".format(title))
def _populate_list(self):
self.__list.clear()
for cfg in self.__config_list:
if cfg["type"] in ("cumulative", "instantaneous"):
# check number of features for this station
if cfg.get("feature_filter_type") == "unique_data_from_values":
# get unique filter values
layerid = cfg["source"]
data_l = QgsProject.instance().mapLayers()[layerid]
values = set()
for feature in self.__features:
feature_id = feature[self.__config["id_column"]]
req = QgsFeatureRequest()
req.setFilterExpression("{}={}".format(cfg["feature_ref_column"],
feature_id))
values.update([f[cfg["feature_filter_column"]]
for f in data_l.getFeatures(req)])
cfg.set_filter_unique_values(sorted(list(values)))
item = QListWidgetItem(cfg["name"])
item.setData(Qt.UserRole, cfg)
self.__list.addItem(item)
def accept(self):
for feature in self.__features:
self.__load_feature(feature)
def __load_feature(self, feature):
# FIXME this code too similar to main_dialog.load_plots
# FIXME find a way to factor
feature_id = feature[self.__config["id_column"]]
feature_name = feature[self.__config["name_column"]]
for item in self.__list.selectedItems():
# now add the selected configuration
cfg = item.data(Qt.UserRole)
if cfg["type"] in ("cumulative", "instantaneous", "continuous"):
layerid = cfg["source"]
data_l = QgsProject.instance().mapLayers()[layerid]
req = QgsFeatureRequest()
filter_expr = "{}='{}'".format(cfg["feature_ref_column"], feature_id)
req.setFilterExpression(filter_expr)
title = cfg["name"]
if cfg.get_filter_value():
filter_expr += " and {}='{}'".format(cfg["feature_filter_column"],
cfg.get_filter_value())
title = cfg.get_filter_value()
else:
title = cfg["name"]
f = None
# test if the layer actually contains data
for f in data_l.getFeatures(req):
break
if f is None:
return
if cfg["type"] == "instantaneous":
uom = cfg.get_uom()
data = LayerData(
data_l,
cfg["event_column"],
cfg["value_column"],
filter_expression=filter_expr,
uom=uom
)
uom = data.uom()
self.__viewer.add_data_cell(
data,
title,
uom,
station_name=feature_name,
config=cfg
)
self.__viewer.add_scale()
if cfg["type"] == "continuous":
uom = cfg.get_uom()
fids = [f.id() for f in data_l.getFeatures(req)]
data = FeatureData(
data_l,
cfg["values_column"],
feature_ids=fids,
x_start_fieldname=cfg["start_measure_column"],
x_delta_fieldname=cfg["interval_column"]
)
self.__viewer.add_data_cell(
data,
title,
uom,
station_name=feature_name,
config=cfg
)
elif cfg["type"] == "cumulative":
self.__viewer.add_histogram(
data_l,
filter_expression=filter_expr,
column_mapping={
f : cfg[f] for f in (
"min_event_column",
"max_event_column",
"value_column"
)
},
title=title,
config=cfg,
station_name=feature_name
)
elif cfg["type"] == "image":
self.__viewer.add_imagery_from_db(cfg, feature_id)
QDialog.accept(self)
def on_selection_changed(self):
self.__sub_selection_combo.clear()
self.__sub_selection_combo.setEnabled(False)
for item in self.__list.selectedItems():
cfg = item.data(Qt.UserRole)
for v in cfg.get_filter_unique_values():
self.__sub_selection_combo.addItem(v)
if cfg.get_filter_value():
self.__sub_selection_combo.setCurrentIndex(
self.__sub_selection_combo.findText(cfg.get_filter_value()))
self.__sub_selection_combo.setEnabled(True)
return
def on_combo_changed(self, text):
for item in self.__list.selectedItems():
cfg = item.data(Qt.UserRole)
cfg.set_filter_value(text)
item.setData(Qt.UserRole, cfg)
return