Skip to content

Commit

Permalink
Add GPS Component enum, useful for referring to specific pieces
Browse files Browse the repository at this point in the history
of GPS information in a generic way
  • Loading branch information
nyalldawson committed Nov 8, 2022
1 parent e41041b commit b436282
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
10 changes: 10 additions & 0 deletions python/core/auto_additions/qgis.py
Expand Up @@ -901,6 +901,16 @@
# --
Qgis.GpsQualityIndicator.baseClass = Qgis
# monkey patching scoped based enum
Qgis.GpsInformationComponent.Location.__doc__ = "2D location (latitude/longitude), as a QgsPointXY value"
Qgis.GpsInformationComponent.Altitude.__doc__ = "Altitude/elevation above or below the mean sea level"
Qgis.GpsInformationComponent.GroundSpeed.__doc__ = "Ground speed"
Qgis.GpsInformationComponent.Bearing.__doc__ = "Bearing measured in degrees clockwise from true north to the direction of travel"
Qgis.GpsInformationComponent.__doc__ = 'GPS information component.\n\n.. versionadded:: 3.30\n\n' + '* ``Location``: ' + Qgis.GpsInformationComponent.Location.__doc__ + '\n' + '* ``Altitude``: ' + Qgis.GpsInformationComponent.Altitude.__doc__ + '\n' + '* ``GroundSpeed``: ' + Qgis.GpsInformationComponent.GroundSpeed.__doc__ + '\n' + '* ``Bearing``: ' + Qgis.GpsInformationComponent.Bearing.__doc__
# --
Qgis.GpsInformationComponent.baseClass = Qgis
Qgis.GpsInformationComponents.baseClass = Qgis
GpsInformationComponents = Qgis # dirty hack since SIP seems to introduce the flags in module
# monkey patching scoped based enum
Qgis.BabelFormatCapability.Import.__doc__ = "Format supports importing"
Qgis.BabelFormatCapability.Export.__doc__ = "Format supports exporting"
Qgis.BabelFormatCapability.Waypoints.__doc__ = "Format supports waypoints"
Expand Down
7 changes: 7 additions & 0 deletions python/core/auto_generated/gps/qgsgpsconnection.sip.in
Expand Up @@ -148,6 +148,13 @@ Returns the fix status
Returns a descriptive string for the signal quality.

.. versionadded:: 3.16
%End

QVariant componentValue( Qgis::GpsInformationComponent component ) const;
%Docstring
Returns the value of the corresponding GPS information ``component``.

.. versionadded:: 3.30
%End

};
Expand Down
13 changes: 13 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -614,6 +614,17 @@ The development version
Simulation,
};

enum class GpsInformationComponent
{
Location,
Altitude,
GroundSpeed,
Bearing,
};

typedef QFlags<Qgis::GpsInformationComponent> GpsInformationComponents;


enum class BabelFormatCapability
{
Import,
Expand Down Expand Up @@ -1542,6 +1553,8 @@ QFlags<Qgis::RasterRendererFlag> operator|(Qgis::RasterRendererFlag f1, QFlags<Q

QFlags<Qgis::LabelingFlag> operator|(Qgis::LabelingFlag f1, QFlags<Qgis::LabelingFlag> f2);

QFlags<Qgis::GpsInformationComponent> operator|(Qgis::GpsInformationComponent f1, QFlags<Qgis::GpsInformationComponent> f2);




Expand Down
22 changes: 21 additions & 1 deletion src/core/gps/qgsgpsconnection.cpp
Expand Up @@ -119,9 +119,29 @@ QString QgsGpsInformation::qualityDescription() const
return QCoreApplication::translate( "QgsGpsInformation", "Invalid" );

case Qgis::GpsQualityIndicator::Unknown:
default:
return QCoreApplication::translate( "QgsGpsInformation", "Unknown (%1)" ).arg( QString::number( quality ) );
}
BUILTIN_UNREACHABLE
}

QVariant QgsGpsInformation::componentValue( Qgis::GpsInformationComponent component ) const
{
if ( !isValid() )
return QVariant();

switch ( component )
{
case Qgis::GpsInformationComponent::Location:
return QgsPointXY( longitude, latitude );

case Qgis::GpsInformationComponent::Altitude:
return elevation;
case Qgis::GpsInformationComponent::GroundSpeed:
return speed;
case Qgis::GpsInformationComponent::Bearing:
return std::isnan( direction ) ? QVariant() : direction;
}
BUILTIN_UNREACHABLE
}

