8#include <QtGui/QPainter>
9#include <QtGui/QPixmap>
10#include <QtNetwork/QNetworkAccessManager>
24void Viewer3DTileQuery::MapTileContainer_t::init()
26 mapWidth = (tileMaxIndex.x() - tileMinIndex.x() + 1) * tileSize;
27 mapHeight = (tileMaxIndex.y() - tileMinIndex.y() + 1) * tileSize;
28 mapTextureImage = QImage(mapWidth, mapHeight, QImage::Format_RGBA32FPx4);
29 mapTextureImage.fill(Qt::gray);
32void Viewer3DTileQuery::MapTileContainer_t::setMapTile()
35 tmpPixmap.loadFromData(currentTileData);
36 QImage tmpImage = tmpPixmap.toImage().convertToFormat(QImage::Format_RGBA32FPx4);
38 QPainter painter(&mapTextureImage);
39 int idxX = (currentTileIndex.x() - tileMinIndex.x()) * tileSize;
40 int idxY = (currentTileIndex.y() - tileMinIndex.y()) * tileSize;
41 painter.drawImage(idxX, idxY, tmpImage);
44QByteArray Viewer3DTileQuery::MapTileContainer_t::mapData()
const
46 return QByteArray(
reinterpret_cast<const char *
>(mapTextureImage.constBits()), mapTextureImage.sizeInBytes());
49void Viewer3DTileQuery::MapTileContainer_t::clear()
54void Viewer3DTileQuery::_loadMapTiles(
int zoomLevel, QPoint tileMinIndex, QPoint tileMaxIndex)
56 _mapToBeLoaded.clear();
57 _mapToBeLoaded.zoomLevel = zoomLevel;
58 _mapToBeLoaded.tileMinIndex = tileMinIndex;
59 _mapToBeLoaded.tileMaxIndex = tileMaxIndex;
60 _mapToBeLoaded.init();
62 if (!_networkManager) {
63 _networkManager =
new QNetworkAccessManager(
this);
64 _networkManager->setTransferTimeout(9000);
67 for (
int x = tileMinIndex.x(); x <= tileMaxIndex.x(); x++) {
68 for (
int y = tileMinIndex.y(); y <= tileMaxIndex.y(); y++) {
69 _mapToBeLoaded.tileList.append(_tileKey(_mapId, x, y, zoomLevel));
71 auto *reply =
new Viewer3DTileReply(zoomLevel, x, y, _mapId, _mapType, _networkManager,
this);
78 _totalTilesCount = _mapToBeLoaded.tileList.size();
79 _downloadedTilesCount = 0;
80 qCDebug(Viewer3DTileQueryLog) <<
"Requesting" << _totalTilesCount <<
"tiles at zoom" << zoomLevel
81 <<
"x:[" << tileMinIndex.x() <<
".." << tileMaxIndex.x() <<
"]"
82 <<
"y:[" << tileMinIndex.y() <<
".." << tileMaxIndex.y() <<
"]";
92 const double lat1 = coordinateMin.latitude();
93 const double lon1 = coordinateMin.longitude();
94 const double lat2 = coordinateMax.latitude();
95 const double lon2 = coordinateMax.longitude();
97 const int minTileX = provider->long2tileX(std::fmin(lon1, lon2), zoomLevel);
98 const int maxTileX = provider->long2tileX(std::fmax(lon1, lon2), zoomLevel);
99 const int minTileY = provider->lat2tileY(std::fmax(lat1, lat2), zoomLevel);
100 const int maxTileY = provider->lat2tileY(std::fmin(lat1, lat2), zoomLevel);
102 const QPoint minTile(minTileX, minTileY);
103 const QPoint maxTile(maxTileX, maxTileY);
105 const QGeoCoordinate nwCoord(provider->tileY2lat(minTileY, zoomLevel),
106 provider->tileX2long(minTileX, zoomLevel), 0);
107 const QGeoCoordinate seCoord(provider->tileY2lat(maxTileY + 1, zoomLevel),
108 provider->tileX2long(maxTileX + 1, zoomLevel), 0);
110 const QGeoCoordinate finalMin = QGeoCoordinate(seCoord.latitude(), nwCoord.longitude(), 0);
111 const QGeoCoordinate finalMax = QGeoCoordinate(nwCoord.latitude(), seCoord.longitude(), 0);
113 _loadMapTiles(zoomLevel, minTile, maxTile);
117 output.coordinateMax = finalMax;
118 output.tileCounts = QSize(maxTile.x() - minTile.x() + 1, maxTile.y() - minTile.y() + 1);
119 output.zoomLevel = zoomLevel;
129 for (_zoomLevel =
kMaxZoomLevel; _zoomLevel > 0; _zoomLevel--) {
135 _textureCoordinateMin = coordinateMin;
136 _textureCoordinateMax = coordinateMax;
147 const double minLon = std::fmin(coordinateMin.longitude(), coordinateMax.longitude());
148 const double maxLon = std::fmax(coordinateMin.longitude(), coordinateMax.longitude());
149 const double minLat = std::fmin(coordinateMin.latitude(), coordinateMax.latitude());
150 const double maxLat = std::fmax(coordinateMin.latitude(), coordinateMax.latitude());
152 const int minTileX = provider->long2tileX(minLon, zoomLevel);
153 const int maxTileX = provider->long2tileX(maxLon, zoomLevel);
154 const int minTileY = provider->lat2tileY(maxLat, zoomLevel);
155 const int maxTileY = provider->lat2tileY(minLat, zoomLevel);
157 return (maxTileX - minTileX + 1) * (maxTileY - minTileY + 1);
165 reply->deleteLater();
170 auto *reply = qobject_cast<Viewer3DTileReply *>(QObject::sender());
172 const QString key = _tileKey(tileData.
mapId, tileData.
x, tileData.
y, tileData.
zoomLevel);
173 const qsizetype itemRemoved = _mapToBeLoaded.tileList.removeAll(key);
175 if (itemRemoved > 0) {
176 _mapToBeLoaded.currentTileIndex = QPoint(tileData.
x, tileData.
y);
177 _mapToBeLoaded.currentTileData = tileData.
data;
178 _mapToBeLoaded.setMapTile();
179 _downloadedTilesCount++;
180 emit
mapTileDownloaded(100.0f * (
static_cast<float>(_downloadedTilesCount) /
static_cast<float>(_totalTilesCount)));
182 if (_mapToBeLoaded.tileList.isEmpty()) {
183 qCDebug(Viewer3DTileQueryLog) <<
"All tiles downloaded";
184 _downloadedTilesCount = _totalTilesCount;
189 _cleanupReply(reply);
194 auto *reply = qobject_cast<Viewer3DTileReply *>(QObject::sender());
195 _cleanupReply(reply);
197 const QString key = _tileKey(tileData.
mapId, tileData.
x, tileData.
y, tileData.
zoomLevel);
198 _mapToBeLoaded.tileList.removeAll(key);
199 _downloadedTilesCount++;
200 emit
mapTileDownloaded(100.0f * (
static_cast<float>(_downloadedTilesCount) /
static_cast<float>(_totalTilesCount)));
202 if (_mapToBeLoaded.tileList.isEmpty()) {
203 _downloadedTilesCount = _totalTilesCount;
210 auto *reply = qobject_cast<Viewer3DTileReply *>(QObject::sender());
211 _cleanupReply(reply);
215 emit
textureGeometryReady(_findAndLoadMapTiles(_zoomLevel, _textureCoordinateMin, _textureCoordinateMax));
219QString Viewer3DTileQuery::_tileKey(
int mapId,
int x,
int y,
int zoomLevel)
221 return QString::asprintf(
"%010d%08d%08d%03d", mapId, x, y, zoomLevel);
#define QGC_LOGGING_CATEGORY(name, categoryStr)
std::shared_ptr< const MapProvider > SharedMapProvider
static constexpr int kMaxZoomLevel
static constexpr int kMaxTileCounts
static std::shared_ptr< const MapProvider > getMapProviderFromQtMapId(int qtMapId)
void textureGeometryReady(TileStatistics_t tileInfo)
void mapTileDownloaded(float progress)
void adaptiveMapTilesLoader(const QString &mapType, int mapId, const QGeoCoordinate &coordinateMin, const QGeoCoordinate &coordinateMax)
int maxTileCount(int zoomLevel, const QGeoCoordinate &coordinateMin, const QGeoCoordinate &coordinateMax)
void loadingMapCompleted()
Viewer3DTileStatistics TileStatistics_t
void tileDone(TileInfo_t)
void tileEmpty(TileInfo_t)
void tileGiveUp(TileInfo_t)
QGeoCoordinate coordinateMin