-
Notifications
You must be signed in to change notification settings - Fork 9
/
mobile_item.py
307 lines (279 loc) · 11.4 KB
/
mobile_item.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
'''
Created on 05.06.2015
@author: jrenken
'''
from __future__ import absolute_import
from builtins import str
from qgis.PyQt.QtCore import QObject, pyqtSlot, QTimer, pyqtSignal
from qgis.core import Qgis, QgsPointXY, QgsCoordinateTransform, \
QgsCoordinateReferenceSystem, QgsCsException, \
QgsException, QgsBearingUtils, QgsProject
from qgis.gui import QgsMessageBar
from .position_marker import PositionMarker
from qgis.PyQt.QtWidgets import QLabel
from qgis.PyQt.QtGui import QMovie
FILTER_FLAGS = ('-head', '-pos', '+course', '+utm')
class MobileItem(QObject):
'''
A Mobile Item that reveives its position from a dataprovider
and is displayed on the canvas
Could be everything liek vehicles or simple beacons
'''
mobileItemCount = 0
newPosition = pyqtSignal(float, QgsPointXY, float, float)
newAttitude = pyqtSignal(float, float, float) # heading, pitch, roll
timeout = pyqtSignal()
def __init__(self, iface, params={}, parent=None):
'''
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
:param params: A dictionary defining all the properties of the item
:type params: dictionary
:param parent: Parent object for the new item. Defaults None.
:type parent: QObject
'''
super(MobileItem, self).__init__(parent)
self.iface = iface
self.canvas = iface.mapCanvas()
MobileItem.mobileItemCount += 1
self.name = params.setdefault('Name',
'MobileItem_' + str(MobileItem.mobileItemCount))
self.marker = PositionMarker(self.canvas, params)
self.marker.setToolTip(self.name)
self.dataProvider = params.get('provider', dict())
self.messageFilter = dict()
self.extData = dict()
self.coordinates = None
self.position = None
self.heading = -9999.9
self.depth = -9999.9
self.altitude = -9999.9
self.lastFix = 0.0
self.crsXform = QgsCoordinateTransform()
self.crsXform.setSourceCrs(QgsCoordinateReferenceSystem('EPSG:4326'))
self.onCrsChange()
self.canvas.destinationCrsChanged.connect(self.onCrsChange)
if hasattr(self.canvas, 'magnificationChanged'):
self.canvas.magnificationChanged.connect(self.onMagnificationChanged)
self.timer = QTimer(self)
self.timer.timeout.connect(self.timeout)
self.notifyCount = int(params.get('nofixNotify', 0))
self.fadeOut = bool(params.get('fadeOut', False))
if self.notifyCount or self.fadeOut:
self.timer.timeout.connect(self.notifyTimeout)
self.timeoutCount = 0
self.timeoutTime = int(params.get('timeout', 3000))
self.notifyDuration = int(params.get('NotifyDuration', 0))
self.timedOut = False
self.enabled = True
def removeFromCanvas(self):
'''
Remove the item and its track from the canvas
'''
self.marker.removeFromCanvas()
def properties(self):
'''
Return the items properties as dictionary
:returns: Items properties
:rtype: dict
'''
d = {'Name' : self.name,
'timeout': self.timeoutTime,
'nofixNotify': self.notifyCount,
'fadeOut': self.fadeOut,
'enabled': self.enabled,
'provider' : self.dataProvider}
d.update(self.marker.properties())
return d
def subscribePositionProvider(self, provider, filterId=None):
'''
Subscribe the provider for this item
by connecting to the providers signals
:param provider: Provider to connect to
:type provider: DataProvider
:param filterId: Filter Id for this item
:type filterId:
'''
provider.newDataReceived.connect(self.processNewData)
try:
if filterId['id'] not in (None, 'None') or filterId['flags']:
self.messageFilter[provider.name] = filterId
elif provider.name in self.messageFilter:
self.messageFilter.pop(provider.name, None)
except (KeyError, TypeError):
self.messageFilter.pop(provider.name, None)
def unsubscribePositionProvider(self, provider):
'''
Unsubscribe provider by disconnecting the providers signals
:param provider: Provider to diconnect from
:type provider: DataProvider
'''
try:
provider.newDataReceived.disconnect(self.processData)
self.messageFilter.pop(provider.name, None)
except KeyError:
pass
@pyqtSlot(dict)
def processNewData(self, data):
'''
Process incoming data from the data provider
:param data: Positon or attitude data
:type data: dict
'''
if not self.enabled:
return
flags = list()
try:
pname = data['name']
flags = self.messageFilter[pname]['flags']
if not self.messageFilter[pname]['id'] in (None, 'None'):
if not data['id'] in (self.messageFilter[pname]['id'], str(self.messageFilter[pname]['id'])):
return
except Exception:
pass
self.extData.update(data)
if '-pos' not in flags:
if ('lat' in data and 'lon' in data) or self.hasUtmCoords(flags, data):
if self.fadeOut and self.timedOut:
self.marker.setVisible(True)
self.timedOut = False
self.position = QgsPointXY(data['lon'], data['lat'])
self.heading = data.get('heading', self.heading)
self.depth = data.get('depth', self.depth)
self.altitude = data.get('altitude', self.altitude)
try:
self.coordinates = self.crsXform.transform(self.position)
self.marker.setMapPosition(self.coordinates)
if 'time' in data:
self.lastFix = data['time']
self.newPosition.emit(self.lastFix, self.position,
self.extData.get('depth', -9999.9),
self.extData.get('altitude', -9999.9))
self.timer.start(self.timeoutTime)
self.timeoutCount = 0
except QgsCsException:
pass
elif self.position is not None:
if 'depth' in data or 'altitude' in data:
self.newPosition.emit(self.lastFix, self.position,
self.extData.get('depth', -9999.9),
self.extData.get('altitude', -9999.9))
if 'heading' in data and '-head' not in flags:
self.newAttitude.emit(data['heading'], data.get('pitch', 0.0),
data.get('roll', 0.0))
self.marker.newHeading(data['heading'])
self.heading = data['heading']
elif 'course' in data and '+course' in flags:
self.newAttitude.emit(data['course'], data.get('pitch', 0.0),
data.get('roll', 0.0))
self.marker.newHeading(data['course'])
self.heading = data['course']
if 'text' in data:
self.marker.setText(data['text'])
def hasUtmCoords(self, flags, data):
if '+utm' in flags:
if 'easting' in data and 'northing' in data:
try:
point = self.crsXform.transform(data['easting'], data['northing'], Qgis.TransformDirection.Reverse)
data['lat'] = point.y()
data['lon'] = point.x()
if data['headtype'] == 'G' and 'heading' in data:
try:
bearing = QgsBearingUtils.bearingTrueNorth(self.crsXform.destinationCrs(), QgsProject.instance().transformContext(),
QgsPointXY(data['easting'], data['northing']))
data['heading'] = (data['heading'] - bearing) % 360.0
except QgsException:
pass
return True
except QgsCsException:
pass
return False
@pyqtSlot(float)
def onScaleChange(self, ):
'''
Slot called when the map is zoomed
:param scale: New scale
:type scale: float
'''
self.marker.updatePosition()
@pyqtSlot()
def onCrsChange(self):
'''
SLot called when the mapcanvas CRS is changed
'''
crsDst = self.canvas.mapSettings().destinationCrs()
self.crsXform.setDestinationCrs(crsDst)
self.marker.updatePosition()
@pyqtSlot(float)
def onMagnificationChanged(self, ):
'''
Slot called when the map magnification has changed
:param scale: New scale
:type scale: float
'''
self.marker.updateMapMagnification()
@pyqtSlot(bool)
def setEnabled(self, enabled):
'''
Hide or display the item and its track on the map
:param enabled: what to do
:type enabled: bool
'''
self.enabled = enabled
self.marker.setVisible(self.enabled)
self.marker.resetPosition()
self.extData.clear()
if self.enabled:
self.timer.start(self.timeoutTime)
self.timeoutCount = 0
else:
self.timer.stop()
@pyqtSlot()
def deleteTrack(self):
'''
Delete the track all points
'''
self.marker.deleteTrack()
@pyqtSlot()
def centerOnMap(self):
'''
Center the item on the map
'''
if self.coordinates is not None:
self.canvas.setCenter(self.coordinates)
self.canvas.refresh()
def reportPosition(self):
'''
Report the position of the item. Used for logging
:returns: geographic postion, depth and altitude
:rtype: float, float, float, float, float
'''
if self.position is None:
return -9999.9, -9999.9, -9999.9, 0.0, -9999.9
return self.position.y(), self.position.x(), self.depth, self.heading, self.altitude
@pyqtSlot()
def notifyTimeout(self):
if self.fadeOut and not self.timedOut:
self.marker.setVisible(False)
self.timedOut = True
if self.notifyCount:
self.timeoutCount += 1
if self.timeoutCount == self.notifyCount:
msg = self.tr(u'No fix for %s since more than %d seconds!') % (self.name, self.timeoutTime * self.timeoutCount / 1000)
w = self.iface.messageBar().createMessage(self.tr(u'PosiView Attention'), msg)
label = QLabel(w)
m = QMovie(':/plugins/PosiView/hand.gif')
m.setSpeed(75)
label.setMovie(m)
m.setParent(label)
m.start()
w.layout().addWidget(label)
self.iface.messageBar().pushWidget(w, level=Qgis.Critical, duration=self.notifyDuration)
def getTrack(self):
tr = [e[1] for e in self.marker.track]
return tr
def applyTrack(self, track):
self.marker.setTrack(track)