QGIS API Documentation 3.99.0-Master (8e76e220402)
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
32
33const QgsSettingsEntryEnumFlag<Qt::TimeSpec> *QgsGpsConnection::settingsGpsTimeStampSpecification = new QgsSettingsEntryEnumFlag<Qt::TimeSpec>( u"timestamp-time-spec"_s, QgsSettingsTree::sTreeGps, Qt::TimeSpec::LocalTime, u"GPS time stamp specification"_s ) SIP_SKIP;
34
35const QgsSettingsEntryString *QgsGpsConnection::settingsGpsdHostName = new QgsSettingsEntryString( u"gpsd-host-name"_s, QgsSettingsTree::sTreeGps, QString(), u"GPSD connection host name"_s ) SIP_SKIP;
36
38
39const QgsSettingsEntryString *QgsGpsConnection::settingsGpsdDeviceName = new QgsSettingsEntryString( u"gpsd-device-name"_s, QgsSettingsTree::sTreeGps, QString(), u"GPSD connection device name"_s ) SIP_SKIP;
40
41const QgsSettingsEntryString *QgsGpsConnection::settingsGpsSerialDevice = new QgsSettingsEntryString( u"gpsd-serial-device"_s, QgsSettingsTree::sTreeGps, QString(), u"GPS serial device name"_s ) SIP_SKIP;
42
43const QgsSettingsEntryInteger *QgsGpsConnection::settingGpsAcquisitionInterval = new QgsSettingsEntryInteger( u"acquisition-interval"_s, QgsSettingsTree::sTreeGps, 0, u"GPS track point acquisition interval"_s ) SIP_SKIP;
44
45const QgsSettingsEntryDouble *QgsGpsConnection::settingGpsDistanceThreshold = new QgsSettingsEntryDouble( u"distance-threshold"_s, QgsSettingsTree::sTreeGps, 0, u"GPS track point distance threshold"_s ) SIP_SKIP;
46
47const QgsSettingsEntryBool *QgsGpsConnection::settingGpsBearingFromTravelDirection = new QgsSettingsEntryBool( u"calculate-bearing-from-travel"_s, QgsSettingsTree::sTreeGps, false, u"Calculate GPS bearing from travel direction"_s ) SIP_SKIP;
48
49const QgsSettingsEntryBool *QgsGpsConnection::settingGpsApplyLeapSecondsCorrection = 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;
50
51const QgsSettingsEntryInteger *QgsGpsConnection::settingGpsLeapSeconds = new QgsSettingsEntryInteger( u"leap-seconds"_s, QgsSettingsTree::sTreeGps, 18, u"Leap seconds correction amount (in seconds)"_s ) SIP_SKIP;
52
53const QgsSettingsEntryString *QgsGpsConnection::settingsGpsTimeStampTimeZone = new QgsSettingsEntryString( u"timestamp-time-zone"_s, QgsSettingsTree::sTreeGps, QString(), u"GPS time stamp time zone"_s ) SIP_SKIP;
54
55const QgsSettingsEntryInteger *QgsGpsConnection::settingsGpsTimeStampOffsetFromUtc = new QgsSettingsEntryInteger( u"timestamp-offset-from-utc"_s, QgsSettingsTree::sTreeGps, 0, u"GPS time stamp offset from UTC (in seconds)"_s ) SIP_SKIP;
56
58 : QObject( nullptr )
59 , mSource( dev )
60{
61 if ( mSource )
62 QObject::connect( mSource.get(), &QIODevice::readyRead, this, &QgsGpsConnection::parseData );
63
64 QObject::connect( this, &QgsGpsConnection::stateChanged, this, &QgsGpsConnection::onStateChanged );
65}
66
68{
69 cleanupSource();
70}
71
73{
74 if ( !mSource )
75 {
76 return false;
77 }
78
79 const bool connected = mSource->open( QIODevice::ReadWrite | QIODevice::Unbuffered );
80 if ( connected )
81 {
83 }
84 return connected;
85}
86
88{
89 if ( !mSource )
90 {
91 return false;
92 }
93
94 mSource->close();
95
96 if ( mLastFixStatus != Qgis::GpsFixStatus::NoData )
97 {
98 mLastFixStatus = Qgis::GpsFixStatus::NoData;
99 emit fixStatusChanged( mLastFixStatus );
100 }
101
102 return true;
103}
104
105void QgsGpsConnection::cleanupSource()
106{
107 if ( mSource )
108 {
109 mSource->close();
110 }
111 mSource.reset();
112
113 if ( mLastFixStatus != Qgis::GpsFixStatus::NoData )
114 {
115 mLastFixStatus = Qgis::GpsFixStatus::NoData;
116 emit fixStatusChanged( mLastFixStatus );
117 }
118}
119
120void QgsGpsConnection::setSource( QIODevice *source )
121{
122 cleanupSource();
123 mSource.reset( source );
124 QObject::connect( mSource.get(), &QIODevice::readyRead, this, &QgsGpsConnection::parseData );
125
126 clearLastGPSInformation();
127}
128
129void QgsGpsConnection::onStateChanged( const QgsGpsInformation &info )
130{
131 if ( info.isValid() )
132 {
133 const QgsPoint oldPosition = mLastLocation;
134 mLastLocation = QgsPoint( info.longitude, info.latitude, info.elevation );
135 if ( mLastLocation != oldPosition )
136 {
137 emit positionChanged( mLastLocation );
138 }
139 }
140
142 Qgis::GpsFixStatus bestFix = info.bestFixStatus( bestFixConstellation );
143 if ( bestFix != mLastFixStatus )
144 {
145 mLastFixStatus = bestFix;
146 emit fixStatusChanged( mLastFixStatus );
147 }
148}
149
150void QgsGpsConnection::clearLastGPSInformation()
151{
152 mLastGPSInformation = QgsGpsInformation();
153}
GnssConstellation
GNSS constellation.
Definition qgis.h:1962
@ Unknown
Unknown/other system.
Definition qgis.h:1963
GpsFixStatus
GPS fix status.
Definition qgis.h:1947
@ NoData
No fix data available.
Definition qgis.h:1948
@ Automatic
Automatically detected GPS device connection.
Definition qgis.h:1919
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:134