2018-09-20 07:59:47 +02:00
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QSqlRecord>
|
|
|
|
#include <QSqlField>
|
2023-04-25 22:52:55 +02:00
|
|
|
#include <QSqlError>
|
2018-09-20 07:59:47 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QPixmapCache>
|
2018-11-03 00:43:52 +01:00
|
|
|
#include <QtConcurrent>
|
2021-01-17 19:33:06 +01:00
|
|
|
#include "common/util.h"
|
2018-09-20 07:59:47 +02:00
|
|
|
#include "osm.h"
|
|
|
|
#include "mbtilesmap.h"
|
|
|
|
|
|
|
|
|
2021-02-09 20:09:14 +01:00
|
|
|
#define META_TYPE(type) static_cast<QMetaType::Type>(type)
|
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
static RectC str2bounds(const QString &str)
|
2018-09-20 07:59:47 +02:00
|
|
|
{
|
2023-11-18 23:29:18 +01:00
|
|
|
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();
|
|
|
|
}
|
2018-09-20 07:59:47 +02:00
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
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()));
|
2023-11-20 23:31:42 +01:00
|
|
|
zoom = OSM::ZOOMS.min();
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-09-20 07:59:47 +02:00
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
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;
|
2018-09-20 08:50:52 +02:00
|
|
|
}
|
2023-11-18 23:29:18 +01:00
|
|
|
} else {
|
|
|
|
qWarning("%s: missing maxzoom metadata", qPrintable(path()));
|
2023-11-20 23:31:42 +01:00
|
|
|
zoom = OSM::ZOOMS.max();
|
2023-11-18 23:29:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
2023-11-18 23:29:18 +01:00
|
|
|
|
2023-11-26 09:07:08 +01:00
|
|
|
if (!_zooms.size()) {
|
|
|
|
_errorString = "Empty tile set";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-19 17:52:18 +01:00
|
|
|
_zi = _zooms.size() - 1;
|
2018-09-20 07:59:47 +02:00
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
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()));
|
|
|
|
|
2022-02-19 17:52:18 +01:00
|
|
|
int z = _zooms.first();
|
2018-09-20 07:59:47 +02:00
|
|
|
QString sql = QString("SELECT min(tile_column), min(tile_row), "
|
|
|
|
"max(tile_column), max(tile_row) FROM tiles WHERE zoom_level = %1")
|
2022-02-19 17:52:18 +01:00
|
|
|
.arg(z);
|
2018-09-20 07:59:47 +02:00
|
|
|
QSqlQuery query(sql, _db);
|
|
|
|
query.first();
|
|
|
|
|
2021-04-10 15:27:40 +02:00
|
|
|
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));
|
2018-09-21 00:19:30 +02:00
|
|
|
// Workaround of broken zoom levels 0 and 1 due to numerical instability
|
2018-09-25 21:07:44 +02:00
|
|
|
tl.rlat() = qMin(tl.lat(), OSM::BOUNDS.top());
|
|
|
|
br.rlat() = qMax(br.lat(), OSM::BOUNDS.bottom());
|
2018-09-20 07:59:47 +02:00
|
|
|
_bounds = RectC(tl, br);
|
|
|
|
}
|
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-15 00:38:03 +01:00
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
bool MBTilesMap::getTileSize()
|
|
|
|
{
|
2023-12-10 08:46:26 +01:00
|
|
|
QString sql("SELECT zoom_level, tile_data FROM tiles LIMIT 1");
|
2023-11-18 23:29:18 +01:00
|
|
|
QSqlQuery query(sql, _db);
|
|
|
|
query.first();
|
|
|
|
|
2023-12-10 08:46:26 +01:00
|
|
|
QByteArray z(QByteArray::number(query.value(0).toInt()));
|
|
|
|
QByteArray data = query.value(1).toByteArray();
|
2023-11-18 23:29:18 +01:00
|
|
|
QBuffer buffer(&data);
|
2023-12-10 08:46:26 +01:00
|
|
|
QImageReader reader(&buffer, z);
|
2023-11-18 23:29:18 +01:00
|
|
|
QSize tileSize(reader.size());
|
|
|
|
|
|
|
|
if (!tileSize.isValid() || tileSize.width() != tileSize.height()) {
|
|
|
|
_errorString = "Unsupported/invalid tile images";
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-15 00:38:03 +01:00
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
_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;
|
2018-11-15 00:38:03 +01:00
|
|
|
}
|
2023-11-18 23:29:18 +01:00
|
|
|
}
|
2018-11-15 00:38:03 +01:00
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
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());
|
2018-09-21 23:18:05 +02:00
|
|
|
}
|
2023-11-18 23:29:18 +01:00
|
|
|
}
|
2018-09-21 23:18:05 +02:00
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
MBTilesMap::MBTilesMap(const QString &fileName, QObject *parent)
|
|
|
|
: Map(fileName, parent), _mapRatio(1.0), _tileRatio(1.0), _scalable(false),
|
|
|
|
_scaledSize(0), _valid(false)
|
|
|
|
{
|
|
|
|
if (!Util::isSQLiteDB(fileName, _errorString))
|
|
|
|
return;
|
|
|
|
|
|
|
|
_db = QSqlDatabase::addDatabase("QSQLITE", fileName);
|
|
|
|
_db.setDatabaseName(fileName);
|
|
|
|
_db.setConnectOptions("QSQLITE_OPEN_READONLY");
|
|
|
|
|
|
|
|
if (!_db.open()) {
|
|
|
|
_errorString = _db.lastError().text();
|
|
|
|
return;
|
2018-09-21 23:18:05 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
QSqlRecord r = _db.record("tiles");
|
|
|
|
if (r.isEmpty()
|
|
|
|
|| r.field(0).name() != "zoom_level"
|
|
|
|
|| META_TYPE(r.field(0).type()) != QMetaType::Int
|
|
|
|
|| r.field(1).name() != "tile_column"
|
|
|
|
|| META_TYPE(r.field(1).type()) != QMetaType::Int
|
|
|
|
|| r.field(2).name() != "tile_row"
|
|
|
|
|| META_TYPE(r.field(2).type()) != QMetaType::Int
|
|
|
|
|| r.field(3).name() != "tile_data"
|
|
|
|
|| META_TYPE(r.field(3).type()) != QMetaType::QByteArray) {
|
|
|
|
_errorString = "Invalid table format";
|
|
|
|
return;
|
2018-09-21 00:19:30 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
if (!getZooms())
|
|
|
|
return;
|
|
|
|
if (!getBounds())
|
|
|
|
return;
|
|
|
|
if (!getTileSize())
|
|
|
|
return;
|
2023-12-10 08:46:26 +01:00
|
|
|
getTileFormat();
|
2023-11-18 23:29:18 +01:00
|
|
|
getTilePixelRatio();
|
|
|
|
getName();
|
|
|
|
|
2018-09-20 07:59:47 +02:00
|
|
|
_db.close();
|
|
|
|
|
|
|
|
_valid = true;
|
|
|
|
}
|
|
|
|
|
2023-05-04 09:38:35 +02:00
|
|
|
void MBTilesMap::load(const Projection &in, const Projection &out,
|
|
|
|
qreal deviceRatio, bool hidpi)
|
2018-09-20 07:59:47 +02:00
|
|
|
{
|
2023-05-04 09:38:35 +02:00
|
|
|
Q_UNUSED(in);
|
|
|
|
Q_UNUSED(out);
|
|
|
|
|
|
|
|
_mapRatio = hidpi ? deviceRatio : 1.0;
|
|
|
|
|
|
|
|
if (_scalable) {
|
|
|
|
_scaledSize = _tileSize * deviceRatio;
|
|
|
|
_tileRatio = deviceRatio;
|
|
|
|
}
|
|
|
|
|
2018-09-20 07:59:47 +02:00
|
|
|
_db.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MBTilesMap::unload()
|
|
|
|
{
|
2023-11-17 17:39:33 +01:00
|
|
|
cancelJobs(true);
|
2018-09-20 07:59:47 +02:00
|
|
|
_db.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF MBTilesMap::bounds()
|
|
|
|
{
|
|
|
|
return QRectF(ll2xy(_bounds.topLeft()), ll2xy(_bounds.bottomRight()));
|
|
|
|
}
|
|
|
|
|
|
|
|
int MBTilesMap::zoomFit(const QSize &size, const RectC &rect)
|
|
|
|
{
|
|
|
|
if (!rect.isValid())
|
2022-02-19 17:52:18 +01:00
|
|
|
_zi = _zooms.size() - 1;
|
2018-09-20 07:59:47 +02:00
|
|
|
else {
|
2018-09-25 21:07:44 +02:00
|
|
|
QRectF tbr(OSM::ll2m(rect.topLeft()), OSM::ll2m(rect.bottomRight()));
|
2018-09-20 07:59:47 +02:00
|
|
|
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
|
2022-02-19 17:52:18 +01:00
|
|
|
int zoom = OSM::scale2zoom(qMax(sc.x(), -sc.y()) / coordinatesRatio(),
|
|
|
|
_tileSize);
|
|
|
|
|
|
|
|
_zi = 0;
|
|
|
|
for (int i = 1; i < _zooms.size(); i++) {
|
|
|
|
if (_zooms.at(i) > zoom)
|
|
|
|
break;
|
|
|
|
_zi = i;
|
|
|
|
}
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
2022-02-19 17:52:18 +01:00
|
|
|
return _zi;
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal MBTilesMap::resolution(const QRectF &rect)
|
|
|
|
{
|
2022-02-19 17:52:18 +01:00
|
|
|
return OSM::resolution(rect.center(), _zooms.at(_zi), _tileSize);
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int MBTilesMap::zoomIn()
|
|
|
|
{
|
2023-11-17 17:39:33 +01:00
|
|
|
cancelJobs(false);
|
|
|
|
|
2022-02-19 17:52:18 +01:00
|
|
|
_zi = qMin(_zi + 1, _zooms.size() - 1);
|
|
|
|
return _zi;
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int MBTilesMap::zoomOut()
|
|
|
|
{
|
2023-11-17 17:39:33 +01:00
|
|
|
cancelJobs(false);
|
|
|
|
|
2022-02-19 17:52:18 +01:00
|
|
|
_zi = qMax(_zi - 1, 0);
|
|
|
|
return _zi;
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal MBTilesMap::coordinatesRatio() const
|
|
|
|
{
|
2018-11-17 10:10:35 +01:00
|
|
|
return _mapRatio > 1.0 ? _mapRatio / _tileRatio : 1.0;
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal MBTilesMap::imageRatio() const
|
|
|
|
{
|
2018-11-17 10:10:35 +01:00
|
|
|
return _mapRatio > 1.0 ? _mapRatio : _tileRatio;
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal MBTilesMap::tileSize() const
|
|
|
|
{
|
2018-09-21 23:18:05 +02:00
|
|
|
return (_tileSize / coordinatesRatio());
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray MBTilesMap::tileData(int zoom, const QPoint &tile) const
|
|
|
|
{
|
|
|
|
QSqlQuery query(_db);
|
|
|
|
query.prepare("SELECT tile_data FROM tiles "
|
|
|
|
"WHERE zoom_level=:zoom AND tile_column=:x AND tile_row=:y");
|
|
|
|
query.bindValue(":zoom", zoom);
|
|
|
|
query.bindValue(":x", tile.x());
|
|
|
|
query.bindValue(":y", (1<<zoom) - tile.y() - 1);
|
|
|
|
query.exec();
|
|
|
|
|
|
|
|
if (query.first())
|
|
|
|
return query.value(0).toByteArray();
|
|
|
|
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
2023-11-17 17:39:33 +01:00
|
|
|
bool MBTilesMap::isRunning(const QString &key) const
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _jobs.size(); i++) {
|
|
|
|
const QList<MBTile> &tiles = _jobs.at(i)->tiles();
|
|
|
|
for (int j = 0; j < tiles.size(); j++)
|
|
|
|
if (tiles.at(j).key() == key)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MBTilesMap::runJob(MBTilesMapJob *job)
|
|
|
|
{
|
|
|
|
_jobs.append(job);
|
|
|
|
|
|
|
|
connect(job, &MBTilesMapJob::finished, this, &MBTilesMap::jobFinished);
|
|
|
|
job->run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MBTilesMap::removeJob(MBTilesMapJob *job)
|
|
|
|
{
|
|
|
|
_jobs.removeOne(job);
|
|
|
|
job->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MBTilesMap::jobFinished(MBTilesMapJob *job)
|
|
|
|
{
|
|
|
|
const QList<MBTile> &tiles = job->tiles();
|
|
|
|
|
|
|
|
for (int i = 0; i < tiles.size(); i++) {
|
|
|
|
const MBTile &mt = tiles.at(i);
|
|
|
|
if (!mt.pixmap().isNull())
|
|
|
|
QPixmapCache::insert(mt.key(), mt.pixmap());
|
|
|
|
}
|
|
|
|
|
|
|
|
removeJob(job);
|
|
|
|
|
|
|
|
emit tilesLoaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MBTilesMap::cancelJobs(bool wait)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _jobs.size(); i++)
|
|
|
|
_jobs.at(i)->cancel(wait);
|
|
|
|
}
|
|
|
|
|
2018-09-20 07:59:47 +02:00
|
|
|
void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
|
|
|
|
{
|
2022-02-19 17:52:18 +01:00
|
|
|
int zoom = _zooms.at(_zi);
|
|
|
|
qreal scale = OSM::zoom2scale(zoom, _tileSize);
|
2018-09-25 21:07:44 +02:00
|
|
|
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
|
2022-02-19 17:52:18 +01:00
|
|
|
-rect.topLeft().y() * scale) * coordinatesRatio(), zoom);
|
2023-11-18 23:29:18 +01:00
|
|
|
Coordinates ctl(OSM::tile2ll(tile, zoom));
|
|
|
|
QPointF tl(ll2xy(Coordinates(ctl.lon(), -ctl.lat())));
|
|
|
|
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
|
2018-11-03 00:43:52 +01:00
|
|
|
int width = ceil(s.width() / tileSize());
|
|
|
|
int height = ceil(s.height() / tileSize());
|
|
|
|
|
2018-11-10 10:40:00 +01:00
|
|
|
QList<MBTile> tiles;
|
2018-11-03 00:43:52 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
for (int j = 0; j < height; j++) {
|
2018-11-10 10:40:00 +01:00
|
|
|
QPixmap pm;
|
2018-09-20 07:59:47 +02:00
|
|
|
QPoint t(tile.x() + i, tile.y() + j);
|
2022-02-19 17:52:18 +01:00
|
|
|
QString key = path() + "-" + QString::number(zoom) + "_"
|
2018-09-20 07:59:47 +02:00
|
|
|
+ QString::number(t.x()) + "_" + QString::number(t.y());
|
|
|
|
|
2023-11-17 17:39:33 +01:00
|
|
|
if (isRunning(key))
|
|
|
|
continue;
|
|
|
|
|
2020-12-22 22:09:09 +01:00
|
|
|
if (QPixmapCache::find(key, &pm)) {
|
2023-11-18 23:29:18 +01:00
|
|
|
QPointF tp(tl.x() + (t.x() - tile.x()) * tileSize(),
|
|
|
|
tl.y() + (t.y() - tile.y()) * tileSize());
|
2018-11-10 10:40:00 +01:00
|
|
|
drawTile(painter, pm, tp);
|
2018-11-15 00:38:03 +01:00
|
|
|
} else {
|
2022-02-19 17:52:18 +01:00
|
|
|
tiles.append(MBTile(zoom, _scaledSize, t, tileData(zoom, t),
|
2018-11-15 00:38:03 +01:00
|
|
|
key));
|
|
|
|
}
|
2018-11-03 00:43:52 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-20 07:59:47 +02:00
|
|
|
|
2023-11-17 17:39:33 +01:00
|
|
|
if (!tiles.isEmpty()) {
|
|
|
|
if (flags & Map::Block || !_scalable) {
|
|
|
|
QFuture<void> future = QtConcurrent::map(tiles, &MBTile::load);
|
|
|
|
future.waitForFinished();
|
2018-11-03 00:43:52 +01:00
|
|
|
|
2023-11-17 17:39:33 +01:00
|
|
|
for (int i = 0; i < tiles.size(); i++) {
|
|
|
|
const MBTile &mt = tiles.at(i);
|
|
|
|
QPixmap pm(mt.pixmap());
|
|
|
|
if (pm.isNull())
|
|
|
|
continue;
|
2018-11-10 10:40:00 +01:00
|
|
|
|
2023-11-17 17:39:33 +01:00
|
|
|
QPixmapCache::insert(mt.key(), pm);
|
2018-11-10 10:44:37 +01:00
|
|
|
|
2023-11-18 23:29:18 +01:00
|
|
|
QPointF tp(tl.x() + (mt.xy().x() - tile.x()) * tileSize(),
|
|
|
|
tl.y() + (mt.xy().y() - tile.y()) * tileSize());
|
2023-11-17 17:39:33 +01:00
|
|
|
drawTile(painter, pm, tp);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
runJob(new MBTilesMapJob(tiles));
|
2018-11-10 10:40:00 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-03 00:43:52 +01:00
|
|
|
|
2018-11-10 10:40:00 +01:00
|
|
|
void MBTilesMap::drawTile(QPainter *painter, QPixmap &pixmap, QPointF &tp)
|
|
|
|
{
|
2018-11-10 10:44:37 +01:00
|
|
|
pixmap.setDevicePixelRatio(imageRatio());
|
|
|
|
painter->drawPixmap(tp, pixmap);
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QPointF MBTilesMap::ll2xy(const Coordinates &c)
|
|
|
|
{
|
2022-02-19 17:52:18 +01:00
|
|
|
qreal scale = OSM::zoom2scale(_zooms.at(_zi), _tileSize);
|
2018-09-25 21:07:44 +02:00
|
|
|
QPointF m = OSM::ll2m(c);
|
2018-09-20 07:59:47 +02:00
|
|
|
return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio();
|
|
|
|
}
|
|
|
|
|
|
|
|
Coordinates MBTilesMap::xy2ll(const QPointF &p)
|
|
|
|
{
|
2022-02-19 17:52:18 +01:00
|
|
|
qreal scale = OSM::zoom2scale(_zooms.at(_zi), _tileSize);
|
2018-09-25 21:07:44 +02:00
|
|
|
return OSM::m2ll(QPointF(p.x() * scale, -p.y() * scale)
|
2018-09-20 07:59:47 +02:00
|
|
|
* coordinatesRatio());
|
|
|
|
}
|
2022-04-29 23:16:10 +02:00
|
|
|
|
2023-09-14 18:36:03 +02:00
|
|
|
Map *MBTilesMap::create(const QString &path, const Projection &proj, bool *isDir)
|
2022-04-29 23:16:10 +02:00
|
|
|
{
|
2023-09-14 18:36:03 +02:00
|
|
|
Q_UNUSED(proj);
|
|
|
|
|
2022-04-29 23:16:10 +02:00
|
|
|
if (isDir)
|
|
|
|
*isDir = false;
|
|
|
|
|
|
|
|
return new MBTilesMap(path);
|
|
|
|
}
|