Terrain.h 2.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/****************************************************************************
 *
 *   (c) 2017 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

#pragma once

Andreas Bircher's avatar
Andreas Bircher committed
12 13 14
#include "TerrainTile.h"
#include "QGCLoggingCategory.h"

15 16 17
#include <QObject>
#include <QGeoCoordinate>
#include <QNetworkAccessManager>
Andreas Bircher's avatar
Andreas Bircher committed
18 19
#include <QHash>
#include <QMutex>
Andreas Bircher's avatar
Andreas Bircher committed
20

21 22 23 24 25 26 27 28 29 30
/* usage example:
    ElevationProvider *p = new ElevationProvider();
    QList<QGeoCoordinate> coordinates;
    QGeoCoordinate c(47.379243, 8.548265);
    coordinates.push_back(c);
    c.setLatitude(c.latitude()+0.01);
    coordinates.push_back(c);
    p->queryTerrainData(coordinates);
 */

Andreas Bircher's avatar
Andreas Bircher committed
31
Q_DECLARE_LOGGING_CATEGORY(TerrainLog)
32 33 34 35 36 37 38 39 40

class ElevationProvider : public QObject
{
    Q_OBJECT
public:
    ElevationProvider(QObject* parent = NULL);

    /**
     * Async elevation query for a list of lon,lat coordinates. When the query is done, the terrainData() signal
Andreas Bircher's avatar
Andreas Bircher committed
41 42 43 44 45 46 47 48 49
     * is emitted. This call directly looks elevations up online.
     * @param coordinates
     * @return true on success
     */
    bool queryTerrainDataPoints(const QList<QGeoCoordinate>& coordinates);

    /**
     * Async elevation query for a list of lon,lat coordinates. When the query is done, the terrainData() signal
     * is emitted. This call caches local elevation tables for faster lookup in the future.
50 51 52 53 54
     * @param coordinates
     * @return true on success
     */
    bool queryTerrainData(const QList<QGeoCoordinate>& coordinates);

Andreas Bircher's avatar
Andreas Bircher committed
55
    /**
Andreas Bircher's avatar
Andreas Bircher committed
56
     * Cache all data in rectangular region given by south west and north east corner.
Andreas Bircher's avatar
Andreas Bircher committed
57
     *
Andreas Bircher's avatar
Andreas Bircher committed
58 59
     * @param southWest
     * @param northEast
60
     * @return true on successful scheduling for download
Andreas Bircher's avatar
Andreas Bircher committed
61
     */
62
    bool cacheTerrainTiles(const QGeoCoordinate& southWest, const QGeoCoordinate& northEast);
Andreas Bircher's avatar
Andreas Bircher committed
63

64
signals:
Andreas Bircher's avatar
Andreas Bircher committed
65
    /// signal returning requested elevation data
66 67 68
    void terrainData(bool success, QList<float> altitudes);

private slots:
Andreas Bircher's avatar
Andreas Bircher committed
69 70
    void _requestFinished();                                    /// slot to handle download of elevation of list of coordinates
    void _requestFinishedTile();                                /// slot to handle download of elevation tiles
Andreas Bircher's avatar
Andreas Bircher committed
71

72 73
private:

Andreas Bircher's avatar
Andreas Bircher committed
74 75
    QString _uniqueTileId(const QGeoCoordinate& coordinate);    /// Method to create a unique string for each tile. Format: south_west_north_east as floats.
    void    _downloadTiles(void);                               /// Method to trigger download of queued tiles, eventually emitting the requested altitudes (if any).
Andreas Bircher's avatar
Andreas Bircher committed
76

77 78 79 80 81
    enum class State {
        Idle,
        Downloading,
    };

Andreas Bircher's avatar
Andreas Bircher committed
82 83
    State                       _state = State::Idle;
    QNetworkAccessManager       _networkManager;
Andreas Bircher's avatar
Andreas Bircher committed
84
    QList<QGeoCoordinate>       _coordinates;
Andreas Bircher's avatar
Andreas Bircher committed
85 86 87

    static QMutex                       _tilesMutex;
    static QHash<QString, TerrainTile>  _tiles;
Andreas Bircher's avatar
Andreas Bircher committed
88
    static QStringList                  _downloadQueue;
89
};