Skip to content

Commit

Permalink
Merge pull request #4681 from opengisch/move_orientation_into_positio…
Browse files Browse the repository at this point in the history
…ning

Move the magnetometer reading into the c++ positioning class, add @{position,gnss}_orientation variable
  • Loading branch information
nirvn authored Oct 24, 2023
2 parents dbcf903 + b18b691 commit c1ce192
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 115 deletions.
19 changes: 0 additions & 19 deletions src/core/appinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <QFileInfo>
#include <QImageReader>
#include <qgsapplication.h>
#include <qgsexiftools.h>
#include <qgsmessagelog.h>
#include <qgsruntimeprofiler.h>
#include <qgsziputils.h>
Expand Down Expand Up @@ -191,24 +190,6 @@ void AppInterface::closeSentry() const
#endif
}

void AppInterface::restrictImageSize( const QString &imagePath, int maximumWidthHeight )
{
QVariantMap metadata = QgsExifTools::readTags( imagePath );
QImage img( imagePath );
if ( !img.isNull() && ( img.width() > maximumWidthHeight || img.height() > maximumWidthHeight ) )
{
QImage scaledImage = img.width() > img.height()
? img.scaledToWidth( maximumWidthHeight, Qt::SmoothTransformation )
: img.scaledToHeight( maximumWidthHeight, Qt::SmoothTransformation );
scaledImage.save( imagePath );

for ( const QString key : metadata.keys() )
{
QgsExifTools::tagImage( imagePath, key, metadata[key] );
}
}
}

void AppInterface::importUrl( const QString &url )
{
QString sanitizedUrl = url.trimmed();
Expand Down
2 changes: 0 additions & 2 deletions src/core/appinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ class AppInterface : public QObject
*/
Q_INVOKABLE void closeSentry() const;

Q_INVOKABLE void restrictImageSize( const QString &imagePath, int maximumWidthHeight );

static void setInstance( AppInterface *instance ) { sAppInterface = instance; }
static AppInterface *instance() { return sAppInterface; }

Expand Down
9 changes: 5 additions & 4 deletions src/core/positioning/gnsspositioninformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
***************************************************************************/

#include "gnsspositioninformation.h"
#include "qgslogger.h"
#include "qgsnmeaconnection.h"

#include <QCoreApplication>
#include <QFileInfo>
Expand All @@ -28,7 +26,7 @@ GnssPositionInformation::GnssPositionInformation( double latitude, double longit
const QList<QgsSatelliteInfo> &satellitesInView, double pdop, double hdop, double vdop, double hacc, double vacc,
QDateTime utcDateTime, QChar fixMode, int fixType, int quality, int satellitesUsed, QChar status, const QList<int> &satPrn,
bool satInfoComplete, double verticalSpeed, double magneticVariation, int averagedCount, const QString &sourceName,
bool imuCorrection )
bool imuCorrection, double orientation )
: mLatitude( latitude )
, mLongitude( longitude )
, mElevation( elevation )
Expand All @@ -54,6 +52,7 @@ GnssPositionInformation::GnssPositionInformation( double latitude, double longit
, mAveragedCount( averagedCount )
, mSourceName( sourceName )
, mImuCorrection( imuCorrection )
, mOrientation( orientation )
{
}

Expand All @@ -78,7 +77,9 @@ bool GnssPositionInformation::operator==( const GnssPositionInformation &other )
mSatInfoComplete == other.mSatInfoComplete &&
mVerticalSpeed == other.mVerticalSpeed &&
mMagneticVariation == other.mMagneticVariation &&
mSourceName == other.mSourceName;
mSourceName == other.mSourceName &&
mImuCorrection== other.mImuCorrection &&
mOrientation == other.mOrientation;
// clang-format on
}

Expand Down
11 changes: 10 additions & 1 deletion src/core/positioning/gnsspositioninformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class GnssPositionInformation
Q_PROPERTY( int averagedCount READ averagedCount )
Q_PROPERTY( QString sourceName READ sourceName )
Q_PROPERTY( bool imuCorrection READ imuCorrection )
Q_PROPERTY( double orientation READ orientation )
Q_PROPERTY( double orientationValid READ orientationValid )

