TerrainTile.h 3.77 KB
Newer Older
Andreas Bircher's avatar
Andreas Bircher committed
1 2 3 4 5 6 7 8 9
#ifndef TERRAINTILE_H
#define TERRAINTILE_H

#include "QGCLoggingCategory.h"

#include <QGeoCoordinate>

Q_DECLARE_LOGGING_CATEGORY(TerrainTileLog)

10 11 12 13 14 15
/**
 * @brief The TerrainTile class
 *
 * Implements an interface for https://developers.airmap.com/v2.0/docs/elevation-api
 */

Andreas Bircher's avatar
Andreas Bircher committed
16 17 18 19 20 21 22 23 24
class TerrainTile
{
public:
    TerrainTile();
    ~TerrainTile();

    /**
    * Constructor from json doc with elevation data (either from file or web)
    *
Andreas Bircher's avatar
Andreas Bircher committed
25
    * @param document
Andreas Bircher's avatar
Andreas Bircher committed
26
    */
Andreas Bircher's avatar
Andreas Bircher committed
27
    TerrainTile(QJsonDocument document);
Andreas Bircher's avatar
Andreas Bircher committed
28

29 30 31 32 33 34 35
    /**
    * Constructor from serialized elevation data (either from file or web)
    *
    * @param document
    */
    TerrainTile(QByteArray byteArray);

Andreas Bircher's avatar
Andreas Bircher committed
36 37 38 39 40 41
    /**
    * Check for whether a coordinate lies within this tile
    *
    * @param coordinate
    * @return true if within
    */
Andreas Bircher's avatar
Andreas Bircher committed
42
    bool isIn(const QGeoCoordinate& coordinate) const;
Andreas Bircher's avatar
Andreas Bircher committed
43 44 45 46 47 48

    /**
    * Check whether valid data is loaded
    *
    * @return true if data is valid
    */
Andreas Bircher's avatar
Andreas Bircher committed
49
    bool isValid(void) const { return _isValid; }
Andreas Bircher's avatar
Andreas Bircher committed
50 51 52 53 54 55 56

    /**
    * Evaluates the elevation at the given coordinate
    *
    * @param coordinate
    * @return elevation
    */
Andreas Bircher's avatar
Andreas Bircher committed
57
    double elevation(const QGeoCoordinate& coordinate) const;
Andreas Bircher's avatar
Andreas Bircher committed
58 59 60 61 62 63

    /**
    * Accessor for the minimum elevation of the tile
    *
    * @return minimum elevation
    */
Andreas Bircher's avatar
Andreas Bircher committed
64
    double minElevation(void) const { return _minElevation; }
Andreas Bircher's avatar
Andreas Bircher committed
65 66 67 68 69 70

    /**
    * Accessor for the maximum elevation of the tile
    *
    * @return maximum elevation
    */
Andreas Bircher's avatar
Andreas Bircher committed
71
    double maxElevation(void) const { return _maxElevation; }
Andreas Bircher's avatar
Andreas Bircher committed
72 73 74 75 76 77

    /**
    * Accessor for the average elevation of the tile
    *
    * @return average elevation
    */
Andreas Bircher's avatar
Andreas Bircher committed
78
    double avgElevation(void) const { return _avgElevation; }
Andreas Bircher's avatar
Andreas Bircher committed
79

Andreas Bircher's avatar
Andreas Bircher committed
80 81 82 83 84 85 86
    /**
    * Accessor for the center coordinate
    *
    * @return center coordinate
    */
    QGeoCoordinate centerCoordinate(void) const;

87 88 89 90 91
    /**
    * Serialize data
    *
    * @return serialized data
    */
92
    static QByteArray serialize(QByteArray input);
93

94 95 96
    /// Approximate spacing of the elevation data measurement points
    static constexpr double terrainAltitudeSpacing = 30.0;

Andreas Bircher's avatar
Andreas Bircher committed
97
private:
98 99 100 101 102 103 104 105 106
    typedef struct {
        double  swLat,swLon, neLat, neLon;
        int16_t minElevation;
        int16_t maxElevation;
        double  avgElevation;
        int16_t gridSizeLat;
        int16_t gridSizeLon;
    } TileInfo_t;

107 108 109
    inline int _latToDataIndex(double latitude) const;
    inline int _lonToDataIndex(double longitude) const;

Andreas Bircher's avatar
Andreas Bircher committed
110 111 112
    QGeoCoordinate      _southWest;                                     /// South west corner of the tile
    QGeoCoordinate      _northEast;                                     /// North east corner of the tile

113 114
    int16_t             _minElevation;                                  /// Minimum elevation in tile
    int16_t             _maxElevation;                                  /// Maximum elevation in tile
115
    double              _avgElevation;                                  /// Average elevation of the tile
Andreas Bircher's avatar
Andreas Bircher committed
116

117 118 119
    int16_t**           _data;                                          /// 2D elevation data array
    int16_t             _gridSizeLat;                                   /// data grid size in latitude direction
    int16_t             _gridSizeLon;                                   /// data grid size in longitude direction
Andreas Bircher's avatar
Andreas Bircher committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    bool                _isValid;                                       /// data loaded is valid

    // Json keys
    static const char*  _jsonStatusKey;
    static const char*  _jsonDataKey;
    static const char*  _jsonBoundsKey;
    static const char*  _jsonSouthWestKey;
    static const char*  _jsonNorthEastKey;
    static const char*  _jsonStatsKey;
    static const char*  _jsonMaxElevationKey;
    static const char*  _jsonMinElevationKey;
    static const char*  _jsonAvgElevationKey;
    static const char*  _jsonCarpetKey;
};

#endif // TERRAINTILE_H