QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsquicksimulatedpositionsource.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsquicksimulatedpositionsource.cpp
3  --------------------------------------
4  Date : Dec. 2017
5  Copyright : (C) 2017 Peter Petrik
6  Email : zilolv at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include <QTimer>
19 
21 
22 QgsQuickSimulatedPositionSource::QgsQuickSimulatedPositionSource( QObject *parent, double longitude, double latitude, double flightRadius )
23  : QGeoPositionInfoSource( parent )
24  , mTimer( qgis::make_unique< QTimer >() )
25  , mFlightRadius( flightRadius )
26  , mLongitude( longitude )
27  , mLatitude( latitude )
28 {
29  connect( mTimer.get(), &QTimer::timeout, this, &QgsQuickSimulatedPositionSource::readNextPosition );
30 }
31 
32 void QgsQuickSimulatedPositionSource::startUpdates()
33 {
34  int interval = updateInterval();
35  if ( interval < minimumUpdateInterval() )
36  interval = minimumUpdateInterval();
37 
38  mTimer->start( interval );
39  readNextPosition();
40 }
41 
42 void QgsQuickSimulatedPositionSource::stopUpdates()
43 {
44  mTimer->stop();
45 }
46 
47 void QgsQuickSimulatedPositionSource::requestUpdate( int /*timeout*/ )
48 {
49  readNextPosition();
50 }
51 
52 
53 
54 void QgsQuickSimulatedPositionSource::readNextPosition()
55 {
56  if ( mFlightRadius <= 0 )
57  readConstantPosition();
58  else
59  readRandomPosition();
60 }
61 
62 void QgsQuickSimulatedPositionSource::readRandomPosition()
63 {
64  double latitude = mLatitude, longitude = mLongitude;
65  latitude += sin( mAngle * M_PI / 180 ) * mFlightRadius;
66  longitude += cos( mAngle * M_PI / 180 ) * mFlightRadius;
67  mAngle += 1;
68 
69  QGeoCoordinate coordinate( latitude, longitude );
70  double altitude = std::rand() % 40 + 20; // rand altitude <20,55>m and lost (0)
71  if ( altitude <= 55 )
72  {
73  coordinate.setAltitude( altitude ); // 3D
74  }
75 
76  QDateTime timestamp = QDateTime::currentDateTime();
77 
78  QGeoPositionInfo info( coordinate, timestamp );
79  if ( info.isValid() )
80  {
81  mLastPosition = info;
82  info.setAttribute( QGeoPositionInfo::Direction, 360 - int( mAngle ) % 360 );
83  int accuracy = std::rand() % 40 + 20; // rand accuracy <20,55>m and lost (-1)
84  if ( accuracy > 55 )
85  {
86  accuracy = -1;
87  }
88  info.setAttribute( QGeoPositionInfo::HorizontalAccuracy, accuracy );
89  emit positionUpdated( info );
90  }
91 }
92 
93 void QgsQuickSimulatedPositionSource::readConstantPosition()
94 {
95  QGeoCoordinate coordinate( mLatitude, mLongitude );
96  coordinate.setAltitude( 20 );
97  QDateTime timestamp = QDateTime::currentDateTime();
98  QGeoPositionInfo info( coordinate, timestamp );
99  info.setAttribute( QGeoPositionInfo::Direction, 0 );
100  info.setAttribute( QGeoPositionInfo::HorizontalAccuracy, 20 );
101  emit positionUpdated( info );
102 }
103 
qgsquicksimulatedpositionsource.h