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

155 lines
4.0 KiB
C++
Raw Normal View History

2018-10-06 23:14:46 +02:00
#include <QtCore>
#include <QPainter>
#include <QDir>
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"
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),
_zoom(_zooms.max()), _tileSize(tileSize), _mapRatio(1.0),
_tileRatio(tileRatio), _scalable(scalable), _invertY(invertY)
{
_tileLoader = new TileLoader(QDir(ProgramPaths::tilesDir()).filePath(_name),
this);
_tileLoader->setUrl(url);
_tileLoader->setHeaders(headers);
2019-05-20 23:23:24 +02:00
_tileLoader->setQuadTiles(quadTiles);
2021-04-28 00:01:07 +02:00
connect(_tileLoader, &TileLoader::finished, this, &OnlineMap::tilesLoaded);
}
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()
{
_zoom = qMin(_zoom + 1, _zooms.max());
2017-06-25 16:23:43 +02:00
return _zoom;
}
int OnlineMap::zoomOut()
{
_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) {
2019-05-20 23:23:24 +02:00
_tileLoader->setScaledSize(_tileSize * deviceRatio);
_tileRatio = deviceRatio;
}
}
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
}
2018-08-23 20:26:10 +02:00
void OnlineMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
qreal scale = OSM::zoom2scale(_zoom, _tileSize);
2022-09-14 00:21:50 +02:00
QRectF b(bounds());
2018-09-25 21:07:44 +02:00
QPoint tile = OSM::mercator2tile(QPointF(rect.topLeft().x() * scale,
2018-08-18 21:06:36 +02:00
-rect.topLeft().y() * scale) * coordinatesRatio(), _zoom);
QPointF tl(floor(rect.left() / tileSize())
* tileSize(), floor(rect.top() / tileSize()) * tileSize());
2022-09-14 00:21:50 +02:00
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());
2018-10-05 07:10:49 +02:00
2022-07-06 14:58:41 +02:00
QVector<FetchTile> tiles;
2018-10-05 07:10:49 +02:00
tiles.reserve(width * height);
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
2022-07-06 14:58:41 +02:00
tiles.append(FetchTile(QPoint(tile.x() + i, _invertY ? (1<<_zoom)
2019-05-20 23:23:24 +02:00
- (tile.y() + j) - 1 : tile.y() + j), _zoom));
2018-08-23 20:26:10 +02:00
if (flags & Map::Block)
_tileLoader->loadTilesSync(tiles);
else
_tileLoader->loadTilesAsync(tiles);
for (int i = 0; i < tiles.count(); i++) {
2022-07-06 14:58:41 +02:00
FetchTile &t = tiles[i];
2022-09-14 00:21:50 +02:00
QPointF tp(qMax(tl.x(), b.left()) + (t.xy().x() - tile.x()) * tileSize(),
qMax(tl.y(), b.top()) + ((_invertY ? (1<<_zoom) - t.xy().y() - 1 :
t.xy().y()) - tile.y()) * tileSize());
2018-08-18 21:06:36 +02:00
if (!t.pixmap().isNull()) {
t.pixmap().setDevicePixelRatio(imageRatio());
2018-02-20 23:37:19 +01:00
painter->drawPixmap(tp, t.pixmap());
2018-08-18 21:06:36 +02:00
}
}
}
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());
}