QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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#include "qgslogger.h"
20
21#include <QLocalSocket>
22#include <QTimer>
23#include <QMetaType>
24
26 : QgsGpsConnection( new QLocalSocket() )
27{
28 //needed to fix https://sourceforge.net/p/necessitas/tickets/146/
29 qRegisterMetaType< QList<QGeoSatelliteInfo> >( "QList<QGeoSatelliteInfo>" );
30
31 startSatelliteMonitor();
32 startGPS();
33
34 //HACK to signal the gpsinformationwidget that we have a QtLocationConnection
35 QTimer::singleShot( 500, this, SLOT( broadcastConnectionAvailable() ) );
36}
37
38//Needed to make connection detectable (half HACK)
39//this signals that the device has started the GPS successfully,
40//not that it has a fix yet.
42{
43 if ( locationDataSource )
44 {
47 }
48}
49
50//TODO: Temporarily needed to workaround https://sourceforge.net/p/necessitas/tickets/147/
51void QgsQtLocationConnection::positionUpdated( const QGeoPositionInfo &info )
52{
53 mInfo = info;
54 parseData();
55}
56
58{
59 if ( locationDataSource )
60 {
62 //const QGeoPositionInfo &info = locationDataSource->lastKnownPosition();
63 if ( mInfo.isValid() )
64 {
65 // mInfo.HorizontalAccuracy;
66 mLastGPSInformation.latitude = mInfo.coordinate().latitude();
67 mLastGPSInformation.longitude = mInfo.coordinate().longitude();
68 mLastGPSInformation.elevation = mInfo.coordinate().altitude();
69 mLastGPSInformation.speed = mInfo.attribute( QGeoPositionInfo::GroundSpeed ) * 3.6; // m/s to km/h
71 mLastGPSInformation.utcDateTime = mInfo.timestamp();
72 mLastGPSInformation.utcTime = mInfo.timestamp().time();
73 mLastGPSInformation.fixType = mInfo.coordinate().type() + 1;
74 switch ( mInfo.coordinate().type() )
75 {
76 case QGeoCoordinate::InvalidCoordinate:
78 break;
79 case QGeoCoordinate::Coordinate2D:
81 break;
82 case QGeoCoordinate::Coordinate3D:
84 break;
85 }
86
87 //< fixType, used for navigation (1 = Fix not available; 2 = 2D; 3 = 3D)
88 //< coordinate().type(), returns 0 = Fix not available; 1 = 2D; 2 = 3D)
89 mLastGPSInformation.hacc = mInfo.attribute( QGeoPositionInfo::HorizontalAccuracy ); //< Horizontal dilution of precision
90 mLastGPSInformation.vacc = mInfo.attribute( QGeoPositionInfo::VerticalAccuracy ); //< Vertical dilution of precision
91
92 //TODO implement dop maybe by getting a
93 //http://developer.android.com/reference/android/location/GpsStatus.NmeaListener.html
94 //http://doc.qt.nokia.com/qtmobility-1.1/qnmeapositioninfosource.html
95 //into QtLocation and subclass QgsNMEAConnection directly?
96 //mLastGPSInformation.pdop; //< Dilution of precision
97 //mLastGPSInformation.hdop; //< Horizontal dilution of precision
98 //mLastGPSInformation.vdop; //< Vertical dilution of precision
99
100 //mLastGPSInformation.fixMode; //< Mode (M = Manual, forced to operate in 2D or 3D; A = Automatic, 3D/2D)
101 //mLastGPSInformation.quality; //< GPS quality indicator (0 = Invalid; 1 = Fix; 2 = Differential, 3 = Sensitive)
102 //mLastGPSInformation.status; //< Status (A = active or V = void)
103
105 QgsDebugMsgLevel( QStringLiteral( "Valid QGeoPositionInfo, positionUpdated" ), 2 );
106 }
107 }
108}
109
111 const QList<QGeoSatelliteInfo> &satellites )
112{
113 // The number of satellites in view is updated
115 for ( int i = 0; i < satellites.size(); ++i )
116 {
117 const QGeoSatelliteInfo currentSatellite = satellites.at( i );
118 QgsSatelliteInfo satelliteInfo;
119 satelliteInfo.azimuth = currentSatellite.attribute( QGeoSatelliteInfo::Azimuth );
120 satelliteInfo.elevation = currentSatellite.attribute( QGeoSatelliteInfo::Elevation );
121 satelliteInfo.id = currentSatellite.satelliteIdentifier();
122 satelliteInfo.signal = currentSatellite.signalStrength();
123 mLastGPSInformation.satellitesInView.append( satelliteInfo );
124 }
125 mLastGPSInformation.satInfoComplete = true; //to be used to determine when to graph signal and satellite position
127 QgsDebugMsgLevel( QStringLiteral( "satellitesInViewUpdated" ), 2 );
128}
129
131 const QList<QGeoSatelliteInfo> &satellites )
132{
133 // The number of satellites in use is updated
134 mLastGPSInformation.satellitesUsed = QString::number( satellites.count() ).toInt();
135
137 for ( const QGeoSatelliteInfo &currentSatellite : satellites )
138 {
139 //add pnr to mLastGPSInformation.satPrn
140 mLastGPSInformation.satPrn.append( currentSatellite.satelliteIdentifier() );
141
142 //set QgsSatelliteInfo.inuse to true for the satellites in use
144 {
145 if ( satInView.id == currentSatellite.satelliteIdentifier() )
146 {
147 satInView.inUse = true;
148 break;
149 }
150 }
151 }
152 mLastGPSInformation.satInfoComplete = true; //to be used to determine when to graph signal and satellite position
154 QgsDebugMsgLevel( QStringLiteral( "satellitesInUseUpdated" ), 2 );
155}
156
157void QgsQtLocationConnection::startGPS()
158{
159 QgsDebugMsgLevel( QStringLiteral( "Starting GPS QtLocation connection" ), 2 );
160 // Obtain the location data source if it is not obtained already
161 if ( !locationDataSource )
162 {
163 locationDataSource = QGeoPositionInfoSource::createDefaultSource( this );
164 if ( locationDataSource )
165 {
166 locationDataSource->setPreferredPositioningMethods( QGeoPositionInfoSource::SatellitePositioningMethods ); //QGeoPositionInfoSource::AllPositioningMethods
167 locationDataSource->setUpdateInterval( 1000 );
168 // Whenever the location data source signals that the current
169 // position is updated, the positionUpdated function is called.
170 QObject::connect( locationDataSource.data(),
171 &QGeoPositionInfoSource::positionUpdated,
172 this,
174 // Start listening for position updates
175 locationDataSource->startUpdates();
176 }
177 else
178 {
179 // Not able to obtain the location data source
180 QgsDebugError( QStringLiteral( "No QtLocation Position Source" ) );
181 }
182 }
183 else
184 {
185 // Start listening for position updates
186 locationDataSource->startUpdates();
187 }
188}
189
190void QgsQtLocationConnection::startSatelliteMonitor()
191{
192 QgsDebugMsgLevel( QStringLiteral( "Starting GPS QtLocation satellite monitor" ), 2 );
193
194 if ( !satelliteInfoSource )
195 {
196 satelliteInfoSource = QGeoSatelliteInfoSource::createDefaultSource( this );
197 if ( satelliteInfoSource )
198 {
199 QgsDebugMsgLevel( QStringLiteral( "satelliteMonitor started" ), 2 );
200 // Whenever the satellite info source signals that the number of
201 // satellites in use is updated, the satellitesInUseUpdated function
202 // is called
203 QObject::connect( satelliteInfoSource.data(),
204 &QGeoSatelliteInfoSource::satellitesInUseUpdated,
205 this,
207
208 // Whenever the satellite info source signals that the number of
209 // satellites in view is updated, the satellitesInViewUpdated function
210 // is called
211 QObject::connect( satelliteInfoSource.data(),
212 &QGeoSatelliteInfoSource::satellitesInViewUpdated,
213 this,
215
216 // Start listening for satellite updates
217 satelliteInfoSource->startUpdates();
218 }
219 else
220 {
221 // Not able to obtain the Satellite data source
222 QgsDebugError( QStringLiteral( "No QtLocation Satellite Source" ) );
223 }
224 }
225 else
226 {
227 // Start listening for position updates
228 satelliteInfoSource->startUpdates();
229 }
230}
@ Unknown
Unknown/other system.
@ NoFix
GPS is not fixed.
Abstract base class for connection to a GPS device.
QgsGpsInformation mLastGPSInformation
Last state of the gps related variables (e.g. position, time, ...)
Status mStatus
Connection status.
void stateChanged(const QgsGpsInformation &info)
Emitted whenever the GPS state is changed.
double direction
The bearing measured in degrees clockwise from true north to the direction of travel.
int fixType
Contains the fix type, where 1 = no fix, 2 = 2d fix, 3 = 3d fix.
double speed
Ground speed, in km/h.
QTime utcTime
The time at which this position was reported, in UTC time.
double vacc
Vertical accuracy in meters.
double latitude
Latitude in decimal degrees, using the WGS84 datum.
double longitude
Longitude in decimal degrees, using the WGS84 datum.
QList< QgsSatelliteInfo > satellitesInView
Contains a list of information relating to the current satellites in view.
QDateTime utcDateTime
The date and time at which this position was reported, in UTC time.
QList< int > satPrn
IDs of satellites used in the position fix.
double elevation
Altitude (in meters) above or below the mean sea level.
bool satInfoComplete
true if satellite information is complete.
int satellitesUsed
Count of satellites used in obtaining the fix.
double hacc
Horizontal accuracy in meters.
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:39
#define QgsDebugError(str)
Definition: qgslogger.h:38