QGIS API Documentation 3.41.0-Master (3440c17df1d)
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#include "moc_qgsqtlocationconnection.cpp"
20#include "qgslogger.h"
21
22#include <QLocalSocket>
23#include <QTimer>
24#include <QMetaType>
25
27 : QgsGpsConnection( new QLocalSocket() )
28{
29 //needed to fix https://sourceforge.net/p/necessitas/tickets/146/
30 qRegisterMetaType< QList<QGeoSatelliteInfo> >( "QList<QGeoSatelliteInfo>" );
31
32 startSatelliteMonitor();
33 startGPS();
34
35 //HACK to signal the gpsinformationwidget that we have a QtLocationConnection
36 QTimer::singleShot( 500, this, SLOT( broadcastConnectionAvailable() ) );
37}
38
39//Needed to make connection detectable (half HACK)
40//this signals that the device has started the GPS successfully,
41//not that it has a fix yet.
43{
44 if ( locationDataSource )
45 {
48 }
49}
50
51//TODO: Temporarily needed to workaround https://sourceforge.net/p/necessitas/tickets/147/
52void QgsQtLocationConnection::positionUpdated( const QGeoPositionInfo &info )
53{
54 mInfo = info;
55 parseData();
56}
57
59{
60 if ( locationDataSource )
61 {
63 //const QGeoPositionInfo &info = locationDataSource->lastKnownPosition();
64 if ( mInfo.isValid() )
65 {
66 // mInfo.HorizontalAccuracy;
67 mLastGPSInformation.latitude = mInfo.coordinate().latitude();
68 mLastGPSInformation.longitude = mInfo.coordinate().longitude();
69 mLastGPSInformation.elevation = mInfo.coordinate().altitude();
70 mLastGPSInformation.speed = mInfo.attribute( QGeoPositionInfo::GroundSpeed ) * 3.6; // m/s to km/h
71 mLastGPSInformation.direction = mInfo.attribute( QGeoPositionInfo::Direction );
72 mLastGPSInformation.utcDateTime = mInfo.timestamp();
73 mLastGPSInformation.utcTime = mInfo.timestamp().time();
74 mLastGPSInformation.fixType = mInfo.coordinate().type() + 1;
75 switch ( mInfo.coordinate().type() )
76 {
77 case QGeoCoordinate::InvalidCoordinate:
79 break;
80 case QGeoCoordinate::Coordinate2D:
82 break;
83 case QGeoCoordinate::Coordinate3D:
85 break;
86 }
87
88 //< fixType, used for navigation (1 = Fix not available; 2 = 2D; 3 = 3D)
89 //< coordinate().type(), returns 0 = Fix not available; 1 = 2D; 2 = 3D)
90 mLastGPSInformation.hacc = mInfo.attribute( QGeoPositionInfo::HorizontalAccuracy ); //< Horizontal dilution of precision
91 mLastGPSInformation.vacc = mInfo.attribute( QGeoPositionInfo::VerticalAccuracy ); //< Vertical dilution of precision
92
93 //TODO implement dop maybe by getting a
94 //http://developer.android.com/reference/android/location/GpsStatus.NmeaListener.html
95 //http://doc.qt.nokia.com/qtmobility-1.1/qnmeapositioninfosource.html
96 //into QtLocation and subclass QgsNMEAConnection directly?
97 //mLastGPSInformation.pdop; //< Dilution of precision
98 //mLastGPSInformation.hdop; //< Horizontal dilution of precision
99 //mLastGPSInformation.vdop; //< Vertical dilution of precision
100
101 //mLastGPSInformation.fixMode; //< Mode (M = Manual, forced to operate in 2D or 3D; A = Automatic, 3D/2D)
102 //mLastGPSInformation.quality; //< GPS quality indicator (0 = Invalid; 1 = Fix; 2 = Differential, 3 = Sensitive)
103 //mLastGPSInformation.status; //< Status (A = active or V = void)
104
106 QgsDebugMsgLevel( QStringLiteral( "Valid QGeoPositionInfo, positionUpdated" ), 2 );
107 }
108 }
109}
110
112 const QList<QGeoSatelliteInfo> &satellites )
113{
114 // The number of satellites in view is updated
116 for ( int i = 0; i < satellites.size(); ++i )
117 {
118 const QGeoSatelliteInfo currentSatellite = satellites.at( i );
119 QgsSatelliteInfo satelliteInfo;
120 satelliteInfo.azimuth = currentSatellite.attribute( QGeoSatelliteInfo::Azimuth );
121 satelliteInfo.elevation = currentSatellite.attribute( QGeoSatelliteInfo::Elevation );
122 satelliteInfo.id = currentSatellite.satelliteIdentifier();
123 satelliteInfo.signal = currentSatellite.signalStrength();
124 mLastGPSInformation.satellitesInView.append( satelliteInfo );
125 }
126 mLastGPSInformation.satInfoComplete = true; //to be used to determine when to graph signal and satellite position
128 QgsDebugMsgLevel( QStringLiteral( "satellitesInViewUpdated" ), 2 );
129}
130
132 const QList<QGeoSatelliteInfo> &satellites )
133{
134 // The number of satellites in use is updated
135 mLastGPSInformation.satellitesUsed = QString::number( satellites.count() ).toInt();
136
138 for ( const QGeoSatelliteInfo &currentSatellite : satellites )
139 {
140 //add pnr to mLastGPSInformation.satPrn
141 mLastGPSInformation.satPrn.append( currentSatellite.satelliteIdentifier() );
142
143 //set QgsSatelliteInfo.inuse to true for the satellites in use
145 {
146 if ( satInView.id == currentSatellite.satelliteIdentifier() )
147 {
148 satInView.inUse = true;
149 break;
150 }
151 }
152 }
153 mLastGPSInformation.satInfoComplete = true; //to be used to determine when to graph signal and satellite position
155 QgsDebugMsgLevel( QStringLiteral( "satellitesInUseUpdated" ), 2 );
156}
157
158void QgsQtLocationConnection::startGPS()
159{
160 QgsDebugMsgLevel( QStringLiteral( "Starting GPS QtLocation connection" ), 2 );
161 // Obtain the location data source if it is not obtained already
162 if ( !locationDataSource )
163 {
164 locationDataSource = QGeoPositionInfoSource::createDefaultSource( this );
165 if ( locationDataSource )
166 {
167 locationDataSource->setPreferredPositioningMethods( QGeoPositionInfoSource::SatellitePositioningMethods ); //QGeoPositionInfoSource::AllPositioningMethods
168 locationDataSource->setUpdateInterval( 1000 );
169 // Whenever the location data source signals that the current
170 // position is updated, the positionUpdated function is called.
171 QObject::connect( locationDataSource.data(),
172 &QGeoPositionInfoSource::positionUpdated,
173 this,
175 // Start listening for position updates
176 locationDataSource->startUpdates();
177 }
178 else
179 {
180 // Not able to obtain the location data source
181 QgsDebugError( QStringLiteral( "No QtLocation Position Source" ) );
182 }
183 }
184 else
185 {
186 // Start listening for position updates
187 locationDataSource->startUpdates();
188 }
189}
190
191void QgsQtLocationConnection::startSatelliteMonitor()
192{
193 QgsDebugMsgLevel( QStringLiteral( "Starting GPS QtLocation satellite monitor" ), 2 );
194
195 if ( !satelliteInfoSource )
196 {
197 satelliteInfoSource = QGeoSatelliteInfoSource::createDefaultSource( this );
198 if ( satelliteInfoSource )
199 {
200 QgsDebugMsgLevel( QStringLiteral( "satelliteMonitor started" ), 2 );
201 // Whenever the satellite info source signals that the number of
202 // satellites in use is updated, the satellitesInUseUpdated function
203 // is called
204 QObject::connect( satelliteInfoSource.data(),
205 &QGeoSatelliteInfoSource::satellitesInUseUpdated,
206 this,
208
209 // Whenever the satellite info source signals that the number of
210 // satellites in view is updated, the satellitesInViewUpdated function
211 // is called
212 QObject::connect( satelliteInfoSource.data(),
213 &QGeoSatelliteInfoSource::satellitesInViewUpdated,
214 this,
216
217 // Start listening for satellite updates
218 satelliteInfoSource->startUpdates();
219 }
220 else
221 {
222 // Not able to obtain the Satellite data source
223 QgsDebugError( QStringLiteral( "No QtLocation Satellite Source" ) );
224 }
225 }
226 else
227 {
228 // Start listening for position updates
229 satelliteInfoSource->startUpdates();
230 }
231}
@ 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.
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