Commit 17fe7893 authored by Andreas Bircher's avatar Andreas Bircher

fixes

parent 54c7e7dd
...@@ -20,6 +20,10 @@ ...@@ -20,6 +20,10 @@
QGC_LOGGING_CATEGORY(TerrainLog, "TerrainLog") QGC_LOGGING_CATEGORY(TerrainLog, "TerrainLog")
QMutex ElevationProvider::_tilesMutex;
QHash<QString, TerrainTile> ElevationProvider::_tiles;
QStringList ElevationProvider::_downloadQueue;
ElevationProvider::ElevationProvider(QObject* parent) ElevationProvider::ElevationProvider(QObject* parent)
: QObject(parent) : QObject(parent)
{ {
...@@ -101,7 +105,7 @@ bool ElevationProvider::cacheTerrainTiles(const QList<QGeoCoordinate>& coordinat ...@@ -101,7 +105,7 @@ bool ElevationProvider::cacheTerrainTiles(const QList<QGeoCoordinate>& coordinat
QString uniqueTileId = _uniqueTileId(coordinate); QString uniqueTileId = _uniqueTileId(coordinate);
_tilesMutex.lock(); _tilesMutex.lock();
if (_downloadQueue.contains(uniqueTileId) || _tiles.contains(uniqueTileId)) { if (_downloadQueue.contains(uniqueTileId) || _tiles.contains(uniqueTileId)) {
continue continue;
} }
_downloadQueue.append(uniqueTileId.replace("-", ",")); _downloadQueue.append(uniqueTileId.replace("-", ","));
_tilesMutex.unlock(); _tilesMutex.unlock();
...@@ -209,7 +213,7 @@ void ElevationProvider::_downloadTiles(void) ...@@ -209,7 +213,7 @@ void ElevationProvider::_downloadTiles(void)
QNetworkReply* networkReply = _networkManager.get(request); QNetworkReply* networkReply = _networkManager.get(request);
if (!networkReply) { if (!networkReply) {
return false; return;
} }
connect(networkReply, &QNetworkReply::finished, this, &ElevationProvider::_requestFinishedTile); connect(networkReply, &QNetworkReply::finished, this, &ElevationProvider::_requestFinishedTile);
...@@ -229,7 +233,7 @@ QString ElevationProvider::_uniqueTileId(const QGeoCoordinate& coordinate) ...@@ -229,7 +233,7 @@ QString ElevationProvider::_uniqueTileId(const QGeoCoordinate& coordinate)
QString ret = QString::number(southEast.latitude(), 'f', 6) + "-" + QString::number(southEast.longitude(), 'f', 6) + "-" + QString ret = QString::number(southEast.latitude(), 'f', 6) + "-" + QString::number(southEast.longitude(), 'f', 6) + "-" +
QString::number(northEast.latitude(), 'f', 6) + "-" + QString::number(northEast.longitude(), 'f', 6); QString::number(northEast.latitude(), 'f', 6) + "-" + QString::number(northEast.longitude(), 'f', 6);
qCDebug << "Computing unique tile id for " << coordinate << ret; qCDebug(TerrainLog) << "Computing unique tile id for " << coordinate << ret;
return ret; return ret;
} }
...@@ -50,7 +50,7 @@ public: ...@@ -50,7 +50,7 @@ public:
* @param southWest * @param southWest
* @param northEast * @param northEast
*/ */
void cacheTerrainData(const QGeoCoordinate& southWest, const QGeoCoordinate& northEast); bool cacheTerrainTiles(const QList<QGeoCoordinate>& coordinates);
signals: signals:
void terrainData(bool success, QList<float> altitudes); void terrainData(bool success, QList<float> altitudes);
...@@ -74,5 +74,5 @@ private: ...@@ -74,5 +74,5 @@ private:
static QMutex _tilesMutex; static QMutex _tilesMutex;
static QHash<QString, TerrainTile> _tiles; static QHash<QString, TerrainTile> _tiles;
QStringList _downloadQueue; static QStringList _downloadQueue;
}; };
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
QGC_LOGGING_CATEGORY(TerrainTileLog, "TerrainTileLog") QGC_LOGGING_CATEGORY(TerrainTileLog, "TerrainTileLog")
const double TerrainTile::_srtm1TileSize = 0.025;
const char* TerrainTile::_jsonStatusKey = "status"; const char* TerrainTile::_jsonStatusKey = "status";
const char* TerrainTile::_jsonDataKey = "data"; const char* TerrainTile::_jsonDataKey = "data";
const char* TerrainTile::_jsonBoundsKey = "bounds"; const char* TerrainTile::_jsonBoundsKey = "bounds";
...@@ -50,7 +51,7 @@ TerrainTile::TerrainTile(QJsonDocument document) ...@@ -50,7 +51,7 @@ TerrainTile::TerrainTile(QJsonDocument document)
}; };
if (!JsonHelper::validateKeys(rootObject, rootVersionKeyInfoList, errorString)) { if (!JsonHelper::validateKeys(rootObject, rootVersionKeyInfoList, errorString)) {
qCDebug(TerrainTileLog) << "Error in reading json: " << errorString; qCDebug(TerrainTileLog) << "Error in reading json: " << errorString;
return false; return;
} }
if (rootObject[_jsonStatusKey].toString() != "success") { if (rootObject[_jsonStatusKey].toString() != "success") {
...@@ -65,7 +66,7 @@ TerrainTile::TerrainTile(QJsonDocument document) ...@@ -65,7 +66,7 @@ TerrainTile::TerrainTile(QJsonDocument document)
}; };
if (!JsonHelper::validateKeys(dataObject, dataVersionKeyInfoList, errorString)) { if (!JsonHelper::validateKeys(dataObject, dataVersionKeyInfoList, errorString)) {
qCDebug(TerrainTileLog) << "Error in reading json: " << errorString; qCDebug(TerrainTileLog) << "Error in reading json: " << errorString;
return false; return;
} }
// Bounds // Bounds
...@@ -76,7 +77,7 @@ TerrainTile::TerrainTile(QJsonDocument document) ...@@ -76,7 +77,7 @@ TerrainTile::TerrainTile(QJsonDocument document)
}; };
if (!JsonHelper::validateKeys(boundsObject, boundsVersionKeyInfoList, errorString)) { if (!JsonHelper::validateKeys(boundsObject, boundsVersionKeyInfoList, errorString)) {
qCDebug(TerrainTileLog) << "Error in reading json: " << errorString; qCDebug(TerrainTileLog) << "Error in reading json: " << errorString;
return false; return;
} }
const QJsonArray& swArray = boundsObject[_jsonSouthWestKey].toArray(); const QJsonArray& swArray = boundsObject[_jsonSouthWestKey].toArray();
const QJsonArray& neArray = boundsObject[_jsonNorthEastKey].toArray(); const QJsonArray& neArray = boundsObject[_jsonNorthEastKey].toArray();
...@@ -98,7 +99,7 @@ TerrainTile::TerrainTile(QJsonDocument document) ...@@ -98,7 +99,7 @@ TerrainTile::TerrainTile(QJsonDocument document)
}; };
if (!JsonHelper::validateKeys(statsObject, statsVersionKeyInfoList, errorString)) { if (!JsonHelper::validateKeys(statsObject, statsVersionKeyInfoList, errorString)) {
qCDebug(TerrainTileLog) << "Error in reading json: " << errorString; qCDebug(TerrainTileLog) << "Error in reading json: " << errorString;
return false; return;
} }
_maxElevation = statsObject[_jsonMaxElevationKey].toInt(); _maxElevation = statsObject[_jsonMaxElevationKey].toInt();
_minElevation = statsObject[_jsonMinElevationKey].toInt(); _minElevation = statsObject[_jsonMinElevationKey].toInt();
...@@ -129,28 +130,33 @@ bool TerrainTile::isIn(const QGeoCoordinate& coordinate) const ...@@ -129,28 +130,33 @@ bool TerrainTile::isIn(const QGeoCoordinate& coordinate) const
qCDebug(TerrainTileLog) << "isIn requested, but tile not valid"; qCDebug(TerrainTileLog) << "isIn requested, but tile not valid";
return false; return false;
} }
bool ret = coord.latitude() >= _southWest.longitude() && coord.longitude() >= _southWest.longitude() && bool ret = coordinate.latitude() >= _southWest.longitude() && coordinate.longitude() >= _southWest.longitude() &&
coord.latitude() <= _northEast.longitude() && coord.longitude() <= _northEast.longitude(); coordinate.latitude() <= _northEast.longitude() && coordinate.longitude() <= _northEast.longitude();
qCDebug(TerrainTileLog) << "Checking isIn: " << coord << " , in sw " << _southWest << " , ne " << _northEast << ": " << ret; qCDebug(TerrainTileLog) << "Checking isIn: " << coordinate << " , in sw " << _southWest << " , ne " << _northEast << ": " << ret;
return ret; return ret;
} }
float TerrainTile::elevation(const QGeoCoordinate& coordinate) const float TerrainTile::elevation(const QGeoCoordinate& coordinate) const
{ {
if (_isValid) { if (_isValid) {
qCDebug << "elevation: " << coord << " , in sw " << _southWest << " , ne " << _northEast; qCDebug(TerrainTileLog) << "elevation: " << coordinate << " , in sw " << _southWest << " , ne " << _northEast;
// Get the index at resolution of 1 arc second // Get the index at resolution of 1 arc second
int indexLat = std::round((coord.latitude() - _southWest.latitude()) / _srtm1Increment); int indexLat = std::round((coordinate.latitude() - _southWest.latitude()) * _gridSize / _srtm1TileSize);
int indexLon = std::round((coord.longitude() - _southWest.longitude()) / _srtm1Increment); int indexLon = std::round((coordinate.longitude() - _southWest.longitude()) * _gridSize / _srtm1TileSize);
qCDebug << "indexLat:indexLon" << indexLat << indexLon; // TODO (birchera): Move this down to the next debug output, once this is all properly working. qCDebug(TerrainTileLog) << "indexLat:indexLon" << indexLat << indexLon; // TODO (birchera): Move this down to the next debug output, once this is all properly working.
Q_ASSERT(indexLat >= 0); Q_ASSERT(indexLat >= 0);
Q_ASSERT(indexLat < _gridSize); Q_ASSERT(indexLat < _gridSize);
Q_ASSERT(indexLon >= 0); Q_ASSERT(indexLon >= 0);
Q_ASSERT(indexLon < _gridSize); Q_ASSERT(indexLon < _gridSize);
qCDebug << "elevation" << _data[indexLat][indexLon]; qCDebug(TerrainTileLog) << "elevation" << _data[indexLat][indexLon];
return _data[indexLat][indexLon]; return _data[indexLat][indexLon];
} else { } else {
qCDebug(TerrainTileLog) << "Asking for elevation, but no valid data."; qCDebug(TerrainTileLog) << "Asking for elevation, but no valid data.";
return -1.0; return -1.0;
} }
} }
QGeoCoordinate TerrainTile::centerCoordinate(void) const
{
return _southWest.atDistanceAndAzimuth(_southWest.distanceTo(_northEast) / 2.0, _southWest.azimuthTo(_northEast));
}
...@@ -66,11 +66,18 @@ public: ...@@ -66,11 +66,18 @@ public:
*/ */
float avgElevation(void) const { return _avgElevation; } float avgElevation(void) const { return _avgElevation; }
/**
* Accessor for the center coordinate
*
* @return center coordinate
*/
QGeoCoordinate centerCoordinate(void) const;
/// tile grid size in lat and lon /// tile grid size in lat and lon
static const int _gridSize = TERRAIN_TILE_SIZE; static const int _gridSize = TERRAIN_TILE_SIZE;
/// grid spacing in degree /// size of a tile in degree
static const float _srtm1Increment = 1.0 / (60.0 * 60.0); static const double _srtm1TileSize;
private: private:
QGeoCoordinate _southWest; /// South west corner of the tile QGeoCoordinate _southWest; /// South west corner of the tile
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment