1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 03:35:53 +01:00

Properly handle MBTiles maps with nonconsecutive zoom levels

This commit is contained in:
Martin Tůma 2022-02-19 17:52:18 +01:00
parent e3d5fe2ec3
commit 22aa472dc3
2 changed files with 37 additions and 39 deletions

View File

@ -72,25 +72,26 @@ MBTilesMap::MBTilesMap(const QString &fileName, QObject *parent)
} }
{ {
QSqlQuery query("SELECT min(zoom_level), max(zoom_level) FROM tiles", QSqlQuery query("SELECT DISTINCT zoom_level FROM tiles"
_db); " ORDER BY zoom_level", _db);
if (!query.first()) { while (query.next())
_zooms.append(query.value(0).toInt());
if (_zooms.isEmpty()) {
_errorString = "Empty tile set"; _errorString = "Empty tile set";
return; return;
} }
_zooms = Range(query.value(0).toInt(), query.value(1).toInt()); if (_zooms.first() < 0) {
if (_zooms.min() < 0 || !_zooms.isValid()) {
_errorString = "Invalid zoom levels"; _errorString = "Invalid zoom levels";
return; return;
} }
} }
_zoom = _zooms.max(); _zi = _zooms.size() - 1;
{ {
int z = _zooms.min(); int z = _zooms.first();
QString sql = QString("SELECT min(tile_column), min(tile_row), " QString sql = QString("SELECT min(tile_column), min(tile_row), "
"max(tile_column), max(tile_row) FROM tiles WHERE zoom_level = %1") "max(tile_column), max(tile_row) FROM tiles WHERE zoom_level = %1")
.arg(_zooms.min()); .arg(z);
QSqlQuery query(sql, _db); QSqlQuery query(sql, _db);
query.first(); query.first();
@ -175,45 +176,42 @@ QRectF MBTilesMap::bounds()
return QRectF(ll2xy(_bounds.topLeft()), ll2xy(_bounds.bottomRight())); return QRectF(ll2xy(_bounds.topLeft()), ll2xy(_bounds.bottomRight()));
} }
int MBTilesMap::limitZoom(int zoom) const
{
if (zoom < _zooms.min())
return _zooms.min();
if (zoom > _zooms.max())
return _zooms.max();
return zoom;
}
int MBTilesMap::zoomFit(const QSize &size, const RectC &rect) int MBTilesMap::zoomFit(const QSize &size, const RectC &rect)
{ {
if (!rect.isValid()) if (!rect.isValid())
_zoom = _zooms.max(); _zi = _zooms.size() - 1;
else { else {
QRectF tbr(OSM::ll2m(rect.topLeft()), OSM::ll2m(rect.bottomRight())); QRectF tbr(OSM::ll2m(rect.topLeft()), OSM::ll2m(rect.bottomRight()));
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height()); QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
_zoom = limitZoom(OSM::scale2zoom(qMax(sc.x(), -sc.y()) int zoom = OSM::scale2zoom(qMax(sc.x(), -sc.y()) / coordinatesRatio(),
/ coordinatesRatio(), _tileSize)); _tileSize);
_zi = 0;
for (int i = 1; i < _zooms.size(); i++) {
if (_zooms.at(i) > zoom)
break;
_zi = i;
}
} }
return _zoom; return _zi;
} }
qreal MBTilesMap::resolution(const QRectF &rect) qreal MBTilesMap::resolution(const QRectF &rect)
{ {
return OSM::resolution(rect.center(), _zoom, _tileSize); return OSM::resolution(rect.center(), _zooms.at(_zi), _tileSize);
} }
int MBTilesMap::zoomIn() int MBTilesMap::zoomIn()
{ {
_zoom = qMin(_zoom + 1, _zooms.max()); _zi = qMin(_zi + 1, _zooms.size() - 1);
return _zoom; return _zi;
} }
int MBTilesMap::zoomOut() int MBTilesMap::zoomOut()
{ {
_zoom = qMax(_zoom - 1, _zooms.min()); _zi = qMax(_zi - 1, 0);
return _zoom; return _zi;
} }
void MBTilesMap::setDevicePixelRatio(qreal deviceRatio, qreal mapRatio) void MBTilesMap::setDevicePixelRatio(qreal deviceRatio, qreal mapRatio)
@ -260,12 +258,13 @@ QByteArray MBTilesMap::tileData(int zoom, const QPoint &tile) const
void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags) void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{ {
Q_UNUSED(flags); Q_UNUSED(flags);
qreal scale = OSM::zoom2scale(_zoom, _tileSize); int zoom = _zooms.at(_zi);
qreal scale = OSM::zoom2scale(zoom, _tileSize);
QRectF b(bounds()); QRectF b(bounds());
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale, QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
-rect.topLeft().y() * scale) * coordinatesRatio(), _zoom); -rect.topLeft().y() * scale) * coordinatesRatio(), zoom);
QPointF tl(floor(rect.left() / tileSize()) QPointF tl(floor(rect.left() / tileSize())
* tileSize(), floor(rect.top() / tileSize()) * tileSize()); * tileSize(), floor(rect.top() / tileSize()) * tileSize());
@ -281,7 +280,7 @@ void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
for (int j = 0; j < height; j++) { for (int j = 0; j < height; j++) {
QPixmap pm; QPixmap pm;
QPoint t(tile.x() + i, tile.y() + j); QPoint t(tile.x() + i, tile.y() + j);
QString key = path() + "-" + QString::number(_zoom) + "_" QString key = path() + "-" + QString::number(zoom) + "_"
+ QString::number(t.x()) + "_" + QString::number(t.y()); + QString::number(t.x()) + "_" + QString::number(t.y());
if (QPixmapCache::find(key, &pm)) { if (QPixmapCache::find(key, &pm)) {
@ -290,7 +289,7 @@ void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
* tileSize()); * tileSize());
drawTile(painter, pm, tp); drawTile(painter, pm, tp);
} else { } else {
tiles.append(MBTile(_zoom, _scaledSize, t, tileData(_zoom, t), tiles.append(MBTile(zoom, _scaledSize, t, tileData(zoom, t),
key)); key));
} }
} }
@ -322,14 +321,14 @@ void MBTilesMap::drawTile(QPainter *painter, QPixmap &pixmap, QPointF &tp)
QPointF MBTilesMap::ll2xy(const Coordinates &c) QPointF MBTilesMap::ll2xy(const Coordinates &c)
{ {
qreal scale = OSM::zoom2scale(_zoom, _tileSize); qreal scale = OSM::zoom2scale(_zooms.at(_zi), _tileSize);
QPointF m = OSM::ll2m(c); QPointF m = OSM::ll2m(c);
return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio(); return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio();
} }
Coordinates MBTilesMap::xy2ll(const QPointF &p) Coordinates MBTilesMap::xy2ll(const QPointF &p)
{ {
qreal scale = OSM::zoom2scale(_zoom, _tileSize); qreal scale = OSM::zoom2scale(_zooms.at(_zi), _tileSize);
return OSM::m2ll(QPointF(p.x() * scale, -p.y() * scale) return OSM::m2ll(QPointF(p.x() * scale, -p.y() * scale)
* coordinatesRatio()); * coordinatesRatio());
} }