public:
/**
Expand All @@ -103,7 +105,7 @@ class GnssPositionInformation
double hacc = std::numeric_limits<double>::quiet_NaN(), double vacc = std::numeric_limits<double>::quiet_NaN(), QDateTime utcDateTime = QDateTime(),
QChar fixMode = QChar(), int fixType = 0, int quality = -1, int satellitesUsed = 0, QChar status = QChar(), const QList<int> &satPrn = QList<int>(), bool satInfoComplete = false,
double verticalSpeed = std::numeric_limits<double>::quiet_NaN(), double magneticVariation = std::numeric_limits<double>::quiet_NaN(), int averagedCount = 0, const QString &sourceName = QString(),
bool imuCorrection = false );
bool imuCorrection = false, double orientation = std::numeric_limits<double>::quiet_NaN() );

bool operator==( const GnssPositionInformation &other ) const;
bool operator!=( const GnssPositionInformation &other ) const { return !operator==( other ); }
Expand Down Expand Up @@ -268,6 +270,12 @@ class GnssPositionInformation
*/
bool imuCorrection() const { return mImuCorrection; }

/**
* Orientation (in degrees).
*/
double orientation() const { return mOrientation; }
bool orientationValid() const { return !std::isnan( mOrientation ); }

private:
double mLatitude = std::numeric_limits<double>::quiet_NaN();
double mLongitude = std::numeric_limits<double>::quiet_NaN();
Expand All @@ -294,6 +302,7 @@ class GnssPositionInformation
int mAveragedCount = 0;
QString mSourceName;
bool mImuCorrection;
double mOrientation = std::numeric_limits<double>::quiet_NaN();
};

Q_DECLARE_METATYPE( GnssPositionInformation )
Expand Down
77 changes: 75 additions & 2 deletions src/core/positioning/positioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@
#include "tcpreceiver.h"
#include "udpreceiver.h"

#include <QScreen>
#include <qgsapplication.h>
#include <qgsunittypes.h>

Positioning::Positioning( QObject *parent )
: QObject( parent )
{
// Setup internal gnss receiver by default
setupDevice();

// Setup the compass
mCompassTimer.setInterval( 200 );
connect( &mCompassTimer, &QTimer::timeout, this, &Positioning::processCompassReading );
}

void Positioning::setActive( bool active )
Expand All @@ -49,13 +55,26 @@ void Positioning::setActive( bool active )
setupDevice();
}
mReceiver->connectDevice();
#if QT_VERSION > QT_VERSION_CHECK( 6, 0, 0 )
if ( !QSensor::sensorsForType( QCompass::sensorType ).isEmpty() )
#else
if ( !QSensor::sensorsForType( QCompass::type ).isEmpty() )
#endif
{
mCompass.setActive( true );
mCompassTimer.start();
}
}
else
{
if ( mReceiver )
{
mReceiver->disconnectDevice();
}
mCompassTimer.stop();
mCompass.setActive( false );
mOrientation = std::numeric_limits<double>::quiet_NaN();
emit orientationChanged();
}

