TerrainTile.cc 7.12 KB
Newer Older
Andreas Bircher's avatar
Andreas Bircher committed
1 2
#include "TerrainTile.h"
#include "JsonHelper.h"
3
#include "QGCMapEngine.h"
Andreas Bircher's avatar
Andreas Bircher committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

QGC_LOGGING_CATEGORY(TerrainTileLog, "TerrainTileLog")

const char*  TerrainTile::_jsonStatusKey        = "status";
const char*  TerrainTile::_jsonDataKey          = "data";
const char*  TerrainTile::_jsonBoundsKey        = "bounds";
const char*  TerrainTile::_jsonSouthWestKey     = "sw";
const char*  TerrainTile::_jsonNorthEastKey     = "ne";
const char*  TerrainTile::_jsonStatsKey         = "stats";
const char*  TerrainTile::_jsonMaxElevationKey  = "max";
const char*  TerrainTile::_jsonMinElevationKey  = "min";
const char*  TerrainTile::_jsonAvgElevationKey  = "avg";
const char*  TerrainTile::_jsonCarpetKey        = "carpet";

TerrainTile::TerrainTile()
    : _minElevation(-1.0)
    , _maxElevation(-1.0)
    , _avgElevation(-1.0)
26 27 28
    , _data(NULL)
    , _gridSizeLat(-1)
    , _gridSizeLon(-1)
Andreas Bircher's avatar
Andreas Bircher committed
29 30 31 32 33 34 35
    , _isValid(false)
{

}

TerrainTile::~TerrainTile()
{
36 37 38 39
    if (_data) {
        for (int i = 0; i < _gridSizeLat; i++) {
            delete _data[i];
        }
Andreas Bircher's avatar
Andreas Bircher committed
40 41
        delete _data;
        _data = NULL;
42
    }
Andreas Bircher's avatar
Andreas Bircher committed
43 44
}

Andreas Bircher's avatar
Andreas Bircher committed
45
TerrainTile::TerrainTile(QJsonDocument document)
Andreas Bircher's avatar
Andreas Bircher committed
46 47 48 49 50
    : _minElevation(-1.0)
    , _maxElevation(-1.0)
    , _avgElevation(-1.0)
    , _isValid(false)
{
Andreas Bircher's avatar
Andreas Bircher committed
51
    if (!document.isObject()) {
Andreas Bircher's avatar
Andreas Bircher committed
52 53 54
        qCDebug(TerrainTileLog) << "Terrain tile json doc is no object";
        return;
    }
Andreas Bircher's avatar
Andreas Bircher committed
55
    QJsonObject rootObject = document.object();
Andreas Bircher's avatar
Andreas Bircher committed
56 57 58 59

    QString errorString;
    QList<JsonHelper::KeyValidateInfo> rootVersionKeyInfoList = {
        { _jsonStatusKey, QJsonValue::String, true },
60
        { _jsonDataKey, QJsonValue::Object, true },
Andreas Bircher's avatar
Andreas Bircher committed
61 62 63
    };
    if (!JsonHelper::validateKeys(rootObject, rootVersionKeyInfoList, errorString)) {
        qCDebug(TerrainTileLog) << "Error in reading json: " << errorString;
Andreas Bircher's avatar
Andreas Bircher committed
64
        return;
Andreas Bircher's avatar
Andreas Bircher committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78
    }

    if (rootObject[_jsonStatusKey].toString() != "success") {
        qCDebug(TerrainTileLog) << "Invalid terrain tile.";
        return;
    }
    const QJsonObject& dataObject = rootObject[_jsonDataKey].toObject();
    QList<JsonHelper::KeyValidateInfo> dataVersionKeyInfoList = {
        { _jsonBoundsKey, QJsonValue::Object, true },
        { _jsonStatsKey, QJsonValue::Object, true },
        { _jsonCarpetKey, QJsonValue::Array, true },
    };
    if (!JsonHelper::validateKeys(dataObject, dataVersionKeyInfoList, errorString)) {
        qCDebug(TerrainTileLog) << "Error in reading json: " << errorString;
Andreas Bircher's avatar
Andreas Bircher committed
79
        return;
Andreas Bircher's avatar
Andreas Bircher committed
80 81 82 83 84 85 86 87 88 89
    }

    // Bounds
    const QJsonObject& boundsObject = dataObject[_jsonBoundsKey].toObject();
    QList<JsonHelper::KeyValidateInfo> boundsVersionKeyInfoList = {
        { _jsonSouthWestKey, QJsonValue::Array, true },
        { _jsonNorthEastKey, QJsonValue::Array, true },
    };
    if (!JsonHelper::validateKeys(boundsObject, boundsVersionKeyInfoList, errorString)) {
        qCDebug(TerrainTileLog) << "Error in reading json: " << errorString;
Andreas Bircher's avatar
Andreas Bircher committed
90
        return;
Andreas Bircher's avatar
Andreas Bircher committed
91 92 93 94 95 96 97
    }
    const QJsonArray& swArray = boundsObject[_jsonSouthWestKey].toArray();
    const QJsonArray& neArray = boundsObject[_jsonNorthEastKey].toArray();
    if (swArray.count() < 2 || neArray.count() < 2 ) {
        qCDebug(TerrainTileLog) << "Incomplete bounding location";
        return;
    }
Andreas Bircher's avatar
Andreas Bircher committed
98 99 100 101
    _southWest.setLatitude(swArray[0].toDouble());
    _southWest.setLongitude(swArray[1].toDouble());
    _northEast.setLatitude(neArray[0].toDouble());
    _northEast.setLongitude(neArray[1].toDouble());
Andreas Bircher's avatar
Andreas Bircher committed
102 103

    // Stats
104
    const QJsonObject& statsObject = dataObject[_jsonStatsKey].toObject();
Andreas Bircher's avatar
Andreas Bircher committed
105 106 107 108 109 110 111
    QList<JsonHelper::KeyValidateInfo> statsVersionKeyInfoList = {
        { _jsonMaxElevationKey, QJsonValue::Double, true },
        { _jsonMinElevationKey, QJsonValue::Double, true },
        { _jsonAvgElevationKey, QJsonValue::Double, true },
    };
    if (!JsonHelper::validateKeys(statsObject, statsVersionKeyInfoList, errorString)) {
        qCDebug(TerrainTileLog) << "Error in reading json: " << errorString;
Andreas Bircher's avatar
Andreas Bircher committed
112
        return;
Andreas Bircher's avatar
Andreas Bircher committed
113 114 115 116 117 118 119
    }
    _maxElevation = statsObject[_jsonMaxElevationKey].toInt();
    _minElevation = statsObject[_jsonMinElevationKey].toInt();
    _avgElevation = statsObject[_jsonAvgElevationKey].toInt();

    // Carpet
    const QJsonArray& carpetArray = dataObject[_jsonCarpetKey].toArray();
120 121 122
    _gridSizeLat = carpetArray.count();
    qCDebug(TerrainTileLog) << "Received tile has size in latitude direction: " << carpetArray.count();
    for (int i = 0; i < _gridSizeLat; i++) {
Andreas Bircher's avatar
Andreas Bircher committed
123
        const QJsonArray& row = carpetArray[i].toArray();
124 125 126 127 128 129 130 131 132 133 134 135
        if (i == 0) {
            _gridSizeLon = row.count();
            qCDebug(TerrainTileLog) << "Received tile has size in longitued direction: " << row.count();
            if (_gridSizeLon > 0) {
                _data = new float*[_gridSizeLat];
            }
            for (int k = 0; k < _gridSizeLat; k++) {
                _data[k] = new float[_gridSizeLon];
            }
        }
        if (row.count() < _gridSizeLon) {
            qCDebug(TerrainTileLog) << "Expected row array of " << _gridSizeLon << ", instead got " << row.count();
Andreas Bircher's avatar
Andreas Bircher committed
136 137
            return;
        }
138
        for (int j = 0; j < _gridSizeLon; j++) {
Andreas Bircher's avatar
Andreas Bircher committed
139 140 141 142 143 144
            _data[i][j] = row[j].toDouble();
        }
    }
    _isValid = true;
}

