2018-09-20 07:59:47 +02:00
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QSqlRecord>
|
|
|
|
#include <QSqlField>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPixmapCache>
|
2018-11-15 00:38:03 +01:00
|
|
|
#include <QImageReader>
|
|
|
|
#include <QBuffer>
|
2018-11-03 00:43:52 +01:00
|
|
|
#include <QtConcurrent>
|
2018-09-20 07:59:47 +02:00
|
|
|
#include "common/rectc.h"
|
2018-11-02 20:01:19 +01:00
|
|
|
#include "common/config.h"
|
2018-09-20 07:59:47 +02:00
|
|
|
#include "osm.h"
|
|
|
|
#include "mbtilesmap.h"
|
|
|
|
|
|
|
|
|
2018-11-10 10:40:00 +01:00
|
|
|
class MBTile
|
2018-11-03 00:43:52 +01:00
|
|
|
{
|
|
|
|
public:
|
2018-11-15 00:38:03 +01:00
|
|
|
MBTile(int zoom, int scaledSize, const QPoint &xy, const QByteArray &data,
|
|
|
|
const QString &key) : _zoom(zoom), _scaledSize(scaledSize), _xy(xy),
|
|
|
|
_data(data), _key(key) {}
|
2018-11-10 10:40:00 +01:00
|
|
|
|
|
|
|
const QPoint &xy() const {return _xy;}
|
|
|
|
const QString &key() const {return _key;}
|
|
|
|
QPixmap pixmap() const {return QPixmap::fromImage(_image);}
|
|
|
|
|
|
|
|
void load() {
|
|
|
|
QByteArray z(QString::number(_zoom).toLatin1());
|
2018-11-15 00:38:03 +01:00
|
|
|
|
|
|
|
QBuffer buffer(&_data);
|
|
|
|
QImageReader reader(&buffer, z);
|
|
|
|
if (_scaledSize)
|
|
|
|
reader.setScaledSize(QSize(_scaledSize, _scaledSize));
|
|
|
|
reader.read(&_image);
|
2018-11-10 10:40:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int _zoom;
|
2018-11-15 00:38:03 +01:00
|
|
|
int _scaledSize;
|
2018-11-10 10:40:00 +01:00
|
|
|
QPoint _xy;
|
|
|
|
QByteArray _data;
|
|
|
|
QString _key;
|
|
|
|
QImage _image;
|
2018-11-03 00:43:52 +01:00
|
|
|
};
|
|
|
|
|
2018-09-20 07:59:47 +02:00
|
|
|
#define META_TYPE(type) static_cast<QMetaType::Type>(type)
|
|
|
|
|
|
|
|
static double index2mercator(int index, int zoom)
|
|
|
|
{
|
|
|
|
return rad2deg(-M_PI + 2 * M_PI * ((double)index / (1<<zoom)));
|
|
|
|
}
|
|
|
|
|
|
|
|
MBTilesMap::MBTilesMap(const QString &fileName, QObject *parent)
|
2020-12-22 22:09:09 +01:00
|
|
|
: Map(fileName, parent), _mapRatio(1.0), _tileRatio(1.0), _scalable(false),
|
|
|
|
_scaledSize(0), _valid(false)
|
2018-09-20 07:59:47 +02:00
|
|
|
{
|
|
|
|
_db = QSqlDatabase::addDatabase("QSQLITE", fileName);
|
|
|
|
_db.setDatabaseName(fileName);
|
2019-01-18 00:29:47 +01:00
|
|
|
_db.setConnectOptions("QSQLITE_OPEN_READONLY");
|
2018-09-20 07:59:47 +02:00
|
|
|
|
|
|
|
if (!_db.open()) {
|
|
|
|
_errorString = fileName + ": Error opening database file";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QSqlQuery query("SELECT min(zoom_level), max(zoom_level) FROM tiles",
|
|
|
|
_db);
|
|
|
|
if (!query.first()) {
|
|
|
|
_errorString = "Empty tile set";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_zooms = Range(query.value(0).toInt(), query.value(1).toInt());
|
2018-09-20 08:50:52 +02:00
|
|
|
if (_zooms.min() < 0 || !_zooms.isValid()) {
|
|
|
|
_errorString = "Invalid zoom levels";
|
|
|
|
return;
|
|
|
|
}
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
_zoom = _zooms.max();
|
|
|
|
|
|
|
|
{
|
|
|
|
QString sql = QString("SELECT min(tile_column), min(tile_row), "
|
|
|
|
"max(tile_column), max(tile_row) FROM tiles WHERE zoom_level = %1")
|
|
|
|
.arg(_zooms.min());
|
|
|
|
QSqlQuery query(sql, _db);
|
|
|
|
query.first();
|
|
|
|
|
2018-09-20 08:50:52 +02:00
|
|
|
double minX = index2mercator(qMin((1<<_zooms.min()) - 1,
|
2018-09-20 07:59:47 +02:00
|
|
|
qMax(0, query.value(0).toInt())), _zooms.min());
|
2018-09-20 08:50:52 +02:00
|
|
|
double minY = index2mercator(qMin((1<<_zooms.min()) - 1,
|
2018-09-20 07:59:47 +02:00
|
|
|
qMax(0, query.value(1).toInt())), _zooms.min());
|
2018-09-20 08:50:52 +02:00
|
|
|
double maxX = index2mercator(qMin((1<<_zooms.min()) - 1,
|
2018-09-20 07:59:47 +02:00
|
|
|
qMax(0, query.value(2).toInt())) + 1, _zooms.min());
|
2018-09-20 08:50:52 +02:00
|
|
|
double maxY = index2mercator(qMin((1<<_zooms.min()) - 1,
|
2018-09-20 07:59:47 +02:00
|
|
|
qMax(0, query.value(3).toInt())) + 1, _zooms.min());
|
2018-09-25 21:07:44 +02:00
|
|
|
Coordinates tl(OSM::m2ll(QPointF(minX, maxY)));
|
|
|
|
Coordinates br(OSM::m2ll(QPointF(maxX, minY)));
|
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);
|
|
|
|
}
|
|
|
|
|
2018-09-21 23:18:05 +02:00
|
|
|
{
|
|
|
|
QString sql = QString("SELECT tile_data FROM tiles LIMIT 1");
|
|
|
|
QSqlQuery query(sql, _db);
|
|
|
|
query.first();
|
2018-11-15 00:38:03 +01:00
|
|
|
|
|
|
|
QByteArray data = query.value(0).toByteArray();
|
|
|
|
QBuffer buffer(&data);
|
|
|
|
QImageReader reader(&buffer);
|
|
|
|
QSize tileSize(reader.size());
|
|
|
|
|
2018-11-17 10:10:35 +01:00
|
|
|
if (!tileSize.isValid() || tileSize.width() != tileSize.height()) {
|
2018-09-21 23:18:05 +02:00
|
|
|
_errorString = "Unsupported/invalid tile images";
|
|
|
|
return;
|
|
|
|
}
|
2018-11-15 00:38:03 +01:00
|
|
|
_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
|
2020-12-22 22:09:09 +01:00
|
|
|
qWarning("%s: missing tiles format", qPrintable(fileName));
|
2018-09-21 23:18:05 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 00:19:30 +02:00
|
|
|
{
|
|
|
|
QSqlQuery query("SELECT value FROM metadata WHERE name = 'name'", _db);
|
|
|
|
if (query.first())
|
|
|
|
_name = query.value(0).toString();
|
2018-09-21 23:18:05 +02:00
|
|
|
else {
|
2020-12-22 22:09:09 +01:00
|
|
|
qWarning("%s: missing map name", qPrintable(fileName));
|
|
|
|
_name = QFileInfo(fileName).fileName();
|
2018-09-21 23:18:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 00:19:30 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 07:59:47 +02:00
|
|
|
_db.close();
|
|
|
|
|
|
|
|
_valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MBTilesMap::load()
|
|
|
|
{
|
|
|
|
_db.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MBTilesMap::unload()
|
|
|
|
{
|
|
|
|
_db.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF MBTilesMap::bounds()
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (!rect.isValid())
|
|
|
|
_zoom = _zooms.max();
|
|
|
|
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());
|
2018-09-25 21:07:44 +02:00
|
|
|
_zoom = limitZoom(OSM::scale2zoom(qMax(sc.x(), -sc.y())
|
2018-09-21 23:18:05 +02:00
|
|
|
/ coordinatesRatio(), _tileSize));
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal MBTilesMap::resolution(const QRectF &rect)
|
|
|
|
{
|
2018-09-25 21:07:44 +02:00
|
|
|
return OSM::resolution(rect.center(), _zoom, _tileSize);
|
2018-09-20 07:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int MBTilesMap::zoomIn()
|
|
|
|
{
|
|
|
|
_zoom = qMin(_zoom + 1, _zooms.max());
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MBTilesMap::zoomOut()
|
|
|
|
{
|
|
|
|
_zoom = qMax(_zoom - 1, _zooms.min());
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
2018-11-17 10:10:35 +01:00
|
|
|
void MBTilesMap::setDevicePixelRatio(qreal deviceRatio, qreal mapRatio)
|
2018-11-15 00:38:03 +01:00
|
|
|
{
|
2018-11-17 10:10:35 +01:00
|
|
|
_mapRatio = mapRatio;
|
2018-11-15 00:38:03 +01:00
|
|
|
|
|
|
|
if (_scalable) {
|
2018-11-17 10:10:35 +01:00
|
|
|
_scaledSize = _tileSize * deviceRatio;
|
|
|
|
_tileRatio = deviceRatio;
|
2018-11-15 00:38:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MBTilesMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
|
|
|
|
{
|
|
|
|
Q_UNUSED(flags);
|
2018-09-25 21:07:44 +02:00
|
|
|
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
|
2018-09-20 07:59:47 +02:00
|
|
|
QRectF b(bounds());
|
|
|
|
|
|
|
|
|
2018-09-25 21:07:44 +02:00
|
|
|
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
|
2018-09-20 07:59:47 +02:00
|
|
|
-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()));
|
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);
|
2020-12-22 22:09:09 +01:00
|
|
|
QString key = path() + "-" + QString::number(_zoom) + "_"
|
2018-09-20 07:59:47 +02:00
|
|
|
+ QString::number(t.x()) + "_" + QString::number(t.y());
|
|
|
|
|
2020-12-22 22:09:09 +01:00
|
|
|
if (QPixmapCache::find(key, &pm)) {
|
2018-11-10 10:40:00 +01:00
|
|
|
QPointF tp(qMax(tl.x(), b.left()) + (t.x() - tile.x())
|
|
|
|
* tileSize(), qMax(tl.y(), b.top()) + (t.y() - tile.y())
|
|
|
|
* tileSize());
|
|
|
|
drawTile(painter, pm, tp);
|
2018-11-15 00:38:03 +01:00
|
|
|
} else {
|
|
|
|
tiles.append(MBTile(_zoom, _scaledSize, t, tileData(_zoom, t),
|
|
|
|
key));
|
|
|
|
}
|
2018-11-03 00:43:52 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-20 07:59:47 +02:00
|
|
|
|
2020-07-02 23:51:15 +02:00
|
|
|
QFuture<void> future = QtConcurrent::map(tiles, &MBTile::load);
|
2018-11-03 00:43:52 +01:00
|
|
|
future.waitForFinished();
|
|
|
|
|
|
|
|
for (int i = 0; i < tiles.size(); i++) {
|
2018-11-10 10:40:00 +01:00
|
|
|
const MBTile &mt = tiles.at(i);
|
|
|
|
QPixmap pm(mt.pixmap());
|
2018-11-10 10:44:37 +01:00
|
|
|
if (pm.isNull())
|
|
|
|
continue;
|
2018-11-10 10:40:00 +01:00
|
|
|
|
2018-11-10 10:44:37 +01:00
|
|
|
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());
|
2018-11-10 10:40:00 +01:00
|
|
|
drawTile(painter, pm, tp);
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
2018-09-25 21:07:44 +02:00
|
|
|
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
|
|
|
|
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)
|
|
|
|
{
|
2018-09-25 21:07:44 +02:00
|
|
|
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
|
|
|
|
return OSM::m2ll(QPointF(p.x() * scale, -p.y() * scale)
|
2018-09-20 07:59:47 +02:00
|
|
|
* coordinatesRatio());
|
|
|
|
}
|