-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpscompass.py
88 lines (71 loc) · 3.14 KB
/
gpscompass.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
from PySide import QtCore
from QtMobility import Location
from QtMobility.Location import QGeoCoordinate, QGeoPositionInfo
from QtMobility.Sensors import QCompass
class GPSCompass(QtCore.QObject):
'''GPSCompass gives distance and bearing to a gps position from the current gps position'''
def __init__(self, parent):
QtCore.QObject.__init__(self, parent)
self.positionSource = Location.QGeoPositionInfoSource.createDefaultSource(self)
if self.positionSource == None:
self.valid = False
return
self.positionSource.positionUpdated.connect(self.locationUpdated)
self.currentPosition = QGeoCoordinate()
self.positionSource.startUpdates()
self.coordinate = QGeoCoordinate()
self.compass = QCompass(self)
self.compass.readingChanged.connect(self.compassReadingChanged)
self.compass.setDataRate(10)
self.compass.start()
self.compassAzimuth = 0
self.gpsactive = False
location_changed = QtCore.Signal()
bearing_changed = QtCore.Signal()
destination_changed = QtCore.Signal()
gps_activated = QtCore.Signal()
def __del__(self):
self.compass.stop()
self.positionSource.stopUpdates()
@QtCore.Slot()
def compassReadingChanged(self):
reading = self.compass.reading()
if reading:
self.compassAzimuth = reading.azimuth()
self.bearing_changed.emit()
@QtCore.Slot(float, float)
def setDestination(self, lat, lon):
self.coordinate = QGeoCoordinate(lat, lon)
self.destination_changed.emit()
def locationUpdated(self, update):
self.currentPosition = update.coordinate()
if not self.gpsactive:
self.gpsactive = True
self.gps_activated.emit()
self.location_changed.emit()
self.bearing_changed.emit()
def _distance(self):
if not self.currentPosition.isValid() or not self.coordinate.isValid():
return 0
return self.currentPosition.distanceTo(self.coordinate)
def _bearing(self):
if not self.currentPosition.isValid() or not self.coordinate.isValid():
return 0
return ((360 - self.compassAzimuth) - (360 - self.currentPosition.azimuthTo(self.coordinate)))
def _destinationLatitude(self):
return self.coordinate.latitude()
def _destinationLongitude(self):
return self.coordinate.longitude()
def _latitude(self):
return self.currentPosition.latitude()
def _longitude(self):
return self.currentPosition.longitude()
def _gpsfix(self):
return self.gpsactive
distance = QtCore.Property(int, _distance, notify=location_changed)
bearing = QtCore.Property(int, _bearing, notify=bearing_changed)
latitude = QtCore.Property(float, _latitude, notify=location_changed)
longitude = QtCore.Property(float, _longitude, notify=location_changed)
dest_latitude = QtCore.Property(float, _destinationLatitude, notify=destination_changed)
dest_longitude = QtCore.Property(float, _destinationLongitude, notify=destination_changed)
gps = QtCore.Property(bool, _gpsfix, notify=gps_activated)