Skip to content

Commit

Permalink
Added support for online vector maps overzoom
Browse files Browse the repository at this point in the history
  • Loading branch information
tumic0 committed Dec 11, 2023
1 parent 6f1f5fd commit 148fc76
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 185 deletions.
8 changes: 4 additions & 4 deletions src/map/aqmmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ void AQMMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
int width = ceil(s.width() / tileSize());
int height = ceil(s.height() / tileSize());

QList<RenderTile> tiles;
QList<DataTile> tiles;

for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Expand All @@ -369,15 +369,15 @@ void AQMMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
tl.y() + (t.y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
} else
tiles.append(RenderTile(t, tileData(t), key));
tiles.append(DataTile(t, tileData(t), key));
}
}

QFuture<void> future = QtConcurrent::map(tiles, &RenderTile::load);
QFuture<void> future = QtConcurrent::map(tiles, &DataTile::load);
future.waitForFinished();

for (int i = 0; i < tiles.size(); i++) {
const RenderTile &mt = tiles.at(i);
const DataTile &mt = tiles.at(i);
QPixmap pm(mt.pixmap());
if (pm.isNull())
continue;
Expand Down
8 changes: 4 additions & 4 deletions src/map/gemfmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ void GEMFMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
int width = ceil(s.width() / tileSize());
int height = ceil(s.height() / tileSize());

QList<RenderTile> tiles;
QList<DataTile> tiles;

for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Expand All @@ -265,15 +265,15 @@ void GEMFMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
tl.y() + (t.y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
} else
tiles.append(RenderTile(t, tileData(t), key));
tiles.append(DataTile(t, tileData(t), key));
}
}

QFuture<void> future = QtConcurrent::map(tiles, &RenderTile::load);
QFuture<void> future = QtConcurrent::map(tiles, &DataTile::load);
future.waitForFinished();

for (int i = 0; i < tiles.size(); i++) {
const RenderTile &mt = tiles.at(i);
const DataTile &mt = tiles.at(i);
QPixmap pm(mt.pixmap());
if (pm.isNull())
continue;
Expand Down
112 changes: 84 additions & 28 deletions src/map/onlinemap.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
#include <QPainter>
#include <QDir>
#include <QPixmapCache>
#include <QtConcurrent>
#include "common/rectc.h"
#include "common/programpaths.h"
#include "common/downloader.h"
#include "osm.h"
#include "onlinemap.h"


#define MAX_OVERZOOM 3

static QString cacheName(const QString &file, unsigned overzoom)
{
return overzoom ? file + ":" + QString::number(overzoom) : file;
}

OnlineMap::OnlineMap(const QString &fileName, const QString &name,
const QString &url, const Range &zooms, const RectC &bounds, qreal tileRatio,
const QList<HTTPHeader> &headers, int tileSize, bool scalable, bool invertY,
bool quadTiles, QObject *parent)
: Map(fileName, parent), _name(name), _zooms(zooms), _bounds(bounds),
_zoom(_zooms.max()), _tileSize(tileSize), _mapRatio(1.0),
_zoom(_zooms.max()), _tileSize(tileSize), _base(0), _mapRatio(1.0),
_tileRatio(tileRatio), _scalable(scalable), _invertY(invertY)
{
_tileLoader = new TileLoader(QDir(ProgramPaths::tilesDir()).filePath(_name),
this);
_tileLoader->setUrl(url, quadTiles ? TileLoader::QuadTiles : TileLoader::XYZ);
_tileLoader->setHeaders(headers);
connect(_tileLoader, &TileLoader::finished, this, &OnlineMap::tilesLoaded);

if (_scalable) {
_base = _zooms.max();
_zooms.setMax(qMin(_zooms.max() + MAX_OVERZOOM, OSM::ZOOMS.max()));
}
}

QRectF OnlineMap::bounds()
Expand Down Expand Up @@ -77,7 +91,7 @@ void OnlineMap::load(const Projection &in, const Projection &out,
_mapRatio = hidpi ? deviceRatio : 1.0;

if (_scalable) {
_tileLoader->setScaledSize(_tileSize * deviceRatio);
_scaledSize = _tileSize * deviceRatio;
_tileRatio = deviceRatio;
}
}
Expand All @@ -97,43 +111,79 @@ qreal OnlineMap::tileSize() const
return (_tileSize / coordinatesRatio());
}

