2022-07-05 11:32:14 +02:00
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QSqlRecord>
|
|
|
|
#include <QSqlField>
|
2023-04-25 22:52:55 +02:00
|
|
|
#include <QSqlError>
|
2022-07-05 11:32:14 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QPixmapCache>
|
|
|
|
#include <QImageReader>
|
|
|
|
#include <QBuffer>
|
|
|
|
#include <QtConcurrent>
|
|
|
|
#include "osm.h"
|
2022-07-06 14:58:41 +02:00
|
|
|
#include "tile.h"
|
2022-07-05 11:32:14 +02:00
|
|
|
#include "osmdroidmap.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define META_TYPE(type) static_cast<QMetaType::Type>(type)
|
|
|
|
|
|
|
|
OsmdroidMap::OsmdroidMap(const QString &fileName, QObject *parent)
|
|
|
|
: Map(fileName, parent), _mapRatio(1.0), _valid(false)
|
|
|
|
{
|
|
|
|
quint64 z, l = 0, r = 0, t = 0, b = 0;
|
|
|
|
|
2023-04-25 22:52:55 +02:00
|
|
|
if (!Util::isSQLiteDB(fileName, _errorString))
|
|
|
|
return;
|
|
|
|
|
2022-07-05 11:32:14 +02:00
|
|
|
_db = QSqlDatabase::addDatabase("QSQLITE", fileName);
|
|
|
|
_db.setDatabaseName(fileName);
|
|
|
|
_db.setConnectOptions("QSQLITE_OPEN_READONLY");
|
|
|
|
|
|
|
|
if (!_db.open()) {
|
2023-04-25 22:52:55 +02:00
|
|
|
_errorString = _db.lastError().text();
|
2022-07-05 11:32:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlRecord rcrd = _db.record("tiles");
|
|
|
|
if (rcrd.isEmpty()
|
|
|
|
|| rcrd.field(0).name() != "key"
|
|
|
|
|| META_TYPE(rcrd.field(0).type()) != QMetaType::Int
|
|
|
|
|| rcrd.field(1).name() != "provider"
|
|
|
|
|| META_TYPE(rcrd.field(1).type()) != QMetaType::QString
|
|
|
|
|| rcrd.field(2).name() != "tile"
|
|
|
|
|| META_TYPE(rcrd.field(2).type()) != QMetaType::QByteArray) {
|
|
|
|
_errorString = "Invalid table format";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QSqlQuery query("SELECT min(key), max(key) FROM tiles", _db);
|
|
|
|
if (!query.first()) {
|
|
|
|
_errorString = "Empty tile set";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
quint64 min = query.value(0).toLongLong();
|
|
|
|
quint64 max = query.value(1).toLongLong();
|
|
|
|
|
|
|
|
for (quint64 i = 0; i < (quint64)OSM::ZOOMS.max(); i++) {
|
|
|
|
quint64 key = ((i << i) << i);
|
|
|
|
|
2022-07-06 12:50:57 +02:00
|
|
|
if (key <= min)
|
2022-07-05 11:32:14 +02:00
|
|
|
_zooms.setMin(i);
|
2022-07-06 12:50:57 +02:00
|
|
|
if (key <= max)
|
2022-07-05 11:32:14 +02:00
|
|
|
_zooms.setMax(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_zoom = _zooms.max();
|
|
|
|
z = _zooms.min();
|
|
|
|
|
|
|
|
{
|
|
|
|
quint64 minz = ((z << z) << z);
|
|
|
|
quint64 maxz = (((z + 1) << (z + 1)) << (z + 1));
|
|
|
|
|
|
|
|
QSqlQuery query(_db);
|
|
|
|
query.prepare("SELECT min(key), max(key) FROM tiles"
|
|
|
|
" where key >= :min AND key < :max");
|
|
|
|
query.bindValue(":min", minz);
|
|
|
|
query.bindValue(":max", maxz);
|
|
|
|
query.exec();
|
|
|
|
if (!query.first())
|
|
|
|
return;
|
|
|
|
|
|
|
|
quint64 min = query.value(0).toLongLong();
|
|
|
|
quint64 max = query.value(1).toLongLong();
|
|
|
|
|
|
|
|
for (quint64 i = 0; i < 1ull<<z; i++) {
|
|
|
|
quint64 key = (((z << z) + i) << z);
|
|
|
|
|
|
|
|
if (key <= min)
|
|
|
|
l = i;
|
|
|
|
if (key <= max)
|
|
|
|
r = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
quint64 minx = (((z << z) + l) << z);
|
|
|
|
quint64 maxx = (((z << z) + l + 1) << z);
|
|
|
|
|
|
|
|
QSqlQuery query(_db);
|
|
|
|
query.prepare("SELECT min(key), max(key) FROM tiles"
|
|
|
|
" where key >= :min AND key < :max");
|
|
|
|
query.bindValue(":min", minx);
|
|
|
|
query.bindValue(":max", maxx);
|
|
|
|
query.exec();
|
|
|
|
if (!query.first())
|
|
|
|
return;
|
|
|
|
|
|
|
|
quint64 min = query.value(0).toLongLong();
|
|
|
|
quint64 max = query.value(1).toLongLong();
|
|
|
|
|
|
|
|
for (quint64 i = 0; i < 1ull<<z; i++) {
|
|
|
|
quint64 key = (((z << z) + l) << z) + i;
|
|
|
|
|
|
|
|
if (key <= min)
|
|
|
|
t = i;
|
|
|
|
if (key <= max)
|
|
|
|
b = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Coordinates tl(OSM::tile2ll(QPoint(l, t), z));
|
|
|
|
tl.rlat() = -tl.lat();
|
|
|
|
Coordinates br(OSM::tile2ll(QPoint(r + 1, b + 1), z));
|
|
|
|
br.rlat() = -br.lat();
|
2022-07-06 12:50:57 +02:00
|
|
|
// 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());
|
2022-07-05 11:32:14 +02:00
|
|
|
_bounds = RectC(tl, br);
|
|
|
|
|
|
|
|
{
|
|
|
|
QString sql = QString("SELECT tile 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;
|
|
|
|
}
|
|
|
|
_tileSize = tileSize.width();
|
|
|
|
}
|
|
|
|
|
|
|
|
_db.close();
|
|
|
|
|
|
|
|
_valid = true;
|
|
|
|
}
|
|
|
|
|
2023-05-04 09:38:35 +02:00
|
|
|
void OsmdroidMap::load(const Projection &in, const Projection &out,
|
|
|
|
qreal deviceRatio, bool hidpi)
|
2022-07-05 11:32:14 +02:00
|
|
|
{
|
2023-05-04 09:38:35 +02:00
|
|
|
Q_UNUSED(in);
|
|
|
|
Q_UNUSED(out);
|
|
|
|
|
|
|
|
_mapRatio = hidpi ? deviceRatio : 1.0;
|
2022-07-05 11:32:14 +02:00
|
|
|
_db.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OsmdroidMap::unload()
|
|
|
|
{
|
|
|
|
_db.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF OsmdroidMap::bounds()
|
|
|
|
{
|
|
|
|
return QRectF(ll2xy(_bounds.topLeft()), ll2xy(_bounds.bottomRight()));
|
|
|
|
}
|
|
|
|
|
|
|
|
int OsmdroidMap::limitZoom(int zoom) const
|
|
|
|
{
|
|
|
|
if (zoom < _zooms.min())
|
|
|
|
return _zooms.min();
|
|
|
|
if (zoom > _zooms.max())
|
|
|
|
return _zooms.max();
|
|
|
|
|
|
|
|
return zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
int OsmdroidMap::zoomFit(const QSize &size, const RectC &rect)
|
|
|
|
{
|
|
|
|
if (!rect.isValid())
|
|
|
|
_zoom = _zooms.max();
|
|
|
|
else {
|
|
|
|
QRectF tbr(OSM::ll2m(rect.topLeft()), OSM::ll2m(rect.bottomRight()));
|
|
|
|
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
|
|
|
|
_zoom = limitZoom(OSM::scale2zoom(qMax(sc.x(), -sc.y()) / _mapRatio,
|
|
|
|
_tileSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal OsmdroidMap::resolution(const QRectF &rect)
|
|
|
|
{
|
|
|
|
return OSM::resolution(rect.center(), _zoom, _tileSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
int OsmdroidMap::zoomIn()
|
|
|
|
{
|
|
|
|
_zoom = qMin(_zoom + 1, _zooms.max());
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
int OsmdroidMap::zoomOut()
|
|
|
|
{
|
|
|
|
_zoom = qMax(_zoom - 1, _zooms.min());
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal OsmdroidMap::tileSize() const
|
|
|
|
{
|
|
|
|
return (_tileSize / _mapRatio);
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray OsmdroidMap::tileData(int zoom, const QPoint &tile) const
|
|
|
|
{
|
|
|
|
quint64 z = zoom;
|
|
|
|
quint64 key = (((z << z) + tile.x()) << z) + tile.y();
|
|
|
|
|
|
|
|
QSqlQuery query(_db);
|
|
|
|
query.prepare("SELECT tile FROM tiles WHERE key=:key");
|
|
|
|
query.bindValue(":key", key);
|
|
|
|
query.exec();
|
|
|
|
|
|
|
|
if (query.first())
|
|
|
|
return query.value(0).toByteArray();
|
|
|
|
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OsmdroidMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
|
|
|
|
{
|
|
|
|
Q_UNUSED(flags);
|
|
|
|
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
|
|
|
|
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
|
|
|
|
-rect.topLeft().y() * scale) * _mapRatio, _zoom);
|
2023-11-18 23:55:58 +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());
|
2022-07-05 11:32:14 +02:00
|
|
|
int width = ceil(s.width() / tileSize());
|
|
|
|
int height = ceil(s.height() / tileSize());
|
|
|
|
|
2022-07-06 14:58:41 +02:00
|
|
|
QList<RenderTile> tiles;
|
2022-07-05 11:32:14 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
for (int j = 0; j < height; j++) {
|
|
|
|
QPixmap pm;
|
|
|
|
QPoint t(tile.x() + i, tile.y() + j);
|
|
|
|
QString key = path() + "-" + QString::number(_zoom) + "_"
|
|
|
|
+ QString::number(t.x()) + "_" + QString::number(t.y());
|
|
|
|
|
|
|
|
if (QPixmapCache::find(key, &pm)) {
|
2023-11-18 23:55:58 +01:00
|
|
|
QPointF tp(tl.x() + (t.x() - tile.x()) * tileSize(),
|
|
|
|
tl.y() + (t.y() - tile.y()) * tileSize());
|
2022-07-05 11:32:14 +02:00
|
|
|
drawTile(painter, pm, tp);
|
2023-11-18 23:55:58 +01:00
|
|
|
} else
|
2022-07-06 14:58:41 +02:00
|
|
|
tiles.append(RenderTile(t, tileData(_zoom, t), key));
|
2022-07-05 11:32:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:58:41 +02:00
|
|
|
QFuture<void> future = QtConcurrent::map(tiles, &RenderTile::load);
|
2022-07-05 11:32:14 +02:00
|
|
|
future.waitForFinished();
|
|
|
|
|
|
|
|
for (int i = 0; i < tiles.size(); i++) {
|
2022-07-06 14:58:41 +02:00
|
|
|
const RenderTile &mt = tiles.at(i);
|
2022-07-05 11:32:14 +02:00
|
|
|
QPixmap pm(mt.pixmap());
|
|
|
|
if (pm.isNull())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QPixmapCache::insert(mt.key(), pm);
|
|
|
|
|
2023-11-18 23:55:58 +01:00
|
|
|
QPointF tp(tl.x() + (mt.xy().x() - tile.x()) * tileSize(),
|
|
|
|
tl.y() + (mt.xy().y() - tile.y()) * tileSize());
|
2022-07-05 11:32:14 +02:00
|
|
|
drawTile(painter, pm, tp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OsmdroidMap::drawTile(QPainter *painter, QPixmap &pixmap, QPointF &tp)
|
|
|
|
{
|
|
|
|
pixmap.setDevicePixelRatio(_mapRatio);
|
|
|
|
painter->drawPixmap(tp, pixmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF OsmdroidMap::ll2xy(const Coordinates &c)
|
|
|
|
{
|
|
|
|
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
|
|
|
|
QPointF m = OSM::ll2m(c);
|
|
|
|
return QPointF(m.x() / scale, m.y() / -scale) / _mapRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
Coordinates OsmdroidMap::xy2ll(const QPointF &p)
|
|
|
|
{
|
|
|
|
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
|
|
|
|
return OSM::m2ll(QPointF(p.x() * scale, -p.y() * scale) * _mapRatio);
|
|
|
|
}
|
|
|
|
|
2023-09-14 18:36:03 +02:00
|
|
|
Map *OsmdroidMap::create(const QString &path, const Projection &proj,
|
|
|
|
bool *isDir)
|
2022-07-05 11:32:14 +02:00
|
|
|
{
|
2023-09-14 18:36:03 +02:00
|
|
|
Q_UNUSED(proj);
|
|
|
|
|
2022-07-05 11:32:14 +02:00
|
|
|
if (isDir)
|
|
|
|
*isDir = false;
|
|
|
|
|
|
|
|
return new OsmdroidMap(path);
|
|
|
|
}
|