TerrainTile.h 4.1 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
    static constexpr double tileSizeDegrees         = 0.01;         ///< Each terrain tile represents a square area .01 degrees in lat/lon
    static constexpr double tileValueSpacingDegrees = 1.0 / 3600;   ///< 1 Arc-Second spacing of elevation values
    static constexpr double tileValueSpacingMeters  = 30.0;
97

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

108 109 110 111
    double  _swCornerClampedLatitude    (double latitude) const;
    double  _swCornerClampedLongitude   (double longitude) const;
    int     _latToDataIndex             (double latitude) const;
    int     _lonToDataIndex             (double longitude) const;
112

Andreas Bircher's avatar
Andreas Bircher committed
113 114 115
    QGeoCoordinate      _southWest;                                     /// South west corner of the tile
    QGeoCoordinate      _northEast;                                     /// North east corner of the tile

116 117
    int16_t             _minElevation;                                  /// Minimum elevation in tile
    int16_t             _maxElevation;                                  /// Maximum elevation in tile
118
    double              _avgElevation;                                  /// Average elevation of the tile
Andreas Bircher's avatar
Andreas Bircher committed
119

120 121 122
    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
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    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