emit activeChanged();
Expand Down Expand Up @@ -230,15 +249,41 @@ void Positioning::lastGnssPositionInformationChanged( const GnssPositionInformat
if ( mPositionInformation == lastGnssPositionInformation )
return;

const GnssPositionInformation positionInformation( lastGnssPositionInformation.latitude(),
lastGnssPositionInformation.longitude(),
lastGnssPositionInformation.elevation(),
lastGnssPositionInformation.speed(),
lastGnssPositionInformation.direction(),
lastGnssPositionInformation.satellitesInView(),
lastGnssPositionInformation.pdop(),
lastGnssPositionInformation.hdop(),
lastGnssPositionInformation.vdop(),
lastGnssPositionInformation.hacc(),
lastGnssPositionInformation.vacc(),
lastGnssPositionInformation.utcDateTime(),
lastGnssPositionInformation.fixMode(),
lastGnssPositionInformation.fixType(),
lastGnssPositionInformation.quality(),
lastGnssPositionInformation.satellitesUsed(),
lastGnssPositionInformation.status(),
lastGnssPositionInformation.satPrn(),
lastGnssPositionInformation.satInfoComplete(),
lastGnssPositionInformation.verticalSpeed(),
lastGnssPositionInformation.magneticVariation(),
lastGnssPositionInformation.averagedCount(),
lastGnssPositionInformation.sourceName(),
lastGnssPositionInformation.imuCorrection(),
mOrientation );

if ( mAveragedPosition )
{
mCollectedPositionInformations << lastGnssPositionInformation;
mCollectedPositionInformations << positionInformation;
mPositionInformation = PositioningUtils::averagedPositionInformation( mCollectedPositionInformations );
emit averagedPositionCountChanged();
}
else
{
mPositionInformation = lastGnssPositionInformation;
mPositionInformation = positionInformation;
}

if ( mPositionInformation.isValid() )
Expand All @@ -258,6 +303,34 @@ void Positioning::lastGnssPositionInformationChanged( const GnssPositionInformat
emit positionInformationChanged();
}

void Positioning::processCompassReading()
{
if ( mCompass.reading() )
{
double orientation = 0.0;
// Take into account the orientation of the device
QScreen *screen = QgsApplication::instance()->primaryScreen();
switch ( screen->orientation() )
{
case Qt::LandscapeOrientation:
orientation = 90;
break;
case Qt::InvertedLandscapeOrientation:
orientation = 270;
break;
case Qt::PortraitOrientation:
default:
break;
}
orientation += mCompass.reading()->azimuth();
if ( mOrientation != orientation )
{
mOrientation = orientation;
emit orientationChanged();
}
}
}

QgsPoint Positioning::sourcePosition() const
{
return mSourcePosition;
Expand Down
14 changes: 14 additions & 0 deletions src/core/positioning/positioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include "gnsspositioninformation.h"
#include "qgsquickcoordinatetransformer.h"

#include <QCompass>
#include <QObject>
#include <QTimer>
#include <qgscoordinatereferencesystem.h>
#include <qgscoordinatetransformcontext.h>
#include <qgspoint.h>
Expand Down Expand Up @@ -53,6 +55,8 @@ class Positioning : public QObject
Q_PROPERTY( ElevationCorrectionMode elevationCorrectionMode READ elevationCorrectionMode WRITE setElevationCorrectionMode NOTIFY elevationCorrectionModeChanged )
Q_PROPERTY( double antennaHeight READ antennaHeight WRITE setAntennaHeight NOTIFY antennaHeightChanged )

Q_PROPERTY( double orientation READ orientation NOTIFY orientationChanged );

Q_PROPERTY( bool logging READ logging WRITE setLogging NOTIFY loggingChanged )

public:
Expand Down Expand Up @@ -179,6 +183,10 @@ class Positioning : public QObject
**/
void setAntennaHeight( double antennaHeight );

/**
* Returns the current device orientation
*/
double orientation() const { return mOrientation; }

/**
* Returns whether GNSS devices will log their incoming position stream into a logfile.
Expand All @@ -205,11 +213,13 @@ class Positioning : public QObject
void projectedPositionChanged();
void elevationCorrectionModeChanged();
void antennaHeightChanged();
void orientationChanged();
void loggingChanged();

private slots:

void lastGnssPositionInformationChanged( const GnssPositionInformation &lastGnssPositionInformation );
void processCompassReading();
void projectedPositionTransformed();

private:
Expand Down Expand Up @@ -240,6 +250,10 @@ class Positioning : public QObject
bool mLogging = false;

AbstractGnssReceiver *mReceiver = nullptr;

QCompass mCompass;
QTimer mCompassTimer;
double mOrientation = std::numeric_limits<double>::quiet_NaN();
};

Q_DECLARE_METATYPE( Positioning::ElevationCorrectionMode )
Expand Down
2 changes: 2 additions & 0 deletions src/core/utils/expressioncontextutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ QgsExpressionContextScope *ExpressionContextUtils::positionScope( const GnssPosi
const QDateTime timestamp = positionInformation.utcDateTime();
const qreal direction = positionInformation.direction();
const qreal groundSpeed = positionInformation.speed();
const qreal orientation = positionInformation.orientation();
const qreal magneticVariation = positionInformation.magneticVariation();
const qreal horizontalAccuracy = positionInformation.hacc();
const qreal verticalAccuracy = positionInformation.vacc();
Expand All @@ -60,6 +61,7 @@ QgsExpressionContextScope *ExpressionContextUtils::positionScope( const GnssPosi
addPositionVariable( scope, QStringLiteral( "timestamp" ), timestamp, positionLocked );
addPositionVariable( scope, QStringLiteral( "direction" ), direction, positionLocked );
addPositionVariable( scope, QStringLiteral( "ground_speed" ), groundSpeed, positionLocked );
addPositionVariable( scope, QStringLiteral( "orientation" ), orientation, positionLocked );
addPositionVariable( scope, QStringLiteral( "magnetic_variation" ), magneticVariation, positionLocked );
addPositionVariable( scope, QStringLiteral( "horizontal_accuracy" ), horizontalAccuracy, positionLocked );
addPositionVariable( scope, QStringLiteral( "vertical_accuracy" ), verticalAccuracy, positionLocked );
Expand Down
Loading

1 comment on commit c1ce192

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.