1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-12-11 03:29:09 +01:00

Compare commits

...

3 Commits

Author SHA1 Message Date
95d354f10c Fixed map bounds limiting 2023-11-18 23:55:58 +01:00
aedbc99a6c Improved MBTiles map loading performance when metadata table present
+ Fixed zoom limiting
2023-11-18 23:29:18 +01:00
04f9457085 Fixed bounds limiting 2023-11-18 21:32:30 +01:00
7 changed files with 227 additions and 179 deletions

View File

@ -347,20 +347,14 @@ void AQMMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
Q_UNUSED(flags);
const Zoom &z = _zooms.at(_zoom);
qreal scale = OSM::zoom2scale(z.zoom, z.tileSize);
QRectF b(bounds());
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
-rect.topLeft().y() * scale) * _mapRatio, z.zoom);
QPointF tl(floor(rect.left() / tileSize())
* tileSize(), floor(rect.top() / tileSize()) * tileSize());
QSizeF s(qMin(rect.right() - tl.x(), b.width()),
qMin(rect.bottom() - tl.y(), b.height()));
Coordinates ctl(OSM::tile2ll(tile, z.zoom));
QPointF tl(ll2xy(Coordinates(ctl.lon(), -ctl.lat())));
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
int width = ceil(s.width() / tileSize());
int height = ceil(s.height() / tileSize());
QList<RenderTile> tiles;
for (int i = 0; i < width; i++) {
@ -371,15 +365,13 @@ void AQMMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
+ QString::number(t.x()) + "_" + QString::number(t.y());
if (QPixmapCache::find(key, &pm)) {
QPointF tp(qMax(tl.x(), b.left()) + (t.x() - tile.x())
* tileSize(), qMax(tl.y(), b.top()) + (t.y() - tile.y())
* tileSize());
QPointF tp(tl.x() + (t.x() - tile.x()) * tileSize(),
tl.y() + (t.y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
} else {
} else
tiles.append(RenderTile(t, tileData(t), key));
}
}
}
QFuture<void> future = QtConcurrent::map(tiles, &RenderTile::load);
future.waitForFinished();
@ -392,9 +384,8 @@ void AQMMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
QPixmapCache::insert(mt.key(), pm);
QPointF tp(qMax(tl.x(), b.left()) + (mt.xy().x() - tile.x())
* tileSize(), qMax(tl.y(), b.top()) + (mt.xy().y() - tile.y())
* tileSize());
QPointF tp(tl.x() + (mt.xy().x() - tile.x()) * tileSize(),
tl.y() + (mt.xy().y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
}
}

View File

@ -243,20 +243,14 @@ void GEMFMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
Q_UNUSED(flags);
const Zoom &z = _zooms.at(_zi);
qreal scale = OSM::zoom2scale(z.level, _tileSize);
QRectF b(bounds());
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
-rect.topLeft().y() * scale) * _mapRatio, z.level);
QPointF tl(floor(rect.left() / tileSize())
* tileSize(), floor(rect.top() / tileSize()) * tileSize());
QSizeF s(qMin(rect.right() - tl.x(), b.width()),
qMin(rect.bottom() - tl.y(), b.height()));
Coordinates ctl(OSM::tile2ll(tile, z.level));
QPointF tl(ll2xy(Coordinates(ctl.lon(), -ctl.lat())));
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
int width = ceil(s.width() / tileSize());
int height = ceil(s.height() / tileSize());
QList<RenderTile> tiles;
for (int i = 0; i < width; i++) {
@ -267,15 +261,13 @@ void GEMFMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
+ QString::number(t.x()) + "_" + QString::number(t.y());
if (QPixmapCache::find(key, &pm)) {
QPointF tp(qMax(tl.x(), b.left()) + (t.x() - tile.x())
* tileSize(), qMax(tl.y(), b.top()) + (t.y() - tile.y())
* tileSize());
QPointF tp(tl.x() + (t.x() - tile.x()) * tileSize(),
tl.y() + (t.y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
} else {
} else
tiles.append(RenderTile(t, tileData(t), key));
}
}
}
QFuture<void> future = QtConcurrent::map(tiles, &RenderTile::load);
future.waitForFinished();
@ -288,9 +280,8 @@ void GEMFMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
QPixmapCache::insert(mt.key(), pm);
QPointF tp(qMax(tl.x(), b.left()) + (mt.xy().x() - tile.x())
* tileSize(), qMax(tl.y(), b.top()) + (mt.xy().y() - tile.y())
* tileSize());
QPointF tp(tl.x() + (mt.xy().x() - tile.x()) * tileSize(),
tl.y() + (mt.xy().y() - tile.y())* tileSize());
drawTile(painter, pm, tp);
}
}