Andreas Bircher's avatar
Andreas Bircher committed
145
bool TerrainTile::isIn(const QGeoCoordinate& coordinate) const
Andreas Bircher's avatar
Andreas Bircher committed
146 147 148 149 150
{
    if (!_isValid) {
        qCDebug(TerrainTileLog) << "isIn requested, but tile not valid";
        return false;
    }
151 152
    bool ret = coordinate.latitude() >= _southWest.latitude() && coordinate.longitude() >= _southWest.longitude() &&
               coordinate.latitude() <= _northEast.latitude() && coordinate.longitude() <= _northEast.longitude();
Andreas Bircher's avatar
Andreas Bircher committed
153
    qCDebug(TerrainTileLog) << "Checking isIn: " << coordinate << " , in sw " << _southWest << " , ne " << _northEast << ": " << ret;
Andreas Bircher's avatar
Andreas Bircher committed
154 155 156
    return ret;
}

Andreas Bircher's avatar
Andreas Bircher committed
157
float TerrainTile::elevation(const QGeoCoordinate& coordinate) const
Andreas Bircher's avatar
Andreas Bircher committed
158 159
{
    if (_isValid) {
Andreas Bircher's avatar
Andreas Bircher committed
160
        qCDebug(TerrainTileLog) << "elevation: " << coordinate << " , in sw " << _southWest << " , ne " << _northEast;
Andreas Bircher's avatar
Andreas Bircher committed
161
        // Get the index at resolution of 1 arc second
162 163 164
        int indexLat = _latToDataIndex(coordinate.latitude());
        int indexLon = _lonToDataIndex(coordinate.longitude());
        qCDebug(TerrainTileLog) << "indexLat:indexLon" << indexLat << indexLon << "elevation" << _data[indexLat][indexLon];
Andreas Bircher's avatar
Andreas Bircher committed
165 166 167 168 169 170
        return _data[indexLat][indexLon];
    } else {
        qCDebug(TerrainTileLog) << "Asking for elevation, but no valid data.";
        return -1.0;
    }
}
Andreas Bircher's avatar
Andreas Bircher committed
171 172 173 174 175

QGeoCoordinate TerrainTile::centerCoordinate(void) const
{
    return _southWest.atDistanceAndAzimuth(_southWest.distanceTo(_northEast) / 2.0, _southWest.azimuthTo(_northEast));
}
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

int TerrainTile::_latToDataIndex(double latitude) const
{
    if (isValid() && _southWest.isValid() && _northEast.isValid()) {
        return qRound((latitude - _southWest.latitude()) / (_northEast.latitude() - _southWest.latitude()) * (_gridSizeLat - 1));
    } else {
        return -1;
    }
}

int TerrainTile::_lonToDataIndex(double longitude) const
{
    if (isValid() && _southWest.isValid() && _northEast.isValid()) {
        return qRound((longitude - _southWest.longitude()) / (_northEast.longitude() - _southWest.longitude()) * (_gridSizeLon - 1));
    } else {
        return -1;
    }
}