QPoint OnlineMap::tileCoordinates(int x, int y, int zoom)
{
return QPoint(x, _invertY ? (1<<zoom) - y - 1 : y);
}

void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
int base = _scalable ? qMin(_base, _zoom) : _zoom;
unsigned overzoom = _zoom - base;
unsigned f = 1U<<overzoom;

qreal scale = OSM::zoom2scale(base, _tileSize * f);
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
-rect.topLeft().y() * scale) * coordinatesRatio(), _zoom);
Coordinates ctl(OSM::tile2ll(tile, _zoom));
-rect.topLeft().y() * scale) * coordinatesRatio(), base);
Coordinates ctl(OSM::tile2ll(tile, base));
QPointF tl(ll2xy(Coordinates(ctl.lon(), -ctl.lat())));
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
int width = ceil(s.width() / tileSize());
int height = ceil(s.height() / tileSize());

QVector<FetchTile> tiles;
tiles.reserve(width * height);
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
tiles.append(FetchTile(QPoint(tile.x() + i, _invertY ? (1<<_zoom)
- (tile.y() + j) - 1 : tile.y() + j), _zoom));
int width = ceil(s.width() / (tileSize() * f));
int height = ceil(s.height() / (tileSize() * f));

QVector<TileLoader::Tile> fetchTiles;
fetchTiles.reserve(width * height);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
QPoint tc(tileCoordinates(tile.x() + i, tile.y() + j, base));
fetchTiles.append(TileLoader::Tile(tc, base));
}
}

if (flags & Map::Block)
_tileLoader->loadTilesSync(tiles);
_tileLoader->loadTilesSync(fetchTiles);
else
_tileLoader->loadTilesAsync(tiles);

for (int i = 0; i < tiles.count(); i++) {
FetchTile &t = tiles[i];
QPointF tp(tl.x() + (t.xy().x() - tile.x()) * tileSize(),
tl.y() + ((_invertY
? (1<<_zoom) - t.xy().y() - 1
: t.xy().y()) - tile.y()) * tileSize());

if (!t.pixmap().isNull()) {
t.pixmap().setDevicePixelRatio(imageRatio());
painter->drawPixmap(tp, t.pixmap());
}
_tileLoader->loadTilesAsync(fetchTiles);

QList<OnlineTile> renderTiles;
for (int i = 0; i < fetchTiles.count(); i++) {
const TileLoader::Tile &t = fetchTiles.at(i);
if (t.file().isNull())
continue;

QPixmap pm;
if (QPixmapCache::find(cacheName(t.file(), overzoom), &pm)) {
QPointF tp(tl.x() + (t.xy().x() - tile.x()) * tileSize() * f,
tl.y() + (t.xy().y() - tile.y()) * tileSize() * f);
drawTile(painter, pm, tp);
} else
renderTiles.append(OnlineTile(t.xy(), t.file(), _zoom, overzoom,
_scaledSize));
}

QFuture<void> future = QtConcurrent::map(renderTiles, &OnlineTile::load);
future.waitForFinished();

for (int i = 0; i < renderTiles.size(); i++) {
const OnlineTile &mt = renderTiles.at(i);
QPixmap pm(mt.pixmap());
if (pm.isNull())
continue;

QPixmapCache::insert(cacheName(mt.file(), overzoom), pm);

QPointF tp(tl.x() + (mt.xy().x() - tile.x()) * tileSize() * f,
tl.y() + (mt.xy().y() - tile.y()) * tileSize() * f);
drawTile(painter, pm, tp);
}
}

void OnlineMap::drawTile(QPainter *painter, QPixmap &pixmap, QPointF &tp)
{
pixmap.setDevicePixelRatio(imageRatio());
painter->drawPixmap(tp, pixmap);
}

QPointF OnlineMap::ll2xy(const Coordinates &c)
{
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
Expand All @@ -147,3 +197,9 @@ Coordinates OnlineMap::xy2ll(const QPointF &p)
return OSM::m2ll(QPointF(p.x() * scale, -p.y() * scale)
* coordinatesRatio());
}

