From 688861bf650049c6185bee4b7b458fda87a99987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Fri, 23 Jun 2023 09:30:44 +0200 Subject: [PATCH] Code cleanup --- src/map/onlinemap.cpp | 3 +-- src/map/tileloader.cpp | 30 ++++++++++++++++-------------- src/map/tileloader.h | 11 ++++++++--- src/map/wmsmap.cpp | 2 +- src/map/wmtsmap.cpp | 2 +- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/map/onlinemap.cpp b/src/map/onlinemap.cpp index e9841670..9723d564 100644 --- a/src/map/onlinemap.cpp +++ b/src/map/onlinemap.cpp @@ -18,9 +18,8 @@ OnlineMap::OnlineMap(const QString &fileName, const QString &name, { _tileLoader = new TileLoader(QDir(ProgramPaths::tilesDir()).filePath(_name), this); - _tileLoader->setUrl(url); + _tileLoader->setUrl(url, quadTiles ? TileLoader::QuadTiles : TileLoader::XYZ); _tileLoader->setHeaders(headers); - _tileLoader->setQuadTiles(quadTiles); connect(_tileLoader, &TileLoader::finished, this, &OnlineMap::tilesLoaded); } diff --git a/src/map/tileloader.cpp b/src/map/tileloader.cpp index 168d5943..c2469607 100644 --- a/src/map/tileloader.cpp +++ b/src/map/tileloader.cpp @@ -52,7 +52,7 @@ static QString quadKey(const QPoint &xy, int zoom) } TileLoader::TileLoader(const QString &dir, QObject *parent) - : QObject(parent), _dir(dir), _scaledSize(0), _quadTiles(false) + : QObject(parent), _urlType(XYZ), _dir(dir), _scaledSize(0) { if (!QDir().mkpath(_dir)) qWarning("%s: %s", qPrintable(_dir), "Error creating tiles directory"); @@ -176,19 +176,21 @@ QUrl TileLoader::tileUrl(const FetchTile &tile) const { QString url(_url); - if (!tile.bbox().isNull()) { - QString bbox = QString("%1,%2,%3,%4").arg( - QString::number(tile.bbox().left(), 'f', 6), - QString::number(tile.bbox().bottom(), 'f', 6), - QString::number(tile.bbox().right(), 'f', 6), - QString::number(tile.bbox().top(), 'f', 6)); - url.replace("$bbox", bbox); - } else if (_quadTiles) { - url.replace("$quadkey", quadKey(tile.xy(), tile.zoom().toInt())); - } else { - url.replace("$z", tile.zoom().toString()); - url.replace("$x", QString::number(tile.xy().x())); - url.replace("$y", QString::number(tile.xy().y())); + switch (_urlType) { + case BoundingBox: + url.replace("$bbox", QString("%1,%2,%3,%4").arg( + QString::number(tile.bbox().left(), 'f', 6), + QString::number(tile.bbox().bottom(), 'f', 6), + QString::number(tile.bbox().right(), 'f', 6), + QString::number(tile.bbox().top(), 'f', 6))); + break; + case QuadTiles: + url.replace("$quadkey", quadKey(tile.xy(), tile.zoom().toInt())); + break; + default: + url.replace("$z", tile.zoom().toString()); + url.replace("$x", QString::number(tile.xy().x())); + url.replace("$y", QString::number(tile.xy().y())); } return QUrl(url); diff --git a/src/map/tileloader.h b/src/map/tileloader.h index 86286b12..dbcb2db8 100644 --- a/src/map/tileloader.h +++ b/src/map/tileloader.h @@ -11,12 +11,17 @@ class TileLoader : public QObject Q_OBJECT public: + enum UrlType { + XYZ, + QuadTiles, + BoundingBox + }; + TileLoader(const QString &dir, QObject *parent = 0); - void setUrl(const QString &url) {_url = url;} + void setUrl(const QString &url, UrlType type) {_url = url; _urlType = type;} void setHeaders(const QList &headers) {_headers = headers;} void setScaledSize(int size); - void setQuadTiles(bool quadTiles) {_quadTiles = quadTiles;} void loadTilesAsync(QVector &list); void loadTilesSync(QVector &list); @@ -31,10 +36,10 @@ private: Downloader *_downloader; QString _url; + UrlType _urlType; QString _dir; QList _headers; int _scaledSize; - bool _quadTiles; }; #endif // TILELOADER_H diff --git a/src/map/wmsmap.cpp b/src/map/wmsmap.cpp index b8b6bce7..decfc615 100644 --- a/src/map/wmsmap.cpp +++ b/src/map/wmsmap.cpp @@ -85,7 +85,7 @@ WMSMap::WMSMap(const QString &fileName, const QString &name, void WMSMap::init() { - _tileLoader->setUrl(tileUrl()); + _tileLoader->setUrl(tileUrl(), TileLoader::BoundingBox); _bounds = RectD(_wms->bbox(), _wms->projection()); computeZooms(); updateTransform(); diff --git a/src/map/wmtsmap.cpp b/src/map/wmtsmap.cpp index f01c82d8..fd262cad 100644 --- a/src/map/wmtsmap.cpp +++ b/src/map/wmtsmap.cpp @@ -31,7 +31,7 @@ WMTSMap::WMTSMap(const QString &fileName, const QString &name, void WMTSMap::init() { - _tileLoader->setUrl(_wmts->tileUrl()); + _tileLoader->setUrl(_wmts->tileUrl(), TileLoader::XYZ); _bounds = RectD(_wmts->bbox(), _wmts->projection()); updateTransform(); }