2017-03-18 01:30:31 +01:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QPainter>
|
2017-11-26 18:54:03 +01:00
|
|
|
#include "common/coordinates.h"
|
|
|
|
#include "common/rectc.h"
|
|
|
|
#include "common/wgs84.h"
|
|
|
|
#include "mercator.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
|
|
|
|
|
|
|
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
|
|
|
static bool loadTileFile(Tile &tile, const QString &file)
|
|
|
|
{
|
|
|
|
if (!tile.pixmap().load(file)) {
|
|
|
|
qWarning("%s: error loading tile file\n", qPrintable(file));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
|
|
|
|
Downloader *OnlineMap::downloader;
|
|
|
|
|
2017-09-09 12:33:43 +02:00
|
|
|
OnlineMap::OnlineMap(const QString &name, const QString &url,
|
|
|
|
const Range &zooms, QObject *parent) : Map(parent)
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
|
|
|
_name = name;
|
|
|
|
_url = url;
|
|
|
|
_block = false;
|
2017-09-09 12:33:43 +02:00
|
|
|
_zooms = zooms;
|
|
|
|
_zoom = zooms.max();
|
2017-03-18 01:30:31 +01:00
|
|
|
|
|
|
|
QString path = TILES_DIR + QString("/") + name;
|
|
|
|
if (!QDir().mkpath(path))
|
|
|
|
qWarning("Error creating tiles dir: %s\n", qPrintable(path));
|
|
|
|
}
|
|
|
|
|
2017-10-04 23:15:39 +02:00
|
|
|
void OnlineMap::load()
|
|
|
|
{
|
|
|
|
connect(downloader, SIGNAL(finished()), this, SLOT(emitLoaded()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnlineMap::unload()
|
|
|
|
{
|
|
|
|
disconnect(downloader, SIGNAL(finished()), this, SLOT(emitLoaded()));
|
|
|
|
}
|
|
|
|
|
2017-09-15 00:07:09 +02:00
|
|
|
void OnlineMap::fillTile(Tile &tile)
|
|
|
|
{
|
|
|
|
tile.pixmap() = QPixmap(TILE_SIZE, TILE_SIZE);
|
|
|
|
tile.pixmap().fill(_backgroundColor);
|
|
|
|
}
|
|
|
|
|
2017-03-18 01:30:31 +01:00
|
|
|
void OnlineMap::emitLoaded()
|
|
|
|
{
|
|
|
|
emit loaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnlineMap::loadTilesAsync(QList<Tile> &list)
|
|
|
|
{
|
|
|
|
QList<Download> dl;
|
|
|
|
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
Tile &t = list[i];
|
|
|
|
QString file = tileFile(t);
|
|
|
|
QFileInfo fi(file);
|
|
|
|
|
|
|
|
if (!fi.exists()) {
|
|
|
|
fillTile(t);
|
|
|
|
dl.append(Download(tileUrl(t), file));
|
|
|
|
} else
|
|
|
|
loadTileFile(t, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dl.empty())
|
|
|
|
downloader->get(dl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnlineMap::loadTilesSync(QList<Tile> &list)
|
|
|
|
{
|
|
|
|
QList<Download> dl;
|
|
|
|
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
Tile &t = list[i];
|
|
|
|
QString file = tileFile(t);
|
|
|
|
QFileInfo fi(file);
|
|
|
|
|
|
|
|
if (!fi.exists())
|
|
|
|
dl.append(Download(tileUrl(t), file));
|
|
|
|
else
|
|
|
|
loadTileFile(t, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dl.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QEventLoop wait;
|
|
|
|
connect(downloader, SIGNAL(finished()), &wait, SLOT(quit()));
|
|
|
|
if (downloader->get(dl))
|
|
|
|
wait.exec();
|
|
|
|
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
Tile &t = list[i];
|
|
|
|
|
|
|
|
if (t.pixmap().isNull()) {
|
|
|
|
QString file = tileFile(t);
|
|
|
|
QFileInfo fi(file);
|
|
|
|
|
|
|
|
if (!(fi.exists() && loadTileFile(t, file)))
|
|
|
|
fillTile(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-09 12:33:43 +02:00
|
|
|
QString OnlineMap::tileUrl(const Tile &tile) const
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
|
|
|
QString url(_url);
|
|
|
|
|
|
|
|
url.replace("$z", QString::number(tile.zoom()));
|
|
|
|
url.replace("$x", QString::number(tile.xy().x()));
|
|
|
|
url.replace("$y", QString::number(tile.xy().y()));
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2017-09-09 12:33:43 +02:00
|
|
|
QString OnlineMap::tileFile(const Tile &tile) const
|
2017-03-18 01:30:31 +01:00
|
|
|
{
|
|
|
|
QString file = TILES_DIR + QString("/%1/%2-%3-%4").arg(name())
|
|
|
|
.arg(tile.zoom()).arg(tile.xy().x()).arg(tile.xy().y());
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnlineMap::clearCache()
|
|
|
|
{
|
|
|
|
QString path = TILES_DIR + QString("/") + name();
|
|
|
|
QDir dir = QDir(path);
|
|
|
|
QStringList list = dir.entryList();
|
|
|
|
|
|
|
|
for (int i = 0; i < list.count(); i++)
|
|
|
|
dir.remove(list.at(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF OnlineMap::bounds() const
|
|
|
|
{
|
2018-01-26 01:26:26 +01:00
|
|
|
return QRectF(ll2xy(Coordinates(-180, 85)), ll2xy(Coordinates(180, -85)));
|
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 {
|
2017-06-29 22:29:27 +02:00
|
|
|
QRectF tbr(Mercator().ll2xy(br.topLeft()),
|
|
|
|
Mercator().ll2xy(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)
|
|
|
|
loadTilesSync(tiles);
|
|
|
|
else
|
|
|
|
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);
|
2017-03-18 01:30:31 +01:00
|
|
|
painter->drawPixmap(tp, t.pixmap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2017-03-29 00:17:47 +02:00
|
|
|
QPointF m = Mercator().ll2xy(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);
|
|
|
|
QPointF m(p.x() * scale, -p.y() * scale);
|
2017-03-29 00:17:47 +02:00
|
|
|
return Mercator().xy2ll(m);
|
2017-03-18 01:30:31 +01:00
|
|
|
}
|