-
Notifications
You must be signed in to change notification settings - Fork 6
/
sensor_things.py
257 lines (192 loc) · 8.51 KB
/
sensor_things.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
# -*- coding: utf-8 -*-
"""SensorThings API Plugin class
Description
-----------
The main working code of the plugin. Contains all the initialization
of the plugin interfaces and commands.
Libraries/Modules
-----------------
- None.
Notes
-----
- None.
Author(s)
---------
- Created by Sandro Moretti on 09/02/2022.
Dedagroup Spa.
Members
-------
"""
import os.path
from qgis.PyQt.QtCore import qVersion, Qt, QObject, QSettings, QTranslator, QCoreApplication
from qgis.PyQt.QtWidgets import QAction, QMenu
from qgis.PyQt.QtGui import QIcon
# import plugin modules
from SensorThingsAPI import __QGIS_PLUGIN_NAME__
from SensorThingsAPI.log.logger import QgisLogger as logger
from SensorThingsAPI.sensor_things_location_dlg import SensorThingsLocationDialog
from SensorThingsAPI.feature_selection_tool import FeatureSelectionTool
from SensorThingsAPI.sensor_things_select_provider import SensorThingsFrostSourceSelectProvider
from SensorThingsAPI.icons.resources import * #@UnusedWildImport
# import Server Frost data provider
from SensorThingsAPI.providers.provider_frost import (
register_frost_data_provider,
unregister_frost_data_provider,
__FROST_PROVIDER_NAME__
)
# import Server Frost provider selector
from SensorThingsAPI.providers.source_select_provider_frost import (
register_frost_sourceselect_provider,
unregister_frost_sourceselect_provider
)
#
#-----------------------------------------------------------
class SensorThingsPlugin(QObject):
"""QGIS Air Brek Plugin Implementation."""
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# call parent cunstructor
QObject.__init__(self)
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialite logger
logger.name = __QGIS_PLUGIN_NAME__
# initialize locale
self.locale = QSettings().value('locale/userLocale')
if self.locale:
self.locale = self.locale[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'SensorThingsAPI_{}.qm'.format(self.locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
else:
self.locale = ''
self.menu_name_plugin = "{} Plugin".format(__QGIS_PLUGIN_NAME__)
# menu
self.menu = None
# toolbar
self.toolbar = None
# actions
self.frost_datasource_action = None
self.frost_identify_action = None
# map tool
self.featureSelectionTool = None
# dialogs
self.frostDataSourceDlg = None
self.postazionedlg = None
# Create and register Frost Server providers
if register_frost_data_provider():
logger.log(logger.Level.Info, self.tr("Registered 'Frost' provider"))
select_provider = SensorThingsFrostSourceSelectProvider()
if register_frost_sourceselect_provider(select_provider):
logger.log(logger.Level.Info, self.tr("Registered data source selector for 'Frost' provider"))
def initGui(self):
"""Initialize plugin resources"""
# create Frost Data Source Dialog
self.frostDataSourceDlg =\
SensorThingsFrostSourceSelectProvider().createDataSourceWidget(
self.iface.mainWindow(), fl=Qt.WindowFlags(), widgetMode=0)
self.frostDataSourceDlg.setModal(True)
# create Postazione info dialog
self.postazionedlg = SensorThingsLocationDialog(self, parent=self.iface.mainWindow())
#########self.postazionedlg.setModal(True)
# create action to show Frost data source dialog
icon_path = ':/plugins/SensorThingsAPI/icons/frost-layer-icon.png'
self.frost_datasource_action = QAction(
QIcon(icon_path), self.tr("Upload layer from remote server"), self.iface.mainWindow())
# connect the action handler
self.frost_datasource_action.triggered.connect(lambda: self.frostDataSourceDlg.show())
self.iface.insertAddLayerAction(self.frost_datasource_action)
# add action to toolbar
self.iface.dataSourceManagerToolBar().addAction(self.frost_datasource_action)
# Create action to show a postazione info
icon_path = ':/plugins/SensorThingsAPI/icons/action-identify-icon.png'
self.frost_identify_action = QAction(
QIcon(icon_path), self.tr("Show location information"), self.iface.mainWindow())
# connect the action handler
self.frost_identify_action.triggered.connect(self.onSelectPostazioneFeature)
# create plugin menu
menu_bar = self.iface.mainWindow().menuBar()
self.menu = QMenu(__QGIS_PLUGIN_NAME__, menu_bar)
menu_bar.insertMenu(menu_bar.actions()[-1], self.menu)
self.menu.addAction(self.frost_datasource_action)
self.menu.addSeparator()
self.menu.addAction(self.frost_identify_action)
# create plugin toolbar
self.toolbar = self.iface.addToolBar(__QGIS_PLUGIN_NAME__)
self.toolbar.setToolTip(self.tr('SensorThings API plugin command toolbar'))
self.toolbar.addAction(self.frost_datasource_action)
self.toolbar.addSeparator()
self.toolbar.addAction(self.frost_identify_action)
def unload(self):
"""Unload plugin resources"""
# deselect map tool
canvas = self.iface.mapCanvas()
if self.featureSelectionTool:
canvas.unsetMapTool(self.featureSelectionTool)
# Unregister Frost Server providers
if unregister_frost_sourceselect_provider():
logger.log(logger.Level.Info, self.tr("Unregistered data source selector for 'Frost' provider"))
if unregister_frost_data_provider():
logger.log(logger.Level.Info, self.tr("Unregistered 'Frost' provider"))
# Remove dialogs
self.frostDataSourceDlg.close()
self.frostDataSourceDlg.deleteLater()
self.frostDataSourceDlg = None
self.postazionedlg.close()
self.postazionedlg.deleteLater()
self.postazionedlg = None
# Remove the plugin menu item and icon
self.iface.removeAddLayerAction(self.frost_datasource_action)
self.iface.dataSourceManagerToolBar().removeAction(self.frost_datasource_action)
if self.menu:
self.menu.deleteLater()
self.menu = None
del self.toolbar
self.toolbar = None
self.frost_identify_action = None
self.frost_datasource_action = None
def onSelectPostazioneFeature(self, checked):
"""Callback method on selected location feature"""
# create and set map tool
canvas = self.iface.mapCanvas()
self.featureSelectionTool = FeatureSelectionTool(canvas)
self.featureSelectionTool.featuresIdentified.connect(self.postazioniShow)
canvas.setMapTool(self.featureSelectionTool)
logger.msgbar(
logger.Level.Info,
self.tr("Select a location"),
title=__QGIS_PLUGIN_NAME__,
clear=True)
def postazioniShow(self, layer, fids):
"""Slot to show Postazioni dialog"""
# check if Frost layer
provider = layer.dataProvider()
if provider.name() != __FROST_PROVIDER_NAME__:
logger.msgbar(
logger.Level.Warning,
"{}, <b>{}</b>".format(
self.tr("Selected entity has an illegal data source"),
self.tr("use a 'SensorThing' vector layer")
),
title=__QGIS_PLUGIN_NAME__)
return
# check if features sellected
fids = fids or []
if len(fids) == 0:
return
fid = fids[0]
# show Postazione dialog
self.postazionedlg.show(layer, fid, fids)