2017-03-18 01:30:31 +01:00
|
|
|
#include <QPainter>
|
2017-11-26 18:54:03 +01:00
|
|
|
#include "common/coordinates.h"
|
|
|
|
#include "common/rectc.h"
|
|
|
|
#include "common/wgs84.h"
|
2017-03-18 01:30:31 +01:00
|
|
|
#include "downloader.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "onlinemap.h"
|
|
|
|
|
|
|
|
|
2017-03-20 09:25:05 +01:00
|
|
|
#define TILE_SIZE 256
|
2017-03-18 01:30:31 +01:00
|
|
|
|
2018-02-16 19:37:04 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
static int scale2zoom(qreal scale)
|
|
|
|
{
|
2017-06-26 00:20:42 +02:00
|
|
|
return (int)log2(360.0/(scale * (qreal)TILE_SIZE));
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-09 12:33:43 +02:00
|
|
|
OnlineMap::OnlineMap(const QString &name, const QString &url,
|
2018-02-25 02:31:01 +01:00
|
|
|
const Range &zooms, const RectC &bounds, QObject *parent) :
|
|
|
|
Map(parent), _name(name), _zooms(zooms), _bounds(bounds),
|
|
|
|
_block(false), _valid(false)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2018-02-25 02:31:01 +01:00
|
|
|
QString dir(TILES_DIR + "/" + _name);
|
|
|
|
|
2018-01-28 22:56:08 +01:00
|
|
|
_zoom = _zooms.max();
|
2018-02-25 02:31:01 +01:00
|
|
|
_tileLoader = TileLoader(url, dir);
|
|
|
|
|
|
|
|
if (!QDir().mkpath(dir)) {
|
|
|
|
_errorString = "Error creating tiles dir";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_valid = true;
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2017-10-04 23:15:39 +02:00
|
|
|
void OnlineMap::load()
|
|
|
|
{
|
2018-02-20 23:37:19 +01:00
|
|
|
connect(TileLoader::downloader(), SIGNAL(finished()), this,
|
|
|
|
SLOT(emitLoaded()));
|
2017-10-04 23:15:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnlineMap::unload()
|
|
|
|
{
|
2018-02-20 23:37:19 +01:00
|
|
|
disconnect(TileLoader::downloader(), SIGNAL(finished()), this,
|
|
|
|
SLOT(emitLoaded()));
|
2017-09-15 00:07:09 +02:00
|
|
|
}
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
void OnlineMap::emitLoaded()
|
|
|
|
{
|
|
|
|
emit loaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF OnlineMap::bounds() const
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2017-06-29 22:29:27 +02:00
|
|
|
qreal OnlineMap::zoomFit(const QSize &size, const RectC &br)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2017-06-30 18:15:22 +02:00
|
|
|
if (!br.isValid())
|
2017-09-09 12:33:43 +02:00
|
|
|
_zoom = _zooms.max();
|
2017-03-18 01:30:31 +01:00
|
|
|
else {
|
2018-02-16 19:37:04 +01:00
|
|
|
QRectF tbr(ll2m(br.topLeft()), ll2m(br.bottomRight()));
|
2017-03-18 01:30:31 +01:00
|
|
|
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
|
2017-06-26 00:20:42 +02:00
|
|
|
|
2017-09-09 12:33:43 +02:00
|
|
|
_zoom = limitZoom(scale2zoom(qMax(sc.x(), sc.y())));
|
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
|
|
|
}
|
|
|
|
|
2017-06-26 00:20:42 +02:00
|
|
|
qreal OnlineMap::zoomFit(qreal resolution, const Coordinates &c)
|
|
|
|
{
|
2017-11-27 23:50:55 +01:00
|
|
|
_zoom = limitZoom((int)log2((WGS84_RADIUS * 2.0 * M_PI
|
|
|
|
* cos(deg2rad(c.lat()))) / (resolution * TILE_SIZE)));
|
2017-06-26 00:20:42 +02:00
|
|
|
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
qreal OnlineMap::resolution(const QPointF &p) 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
|
2017-06-25 16:23:43 +02:00
|
|
|
* cos(2.0 * atan(exp(deg2rad(-p.y() * scale))) - M_PI/2));
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal OnlineMap::zoomIn()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
qreal OnlineMap::zoomOut()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void OnlineMap::draw(QPainter *painter, const QRectF &rect)
|
|
|
|
{
|
2017-06-25 16:23:43 +02:00
|
|
|
qreal scale = zoom2scale(_zoom);
|
2017-03-18 01:30:31 +01:00
|
|
|
|
2017-12-03 10:41:07 +01:00
|
|
|
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);
|
2017-03-18 01:30:31 +01:00
|
|
|
|
|
|
|
QList<Tile> tiles;
|
2017-12-03 10:41:07 +01:00
|
|
|
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));
|
2017-03-18 01:30:31 +01:00
|
|
|
|
|
|
|
if (_block)
|
2018-02-20 23:37:19 +01:00
|
|
|
_tileLoader.loadTilesSync(tiles);
|
2017-03-18 01:30:31 +01:00
|
|
|
else
|
2018-02-20 23:37:19 +01:00
|
|
|
_tileLoader.loadTilesAsync(tiles);
|
2017-03-18 01:30:31 +01:00
|
|
|
|
|
|
|
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());
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 01:26:26 +01:00
|
|
|
QPointF OnlineMap::ll2xy(const Coordinates &c) const
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2017-06-25 16:23:43 +02:00
|
|
|
qreal scale = zoom2scale(_zoom);
|
2018-02-16 19:37:04 +01:00
|
|
|
QPointF m = ll2m(c);
|
2017-06-25 16:23:43 +02:00
|
|
|
return QPointF(m.x() / scale, m.y() / -scale);
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|
|
|
|
|
2018-01-26 01:26:26 +01:00
|
|
|
Coordinates OnlineMap::xy2ll(const QPointF &p) const
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
2017-06-25 16:23:43 +02:00
|
|
|
qreal scale = zoom2scale(_zoom);
|
2018-02-16 19:37:04 +01:00
|
|
|
return m2ll(QPointF(p.x() * scale, -p.y() * scale));
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|