View File

@ -12,6 +12,170 @@
#define META_TYPE(type) static_cast<QMetaType::Type>(type)
static RectC str2bounds(const QString &str)
{
QStringList list(str.split(','));
if (list.size() != 4)
return RectC();
bool lok, rok, bok, tok;
double left = list.at(0).toDouble(&lok);
double bottom = list.at(1).toDouble(&bok);
double right = list.at(2).toDouble(&rok);
double top = list.at(3).toDouble(&tok);
return (lok && rok && bok && tok)
? RectC(Coordinates(left, top), Coordinates(right, bottom))
: RectC();
}
bool MBTilesMap::getMinZoom(int &zoom)
{
QSqlQuery query("SELECT value FROM metadata WHERE name = 'minzoom'", _db);
if (query.first()) {
bool ok;
zoom = query.value(0).toString().toInt(&ok);
if (!ok || zoom < 0) {
_errorString = "Invalid minzoom metadata";
return false;
}
} else {
qWarning("%s: missing minzoom metadata", qPrintable(path()));
zoom = 0;
}
return true;
}
bool MBTilesMap::getMaxZoom(int &zoom)
{
QSqlQuery query("SELECT value FROM metadata WHERE name = 'maxzoom'", _db);
if (query.first()) {
bool ok;
zoom = query.value(0).toString().toInt(&ok);
if (!ok && zoom < 0) {
_errorString = "Invalid maxzoom metadata";
return false;
}
} else {
qWarning("%s: missing maxzoom metadata", qPrintable(path()));
zoom = 20;
}
return true;
}
bool MBTilesMap::getZooms()
{
int minZoom, maxZoom;
if (!(getMinZoom(minZoom) && getMaxZoom(maxZoom)))
return false;
for (int i = minZoom; i <= maxZoom; i++) {
QString sql = QString("SELECT zoom_level FROM tiles"
" WHERE zoom_level = %1 LIMIT 1").arg(i);
QSqlQuery query(sql, _db);
if (query.first())
_zooms.append(i);
}
_zi = _zooms.size() - 1;
return true;
}
bool MBTilesMap::getBounds()
{
QSqlQuery query("SELECT value FROM metadata WHERE name = 'bounds'", _db);
if (query.first()) {
RectC b(str2bounds(query.value(0).toString()));
if (!b.isValid()) {
_errorString = "Invalid bounds metadata";
return false;
}
_bounds = b;
} else {
qWarning("%s: missing bounds metadata", qPrintable(path()));
int z = _zooms.first();
QString sql = QString("SELECT min(tile_column), min(tile_row), "
"max(tile_column), max(tile_row) FROM tiles WHERE zoom_level = %1")
.arg(z);
QSqlQuery query(sql, _db);
query.first();
int minX = qMin((1<<z) - 1, qMax(0, query.value(0).toInt()));
int minY = qMin((1<<z) - 1, qMax(0, query.value(1).toInt()));
int maxX = qMin((1<<z) - 1, qMax(0, query.value(2).toInt())) + 1;
int maxY = qMin((1<<z) - 1, qMax(0, query.value(3).toInt())) + 1;
Coordinates tl(OSM::tile2ll(QPoint(minX, maxY), z));
Coordinates br(OSM::tile2ll(QPoint(maxX, minY), z));
// Workaround of broken zoom levels 0 and 1 due to numerical instability
tl.rlat() = qMin(tl.lat(), OSM::BOUNDS.top());
br.rlat() = qMax(br.lat(), OSM::BOUNDS.bottom());
_bounds = RectC(tl, br);
}
return true;
}
bool MBTilesMap::getTileSize()
{
QString sql("SELECT tile_data FROM tiles LIMIT 1");
QSqlQuery query(sql, _db);
query.first();
QByteArray data = query.value(0).toByteArray();
QBuffer buffer(&data);
QImageReader reader(&buffer);
QSize tileSize(reader.size());
if (!tileSize.isValid() || tileSize.width() != tileSize.height()) {
_errorString = "Unsupported/invalid tile images";
return false;
}
_tileSize = tileSize.width();
return true;
}
void MBTilesMap::getTileFormat()
{
QSqlQuery query("SELECT value FROM metadata WHERE name = 'format'", _db);
if (query.first()) {
if (query.value(0).toString() == "pbf")
_scalable = true;
} else
qWarning("%s: missing tiles format metadata", qPrintable(path()));
}
void MBTilesMap::getTilePixelRatio()
{
QSqlQuery query("SELECT value FROM metadata WHERE name = 'tilepixelratio'",
_db);
if (query.first()) {
bool ok;
double ratio = query.value(0).toString().toDouble(&ok);
if (ok)
_tileRatio = ratio;
}
}
void MBTilesMap::getName()
{
QSqlQuery query("SELECT value FROM metadata WHERE name = 'name'", _db);
if (query.first())
_name = query.value(0).toString();
else {
qWarning("%s: missing map name", qPrintable(path()));
_name = Util::file2name(path());
}
}
MBTilesMap::MBTilesMap(const QString &fileName, QObject *parent)
: Map(fileName, parent), _mapRatio(1.0), _tileRatio(1.0), _scalable(false),
_scaledSize(0), _valid(false)
@ -42,90 +206,15 @@ MBTilesMap::MBTilesMap(const QString &fileName, QObject *parent)
return;
}
{
QSqlQuery query("SELECT DISTINCT zoom_level FROM tiles"
" ORDER BY zoom_level", _db);
while (query.next())
_zooms.append(query.value(0).toInt());
if (_zooms.isEmpty()) {
_errorString = "Empty tile set";
if (!getZooms())
return;
}
if (_zooms.first() < 0) {
_errorString = "Invalid zoom levels";
if (!getBounds())
return;
}
}
_zi = _zooms.size() - 1;
{
int z = _zooms.first();
QString sql = QString("SELECT min(tile_column), min(tile_row), "
"max(tile_column), max(tile_row) FROM tiles WHERE zoom_level = %1")
.arg(z);
QSqlQuery query(sql, _db);
query.first();
int minX = qMin((1<<z) - 1, qMax(0, query.value(0).toInt()));
int minY = qMin((1<<z) - 1, qMax(0, query.value(1).toInt()));
int maxX = qMin((1<<z) - 1, qMax(0, query.value(2).toInt())) + 1;
int maxY = qMin((1<<z) - 1, qMax(0, query.value(3).toInt())) + 1;
Coordinates tl(OSM::tile2ll(QPoint(minX, maxY), z));
Coordinates br(OSM::tile2ll(QPoint(maxX, minY), z));
// Workaround of broken zoom levels 0 and 1 due to numerical instability
tl.rlat() = qMin(tl.lat(), OSM::BOUNDS.top());
br.rlat() = qMax(br.lat(), OSM::BOUNDS.bottom());
_bounds = RectC(tl, br);
}
{
QString sql = QString("SELECT tile_data FROM tiles LIMIT 1");
QSqlQuery query(sql, _db);
query.first();
QByteArray data = query.value(0).toByteArray();
QBuffer buffer(&data);
QImageReader reader(&buffer);
QSize tileSize(reader.size());
if (!tileSize.isValid() || tileSize.width() != tileSize.height()) {
_errorString = "Unsupported/invalid tile images";
if (!getTileSize())
return;
}
_tileSize = tileSize.width();
}
{
QSqlQuery query("SELECT value FROM metadata WHERE name = 'format'", _db);
if (query.first()) {
if (query.value(0).toString() == "pbf")
_scalable = true;
} else
qWarning("%s: missing tiles format", qPrintable(fileName));
}
{
QSqlQuery query("SELECT value FROM metadata WHERE name = 'name'", _db);
if (query.first())
_name = query.value(0).toString();
else {
qWarning("%s: missing map name", qPrintable(fileName));
_name = Util::file2name(fileName);
}
}
{
QSqlQuery query(
"SELECT value FROM metadata WHERE name = 'tilepixelratio'", _db);
if (query.first()) {
bool ok;
_tileRatio = query.value(0).toString().toDouble(&ok);
if (!ok) {
_errorString = "Invalid tile pixel ratio";
return;
}
}
}
getTileFormat();
getTilePixelRatio();
getName();
_db.close();
@ -281,23 +370,16 @@ void MBTilesMap::cancelJobs(bool wait)
void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
Q_UNUSED(flags);
int zoom = _zooms.at(_zi);
qreal scale = OSM::zoom2scale(zoom, _tileSize);
QRectF b(bounds());
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
-rect.topLeft().y() * scale) * coordinatesRatio(), zoom);
QPointF tl(floor(rect.left() / tileSize())
* tileSize(), floor(rect.top() / tileSize()) * tileSize());
QSizeF s(qMin(rect.right() - tl.x(), b.width()),
qMin(rect.bottom() - tl.y(), b.height()));
Coordinates ctl(OSM::tile2ll(tile, zoom));
QPointF tl(ll2xy(Coordinates(ctl.lon(), -ctl.lat())));
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
int width = ceil(s.width() / tileSize());
int height = ceil(s.height() / tileSize());
QList<MBTile> tiles;
for (int i = 0; i < width; i++) {
@ -311,9 +393,8 @@ void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
continue;
if (QPixmapCache::find(key, &pm)) {
QPointF tp(qMax(tl.x(), b.left()) + (t.x() - tile.x())
* tileSize(), qMax(tl.y(), b.top()) + (t.y() - tile.y())
* tileSize());
QPointF tp(tl.x() + (t.x() - tile.x()) * tileSize(),
tl.y() + (t.y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
} else {
tiles.append(MBTile(zoom, _scaledSize, t, tileData(zoom, t),
@ -335,9 +416,8 @@ void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
QPixmapCache::insert(mt.key(), pm);
QPointF tp(qMax(tl.x(), b.left()) + (mt.xy().x() - tile.x())
* tileSize(), qMax(tl.y(), b.top()) + (mt.xy().y() - tile.y())
* tileSize());
QPointF tp(tl.x() + (mt.xy().x() - tile.x()) * tileSize(),
tl.y() + (mt.xy().y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
}
} else

View File

@ -110,6 +110,14 @@ private slots:
void jobFinished(MBTilesMapJob *job);
private:
bool getMinZoom(int &zoom);
bool getMaxZoom(int &zoom);
bool getZooms();
bool getBounds();
bool getTileSize();
void getTileFormat();
void getTilePixelRatio();
void getName();
qreal tileSize() const;
qreal coordinatesRatio() const;
qreal imageRatio() const;

View File

@ -1,4 +1,3 @@
#include <QtCore>
#include <QPainter>
#include <QDir>
#include "common/rectc.h"
@ -101,15 +100,11 @@ qreal OnlineMap::tileSize() const
void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
QRectF b(bounds());
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
-rect.topLeft().y() * scale) * coordinatesRatio(), _zoom);
QPointF tl(floor(rect.left() / tileSize())
* tileSize(), floor(rect.top() / tileSize()) * tileSize());
QSizeF s(qMin(rect.right() - tl.x(), b.width()),
qMin(rect.bottom() - tl.y(), b.height()));
Coordinates ctl(OSM::tile2ll(tile, _zoom));
QPointF tl(ll2xy(Coordinates(ctl.lon(), -ctl.lat())));
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
int width = ceil(s.width() / tileSize());
int height = ceil(s.height() / tileSize());
@ -127,9 +122,10 @@ void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
for (int i = 0; i < tiles.count(); i++) {
FetchTile &t = tiles[i];
QPointF tp(qMax(tl.x(), b.left()) + (t.xy().x() - tile.x()) * tileSize(),
qMax(tl.y(), b.top()) + ((_invertY ? (1<<_zoom) - t.xy().y() - 1 :
t.xy().y()) - tile.y()) * tileSize());
QPointF tp(tl.x() + (t.xy().x() - tile.x()) * tileSize(),
tl.y() + ((_invertY
? (1<<_zoom) - t.xy().y() - 1
: t.xy().y()) - tile.y()) * tileSize());
if (!t.pixmap().isNull()) {
t.pixmap().setDevicePixelRatio(imageRatio());

View File

@ -238,20 +238,14 @@ void OsmdroidMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
Q_UNUSED(flags);
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
QRectF b(bounds());
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
-rect.topLeft().y() * scale) * _mapRatio, _zoom);
QPointF tl(floor(rect.left() / tileSize())
* tileSize(), floor(rect.top() / tileSize()) * tileSize());
QSizeF s(qMin(rect.right() - tl.x(), b.width()),
qMin(rect.bottom() - tl.y(), b.height()));
Coordinates ctl(OSM::tile2ll(tile, _zoom));
QPointF tl(ll2xy(Coordinates(ctl.lon(), -ctl.lat())));
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
int width = ceil(s.width() / tileSize());
int height = ceil(s.height() / tileSize());
QList<RenderTile> tiles;
for (int i = 0; i < width; i++) {
@ -262,15 +256,13 @@ void OsmdroidMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
+ QString::number(t.x()) + "_" + QString::number(t.y());
if (QPixmapCache::find(key, &pm)) {
QPointF tp(qMax(tl.x(), b.left()) + (t.x() - tile.x())
* tileSize(), qMax(tl.y(), b.top()) + (t.y() - tile.y())
* tileSize());
QPointF tp(tl.x() + (t.x() - tile.x()) * tileSize(),
tl.y() + (t.y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
} else {
} else
tiles.append(RenderTile(t, tileData(_zoom, t), key));
}
}
}
QFuture<void> future = QtConcurrent::map(tiles, &RenderTile::load);
future.waitForFinished();
@ -283,9 +275,8 @@ void OsmdroidMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
QPixmapCache::insert(mt.key(), pm);
QPointF tp(qMax(tl.x(), b.left()) + (mt.xy().x() - tile.x())
* tileSize(), qMax(tl.y(), b.top()) + (mt.xy().y() - tile.y())
* tileSize());
QPointF tp(tl.x() + (mt.xy().x() - tile.x()) * tileSize(),
tl.y() + (mt.xy().y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
}
}

View File

@ -185,20 +185,14 @@ void SqliteMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
Q_UNUSED(flags);
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
QRectF b(bounds());
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
-rect.topLeft().y() * scale) * _mapRatio, _zoom);
QPointF tl(floor(rect.left() / tileSize())
* tileSize(), floor(rect.top() / tileSize()) * tileSize());
QSizeF s(qMin(rect.right() - tl.x(), b.width()),
qMin(rect.bottom() - tl.y(), b.height()));
Coordinates ctl(OSM::tile2ll(tile, _zoom));
QPointF tl(ll2xy(Coordinates(ctl.lon(), -ctl.lat())));
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
int width = ceil(s.width() / tileSize());
int height = ceil(s.height() / tileSize());
QList<RenderTile> tiles;
for (int i = 0; i < width; i++) {
@ -209,15 +203,13 @@ void SqliteMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
+ QString::number(t.x()) + "_" + QString::number(t.y());
if (QPixmapCache::find(key, &pm)) {
QPointF tp(qMax(tl.x(), b.left()) + (t.x() - tile.x())
* tileSize(), qMax(tl.y(), b.top()) + (t.y() - tile.y())
* tileSize());
QPointF tp(tl.x() + (t.x() - tile.x()) * tileSize(),
tl.y() + (t.y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
} else {
} else
tiles.append(RenderTile(t, tileData(_zoom, t), key));
}
}
}
QFuture<void> future = QtConcurrent::map(tiles, &RenderTile::load);
future.waitForFinished();
@ -230,9 +222,8 @@ void SqliteMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
QPixmapCache::insert(mt.key(), pm);
QPointF tp(qMax(tl.x(), b.left()) + (mt.xy().x() - tile.x())
* tileSize(), qMax(tl.y(), b.top()) + (mt.xy().y() - tile.y())
* tileSize());
QPointF tp(tl.x() + (mt.xy().x() - tile.x()) * tileSize(),
tl.y() + (mt.xy().y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
}
}