QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
qgsgpsconnection.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgpsconnection.cpp - description
3 --------------------
4 begin : November 30th, 2009
5 copyright : (C) 2009 by Marco Hugentobler
6 email : marco at hugis dot net
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
18#include "qgsgpsconnection.h"
19
22#include "qgssettingstree.h"
23
24#include <QIODevice>
25#include <QString>
26
27#include "moc_qgsgpsconnection.cpp"
28
29using namespace Qt::StringLiterals;
30
33
35 = new QgsSettingsEntryEnumFlag<Qt::TimeSpec>( u"timestamp-time-spec"_s, QgsSettingsTree::sTreeGps, Qt::TimeSpec::LocalTime, u"GPS time stamp specification"_s ) SIP_SKIP;
36
37const QgsSettingsEntryString *QgsGpsConnection::settingsGpsdHostName = new QgsSettingsEntryString( u"gpsd-host-name"_s, QgsSettingsTree::sTreeGps, QString(), u"GPSD connection host name"_s ) SIP_SKIP;
38
40
41const QgsSettingsEntryString *QgsGpsConnection::settingsGpsdDeviceName = new QgsSettingsEntryString( u"gpsd-device-name"_s, QgsSettingsTree::sTreeGps, QString(), u"GPSD connection device name"_s )
43
44const QgsSettingsEntryString *QgsGpsConnection::settingsGpsSerialDevice = new QgsSettingsEntryString( u"gpsd-serial-device"_s, QgsSettingsTree::sTreeGps, QString(), u"GPS serial device name"_s )
46
48 = new QgsSettingsEntryInteger( u"acquisition-interval"_s, QgsSettingsTree::sTreeGps, 0, u"GPS track point acquisition interval"_s ) SIP_SKIP;
49
50const QgsSettingsEntryDouble *QgsGpsConnection::settingGpsDistanceThreshold = new QgsSettingsEntryDouble( u"distance-threshold"_s, QgsSettingsTree::sTreeGps, 0, u"GPS track point distance threshold"_s )
52
54 = new QgsSettingsEntryBool( u"calculate-bearing-from-travel"_s, QgsSettingsTree::sTreeGps, false, u"Calculate GPS bearing from travel direction"_s ) SIP_SKIP;
55
57 = new QgsSettingsEntryBool( u"apply-leap-seconds-correction"_s, QgsSettingsTree::sTreeGps, true, u"Whether leap seconds corrections should be applied to GPS timestamps"_s ) SIP_SKIP;
58
59const QgsSettingsEntryInteger *QgsGpsConnection::settingGpsLeapSeconds = new QgsSettingsEntryInteger( u"leap-seconds"_s, QgsSettingsTree::sTreeGps, 18, u"Leap seconds correction amount (in seconds)"_s )
61
62const QgsSettingsEntryString *QgsGpsConnection::settingsGpsTimeStampTimeZone = new QgsSettingsEntryString( u"timestamp-time-zone"_s, QgsSettingsTree::sTreeGps, QString(), u"GPS time stamp time zone"_s )
64
66 = new QgsSettingsEntryInteger( u"timestamp-offset-from-utc"_s, QgsSettingsTree::sTreeGps, 0, u"GPS time stamp offset from UTC (in seconds)"_s ) SIP_SKIP;
67
69 : QObject( nullptr )
70 , mSource( dev )
71{
72 if ( mSource )
73 QObject::connect( mSource.get(), &QIODevice::readyRead, this, &QgsGpsConnection::parseData );
74
75 QObject::connect( this, &QgsGpsConnection::stateChanged, this, &QgsGpsConnection::onStateChanged );
76}
77
79{
80 cleanupSource();
81}
82
84{
85 if ( !mSource )
86 {
87 return false;
88 }
89
90 const bool connected = mSource->open( QIODevice::ReadWrite | QIODevice::Unbuffered );
91 if ( connected )
92 {
94 }
95 return connected;
96}
97
99{
100 if ( !mSource )
101 {
102 return false;
103 }
104
105 mSource->close();
106
107 if ( mLastFixStatus != Qgis::GpsFixStatus::NoData )
108 {
109 mLastFixStatus = Qgis::GpsFixStatus::NoData;
110 emit fixStatusChanged( mLastFixStatus );
111 }
112
113 return true;
114}
115
116void QgsGpsConnection::cleanupSource()
117{
118 if ( mSource )
119 {
120 mSource->close();
121 }
122 mSource.reset();
123
124 if ( mLastFixStatus != Qgis::GpsFixStatus::NoData )
125 {
126 mLastFixStatus = Qgis::GpsFixStatus::NoData;
127 emit fixStatusChanged( mLastFixStatus );
128 }
129}
130
131void QgsGpsConnection::setSource( QIODevice *source )
132{
133 cleanupSource();
134 mSource.reset( source );
135 QObject::connect( mSource.get(), &QIODevice::readyRead, this, &QgsGpsConnection::parseData );
136
137 clearLastGPSInformation();
138}
139
140void QgsGpsConnection::onStateChanged( const QgsGpsInformation &info )
141{
142 if ( info.isValid() )
143 {
144 const QgsPoint oldPosition = mLastLocation;
145 mLastLocation = QgsPoint( info.longitude, info.latitude, info.elevation );
146 if ( mLastLocation != oldPosition )
147 {
148 emit positionChanged( mLastLocation );
149 }
150 }
151
153 Qgis::GpsFixStatus bestFix = info.bestFixStatus( bestFixConstellation );
154 if ( bestFix != mLastFixStatus )
155 {
156 mLastFixStatus = bestFix;
157 emit fixStatusChanged( mLastFixStatus );
158 }
159}
160
161void QgsGpsConnection::clearLastGPSInformation()
162{
163 mLastGPSInformation = QgsGpsInformation();
164}
GnssConstellation
GNSS constellation.
Definition qgis.h:1983
@ Unknown
Unknown/other system.
Definition qgis.h:1984
GpsFixStatus
GPS fix status.
Definition qgis.h:1968
@ NoData
No fix data available.
Definition qgis.h:1969
@ Automatic
Automatically detected GPS device connection.
Definition qgis.h:1940
static const QgsSettingsEntryString * settingsGpsSerialDevice
Settings entry GPS serial device name.
static const QgsSettingsEntryEnumFlag< Qt::TimeSpec > * settingsGpsTimeStampSpecification
Settings entry time specification for GPS time stamps.
static const QgsSettingsEntryString * settingsGpsTimeStampTimeZone
Settings entry GPS time stamp time zone.
static const QgsSettingsEntryString * settingsGpsdHostName
Settings entry GPSD host name.
QgsGpsInformation mLastGPSInformation
Last state of the gps related variables (e.g. position, time, ...).
virtual void parseData()=0
Parse available data source content.
static const QgsSettingsEntryBool * settingGpsApplyLeapSecondsCorrection
Settings entry GPS apply leap seconds correction.
static const QgsSettingsEntryInteger * settingGpsLeapSeconds
Settings entry GPS leap seconds correction amount (in seconds).
void setSource(QIODevice *source)
Sets the GPS source. The class takes ownership of the device class.
~QgsGpsConnection() override
static const QgsSettingsEntryEnumFlag< Qgis::GpsConnectionType > * settingsGpsConnectionType
Settings entry GPS connection type.
bool connect()
Opens connection to device.
bool close()
Closes connection to device.
void positionChanged(const QgsPoint &point)
Emitted when the GPS position changes.
std::unique_ptr< QIODevice > mSource
Data source (e.g. serial device, socket, file,...).
static const QgsSettingsEntryInteger * settingsGpsTimeStampOffsetFromUtc
Settings entry GPS time offset from UTC in seconds.
void fixStatusChanged(Qgis::GpsFixStatus status)
Emitted when the GPS device fix status is changed.
static const QgsSettingsEntryBool * settingGpsBearingFromTravelDirection
Settings entry GPS calculate bearing from travel direction.
static const QgsSettingsEntryInteger * settingGpsAcquisitionInterval
Settings entry GPS track point acquisition interval.
static const QgsSettingsEntryDouble * settingGpsDistanceThreshold
Settings entry GPS track point distance threshold.
QgsGpsConnection(QIODevice *dev)
Constructor.
static const QgsSettingsEntryString * settingsGpsdDeviceName
Settings entry GPSD device name.
Status mStatus
Connection status.
void stateChanged(const QgsGpsInformation &info)
Emitted whenever the GPS state is changed.
static const QgsSettingsEntryInteger * settingsGpsdPortNumber
Settings entry GPSD port number.
Encapsulates information relating to a GPS position fix.
bool isValid() const
Returns whether the connection information is valid.
double latitude
Latitude in decimal degrees, using the WGS84 datum.
double longitude
Longitude in decimal degrees, using the WGS84 datum.
double elevation
Altitude (in meters) above or below the mean sea level.
Qgis::GpsFixStatus bestFixStatus(Qgis::GnssConstellation &constellation) const
Returns the best fix status and corresponding constellation.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
A boolean settings entry.
A double settings entry.
A template class for enum and flag settings entry.
An integer settings entry.
A string settings entry.
static QgsSettingsTreeNode * sTreeGps
#define SIP_SKIP
Definition qgis_sip.h:133