forked from jrenken/qgis-PosiView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobile_item.py
254 lines (229 loc) · 8.7 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
'''
Created on 05.06.2015
@author: jrenken
'''
from PyQt4.QtCore import QObject, pyqtSlot, QTimer, pyqtSignal
from qgis.core import QgsPoint, QgsCoordinateTransform, \
QgsCoordinateReferenceSystem, QgsCsException
from qgis.gui import QgsMessageBar
from position_marker import PositionMarker
from PyQt4.QtGui import QLabel, QMovie
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, QgsPoint, 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 = 0.0
self.depth = 0.0
self.lastFix = 0.0
self.crsXform = QgsCoordinateTransform()
self.crsXform.setSourceCrs(QgsCoordinateReferenceSystem(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))
if self.notifyCount:
self.timer.timeout.connect(self.notifyTimeout)
self.timeoutCount = 0
self.timeoutTime = int(params.get('timeout', 3000))
self.notifyDuration = int(params.get('NotifyDuration', 0))
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,
'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)
if filterId not in (None, 'None'):
self.messageFilter[provider.name] = filterId
elif provider.name in self.messageFilter.keys():
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
try:
name = data['name']
if name in self.messageFilter.keys():
if data['id'] != self.messageFilter[name]:
return
except:
pass
self.extData.update(data)
if 'lat' in data and 'lon' in data:
self.position = QgsPoint(data['lon'], data['lat'])
self.heading = data.get('heading', -9999.9)
self.depth = data.get('depth', -9999.9)
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:
self.newAttitude.emit(data['heading'], data.get('pitch', 0.0),
data.get('roll', 0.0))
self.marker.newHeading(data['heading'])
@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.setDestCRS(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()
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
'''
if self.position is None:
return -9999.9, -9999.9, -9999.9, 0.0
return self.position.y(), self.position.x(), self.depth, self.heading
@pyqtSlot()
def notifyTimeout(self):
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)
l = QLabel(w)
m = QMovie(':/plugins/PosiView/hand.gif')
m.setSpeed(75)
l.setMovie(m)
m.setParent(l)
m.start()
w.layout().addWidget(l)
self.iface.messageBar().pushWidget(w, QgsMessageBar.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)