QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsqtlocationconnection.cpp
Go to the documentation of this file.
1/***************************************************************************
2 QgsQtLocationConnection.cpp - description
3 ---------------------
4 begin : December 7th, 2011
5 copyright : (C) 2011 by Marco Bernasocchi, Bernawebdesign.ch
6 email : marco at bernawebdesign dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgslogger.h"
21
22#include <QLocalSocket>
23#include <QMetaType>
24#include <QString>
25#include <QTimer>
26
27#include "moc_qgsqtlocationconnection.cpp"
28
29using namespace Qt::StringLiterals;
30
32 : QgsGpsConnection( new QLocalSocket() )
33{
34 //needed to fix https://sourceforge.net/p/necessitas/tickets/146/
35 qRegisterMetaType< QList<QGeoSatelliteInfo> >( "QList<QGeoSatelliteInfo>" );
36
37 startSatelliteMonitor();
38 startGPS();
39
40 //HACK to signal the gpsinformationwidget that we have a QtLocationConnection
41 QTimer::singleShot( 500, this, &QgsQtLocationConnection::broadcastConnectionAvailable );
42}
43
44//Needed to make connection detectable (half HACK)
45//this signals that the device has started the GPS successfully,
46//not that it has a fix yet.
48{
49 if ( locationDataSource )
50 {
53 }
54}
55
56//TODO: Temporarily needed to workaround https://sourceforge.net/p/necessitas/tickets/147/
57void QgsQtLocationConnection::positionUpdated( const QGeoPositionInfo &info )
58{
59 mInfo = info;
60 parseData();
61}
62
64{
65 if ( locationDataSource )
66 {
68 //const QGeoPositionInfo &info = locationDataSource->lastKnownPosition();
69 if ( mInfo.isValid() )
70 {
71 // mInfo.HorizontalAccuracy;
72 mLastGPSInformation.latitude = mInfo.coordinate().latitude();
73 mLastGPSInformation.longitude = mInfo.coordinate().longitude();
74 mLastGPSInformation.elevation = mInfo.coordinate().altitude();
75 mLastGPSInformation.speed = mInfo.attribute( QGeoPositionInfo::GroundSpeed ) * 3.6; // m/s to km/h
76 mLastGPSInformation.direction = mInfo.attribute( QGeoPositionInfo::Direction );
77 mLastGPSInformation.utcDateTime = mInfo.timestamp();
78 mLastGPSInformation.utcTime = mInfo.timestamp().time();
79 mLastGPSInformation.fixType = mInfo.coordinate().type() + 1;
80 switch ( mInfo.coordinate().type() )
81 {
82 case QGeoCoordinate::InvalidCoordinate:
84 break;
85 case QGeoCoordinate::Coordinate2D:
87 break;
88 case QGeoCoordinate::Coordinate3D:
90 break;
91 }
92
93 //< fixType, used for navigation (1 = Fix not available; 2 = 2D; 3 = 3D)
94 //< coordinate().type(), returns 0 = Fix not available; 1 = 2D; 2 = 3D)
95 mLastGPSInformation.hacc = mInfo.attribute( QGeoPositionInfo::HorizontalAccuracy ); //< Horizontal dilution of precision
96 mLastGPSInformation.vacc = mInfo.attribute( QGeoPositionInfo::VerticalAccuracy ); //< Vertical dilution of precision
97
98 //TODO implement dop maybe by getting a
99 //http://developer.android.com/reference/android/location/GpsStatus.NmeaListener.html
100 //http://doc.qt.nokia.com/qtmobility-1.1/qnmeapositioninfosource.html
101 //into QtLocation and subclass QgsNMEAConnection directly?
102 //mLastGPSInformation.pdop; //< Dilution of precision
103 //mLastGPSInformation.hdop; //< Horizontal dilution of precision
104 //mLastGPSInformation.vdop; //< Vertical dilution of precision
105
106 //mLastGPSInformation.fixMode; //< Mode (M = Manual, forced to operate in 2D or 3D; A = Automatic, 3D/2D)
107 //mLastGPSInformation.quality; //< GPS quality indicator (0 = Invalid; 1 = Fix; 2 = Differential, 3 = Sensitive)
108 //mLastGPSInformation.status; //< Status (A = active or V = void)
109
111 QgsDebugMsgLevel( u"Valid QGeoPositionInfo, positionUpdated"_s, 2 );
112 }
113 }
114}
115
117 const QList<QGeoSatelliteInfo> &satellites )
118{
119 // The number of satellites in view is updated
120 mLastGPSInformation.satellitesInView.clear();
121 for ( int i = 0; i < satellites.size(); ++i )
122 {
123 const QGeoSatelliteInfo currentSatellite = satellites.at( i );
124 QgsSatelliteInfo satelliteInfo;
125 satelliteInfo.azimuth = currentSatellite.attribute( QGeoSatelliteInfo::Azimuth );
126 satelliteInfo.elevation = currentSatellite.attribute( QGeoSatelliteInfo::Elevation );
127 satelliteInfo.id = currentSatellite.satelliteIdentifier();
128 satelliteInfo.signal = currentSatellite.signalStrength();
129 mLastGPSInformation.satellitesInView.append( satelliteInfo );
130 }
131 mLastGPSInformation.satInfoComplete = true; //to be used to determine when to graph signal and satellite position
133 QgsDebugMsgLevel( u"satellitesInViewUpdated"_s, 2 );
134}
135
137 const QList<QGeoSatelliteInfo> &satellites )
138{
139 // The number of satellites in use is updated
140 mLastGPSInformation.satellitesUsed = QString::number( satellites.count() ).toInt();
141
142 mLastGPSInformation.satPrn.clear();
143 for ( const QGeoSatelliteInfo &currentSatellite : satellites )
144 {
145 //add pnr to mLastGPSInformation.satPrn
146 mLastGPSInformation.satPrn.append( currentSatellite.satelliteIdentifier() );
147
148 //set QgsSatelliteInfo.inuse to true for the satellites in use
149 for ( QgsSatelliteInfo &satInView : mLastGPSInformation.satellitesInView )
150 {
151 if ( satInView.id == currentSatellite.satelliteIdentifier() )
152 {
153 satInView.inUse = true;
154 break;
155 }
156 }
157 }
158 mLastGPSInformation.satInfoComplete = true; //to be used to determine when to graph signal and satellite position
160 QgsDebugMsgLevel( u"satellitesInUseUpdated"_s, 2 );
161}
162
163void QgsQtLocationConnection::startGPS()
164{
165 QgsDebugMsgLevel( u"Starting GPS QtLocation connection"_s, 2 );
166 // Obtain the location data source if it is not obtained already
167 if ( !locationDataSource )
168 {
169 locationDataSource = QGeoPositionInfoSource::createDefaultSource( this );
170 if ( locationDataSource )
171 {
172 locationDataSource->setPreferredPositioningMethods( QGeoPositionInfoSource::SatellitePositioningMethods ); //QGeoPositionInfoSource::AllPositioningMethods
173 locationDataSource->setUpdateInterval( 1000 );
174 // Whenever the location data source signals that the current
175 // position is updated, the positionUpdated function is called.
176 QObject::connect( locationDataSource.data(),
177 &QGeoPositionInfoSource::positionUpdated,
178 this,
180 // Start listening for position updates
181 locationDataSource->startUpdates();
182 }
183 else
184 {
185 // Not able to obtain the location data source
186 QgsDebugError( u"No QtLocation Position Source"_s );
187 }
188 }
189 else
190 {
191 // Start listening for position updates
192 locationDataSource->startUpdates();
193 }
194}
195
196void QgsQtLocationConnection::startSatelliteMonitor()
197{
198 QgsDebugMsgLevel( u"Starting GPS QtLocation satellite monitor"_s, 2 );
199
200 if ( !satelliteInfoSource )
201 {
202 satelliteInfoSource = QGeoSatelliteInfoSource::createDefaultSource( this );
203 if ( satelliteInfoSource )
204 {
205 QgsDebugMsgLevel( u"satelliteMonitor started"_s, 2 );
206 // Whenever the satellite info source signals that the number of
207 // satellites in use is updated, the satellitesInUseUpdated function
208 // is called
209 QObject::connect( satelliteInfoSource.data(),
210 &QGeoSatelliteInfoSource::satellitesInUseUpdated,
211 this,
213
214 // Whenever the satellite info source signals that the number of
215 // satellites in view is updated, the satellitesInViewUpdated function
216 // is called
217 QObject::connect( satelliteInfoSource.data(),
218 &QGeoSatelliteInfoSource::satellitesInViewUpdated,
219 this,
221
222 // Start listening for satellite updates
223 satelliteInfoSource->startUpdates();
224 }
225 else
226 {
227 // Not able to obtain the Satellite data source
228 QgsDebugError( u"No QtLocation Satellite Source"_s );
229 }
230 }
231 else
232 {
233 // Start listening for position updates
234 satelliteInfoSource->startUpdates();
235 }
236}
@ Unknown
Unknown/other system.
Definition qgis.h:1963
@ NoFix
GPS is not fixed.
Definition qgis.h:1949
@ Fix2D
2D fix
Definition qgis.h:1950
@ Fix3D
3D fix
Definition qgis.h:1951
QgsGpsInformation mLastGPSInformation
Last state of the gps related variables (e.g. position, time, ...).
QgsGpsConnection(QIODevice *dev)
Constructor.
Status mStatus
Connection status.
void stateChanged(const QgsGpsInformation &info)
Emitted whenever the GPS state is changed.
void satellitesInViewUpdated(const QList< QGeoSatelliteInfo > &satellites)
Called when the number of satellites in view is updated.
void broadcastConnectionAvailable()
Needed to make QtLocation detected.
void positionUpdated(const QGeoPositionInfo &info)
Called when the position updated.
void satellitesInUseUpdated(const QList< QGeoSatelliteInfo > &satellites)
Called when the number of satellites in use is updated.
void parseData() override
Parse available data source content.
Encapsulates information relating to a GPS satellite.
double elevation
Elevation of the satellite, in degrees.
bool inUse
true if satellite was used in obtaining the position fix.
int signal
Signal strength (0-99dB), or -1 if not available.
int id
Contains the satellite identifier number.
double azimuth
The azimuth of the satellite to true north, in degrees.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59