2018-02-20 23:37:19 +01:00
|
|
|
#include <QPainter>
|
|
|
|
#include "common/rectc.h"
|
2018-02-22 21:02:56 +01:00
|
|
|
#include "common/wgs84.h"
|
2018-02-20 23:37:19 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "transform.h"
|
2018-03-14 07:23:07 +01:00
|
|
|
#include "downloader.h"
|
2018-02-20 23:37:19 +01:00
|
|
|
#include "wmts.h"
|
|
|
|
#include "wmtsmap.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define CAPABILITIES_FILE "capabilities.xml"
|
|
|
|
|
2018-03-30 10:25:05 +02:00
|
|
|
bool WMTSMap::loadWMTS()
|
2018-02-20 23:37:19 +01:00
|
|
|
{
|
2018-03-30 10:25:05 +02:00
|
|
|
QString file = tilesDir() + "/" + CAPABILITIES_FILE;
|
2018-02-20 23:37:19 +01:00
|
|
|
|
2018-03-30 10:25:05 +02:00
|
|
|
WMTS wmts(file, _setup);
|
|
|
|
if (!wmts.isValid()) {
|
2018-02-20 23:37:19 +01:00
|
|
|
_errorString = wmts.errorString();
|
2018-03-30 10:25:05 +02:00
|
|
|
return false;
|
2018-02-20 23:37:19 +01:00
|
|
|
}
|
2018-03-30 10:25:05 +02:00
|
|
|
|
2018-02-24 16:44:30 +01:00
|
|
|
_bounds = wmts.bounds();
|
2018-02-20 23:37:19 +01:00
|
|
|
_zooms = wmts.zooms();
|
|
|
|
_projection = wmts.projection();
|
2018-04-01 20:01:25 +02:00
|
|
|
_tileLoader = TileLoader(wmts.tileUrl(), tilesDir(),
|
|
|
|
_setup.authorization());
|
2018-03-30 10:25:05 +02:00
|
|
|
|
2018-04-05 21:13:48 +02:00
|
|
|
if (_setup.axisOrder() == CoordinateSystem::Unknown)
|
2018-04-05 20:38:23 +02:00
|
|
|
_axisOrder = _projection.axisOrder();
|
|
|
|
else
|
|
|
|
_axisOrder = _setup.axisOrder();
|
|
|
|
|
2018-02-20 23:37:19 +01:00
|
|
|
updateTransform();
|
|
|
|
|
2018-03-30 10:25:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
WMTSMap::WMTSMap(const QString &name, const WMTS::Setup &setup, QObject *parent)
|
2018-04-05 21:13:48 +02:00
|
|
|
: Map(parent), _name(name), _setup(setup), _zoom(0), _block(false),
|
|
|
|
_valid(false)
|
2018-03-30 10:25:05 +02:00
|
|
|
{
|
|
|
|
if (!QDir().mkpath(tilesDir())) {
|
|
|
|
_errorString = "Error creating tiles dir";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_valid = loadWMTS();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WMTSMap::clearCache()
|
|
|
|
{
|
|
|
|
_tileLoader.clearCache();
|
|
|
|
_zoom = 0;
|
|
|
|
|
|
|
|
if (!loadWMTS())
|
|
|
|
qWarning("%s: %s\n", qPrintable(_name), qPrintable(_errorString));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString WMTSMap::tilesDir() const
|
|
|
|
{
|
|
|
|
return QString(TILES_DIR + "/" + _name);
|
2018-02-20 23:37:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
qreal WMTSMap::sd2res(qreal scaleDenominator) const
|
|
|
|
{
|
|
|
|
return scaleDenominator * 0.28e-3 * _projection.units().fromMeters(1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WMTSMap::updateTransform()
|
|
|
|
{
|
|
|
|
const WMTS::Zoom &z = _zooms.at(_zoom);
|
|
|
|
ReferencePoint tl, br;
|
|
|
|
|
2018-04-05 21:13:48 +02:00
|
|
|
QPointF topLeft = (_axisOrder == CoordinateSystem::YX)
|
2018-04-05 20:38:23 +02:00
|
|
|
? QPointF(z.topLeft().y(), z.topLeft().x()) : z.topLeft();
|
|
|
|
|
2018-03-30 10:25:05 +02:00
|
|
|
qreal pixelSpan = sd2res(z.scaleDenominator());
|
2018-02-22 21:02:56 +01:00
|
|
|
if (_projection.isGeographic())
|
|
|
|
pixelSpan /= deg2rad(WGS84_RADIUS);
|
2018-03-30 10:25:05 +02:00
|
|
|
QPointF tileSpan(z.tile().width() * pixelSpan, z.tile().height() * pixelSpan);
|
2018-04-05 20:38:23 +02:00
|
|
|
QPointF bottomRight(topLeft.x() + tileSpan.x() * z.matrix().width(),
|
|
|
|
topLeft.y() - tileSpan.y() * z.matrix().height());
|
2018-02-20 23:37:19 +01:00
|
|
|
|
|
|
|
tl.xy = QPoint(0, 0);
|
2018-04-05 20:38:23 +02:00
|
|
|
tl.pp = topLeft;
|
2018-03-30 10:25:05 +02:00
|
|
|
br.xy = QPoint(z.tile().width() * z.matrix().width(),
|
|
|
|
z.tile().height() * z.matrix().height());
|
2018-02-20 23:37:19 +01:00
|
|
|
br.pp = bottomRight;
|
|
|
|
|
|
|
|
QList<ReferencePoint> points;
|
|
|
|
points << tl << br;
|
2018-03-22 20:00:30 +01:00
|
|
|
_transform = Transform(points);
|
2018-02-20 23:37:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WMTSMap::load()
|
|
|
|
{
|
|
|
|
connect(TileLoader::downloader(), SIGNAL(finished()), this,
|
|
|
|
SLOT(emitLoaded()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void WMTSMap::unload()
|
|
|
|
{
|
|
|
|
disconnect(TileLoader::downloader(), SIGNAL(finished()), this,
|
|
|
|
SLOT(emitLoaded()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void WMTSMap::emitLoaded()
|
|
|
|
{
|
|
|
|
emit loaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF WMTSMap::bounds() const
|
|
|
|
{
|
|
|
|
const WMTS::Zoom &z = _zooms.at(_zoom);
|
2018-02-24 16:44:30 +01:00
|
|
|
QRectF tileBounds, bounds;
|
2018-02-24 10:34:27 +01:00
|
|
|
|
2018-03-30 10:25:05 +02:00
|
|
|
tileBounds = (z.limits().isNull()) ?
|
|
|
|
QRectF(QPointF(0, 0), QSize(z.tile().width() * z.matrix().width(),
|
|
|
|
z.tile().height() * z.matrix().height()))
|
|
|
|
: QRectF(QPointF(z.limits().left() * z.tile().width(), z.limits().top()
|
|
|
|
* z.tile().height()), QSize(z.tile().width() * z.limits().width(),
|
|
|
|
z.tile().height() * z.limits().height()));
|
2018-02-24 16:44:30 +01:00
|
|
|
|
|
|
|
bounds = _bounds.isValid() ? QRectF(ll2xy(_bounds.topLeft()),
|
|
|
|
ll2xy(_bounds.bottomRight())) : QRectF();
|
|
|
|
|
|
|
|
return _bounds.isValid() ? tileBounds.intersected(bounds) : tileBounds;
|
2018-02-20 23:37:19 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 22:19:46 +01:00
|
|
|
int WMTSMap::zoomFit(const QSize &size, const RectC &br)
|
2018-02-20 23:37:19 +01:00
|
|
|
{
|
|
|
|
if (br.isValid()) {
|
|
|
|
QRectF tbr(_projection.ll2xy(br.topLeft()),
|
|
|
|
_projection.ll2xy(br.bottomRight()));
|
|
|
|
QPointF sc(tbr.width() / size.width(), tbr.height() / size.height());
|
|
|
|
qreal resolution = qMax(qAbs(sc.x()), qAbs(sc.y()));
|
2018-02-22 21:02:56 +01:00
|
|
|
if (_projection.isGeographic())
|
|
|
|
resolution *= deg2rad(WGS84_RADIUS);
|
2018-02-20 23:37:19 +01:00
|
|
|
|
2018-02-25 19:33:12 +01:00
|
|
|
_zoom = 0;
|
2018-02-20 23:37:19 +01:00
|
|
|
for (int i = 0; i < _zooms.size(); i++) {
|
2018-03-30 10:25:05 +02:00
|
|
|
if (sd2res(_zooms.at(i).scaleDenominator()) < resolution)
|
2018-02-20 23:37:19 +01:00
|
|
|
break;
|
|
|
|
_zoom = i;
|
|
|
|
}
|
2018-02-25 19:33:12 +01:00
|
|
|
} else
|
|
|
|
_zoom = _zooms.size() - 1;
|
2018-02-20 23:37:19 +01:00
|
|
|
|
|
|
|
updateTransform();
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
2018-02-28 22:19:46 +01:00
|
|
|
qreal WMTSMap::resolution(const QRectF &rect) const
|
2018-02-20 23:37:19 +01:00
|
|
|
{
|
2018-02-28 22:19:46 +01:00
|
|
|
Coordinates tl = xy2ll((rect.topLeft()));
|
|
|
|
Coordinates br = xy2ll(rect.bottomRight());
|
2018-02-20 23:37:19 +01:00
|
|
|
|
2018-02-28 22:19:46 +01:00
|
|
|
qreal ds = tl.distanceTo(br);
|
|
|
|
qreal ps = QLineF(rect.topLeft(), rect.bottomRight()).length();
|
2018-02-20 23:37:19 +01:00
|
|
|
|
2018-02-28 22:19:46 +01:00
|
|
|
return ds/ps;
|
2018-02-20 23:37:19 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 22:19:46 +01:00
|
|
|
int WMTSMap::zoomIn()
|
2018-02-20 23:37:19 +01:00
|
|
|
{
|
|
|
|
_zoom = qMin(_zoom + 1, _zooms.size() - 1);
|
|
|
|
updateTransform();
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
2018-02-28 22:19:46 +01:00
|
|
|
int WMTSMap::zoomOut()
|
2018-02-20 23:37:19 +01:00
|
|
|
{
|
|
|
|
_zoom = qMax(_zoom - 1, 0);
|
|
|
|
updateTransform();
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WMTSMap::draw(QPainter *painter, const QRectF &rect)
|
|
|
|
{
|
|
|
|
const WMTS::Zoom &z = _zooms.at(_zoom);
|
2018-03-30 10:25:05 +02:00
|
|
|
QPoint tl = QPoint((int)floor(rect.left() / (qreal)z.tile().width()),
|
|
|
|
(int)floor(rect.top() / (qreal)z.tile().height()));
|
|
|
|
QPoint br = QPoint((int)ceil(rect.right() / (qreal)z.tile().width()),
|
|
|
|
(int)ceil(rect.bottom() / (qreal)z.tile().height()));
|
2018-02-20 23:37:19 +01:00
|
|
|
|
|
|
|
QList<Tile> tiles;
|
2018-02-24 01:59:03 +01:00
|
|
|
for (int i = tl.x(); i < br.x(); i++)
|
|
|
|
for (int j = tl.y(); j < br.y(); j++)
|
2018-03-30 10:25:05 +02:00
|
|
|
tiles.append(Tile(QPoint(i, j), z.id()));
|
2018-02-20 23:37:19 +01:00
|
|
|
|
|
|
|
if (_block)
|
|
|
|
_tileLoader.loadTilesSync(tiles);
|
|
|
|
else
|
|
|
|
_tileLoader.loadTilesAsync(tiles);
|
|
|
|
|
|
|
|
for (int i = 0; i < tiles.count(); i++) {
|
|
|
|
Tile &t = tiles[i];
|
2018-03-30 10:25:05 +02:00
|
|
|
QPoint tp(t.xy().x() * z.tile().width(), t.xy().y() * z.tile().height());
|
2018-02-20 23:37:19 +01:00
|
|
|
if (t.pixmap().isNull())
|
2018-03-30 10:25:05 +02:00
|
|
|
painter->fillRect(QRect(tp, z.tile()), _backgroundColor);
|
2018-02-20 23:37:19 +01:00
|
|
|
else
|
|
|
|
painter->drawPixmap(tp, t.pixmap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF WMTSMap::ll2xy(const Coordinates &c) const
|
|
|
|
{
|
2018-03-22 20:00:30 +01:00
|
|
|
return _transform.proj2img(_projection.ll2xy(c));
|
2018-02-20 23:37:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Coordinates WMTSMap::xy2ll(const QPointF &p) const
|
|
|
|
{
|
2018-03-22 20:00:30 +01:00
|
|
|
return _projection.xy2ll(_transform.img2proj(p));
|
2018-02-20 23:37:19 +01:00
|
|
|
}
|