forked from AirBreak-UIA/SensorThingsAPI_QGIS-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_things_location_dlg.py
448 lines (363 loc) · 14.6 KB
/
sensor_things_location_dlg.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# -*- coding: utf-8 -*-
"""Modulo per la visualizzazione delle informazioni di una postazione.
Descrizione
-----------
Librerie/Moduli
-----------------
Notes
-----
- None.
Autore
-------
- Creato da Sandro Moretti il 09/02/2022.
2022 Dedagroup spa.
Membri
-------
"""
# Qgis\PyQt5 modules
from qgis.PyQt.QtCore import pyqtSlot, Qt, QUrl, QVariant
from qgis.PyQt.QtGui import QColor
from qgis.PyQt import QtWidgets
from qgis.core import QgsWkbTypes, QgsProject, QgsMapLayer
from qgis.gui import QgsGui, QgsRubberBand
from qgis.utils import iface
# plugin modules
from SensorThingsAPI import plgConfig, __QGIS_PLUGIN_NAME__
from SensorThingsAPI.log.logger import QgisLogger as logger
from SensorThingsAPI.html.generate import htmlUtil
from SensorThingsAPI.sensor_things_osservazioni_dlg import SensorThingsObservationDialog
from SensorThingsAPI.sensor_things_browser import (SensorThingsRequestError,
SensorThingsWebView,
SensorThingsNetworkAccessManager)
#
#-----------------------------------------------------------
class SensorThingsLocationDialog(QtWidgets.QDialog):
"""Dialog to show Location info"""
# --------------------------------------
#
# --------------------------------------
def __init__(self, plugin, parent=None, flags=Qt.WindowFlags()):
"""Constructor
:param parent:
:type parent: QtWidgets
"""
# init
super().__init__(parent, flags)
QgsGui.enableAutoGeometryRestore(self)
self.plugin = plugin
self.page_data = {}
self.replyPromise = None
self._lay_id = None
self._feat_id = None
self._fids = []
self._rubberBand = None
# setting the minimum size
self.setMinimumSize(900, 500)
# add widgets
self.setLayout(QtWidgets.QGridLayout())
self.layout().setContentsMargins(0, 0, 0, 0)
self.webView = SensorThingsWebView(parent=self)
self.webView.injectPyToJs(self, 'pyjsapi')
self.layout().addWidget(self.webView)
self.osservazDlg = SensorThingsObservationDialog(self.plugin, parent=self)
# settings
self.setWindowTitle(self.tr("Location"))
# --------------------------------------
#
# --------------------------------------
def closeEvent(self, _):
"""Close event method"""
# reset
self.page_data = {}
logger.restoreOverrideCursor()
# close Observations dialog
self.osservazDlg.close()
# remove rubber band
self._removeRubberBand()
# --------------------------------------
#
# --------------------------------------
def show(self, layer, fid, fids):
"""Show a location info"""
try:
# init
self._lay_id = layer.id()
self._feat_id = fid
self._fids = fids or []
# remove rubber band
self._removeRubberBand()
# get feature
feature = layer.getFeature(self._feat_id)
if not feature:
return
# show rubber band
self._showRubberBand(layer, feature)
# collect features info
feats_info = []
for fid in fids:
feat = layer.getFeature(fid)
if feat:
feats_info.append({
'fid': fid,
'locId': feat.attribute( "id" ),
'locName': feat.attribute( "name" ),
'lodDesc': feat.attribute( "description" )
})
# hide Osservazioni dialog
self.osservazDlg.hide()
# show wait cursor
logger.setOverrideCursor()
# get faeature location attribute
location_id = feature.attribute( "id" )
location_name = feature.attribute( "name" )
location_desc = feature.attribute( "description" )
# compose url
provider = layer.dataProvider()
service_url = QUrl(provider.dataSourceUri())
if not service_url.isValid():
raise ValueError(
"{}: {}".format(self.tr("Invalid layer URL"), service_url.toString()))
# prepare data for HTM template
self.page_data = {
'locale': self.plugin.locale,
'base_folder': htmlUtil.getBaseUrl(),
'feature': {
'fid': self._feat_id,
'locId': location_id,
'locName': location_name,
'lodDesc': location_desc
},
'features': feats_info,
'location': None,
'things': None,
}
# Request location data asyncronously
url = service_url.resolved(QUrl(f"./Locations({location_id})")).toString()
request = self.getRequest()
request.resolved.connect(self._location_callback)
request.get(url, { "tag": 'Location' })
# show spinner
self._show_web_spinner(True)
except SensorThingsRequestError as ex:
logger.restoreOverrideCursor()
self.logError(
"{}: {}".format(self.tr("Location dialog visualization"), str(ex)))
except Exception as ex:
logger.restoreOverrideCursor()
self.logError(
"{}: {}".format(self.tr("Location dialog visualization"), str(ex)))
# --------------------------------------
#
# --------------------------------------
def logError(self, message):
"""Log error message"""
# hide spinner
self._show_web_spinner(False)
# log
logger.msgbar(
logger.Level.Critical,
str(message),
title=__QGIS_PLUGIN_NAME__,
clear=True
)
logger.msgbox(
logger.Level.Critical,
str(message),
title=__QGIS_PLUGIN_NAME__
)
# --------------------------------------
#
# --------------------------------------
def rejectRequest(self, message):
"""Show Observations dialog"""
# hide spinner
self._show_web_spinner(False)
# restore cursor
logger.restoreOverrideCursor()
if message:
self.logError(str(message))
# --------------------------------------
#
# --------------------------------------
def _show_web_spinner(self, show):
frame =self.webView.page().mainFrame()
if show:
frame.evaluateJavaScript("sensorThingsShowSpinner(true);")
else:
frame.evaluateJavaScript("sensorThingsShowSpinner(false);")
# --------------------------------------
#
# --------------------------------------
def _show_callback(self):
try:
# check if got all data
if self.page_data.get('location') is None or\
self.page_data.get('things') is None:
self.rejectRequest(None)
return
logger.restoreOverrideCursor()
# load HTL document
template_name = 'location.html'
template = htmlUtil.generateTemplate(template_name)
self.webView.setHtml(template.render(self.page_data))
# show dialog
QtWidgets.QDialog.show(self)
except Exception as ex:
self.logError(
"{}: {}".format(self.tr("Location dialog visualization"), str(ex)))
# --------------------------------------
#
# --------------------------------------
def _location_callback(self, loc_data):
try:
if not self.page_data:
self.rejectRequest(None)
return
# store data
self.page_data['location'] = loc_data or {}
# Request things data asyncronously
url = loc_data.get('[email protected]', None)
if url is None:
raise ValueError(self.tr('Things URL not valued'))
request = self.getRequest()
request.resolved.connect(self._things_callback)
request.get(url, { "dataSrc": "value", "tag": 'Things' })
except Exception as ex:
self.logError(
"{}: {}".format(self.tr("Location dialog visualization"), str(ex)))
# --------------------------------------
#
# --------------------------------------
def _things_callback(self, things_data):
try:
if not self.page_data:
self.rejectRequest(None)
return
# store data
self.page_data['things'] = things_data or []
# try to show dialog
self._show_callback()
except Exception as ex:
self.logError(
"{}: {}".format(self.tr("Location dialog visualization"), str(ex)))
# --------------------------------------
#
# --------------------------------------
def _showRubberBand(self, layer, feature):
"""Show rubber band in scene"""
# remove previous rubber band
self._removeRubberBand()
try:
# create rubber band
canvas = iface.mapCanvas()
geom = feature.geometry()
geomType = geom.type()
color = QColor("red")
color.setAlphaF(0.78)
fillColor = QColor(255, 71, 25, 150)
self._rubberBand = QgsRubberBand(canvas, geomType)
self._rubberBand.setColor(color)
self._rubberBand.setFillColor(fillColor)
if geomType == QgsWkbTypes.PointGeometry:
cfg_styles = plgConfig.get_value('layer/styles', {})
properties = cfg_styles.get('default', {})
symbol_size = properties.get('pointRadius', 8)
self._rubberBand.setWidth(symbol_size * 5)
self._rubberBand.setIcon(QgsRubberBand.ICON_CIRCLE)
self._rubberBand.addPoint(geom.asPoint(), True, 0, 0)
else:
self._rubberBand.addGeometry(geom, layer)
self._rubberBand.setWidth(5)
# show rubber band
self._rubberBand.show()
except:
pass
# --------------------------------------
#
# --------------------------------------
def _removeRubberBand(self):
"""Remove rubber band from scene"""
if self._rubberBand:
canvas = iface.mapCanvas()
canvas.scene().removeItem(self._rubberBand)
self._rubberBand = None
@pyqtSlot(result=QVariant)
def getPageData(self):
"""Injected method to get page data"""
return self.page_data
@pyqtSlot(str, result=QVariant)
def getThingData(self, thing_id):
"""Injected method to get thing data"""
try:
# Get thing data
things_data = self.page_data.get('things', {})
thing_data = next((i for i in things_data if str(i['@iot.id']) == str(thing_id)), None)
if not thing_data:
raise ValueError(self.tr("Invalid Thing ID"))
return thing_data
except Exception as ex:
self.logError(str(ex))
return {}
@pyqtSlot(str, QVariant, result=QVariant)
def loadData(self, url, options):
"""Injected method to get data synchronously"""
try:
options = options if isinstance(options, dict) else {}
tag = options.get("tag", "")
# Request datastream data
return SensorThingsNetworkAccessManager.requestExternalData(url, options) or {}
except Exception as ex:
self.logError(
"{} {}: {}".format(self.tr("Loading data"), tag, str(ex)))
@pyqtSlot(result=QVariant)
def getRequest(self):
"""Injected method to return a new request to get data asynchronously"""
try:
# Create request async
self.replyPromise = SensorThingsNetworkAccessManager.requestExternalDataAsync(parent=self)
self.replyPromise.rejected.connect(self.rejectRequest)
return self.replyPromise
except Exception as ex:
self.logError(str(ex))
return None
@pyqtSlot(QVariant, QVariant)
def loadObservationsData(self, ds_row, options):
"""Show a new Osservazioni dialog"""
try:
# check if visible
if not self.isVisible():
return
# init
self.osservazDlg.hide()
ds_row = ds_row or {}
options = options or {}
date_filter = options.get('filterTime', '')
query_params = options.get('queryParams', '')
is_multidatastream = bool(options.get('isMultidatastream', False))
# Request Osservazioni data
self.page_data['selectRow'] = ds_row
self.page_data['filterTime'] = date_filter
self.page_data['queryParams'] = query_params
self.page_data['isMultidataStream'] = is_multidatastream
# create a new Osservazioni dialog
self.osservazDlg.show(self.page_data)
except Exception as ex:
self.logError(
"{}: {}".format(self.tr("Loading Observations data"), str(ex)))
@pyqtSlot(str)
def changeLocation(self, str_fid):
"""Injected method to location in multi feature selection"""
# init
if self._lay_id is None or\
self._feat_id is None:
return
fid = int(str_fid)
# search layers in TOC
root_node = QgsProject.instance().layerTreeRoot()
tree_layer = root_node.findLayer(self._lay_id)
if tree_layer:
layer = tree_layer.layer()
# check if vector layer
if layer.type() == QgsMapLayer.VectorLayer:
# show Location info
self.show(layer, fid, self._fids)