QgsGpsConnection::QgsGpsConnection( QIODevice *dev )
Expand Down
7 changes: 7 additions & 0 deletions src/core/gps/qgsgpsconnection.h
Expand Up @@ -310,6 +310,13 @@ class CORE_EXPORT QgsGpsInformation
*/
QString qualityDescription() const;

/**
* Returns the value of the corresponding GPS information \a component.
*
* \since QGIS 3.30
*/
QVariant componentValue( Qgis::GpsInformationComponent component ) const;

private:

QMap< Qgis::GnssConstellation, Qgis::GpsFixStatus > mConstellationFixStatus;
Expand Down
23 changes: 23 additions & 0 deletions src/core/qgis.h
Expand Up @@ -1000,6 +1000,28 @@ class CORE_EXPORT Qgis
};
Q_ENUM( GpsQualityIndicator )

/**
* GPS information component.
*
* \since QGIS 3.30
*/
enum class GpsInformationComponent : int
{
Location = 1 << 0, //!< 2D location (latitude/longitude), as a QgsPointXY value
Altitude = 1 << 1, //!< Altitude/elevation above or below the mean sea level
GroundSpeed = 1 << 2, //!< Ground speed
Bearing = 1 << 3, //!< Bearing measured in degrees clockwise from true north to the direction of travel
};

/**
* GPS information component.
*
* \since QGIS 3.30
*/
Q_DECLARE_FLAGS( GpsInformationComponents, GpsInformationComponent )
Q_ENUM( GpsInformationComponent )
Q_FLAG( GpsInformationComponents )

/**
* Babel GPS format capabilities.
*
Expand Down Expand Up @@ -2584,6 +2606,7 @@ Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::RasterTemporalCapabilityFlags )
Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::SelectionFlags )
Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::RasterRendererFlags )
Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::LabelingFlags )
Q_DECLARE_OPERATORS_FOR_FLAGS( Qgis::GpsInformationComponents )

// hack to workaround warnings when casting void pointers
// retrieved from QLibrary::resolve to function pointers.
Expand Down
19 changes: 19 additions & 0 deletions tests/src/core/testqgsnmeaconnection.cpp
Expand Up @@ -72,6 +72,7 @@ class TestQgsNmeaConnection : public QgsTest
void testFixStatusAcrossConstellations();
void testConstellation();
void testPosition();
void testComponent();

};

Expand Down Expand Up @@ -410,5 +411,23 @@ void TestQgsNmeaConnection::testPosition()
QCOMPARE( connection.lastValidLocation(), QgsPoint( 19, 69, 35 ) );
}

void TestQgsNmeaConnection::testComponent()
{
ReplayNmeaConnection connection;

QgsGpsInformation info = connection.push( QStringLiteral( "$GPGGA,084112.185,6900.0,N,01800.0,E,1,04,1.4,35.0,M,29.4,M,,0000*63" ) );

QCOMPARE( info.componentValue( Qgis::GpsInformationComponent::Location ).value< QgsPointXY >(), QgsPointXY( 18, 69 ) );
QCOMPARE( info.componentValue( Qgis::GpsInformationComponent::Altitude ).toDouble(), 35 );
QCOMPARE( info.componentValue( Qgis::GpsInformationComponent::GroundSpeed ).toDouble(), 0 );
QCOMPARE( info.componentValue( Qgis::GpsInformationComponent::Bearing ).toDouble(), 0 );

info = connection.push( QStringLiteral( "$GPRMC,084111.185,A,6938.6531,N,01856.8527,E,0.16,2.00,220120,,,A*6E" ) );
QCOMPARE( info.componentValue( Qgis::GpsInformationComponent::Location ).value< QgsPointXY >(), QgsPointXY( 18.94754499999999808, 69.644218333333341779 ) );
QCOMPARE( info.componentValue( Qgis::GpsInformationComponent::Altitude ).toDouble(), 35 );
QCOMPARE( info.componentValue( Qgis::GpsInformationComponent::GroundSpeed ).toDouble(), 0.29632 );
QCOMPARE( info.componentValue( Qgis::GpsInformationComponent::Bearing ).toDouble(), 2 );
}

QGSTEST_MAIN( TestQgsNmeaConnection )
#include "testqgsnmeaconnection.moc"

0 comments on commit b436282

Please sign in to comment.