TerrainQuery.h 11 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9 10 11
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#pragma once

12 13
#include "TerrainTile.h"
#include "QGCMapEngineData.h"
14 15 16 17 18 19 20
#include "QGCLoggingCategory.h"

#include <QObject>
#include <QGeoCoordinate>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QTimer>
21
#include <QtLocation/private/qgeotiledmapreply_p.h>
22 23

Q_DECLARE_LOGGING_CATEGORY(TerrainQueryLog)
DonLakeFlyer's avatar
DonLakeFlyer committed
24
Q_DECLARE_LOGGING_CATEGORY(TerrainQueryVerboseLog)
25 26 27

class TerrainAtCoordinateQuery;

28 29 30
/// Base class for offline/online terrain queries
class TerrainQueryInterface : public QObject
{
31 32 33
    Q_OBJECT

public:
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    TerrainQueryInterface(QObject* parent) : QObject(parent) { }

    /// Request terrain heights for specified coodinates.
    /// Signals: coordinateHeights when data is available
    virtual void requestCoordinateHeights(const QList<QGeoCoordinate>& coordinates) = 0;

    /// Requests terrain heights along the path specified by the two coordinates.
    /// Signals: pathHeights
    ///     @param coordinates to query
    virtual void requestPathHeights(const QGeoCoordinate& fromCoord, const QGeoCoordinate& toCoord) = 0;

    /// Request terrain heights for the rectangular area specified.
    /// Signals: carpetHeights when data is available
    ///     @param swCoord South-West bound of rectangular area to query
    ///     @param neCoord North-East bound of rectangular area to query
    ///     @param statsOnly true: Return only stats, no carpet data
    virtual void requestCarpetHeights(const QGeoCoordinate& swCoord, const QGeoCoordinate& neCoord, bool statsOnly) = 0;

signals:
DonLakeFlyer's avatar
DonLakeFlyer committed
53 54 55
    void coordinateHeightsReceived(bool success, QList<double> heights);
    void pathHeightsReceived(bool success, double latStep, double lonStep, const QList<double>& heights);
    void carpetHeightsReceived(bool success, double minHeight, double maxHeight, const QList<QList<double>>& carpet);
56
};
57

58 59 60 61 62
/// AirMap online implementation of terrain queries
class TerrainAirMapQuery : public TerrainQueryInterface {
    Q_OBJECT

public:
63
    TerrainAirMapQuery(QObject* parent = nullptr);
64

65
    // Overrides from TerrainQueryInterface
66 67 68
    void requestCoordinateHeights   (const QList<QGeoCoordinate>& coordinates) final;
    void requestPathHeights         (const QGeoCoordinate& fromCoord, const QGeoCoordinate& toCoord) final;
    void requestCarpetHeights       (const QGeoCoordinate& swCoord, const QGeoCoordinate& neCoord, bool statsOnly) final;
69 70

private slots:
71
    void _requestError              (QNetworkReply::NetworkError code);
72 73
    void _requestFinished           (void);
    void _sslErrors                 (const QList<QSslError> &errors);
74 75

private:
76 77 78 79 80
    void _sendQuery                 (const QString& path, const QUrlQuery& urlQuery);
    void _requestFailed             (void);
    void _parseCoordinateData       (const QJsonValue& coordinateJson);
    void _parsePathData             (const QJsonValue& pathJson);
    void _parseCarpetData           (const QJsonValue& carpetJson);
81 82 83 84 85 86 87 88 89 90

    enum QueryMode {
        QueryModeCoordinates,
        QueryModePath,
        QueryModeCarpet
    };

    QNetworkAccessManager   _networkManager;
    QueryMode               _queryMode;
    bool                    _carpetStatsOnly;
91 92
};

93
/// AirMap offline cachable implementation of terrain queries
94 95 96 97
class TerrainOfflineAirMapQuery : public TerrainQueryInterface {
    Q_OBJECT

public:
98
    TerrainOfflineAirMapQuery(QObject* parent = nullptr);
99 100 101 102 103 104

    // Overrides from TerrainQueryInterface
    void requestCoordinateHeights(const QList<QGeoCoordinate>& coordinates) final;
    void requestPathHeights(const QGeoCoordinate& fromCoord, const QGeoCoordinate& toCoord) final;
    void requestCarpetHeights(const QGeoCoordinate& swCoord, const QGeoCoordinate& neCoord, bool statsOnly) final;

105
    // Internal methods
106 107 108 109 110 111 112 113 114 115 116 117
    void _signalCoordinateHeights(bool success, QList<double> heights);
    void _signalPathHeights(bool success, double latStep, double lonStep, const QList<double>& heights);
    void _signalCarpetHeights(bool success, double minHeight, double maxHeight, const QList<QList<double>>& carpet);
};