View File

@ -2,7 +2,7 @@
#define MBTILESMAP_H #define MBTILESMAP_H
#include <QSqlDatabase> #include <QSqlDatabase>
#include "common/range.h" #include <QVector>
#include "map.h" #include "map.h"
class MBTilesMap : public Map class MBTilesMap : public Map
@ -16,8 +16,8 @@ public:
RectC llBounds() {return _bounds;} RectC llBounds() {return _bounds;}
qreal resolution(const QRectF &rect); qreal resolution(const QRectF &rect);
int zoom() const {return _zoom;} int zoom() const {return _zi;}
void setZoom(int zoom) {_zoom = zoom;} void setZoom(int zoom) {_zi = zoom;}
int zoomFit(const QSize &size, const RectC &rect); int zoomFit(const QSize &size, const RectC &rect);
int zoomIn(); int zoomIn();
int zoomOut(); int zoomOut();
@ -35,7 +35,6 @@ public:
QString errorString() const {return _errorString;} QString errorString() const {return _errorString;}
private: private:
int limitZoom(int zoom) const;
qreal tileSize() const; qreal tileSize() const;
qreal coordinatesRatio() const; qreal coordinatesRatio() const;
qreal imageRatio() const; qreal imageRatio() const;
@ -46,8 +45,8 @@ private:
QString _name; QString _name;
RectC _bounds; RectC _bounds;
Range _zooms; QVector<int> _zooms;
int _zoom; int _zi;
int _tileSize; int _tileSize;
qreal _mapRatio, _tileRatio; qreal _mapRatio, _tileRatio;
bool _scalable; bool _scalable;