From aa892f6c3fa14bd98f606dc8929367dfe7851c03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Mon, 18 Dec 2023 20:32:00 +0100 Subject: [PATCH] Limit the overzoom by the resulting tile size rather than number of levels Huge sizes may cause broken rendering and cache ping-pong. Do not allow resulting tile sizes > 4096x4096px. --- src/map/mbtilesmap.cpp | 31 +++++++++++++++---------------- src/map/mbtilesmap.h | 2 +- src/map/onlinemap.cpp | 16 ++++++++++------ 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/map/mbtilesmap.cpp b/src/map/mbtilesmap.cpp index 12c0ad33..c971a197 100644 --- a/src/map/mbtilesmap.cpp +++ b/src/map/mbtilesmap.cpp @@ -9,7 +9,7 @@ #include "osm.h" #include "mbtilesmap.h" -#define MAX_OVERZOOM 3 +#define MAX_TILE_SIZE 4096 #define META_TYPE(type) static_cast(type) static RectC str2bounds(const QString &str) @@ -79,24 +79,15 @@ bool MBTilesMap::getZooms() " WHERE zoom_level = %1 LIMIT 1").arg(i); QSqlQuery query(sql, _db); if (query.first()) - _zooms.append(Zoom(i, i)); + _zoomsBase.append(Zoom(i, i)); } - if (!_zooms.size()) { + if (!_zoomsBase.size()) { _errorString = "Empty tile set"; return false; } - if (_scalable) { - for (int i = _zooms.last().base + 1; i <= OSM::ZOOMS.max(); i++) { - Zoom z(i, _zooms.last().base); - if (z.z - z.base > MAX_OVERZOOM) - break; - _zooms.append(Zoom(i, _zooms.last().base)); - } - } - - _zi = _zooms.size() - 1; + _zi = _zoomsBase.size() - 1; return true; } @@ -114,7 +105,7 @@ bool MBTilesMap::getBounds() } else { qWarning("%s: missing bounds metadata", qPrintable(path())); - int z = _zooms.first().z; + int z = _zoomsBase.first().z; QString sql = QString("SELECT min(tile_column), min(tile_row), " "max(tile_column), max(tile_row) FROM tiles WHERE zoom_level = %1") .arg(z); @@ -222,12 +213,12 @@ MBTilesMap::MBTilesMap(const QString &fileName, QObject *parent) } getTileFormat(); + if (!getTileSize()) + return; if (!getZooms()) return; if (!getBounds()) return; - if (!getTileSize()) - return; getTilePixelRatio(); getName(); @@ -243,10 +234,18 @@ void MBTilesMap::load(const Projection &in, const Projection &out, Q_UNUSED(out); _mapRatio = hidpi ? deviceRatio : 1.0; + _zooms = _zoomsBase; if (_scalable) { _scaledSize = _tileSize * deviceRatio; _tileRatio = deviceRatio; + + for (int i = _zooms.last().base + 1; i <= OSM::ZOOMS.max(); i++) { + Zoom z(i, _zooms.last().base); + if (_tileSize * _tileRatio * (1U<<(z.z - z.base)) > MAX_TILE_SIZE) + break; + _zooms.append(Zoom(i, _zooms.last().base)); + } } _db.open(); diff --git a/src/map/mbtilesmap.h b/src/map/mbtilesmap.h index 28945248..fe956131 100644 --- a/src/map/mbtilesmap.h +++ b/src/map/mbtilesmap.h @@ -146,7 +146,7 @@ private: QString _name; RectC _bounds; - QVector _zooms; + QVector _zooms, _zoomsBase; int _zi; int _tileSize; qreal _mapRatio, _tileRatio; diff --git a/src/map/onlinemap.cpp b/src/map/onlinemap.cpp index aa9da20a..6a198e86 100644 --- a/src/map/onlinemap.cpp +++ b/src/map/onlinemap.cpp @@ -8,7 +8,7 @@ #include "onlinemap.h" -#define MAX_OVERZOOM 3 +#define MAX_TILE_SIZE 4096 OnlineMap::OnlineMap(const QString &fileName, const QString &name, const QString &url, const Range &zooms, const RectC &bounds, qreal tileRatio, @@ -24,10 +24,7 @@ OnlineMap::OnlineMap(const QString &fileName, const QString &name, _tileLoader->setHeaders(headers); connect(_tileLoader, &TileLoader::finished, this, &OnlineMap::tilesLoaded); - if (_scalable) { - _baseZoom = _zooms.max(); - _zooms.setMax(qMin(_zooms.max() + MAX_OVERZOOM, OSM::ZOOMS.max())); - } + _baseZoom = _zooms.max(); } QRectF OnlineMap::bounds() @@ -87,10 +84,17 @@ void OnlineMap::load(const Projection &in, const Projection &out, Q_UNUSED(out); _mapRatio = hidpi ? deviceRatio : 1.0; + _zooms.setMax(_baseZoom); if (_scalable) { _scaledSize = _tileSize * deviceRatio; _tileRatio = deviceRatio; + + for (int i = _baseZoom + 1; i <= OSM::ZOOMS.max(); i++) { + if (_tileSize * _tileRatio * (1U<<(i - _baseZoom)) > MAX_TILE_SIZE) + break; + _zooms.setMax(i); + } } } @@ -168,7 +172,7 @@ void OnlineMap::cancelJobs(bool wait) void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags) { - int baseZoom = _scalable ? qMin(_baseZoom, _zoom) : _zoom; + int baseZoom = qMin(_baseZoom, _zoom); unsigned overzoom = _zoom - baseZoom; unsigned f = 1U<