1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-12-11 03:29:09 +01:00
GPXSee/src/map/onlinemap.cpp

269 lines
6.6 KiB
C++
Raw Normal View History

#include <QPainter>
#include <QDir>
#include <QPixmapCache>
2017-11-26 18:54:03 +01:00
#include "common/rectc.h"
#include "common/programpaths.h"
#include "common/downloader.h"
2018-09-20 07:59:47 +02:00
#include "osm.h"
#include "onlinemap.h"
#define MAX_OVERZOOM 3
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,
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 21:13:11 +01:00
_zoom(_zooms.max()), _tileSize(tileSize), _baseZoom(0), _mapRatio(1.0),
_tileRatio(tileRatio), _scalable(scalable), _scaledSize(0), _invertY(invertY)
{
_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);
_tileLoader->setHeaders(headers);
2021-04-28 00:01:07 +02:00
connect(_tileLoader, &TileLoader::finished, this, &OnlineMap::tilesLoaded);
if (_scalable) {
2023-12-11 21:13:11 +01:00
_baseZoom = _zooms.max();
_zooms.setMax(qMin(_zooms.max() + MAX_OVERZOOM, OSM::ZOOMS.max()));
}
}
2018-07-13 09:51:41 +02:00
QRectF OnlineMap::bounds()
{
return QRectF(ll2xy(_bounds.topLeft()), ll2xy(_bounds.bottomRight()));
}
int OnlineMap::limitZoom(int zoom) const
{
if (zoom < _zooms.min())
return _zooms.min();
if (zoom > _zooms.max())
return _zooms.max();
return zoom;
}
int OnlineMap::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()));
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())
/ coordinatesRatio(), _tileSize));
}
2017-06-25 16:23:43 +02:00
return _zoom;
}
qreal OnlineMap::resolution(const QRectF &rect)
{
return OSM::resolution(rect.center(), _zoom, _tileSize);
}
int OnlineMap::zoomIn()
{
cancelJobs(false);
_zoom = qMin(_zoom + 1, _zooms.max());
2017-06-25 16:23:43 +02:00
return _zoom;
}
int OnlineMap::zoomOut()
{
cancelJobs(false);
_zoom = qMax(_zoom - 1, _zooms.min());
2017-06-25 16:23:43 +02:00
return _zoom;
}
void OnlineMap::load(const Projection &in, const Projection &out,
qreal deviceRatio, bool hidpi)
{
Q_UNUSED(in);
Q_UNUSED(out);
_mapRatio = hidpi ? deviceRatio : 1.0;
if (_scalable) {
_scaledSize = _tileSize * deviceRatio;
_tileRatio = deviceRatio;
}
}
void OnlineMap::unload()
{
cancelJobs(true);
}
2018-08-18 21:06:36 +02:00
qreal OnlineMap::coordinatesRatio() const
{
return _mapRatio > 1.0 ? _mapRatio / _tileRatio : 1.0;
2018-08-18 21:06:36 +02:00
}
qreal OnlineMap::imageRatio() const
{
return _mapRatio > 1.0 ? _mapRatio : _tileRatio;
2018-08-18 21:06:36 +02:00
}
qreal OnlineMap::tileSize() const
{
return (_tileSize / coordinatesRatio());
2018-08-18 21:06:36 +02:00
}
QPoint OnlineMap::tileCoordinates(int x, int y, int zoom)
{
return QPoint(x, _invertY ? (1<<zoom) - y - 1 : y);
}
bool OnlineMap::isRunning(const QString &key) const
{
for (int i = 0; i < _jobs.size(); i++) {
const QList<OnlineMapTile> &tiles = _jobs.at(i)->tiles();
for (int j = 0; j < tiles.size(); j++)
if (tiles.at(j).key() == key)
return true;
}
return false;
}
void OnlineMap::runJob(OnlineMapJob *job)
{
_jobs.append(job);
connect(job, &OnlineMapJob::finished, this, &OnlineMap::jobFinished);
job->run();
}
void OnlineMap::removeJob(OnlineMapJob *job)
{
_jobs.removeOne(job);
job->deleteLater();
}
void OnlineMap::jobFinished(OnlineMapJob *job)
{
const QList<OnlineMapTile> &tiles = job->tiles();
for (int i = 0; i < tiles.size(); i++) {
const OnlineMapTile &mt = tiles.at(i);
if (!mt.pixmap().isNull())
QPixmapCache::insert(mt.key(), mt.pixmap());
}
removeJob(job);
emit tilesLoaded();
}
void OnlineMap::cancelJobs(bool wait)
{
for (int i = 0; i < _jobs.size(); i++)
_jobs.at(i)->cancel(wait);
}
2018-08-23 20:26:10 +02:00
void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
2023-12-11 21:13:11 +01:00
int baseZoom = _scalable ? qMin(_baseZoom, _zoom) : _zoom;
unsigned overzoom = _zoom - baseZoom;
unsigned f = 1U<<overzoom;
2023-12-11 21:13:11 +01:00
qreal scale = OSM::zoom2scale(baseZoom, _tileSize * f);
2018-09-25 21:07:44 +02:00
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
2023-12-11 21:13:11 +01:00
-rect.topLeft().y() * scale) * coordinatesRatio(), baseZoom);
Coordinates ctl(OSM::tile2ll(tile, baseZoom));
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());
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++) {
2023-12-11 21:13:11 +01:00
QPoint tc(tileCoordinates(tile.x() + i, tile.y() + j, baseZoom));
fetchTiles.append(TileLoader::Tile(tc, baseZoom));
}
}
2018-08-23 20:26:10 +02:00
if (flags & Map::Block)
_tileLoader->loadTilesSync(fetchTiles);
else
_tileLoader->loadTilesAsync(fetchTiles);
QList<OnlineMapTile> renderTiles;
for (int i = 0; i < fetchTiles.count(); i++) {
const TileLoader::Tile &t = fetchTiles.at(i);
if (t.file().isNull())
continue;
QString key(overzoom
? t.file() + ":" + QString::number(overzoom) : t.file());
if (isRunning(key))
continue;
QPixmap pm;
if (QPixmapCache::find(key, &pm)) {
2023-12-11 21:13:11 +01:00
QPoint tc(tileCoordinates(t.xy().x(), t.xy().y(), baseZoom));
2023-12-11 20:31:13 +01:00
QPointF tp(tl.x() + (tc.x() - tile.x()) * tileSize() * f,
tl.y() + (tc.y() - tile.y()) * tileSize() * f);
drawTile(painter, pm, tp);
} else
renderTiles.append(OnlineMapTile(t.xy(), t.file(), _zoom, overzoom,
_scaledSize, key));
}
if (!renderTiles.isEmpty()) {
if (flags & Map::Block || !_scalable) {
QFuture<void> future = QtConcurrent::map(renderTiles,
&OnlineMapTile::load);
future.waitForFinished();
for (int i = 0; i < renderTiles.size(); i++) {
const OnlineMapTile &mt = renderTiles.at(i);
QPixmap pm(mt.pixmap());
if (pm.isNull())
continue;
QPixmapCache::insert(mt.key(), pm);
2023-12-11 21:13:11 +01:00
QPoint tc(tileCoordinates(mt.xy().x(), mt.xy().y(), baseZoom));
2023-12-11 20:31:13 +01:00
QPointF tp(tl.x() + (tc.x() - tile.x()) * tileSize() * f,
tl.y() + (tc.y() - tile.y()) * tileSize() * f);
drawTile(painter, pm, tp);
}
} else
runJob(new OnlineMapJob(renderTiles));
}
}
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)
{
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();
}
2018-07-13 09:51:41 +02:00
Coordinates OnlineMap::xy2ll(const QPointF &p)
{
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());
}
void OnlineMap::clearCache()
{
_tileLoader->clearCache();
QPixmapCache::clear();
}