#include #include #include #include #include #include #include #include "common/util.h" #include "osm.h" #include "mbtilesmap.h" #define META_TYPE(type) static_cast(type) 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; } 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 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"; return; } if (_zooms.first() < 0) { _errorString = "Invalid zoom levels"; 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< zoom) break; _zi = i; } } return _zi; } qreal MBTilesMap::resolution(const QRectF &rect) { return OSM::resolution(rect.center(), _zooms.at(_zi), _tileSize); } int MBTilesMap::zoomIn() { cancelJobs(false); _zi = qMin(_zi + 1, _zooms.size() - 1); return _zi; } int MBTilesMap::zoomOut() { cancelJobs(false); _zi = qMax(_zi - 1, 0); return _zi; } qreal MBTilesMap::coordinatesRatio() const { return _mapRatio > 1.0 ? _mapRatio / _tileRatio : 1.0; } qreal MBTilesMap::imageRatio() const { return _mapRatio > 1.0 ? _mapRatio : _tileRatio; } qreal MBTilesMap::tileSize() const { return (_tileSize / coordinatesRatio()); } 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< &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 &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); } 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())); int width = ceil(s.width() / tileSize()); int height = ceil(s.height() / tileSize()); QList tiles; 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 (isRunning(key)) 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()); drawTile(painter, pm, tp); } else { tiles.append(MBTile(zoom, _scaledSize, t, tileData(zoom, t), key)); } } } if (!tiles.isEmpty()) { if (flags & Map::Block || !_scalable) { QFuture future = QtConcurrent::map(tiles, &MBTile::load); future.waitForFinished(); for (int i = 0; i < tiles.size(); i++) { const MBTile &mt = tiles.at(i); QPixmap pm(mt.pixmap()); if (pm.isNull()) continue; 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()); drawTile(painter, pm, tp); } } else runJob(new MBTilesMapJob(tiles)); } } void MBTilesMap::drawTile(QPainter *painter, QPixmap &pixmap, QPointF &tp) { pixmap.setDevicePixelRatio(imageRatio()); painter->drawPixmap(tp, pixmap); } QPointF MBTilesMap::ll2xy(const Coordinates &c) { qreal scale = OSM::zoom2scale(_zooms.at(_zi), _tileSize); QPointF m = OSM::ll2m(c); return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio(); } Coordinates MBTilesMap::xy2ll(const QPointF &p) { qreal scale = OSM::zoom2scale(_zooms.at(_zi), _tileSize); return OSM::m2ll(QPointF(p.x() * scale, -p.y() * scale) * coordinatesRatio()); } Map *MBTilesMap::create(const QString &path, const Projection &proj, bool *isDir) { Q_UNUSED(proj); if (isDir) *isDir = false; return new MBTilesMap(path); }