QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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: Temporarely needed to workaround https://sourceforge.net/p/necessitas/tickets/147/
51 void 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
70  mLastGPSInformation.direction = mInfo.attribute( QGeoPositionInfo::Direction );
71  mLastGPSInformation.utcDateTime = mInfo.timestamp();
72  mLastGPSInformation.fixType = mInfo.coordinate().type() + 1;
73  //< fixType, used for navigation (1 = Fix not available; 2 = 2D; 3 = 3D)
74  //< coordinate().type(), returns 0 = Fix not available; 1 = 2D; 2 = 3D)
75  mLastGPSInformation.hacc = mInfo.attribute( QGeoPositionInfo::HorizontalAccuracy ); //< Horizontal dilution of precision
76  mLastGPSInformation.vacc = mInfo.attribute( QGeoPositionInfo::VerticalAccuracy ); //< Vertical dilution of precision
77 
78  //TODO implement dop maybe by getting a
79  //http://developer.android.com/reference/android/location/GpsStatus.NmeaListener.html
80  //http://doc.qt.nokia.com/qtmobility-1.1/qnmeapositioninfosource.html
81  //into QtLocation and subclass QgsNMEAConnection directly?
82  //mLastGPSInformation.pdop; //< Dilution of precision
83  //mLastGPSInformation.hdop; //< Horizontal dilution of precision
84  //mLastGPSInformation.vdop; //< Vertical dilution of precision
85 
86  //mLastGPSInformation.fixMode; //< Mode (M = Manual, forced to operate in 2D or 3D; A = Automatic, 3D/2D)
87  //mLastGPSInformation.quality; //< GPS quality indicator (0 = Invalid; 1 = Fix; 2 = Differential, 3 = Sensitive)
88  //mLastGPSInformation.status; //< Status (A = active or V = void)
89 
91  QgsDebugMsg( QStringLiteral( "Valid QGeoPositionInfo, positionUpdated" ) );
92  }
93  }
94 }
95 
97  const QList<QGeoSatelliteInfo> &satellites )
98 {
99  // The number of satellites in view is updated
101  for ( int i = 0; i < satellites.size(); ++i )
102  {
103  QGeoSatelliteInfo currentSatellite = satellites.at( i );
104  QgsSatelliteInfo satelliteInfo;
105  satelliteInfo.azimuth = currentSatellite.attribute( QGeoSatelliteInfo::Azimuth );
106  satelliteInfo.elevation = currentSatellite.attribute( QGeoSatelliteInfo::Elevation );
107 #if defined(HAVE_QT_MOBILITY_LOCATION )
108  satelliteInfo.id = currentSatellite.prnNumber();
109 #else // QtPositioning
110  satelliteInfo.id = currentSatellite.satelliteIdentifier();
111 #endif
112  satelliteInfo.signal = currentSatellite.signalStrength();
113  mLastGPSInformation.satellitesInView.append( satelliteInfo );
114  }
115  mLastGPSInformation.satInfoComplete = true; //to be used to determine when to graph signal and satellite position
117  QgsDebugMsg( QStringLiteral( "satellitesInViewUpdated" ) );
118 }
119 
121  const QList<QGeoSatelliteInfo> &satellites )
122 {
123  // The number of satellites in use is updated
124  mLastGPSInformation.satellitesUsed = QString::number( satellites.count() ).toInt();
125 
126  mLastGPSInformation.satPrn.clear();
127  for ( int i = 0; i < satellites.size(); ++i )
128  {
129  QGeoSatelliteInfo currentSatellite = satellites.at( i );
130  //add pnr to mLastGPSInformation.satPrn
131 #if defined(HAVE_QT_MOBILITY_LOCATION )
132  mLastGPSInformation.satPrn.append( currentSatellite.prnNumber() );
133 #else // QtPositioning
134  mLastGPSInformation.satPrn.append( currentSatellite.satelliteIdentifier() );
135 #endif
136 
137  //set QgsSatelliteInfo.inuse to true for the satellites in use
138  for ( int i = 0; i < mLastGPSInformation.satellitesInView.size(); ++i )
139  {
141 #if defined(HAVE_QT_MOBILITY_LOCATION )
142  if ( satInView.id == currentSatellite.prnNumber() )
143 #else // QtPositioning
144  if ( satInView.id == currentSatellite.satelliteIdentifier() )
145 #endif
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  QgsDebugMsg( QStringLiteral( "satellitesInUseUpdated" ) );
155 }
156 
157 void QgsQtLocationConnection::startGPS()
158 {
159  QgsDebugMsg( QStringLiteral( "Starting GPS QtLocation connection" ) );
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  QgsDebugMsg( QStringLiteral( "No QtLocation Position Source" ) );
181  }
182  }
183  else
184  {
185  // Start listening for position updates
186  locationDataSource->startUpdates();
187  }
188 }
189 
190 void QgsQtLocationConnection::startSatelliteMonitor()
191 {
192  QgsDebugMsg( QStringLiteral( "Starting GPS QtLocation satellite monitor" ) );
193 
194  if ( !satelliteInfoSource )
195  {
196  satelliteInfoSource = QGeoSatelliteInfoSource::createDefaultSource( this );
197  if ( satelliteInfoSource )
198  {
199  QgsDebugMsg( QStringLiteral( "satelliteMonitor started" ) );
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  QgsDebugMsg( QStringLiteral( "No QtLocation Satellite Source" ) );
223  }
224  }
225  else
226  {
227  // Start listening for position updates
228  satelliteInfoSource->startUpdates();
229  }
230 }
void satellitesInUseUpdated(const QList< QGeoSatelliteInfo > &satellites)
Called when the number of satellites in use is updated.
QgsGpsInformation mLastGPSInformation
Last state of the gps related variables (e.g. position, time, ...)
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
void positionUpdated(const QGeoPositionInfo &info)
Called when the position updated.
void parseData() override
Parse available data source content.
void satellitesInViewUpdated(const QList< QGeoSatelliteInfo > &satellites)
Called when the number of satellites in view is updated.
QList< QgsSatelliteInfo > satellitesInView
QList< int > satPrn
void stateChanged(const QgsGpsInformation &info)
void broadcastConnectionAvailable()
Needed to make QtLocation detected.
Status mStatus
Connection status.
Abstract base class for connection to a GPS device.