1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-07 07:13:21 +02:00
GPXSee/src/map/onlinemap.cpp

159 lines
3.7 KiB
C++
Raw Normal View History

#include <QPainter>
2017-11-26 18:54:03 +01:00
#include "common/coordinates.h"
#include "common/rectc.h"
#include "common/wgs84.h"
#include "downloader.h"
#include "config.h"
#include "onlinemap.h"
2017-03-20 09:25:05 +01:00
#define TILE_SIZE 256
2018-04-17 22:26:20 +02:00
#define EPSILON 1e-6
static QPointF ll2m(const Coordinates &c)
{
return QPointF(c.lon(), rad2deg(log(tan(M_PI/4.0 + deg2rad(c.lat())/2.0))));
}
static Coordinates m2ll(const QPointF &p)
{
return Coordinates(p.x(), rad2deg(2 * atan(exp(deg2rad(p.y()))) - M_PI/2));
}
static QPoint mercator2tile(const QPointF &m, int z)
{
QPoint tile;
tile.setX((int)(floor((m.x() + 180.0) / 360.0 * (1<<z))));
tile.setY((int)(floor((1.0 - (m.y() / 180.0)) / 2.0 * (1<<z))));
return tile;
}
2017-06-25 16:23:43 +02:00
static qreal zoom2scale(int zoom)
{
2017-06-26 00:20:42 +02:00
return (360.0/(qreal)((1<<zoom) * TILE_SIZE));
2017-06-25 16:23:43 +02:00
}
static int scale2zoom(qreal scale)
{
2018-04-17 22:26:20 +02:00
return (int)(log2(360.0/(scale * (qreal)TILE_SIZE)) + EPSILON);
}
OnlineMap::OnlineMap(const QString &name, const QString &url,
2018-04-27 21:13:10 +02:00
const Range &zooms, const RectC &bounds, const Authorization &authorization,
QObject *parent) : Map(parent), _name(name), _zooms(zooms), _bounds(bounds),
_block(false), _valid(false)
{
2018-02-25 02:31:01 +01:00
QString dir(TILES_DIR + "/" + _name);
_zoom = _zooms.max();
_tileLoader = new TileLoader(this);
_tileLoader->setUrl(url);
_tileLoader->setDir(dir);
2018-04-27 21:13:10 +02:00
_tileLoader->setAuthorization(authorization);
connect(_tileLoader, SIGNAL(finished()), this, SIGNAL(loaded()));
2018-02-25 02:31:01 +01:00
if (!QDir().mkpath(dir)) {
_errorString = "Error creating tiles dir";
return;
}
_valid = true;
}
QRectF OnlineMap::bounds() const
{
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 {
QRectF tbr(ll2m(rect.topLeft()), ll2m(rect.bottomRight()));
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
_zoom = limitZoom(scale2zoom(qMax(sc.x(), -sc.y())));
}
2017-06-25 16:23:43 +02:00
return _zoom;
}
qreal OnlineMap::resolution(const QRectF &rect) const
{
2017-06-25 16:23:43 +02:00
qreal scale = zoom2scale(_zoom);
2017-11-27 23:50:55 +01:00
return (WGS84_RADIUS * 2.0 * M_PI * scale / 360.0
* cos(2.0 * atan(exp(deg2rad(-rect.center().y() * scale))) - M_PI/2));
}
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::draw(QPainter *painter, const QRectF &rect)
{
2017-06-25 16:23:43 +02:00
qreal scale = zoom2scale(_zoom);
QPoint tile = mercator2tile(QPointF(rect.topLeft().x() * scale,
-rect.topLeft().y() * scale), _zoom);
QPoint tl = QPoint((int)floor(rect.left() / (qreal)TILE_SIZE)
* TILE_SIZE, (int)floor(rect.top() / TILE_SIZE) * TILE_SIZE);
QList<Tile> tiles;
QSizeF s(rect.right() - tl.x(), rect.bottom() - tl.y());
2017-03-20 09:25:05 +01:00
for (int i = 0; i < ceil(s.width() / TILE_SIZE); i++)
for (int j = 0; j < ceil(s.height() / TILE_SIZE); j++)
2017-06-25 16:23:43 +02:00
tiles.append(Tile(QPoint(tile.x() + i, tile.y() + j), _zoom));
if (_block)
_tileLoader->loadTilesSync(tiles);
else
_tileLoader->loadTilesAsync(tiles);
for (int i = 0; i < tiles.count(); i++) {
Tile &t = tiles[i];
2017-03-20 09:25:05 +01:00
QPoint tp(tl.x() + (t.xy().x() - tile.x()) * TILE_SIZE,
tl.y() + (t.xy().y() - tile.y()) * TILE_SIZE);
2018-02-20 23:37:19 +01:00
if (t.pixmap().isNull())
painter->fillRect(QRect(tp, QSize(TILE_SIZE, TILE_SIZE)),
_backgroundColor);
else
painter->drawPixmap(tp, t.pixmap());
}
}
QPointF OnlineMap::ll2xy(const Coordinates &c) const
{
2017-06-25 16:23:43 +02:00
qreal scale = zoom2scale(_zoom);
QPointF m = ll2m(c);
2017-06-25 16:23:43 +02:00
return QPointF(m.x() / scale, m.y() / -scale);
}
Coordinates OnlineMap::xy2ll(const QPointF &p) const
{
2017-06-25 16:23:43 +02:00
qreal scale = zoom2scale(_zoom);
return m2ll(QPointF(p.x() * scale, -p.y() * scale));
}