void OnlineMap::clearCache()
{
_tileLoader->clearCache();
QPixmapCache::clear();
}
39 changes: 38 additions & 1 deletion src/map/onlinemap.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
#ifndef ONLINEMAP_H
#define ONLINEMAP_H

#include <QImageReader>
#include <QPixmap>
#include "common/range.h"
#include "common/rectc.h"
#include "map.h"
#include "tileloader.h"

class OnlineTile
{
public:
OnlineTile(const QPoint &xy, const QString &file, int zoom, int overzoom,
int scaledSize) : _xy(xy), _file(file), _zoom(zoom), _overzoom(overzoom),
_scaledSize(scaledSize) {}

void load()
{
QByteArray format(_overzoom
? QByteArray::number(_zoom) + ';' + QByteArray::number(_overzoom)
: QByteArray::number(_zoom));
QImageReader reader(_file, format);
if (_scaledSize)
reader.setScaledSize(QSize(_scaledSize, _scaledSize));
_pixmap = QPixmap::fromImage(reader.read());
}

const QPoint &xy() const {return _xy;}
const QString &file() const {return _file;}
const QPixmap &pixmap() const {return _pixmap;}

private:
QPoint _xy;
QString _file;
int _zoom;
int _overzoom;
int _scaledSize;
QPixmap _pixmap;
};

class OnlineMap : public Map
{
Q_OBJECT
Expand Down Expand Up @@ -35,22 +68,26 @@ class OnlineMap : public Map

void load(const Projection &in, const Projection &out, qreal deviceRatio,
bool hidpi);
void clearCache() {_tileLoader->clearCache();}
void clearCache();

private:
int limitZoom(int zoom) const;
qreal tileSize() const;
qreal coordinatesRatio() const;
qreal imageRatio() const;
QPoint tileCoordinates(int x, int y, int zoom);
void drawTile(QPainter *painter, QPixmap &pixmap, QPointF &tp);

TileLoader *_tileLoader;
QString _name;
Range _zooms;
RectC _bounds;
int _zoom;
int _tileSize;
int _base;
qreal _mapRatio, _tileRatio;
bool _scalable;
int _scaledSize;
bool _invertY;
};

Expand Down
8 changes: 4 additions & 4 deletions src/map/osmdroidmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void OsmdroidMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
int width = ceil(s.width() / tileSize());
int height = ceil(s.height() / tileSize());

QList<RenderTile> tiles;
QList<DataTile> tiles;

for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Expand All @@ -260,15 +260,15 @@ void OsmdroidMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
tl.y() + (t.y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
} else
tiles.append(RenderTile(t, tileData(_zoom, t), key));
tiles.append(DataTile(t, tileData(_zoom, t), key));
}
}

QFuture<void> future = QtConcurrent::map(tiles, &RenderTile::load);
QFuture<void> future = QtConcurrent::map(tiles, &DataTile::load);
future.waitForFinished();

for (int i = 0; i < tiles.size(); i++) {
const RenderTile &mt = tiles.at(i);
const DataTile &mt = tiles.at(i);
QPixmap pm(mt.pixmap());
if (pm.isNull())
continue;
Expand Down
8 changes: 4 additions & 4 deletions src/map/sqlitemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void SqliteMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
int width = ceil(s.width() / tileSize());
int height = ceil(s.height() / tileSize());

QList<RenderTile> tiles;
QList<DataTile> tiles;

for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Expand All @@ -207,15 +207,15 @@ void SqliteMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
tl.y() + (t.y() - tile.y()) * tileSize());
drawTile(painter, pm, tp);
} else
tiles.append(RenderTile(t, tileData(_zoom, t), key));
tiles.append(DataTile(t, tileData(_zoom, t), key));
}
}

QFuture<void> future = QtConcurrent::map(tiles, &RenderTile::load);
QFuture<void> future = QtConcurrent::map(tiles, &DataTile::load);
future.waitForFinished();

for (int i = 0; i < tiles.size(); i++) {
const RenderTile &mt = tiles.at(i);
const DataTile &mt = tiles.at(i);
QPixmap pm(mt.pixmap());
if (pm.isNull())
continue;
Expand Down
Loading

0 comments on commit 148fc76

Please sign in to comment.