2017-03-18 01:30:31 +01:00
|
|
|
#include <QPainter>
|
2018-11-02 20:01:19 +01:00
|
|
|
#include <QDir>
|
2023-12-11 18:54:46 +01:00
|
|
|
#include <QPixmapCache>
|
|
|
|
#include <QtConcurrent>
|
2017-11-26 18:54:03 +01:00
|
|
|
#include "common/rectc.h"
|
2018-11-02 20:01:19 +01:00
|
|
|
#include "common/programpaths.h"
|
2021-08-26 22:22:18 +02:00
|
|
|
#include "common/downloader.h"
|
2018-09-20 07:59:47 +02:00
|
|
|
#include "osm.h"
|
2017-03-18 01:30:31 +01:00
|
|
|
#include "onlinemap.h"
|
|
|
|
|
|
|
|
|
2023-12-11 18:54:46 +01:00
|
|
|
#define MAX_OVERZOOM 3
|
|
|
|
|
|
|
|
static QString cacheName(const QString &file, unsigned overzoom)
|
|
|
|
{
|
|
|
|
return overzoom ? file + ":" + QString::number(overzoom) : file;
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:58:11 +01:00
|
|
|
OnlineMap::OnlineMap(const QString &fileName, const QString &name,
|
|
|
|
const QString &url, const Range &zooms, const RectC &bounds, qreal tileRatio,
|
2023-05-13 15:01:35 +02:00
|
|
|
const QList<HTTPHeader> &headers, int tileSize, bool scalable, bool invertY,
|
2019-05-20 23:23:24 +02:00
|
|
|
bool quadTiles, QObject *parent)
|
2020-12-02 23:58:11 +01:00
|
|
|
: Map(fileName, parent), _name(name), _zooms(zooms), _bounds(bounds),
|
2023-12-11 18:54:46 +01:00
|
|
|
_zoom(_zooms.max()), _tileSize(tileSize), _base(0), _mapRatio(1.0),
|
2023-05-04 09:38:35 +02:00
|
|
|
_tileRatio(tileRatio), _scalable(scalable), _invertY(invertY)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-11-02 20:01:19 +01:00
|
|
|
_tileLoader = new TileLoader(QDir(ProgramPaths::tilesDir()).filePath(_name),
|
|
|
|
this);
|
2023-06-23 09:30:44 +02:00
|
|
|
_tileLoader->setUrl(url, quadTiles ? TileLoader::QuadTiles : TileLoader::XYZ);
|
2023-05-13 15:01:35 +02:00
|
|
|
_tileLoader->setHeaders(headers);
|
2021-04-28 00:01:07 +02:00
|
|
|
connect(_tileLoader, &TileLoader::finished, this, &OnlineMap::tilesLoaded);
|
2023-12-11 18:54:46 +01:00
|
|
|
|
|
|
|
if (_scalable) {
|
|
|
|
_base = _zooms.max();
|
|
|
|
_zooms.setMax(qMin(_zooms.max() + MAX_OVERZOOM, OSM::ZOOMS.max()));
|
|
|
|
}
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-07-13 09:51:41 +02:00
|
|
|
QRectF OnlineMap::bounds()
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-01-28 22:56:08 +01:00
|
|
|
return QRectF(ll2xy(_bounds.topLeft()), ll2xy(_bounds.bottomRight()));
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2017-09-09 12:33:43 +02:00
|
|
|
int OnlineMap::limitZoom(int zoom) const
|
|
|
|
{
|
|
|
|
if (zoom < _zooms.min())
|
|
|
|
return _zooms.min();
|
|
|
|
if (zoom > _zooms.max())
|
|
|
|
return _zooms.max();
|
|
|
|
|
|
|
|
return zoom;
|
|
|
|
}
|
|
|
|
|
2018-04-16 20:26:10 +02:00
|
|
|
int OnlineMap::zoomFit(const QSize &size, const RectC &rect)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-04-16 20:26:10 +02:00
|
|
|
if (!rect.isValid())
|
2017-09-09 12:33:43 +02:00
|
|
|
_zoom = _zooms.max();
|
2017-03-18 01:30:31 +01:00
|
|
|
else {
|
2018-09-25 21:07:44 +02:00
|
|
|
QRectF tbr(OSM::ll2m(rect.topLeft()), OSM::ll2m(rect.bottomRight()));
|
2017-03-18 01:30:31 +01: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-11-17 10:10:35 +01:00
|
|
|
/ coordinatesRatio(), _tileSize));
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2017-06-25 16:23:43 +02:00
|
|
|
return _zoom;
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-06-30 12:14:58 +02:00
|
|
|
qreal OnlineMap::resolution(const QRectF &rect)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-11-17 10:10:35 +01:00
|
|
|
return OSM::resolution(rect.center(), _zoom, _tileSize);
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 22:19:46 +01:00
|
|
|
int OnlineMap::zoomIn()
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2017-09-09 12:33:43 +02:00
|
|
|
_zoom = qMin(_zoom + 1, _zooms.max());
|
2017-06-25 16:23:43 +02:00
|
|
|
return _zoom;
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 22:19:46 +01:00
|
|
|
int OnlineMap::zoomOut()
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2017-09-09 12:33:43 +02:00
|
|
|
_zoom = qMax(_zoom - 1, _zooms.min());
|
2017-06-25 16:23:43 +02:00
|
|
|
return _zoom;
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2023-05-04 09:38:35 +02:00
|
|
|
void OnlineMap::load(const Projection &in, const Projection &out,
|
|
|
|
qreal deviceRatio, bool hidpi)
|
2018-11-15 00:38:03 +01:00
|
|
|
{
|
2023-05-04 09:38:35 +02:00
|
|
|
Q_UNUSED(in);
|
|
|
|
Q_UNUSED(out);
|
|
|
|
|
|
|
|
_mapRatio = hidpi ? deviceRatio : 1.0;
|
2018-11-15 00:38:03 +01:00
|
|
|
|
|
|
|
if (_scalable) {
|
2023-12-11 18:54:46 +01:00
|
|
|
_scaledSize = _tileSize * deviceRatio;
|
2018-11-17 10:10:35 +01:00
|
|
|
_tileRatio = deviceRatio;
|
2018-11-15 00:38:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-18 21:06:36 +02:00
|
|
|
qreal OnlineMap::coordinatesRatio() const
|
|
|
|
{
|
2018-11-17 10:10:35 +01:00
|
|
|
return _mapRatio > 1.0 ? _mapRatio / _tileRatio : 1.0;
|
2018-08-18 21:06:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal OnlineMap::imageRatio() const
|
|
|
|
{
|
2018-11-17 10:10:35 +01:00
|
|
|
return _mapRatio > 1.0 ? _mapRatio : _tileRatio;
|
2018-08-18 21:06:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal OnlineMap::tileSize() const
|
|
|
|
{
|
2018-11-17 10:10:35 +01:00
|
|
|
return (_tileSize / coordinatesRatio());
|
2018-08-18 21:06:36 +02:00
|
|
|
}
|
|
|
|
|
2023-12-11 18:54:46 +01:00
|
|
|
QPoint OnlineMap::tileCoordinates(int x, int y, int zoom)
|
|
|
|
{
|
|
|
|
return QPoint(x, _invertY ? (1<<zoom) - y - 1 : y);
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:26:10 +02:00
|
|
|
void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2023-12-11 18:54:46 +01:00
|
|
|
int base = _scalable ? qMin(_base, _zoom) : _zoom;
|
|
|
|
unsigned overzoom = _zoom - base;
|
|
|
|
unsigned f = 1U<<overzoom;
|
|
|
|
|
|
|
|
qreal scale = OSM::zoom2scale(base, _tileSize * f);
|
2018-09-25 21:07:44 +02:00
|
|
|
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
|
2023-12-11 18:54:46 +01:00
|
|
|
-rect.topLeft().y() * scale) * coordinatesRatio(), base);
|
|
|
|
Coordinates ctl(OSM::tile2ll(tile, base));
|
2023-11-18 21:32:30 +01:00
|
|
|
QPointF tl(ll2xy(Coordinates(ctl.lon(), -ctl.lat())));
|
|
|
|
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
|
2023-12-11 18:54:46 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 01:30:31 +01:00
|
|
|
|
2018-08-23 20:26:10 +02:00
|
|
|
if (flags & Map::Block)
|
2023-12-11 18:54:46 +01:00
|
|
|
_tileLoader->loadTilesSync(fetchTiles);
|
2017-03-18 01:30:31 +01:00
|
|
|
else
|
2023-12-11 18:54:46 +01:00
|
|
|
_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);
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 18:54:46 +01:00
|
|
|
void OnlineMap::drawTile(QPainter *painter, QPixmap &pixmap, QPointF &tp)
|
|
|
|
{
|
|
|
|
pixmap.setDevicePixelRatio(imageRatio());
|
|
|
|
painter->drawPixmap(tp, pixmap);
|
|
|
|
}
|
|
|
|
|
2018-07-13 09:51:41 +02:00
|
|
|
QPointF OnlineMap::ll2xy(const Coordinates &c)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-11-17 10:10:35 +01:00
|
|
|
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
|
2018-09-25 21:07:44 +02:00
|
|
|
QPointF m = OSM::ll2m(c);
|
2018-08-18 21:06:36 +02:00
|
|
|
return QPointF(m.x() / scale, m.y() / -scale) / coordinatesRatio();
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-07-13 09:51:41 +02:00
|
|
|
Coordinates OnlineMap::xy2ll(const QPointF &p)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-11-17 10:10:35 +01:00
|
|
|
qreal scale = OSM::zoom2scale(_zoom, _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());
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
2023-12-11 18:54:46 +01:00
|
|
|
|
|
|
|
void OnlineMap::clearCache()
|
|
|
|
{
|
|
|
|
_tileLoader->clearCache();
|
|
|
|
QPixmapCache::clear();
|
|
|
|
}
|