/// Used internally by TerrainOfflineAirMapQuery to manage terrain tiles
class TerrainTileManager : public QObject {
    Q_OBJECT

public:
    TerrainTileManager(void);

118 119 120
    void addCoordinateQuery         (TerrainOfflineAirMapQuery* terrainQueryInterface, const QList<QGeoCoordinate>& coordinates);
    void addPathQuery               (TerrainOfflineAirMapQuery* terrainQueryInterface, const QGeoCoordinate& startPoint, const QGeoCoordinate& endPoint);
    bool getAltitudesForCoordinates (const QList<QGeoCoordinate>& coordinates, QList<double>& altitudes, bool& error);
121 122

private slots:
123
    void _terrainDone       (QByteArray responseBytes, QNetworkReply::NetworkError error);
124 125 126 127 128 129 130 131 132 133 134 135 136

private:
    enum class State {
        Idle,
        Downloading,
    };

    enum QueryMode {
        QueryModeCoordinates,
        QueryModePath,
        QueryModeCarpet
    };

Andreas Bircher's avatar
Andreas Bircher committed
137 138 139
    typedef struct {
        TerrainOfflineAirMapQuery*  terrainQueryInterface;
        QueryMode                   queryMode;
DonLakeFlyer's avatar
DonLakeFlyer committed
140
        double                      latStep, lonStep;
141
        QList<QGeoCoordinate>       coordinates;
Andreas Bircher's avatar
Andreas Bircher committed
142 143
    } QueuedRequestInfo_t;

144 145
    void    _tileFailed                         (void);
    QString _getTileHash                        (const QGeoCoordinate& coordinate);
146 147 148 149 150 151 152 153 154

    QList<QueuedRequestInfo_t>  _requestQueue;
    State                       _state = State::Idle;
    QNetworkAccessManager       _networkManager;

    QMutex                      _tilesMutex;
    QHash<QString, TerrainTile> _tiles;
};

155
/// Used internally by TerrainAtCoordinateQuery to batch coordinate requests together
156
class TerrainAtCoordinateBatchManager : public QObject {
157 158 159 160 161 162 163 164 165 166
    Q_OBJECT

public:
    TerrainAtCoordinateBatchManager(void);

    void addQuery(TerrainAtCoordinateQuery* terrainAtCoordinateQuery, const QList<QGeoCoordinate>& coordinates);

private slots:
    void _sendNextBatch         (void);
    void _queryObjectDestroyed  (QObject* elevationProvider);
167
    void _coordinateHeights     (bool success, QList<double> heights);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

private:
    typedef struct {
        TerrainAtCoordinateQuery*   terrainAtCoordinateQuery;
        QList<QGeoCoordinate>       coordinates;
    } QueuedRequestInfo_t;

    typedef struct {
        TerrainAtCoordinateQuery*   terrainAtCoordinateQuery;
        bool                        queryObjectDestroyed;
        int                         cCoord;
    } SentRequestInfo_t;


    enum class State {
        Idle,
        Downloading,
    };

    void _batchFailed(void);
    QString _stateToString(State state);

    QList<QueuedRequestInfo_t>  _requestQueue;
    QList<SentRequestInfo_t>    _sentRequests;
    State                       _state = State::Idle;
    const int                   _batchTimeout = 500;
    QTimer                      _batchTimer;
195
    TerrainOfflineAirMapQuery   _terrainQuery;
196 197 198 199 200 201 202
};

/// NOTE: TerrainAtCoordinateQuery is not thread safe. All instances/calls to ElevationProvider must be on main thread.
class TerrainAtCoordinateQuery : public QObject
{
    Q_OBJECT
public:
203
    TerrainAtCoordinateQuery(QObject* parent = nullptr);
204

DonLakeFlyer's avatar
DonLakeFlyer committed
205 206 207
    /// Async terrain query for a list of lon,lat coordinates. When the query is done, the terrainData() signal
    /// is emitted.
    ///     @param coordinates to query
208 209
    void requestData(const QList<QGeoCoordinate>& coordinates);

210 211 212 213 214
    /// Either returns altitudes from cache or queues database request
    ///     @param[out] error true: altitude not returned due to error, false: altitudes returned
    /// @return true: altitude returned (check error as well), false: database query queued (altitudes not returned)
    static bool getAltitudesForCoordinates(const QList<QGeoCoordinate>& coordinates, QList<double>& altitudes, bool& error);

215
    // Internal method
216
    void _signalTerrainData(bool success, QList<double>& heights);
217 218

signals:
DonLakeFlyer's avatar
DonLakeFlyer committed
219
    void terrainDataReceived(bool success, QList<double> heights);
220 221
};

