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

Added support for vector MBtiles

This commit is contained in:
Martin Tůma 2018-11-03 00:43:52 +01:00
parent f762013e1e
commit 9c6c574443
2 changed files with 59 additions and 15 deletions

View File

@ -4,7 +4,8 @@ VERSION = 7.0
QT += core \ QT += core \
gui \ gui \
network \ network \
sql sql \
concurrent
greaterThan(QT_MAJOR_VERSION, 4) { greaterThan(QT_MAJOR_VERSION, 4) {
QT += widgets QT += widgets
QT += printsupport QT += printsupport

View File

@ -4,14 +4,39 @@
#include <QFileInfo> #include <QFileInfo>
#include <QPainter> #include <QPainter>
#include <QPixmapCache> #include <QPixmapCache>
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
#include <QtCore>
#else // QT_VERSION < 5
#include <QtConcurrent>
#endif // QT_VERSION < 5
#include "common/rectc.h" #include "common/rectc.h"
#include "common/config.h" #include "common/config.h"
#include "osm.h" #include "osm.h"
#include "mbtilesmap.h" #include "mbtilesmap.h"
struct MBTile
{
public:
MBTile() {}
MBTile(const QPoint &xy) : xy(xy) {}
QPixmap pixmap;
QByteArray data;
QString key;
int zoom;
QPoint xy;
};
#define META_TYPE(type) static_cast<QMetaType::Type>(type) #define META_TYPE(type) static_cast<QMetaType::Type>(type)
static void render(MBTile &tile)
{
if (tile.pixmap.isNull())
tile.pixmap.loadFromData(tile.data, QString::number(tile.zoom)
.toLatin1());
}
static double index2mercator(int index, int zoom) static double index2mercator(int index, int zoom)
{ {
return rad2deg(-M_PI + 2 * M_PI * ((double)index / (1<<zoom))); return rad2deg(-M_PI + 2 * M_PI * ((double)index / (1<<zoom)));
@ -222,27 +247,45 @@ void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
QSizeF s(qMin(rect.right() - tl.x(), b.width()), QSizeF s(qMin(rect.right() - tl.x(), b.width()),
qMin(rect.bottom() - tl.y(), b.height())); qMin(rect.bottom() - tl.y(), b.height()));
for (int i = 0; i < ceil(s.width() / tileSize()); i++) { int width = ceil(s.width() / tileSize());
for (int j = 0; j < ceil(s.height() / tileSize()); j++) { int height = ceil(s.height() / tileSize());
QPixmap pm;
QVector<MBTile> tiles;
tiles.reserve(width * height);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
QPoint t(tile.x() + i, tile.y() + j); QPoint t(tile.x() + i, tile.y() + j);
QString key = _fileName + "-" + QString::number(_zoom) + "_" QString key = _fileName + "-" + QString::number(_zoom) + "_"
+ QString::number(t.x()) + "_" + QString::number(t.y()); + QString::number(t.x()) + "_" + QString::number(t.y());
if (!QPixmapCache::find(key, &pm)) tiles.append(MBTile(t));
if (pm.loadFromData(tileData(_zoom, t))) MBTile &mt = tiles.last();
QPixmapCache::insert(key, pm); if (!QPixmapCache::find(key, &(mt.pixmap))) {
mt.data = tileData(_zoom, t);
QPointF tp(qMax(tl.x(), b.left()) + (t.x() - tile.x()) * tileSize(), mt.key = key;
qMax(tl.y(), b.top()) + (t.y() - tile.y()) * tileSize()); mt.zoom = _zoom;
if (!pm.isNull()) {
#ifdef ENABLE_HIDPI
pm.setDevicePixelRatio(imageRatio());
#endif // ENABLE_HIDPI
painter->drawPixmap(tp, pm);
} }
} }
} }
QFuture<void> future = QtConcurrent::map(tiles, render);
future.waitForFinished();
for (int i = 0; i < tiles.size(); i++) {
MBTile &mt = tiles[i];
if (!mt.pixmap.isNull() && !mt.data.isNull())
QPixmapCache::insert(mt.key, mt.pixmap);
QPointF tp(qMax(tl.x(), b.left()) + (mt.xy.x() - tile.x()) * tileSize(),
qMax(tl.y(), b.top()) + (mt.xy.y() - tile.y()) * tileSize());
if (!mt.pixmap.isNull()) {
#ifdef ENABLE_HIDPI
mt.pixmap.setDevicePixelRatio(imageRatio());
#endif // ENABLE_HIDPI
painter->drawPixmap(tp, mt.pixmap);
}
}
} }
QPointF MBTilesMap::ll2xy(const Coordinates &c) QPointF MBTilesMap::ll2xy(const Coordinates &c)