222
class TerrainPathQuery : public QObject
223 224 225 226
{
    Q_OBJECT

public:
227
    TerrainPathQuery(QObject* parent = nullptr);
228

DonLakeFlyer's avatar
DonLakeFlyer committed
229 230 231
    /// Async terrain query for terrain heights between two lat/lon coordinates. When the query is done, the terrainData() signal
    /// is emitted.
    ///     @param coordinates to query
232 233
    void requestData(const QGeoCoordinate& fromCoord, const QGeoCoordinate& toCoord);

234 235 236
    typedef struct {
        double          latStep;    ///< Amount of latitudinal distance between each returned height
        double          lonStep;    ///< Amount of longitudinal distance between each returned height
237
        QList<double>   heights;    ///< Terrain heights along path
238 239 240 241
    } PathHeightInfo_t;

signals:
    /// Signalled when terrain data comes back from server
DonLakeFlyer's avatar
DonLakeFlyer committed
242
    void terrainDataReceived(bool success, const PathHeightInfo_t& pathHeightInfo);
243

244 245 246 247
private slots:
    void _pathHeights(bool success, double latStep, double lonStep, const QList<double>& heights);

private:
248
    TerrainOfflineAirMapQuery _terrainQuery;
249 250 251 252 253 254 255 256 257
};

Q_DECLARE_METATYPE(TerrainPathQuery::PathHeightInfo_t)

class TerrainPolyPathQuery : public QObject
{
    Q_OBJECT

public:
258
    TerrainPolyPathQuery(QObject* parent = nullptr);
259

DonLakeFlyer's avatar
DonLakeFlyer committed
260 261 262
    /// Async terrain query for terrain heights for the paths between each specified QGeoCoordinate.
    /// When the query is done, the terrainData() signal is emitted.
    ///     @param polyPath List of QGeoCoordinate
263 264
    void requestData(const QVariantList& polyPath);
    void requestData(const QList<QGeoCoordinate>& polyPath);
265 266 267

signals:
    /// Signalled when terrain data comes back from server
DonLakeFlyer's avatar
DonLakeFlyer committed
268
    void terrainDataReceived(bool success, const QList<TerrainPathQuery::PathHeightInfo_t>& rgPathHeightInfo);
269 270 271 272 273 274 275 276 277

private slots:
    void _terrainDataReceived(bool success, const TerrainPathQuery::PathHeightInfo_t& pathHeightInfo);

private:
    int                                         _curIndex;
    QList<QGeoCoordinate>                       _rgCoords;
    QList<TerrainPathQuery::PathHeightInfo_t>   _rgPathHeightInfo;
    TerrainPathQuery                            _pathQuery;
278
};
279

DonLakeFlyer's avatar
DonLakeFlyer committed
280

281
class TerrainCarpetQuery : public QObject
DonLakeFlyer's avatar
DonLakeFlyer committed
282 283 284 285
{
    Q_OBJECT

public:
286
    TerrainCarpetQuery(QObject* parent = nullptr);
DonLakeFlyer's avatar
DonLakeFlyer committed
287 288 289 290 291 292 293 294 295 296

    /// Async terrain query for terrain information bounded by the specifed corners.
    /// When the query is done, the terrainData() signal is emitted.
    ///     @param swCoord South-West bound of rectangular area to query
    ///     @param neCoord North-East bound of rectangular area to query
    ///     @param statsOnly true: Return only stats, no carpet data
    void requestData(const QGeoCoordinate& swCoord, const QGeoCoordinate& neCoord, bool statsOnly);

signals:
    /// Signalled when terrain data comes back from server
DonLakeFlyer's avatar
DonLakeFlyer committed
297
    void terrainDataReceived(bool success, double minHeight, double maxHeight, const QList<QList<double>>& carpet);
DonLakeFlyer's avatar
DonLakeFlyer committed
298 299

private:
300
    TerrainAirMapQuery _terrainQuery;
DonLakeFlyer's avatar
DonLakeFlyer committed
301 302
};