1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-25 04:05:53 +01:00
GPXSee/src/map/wmtsmap.cpp

215 lines
5.3 KiB
C++
Raw Normal View History

2018-10-06 23:14:46 +02:00
#include <QtCore>
2018-02-20 23:37:19 +01:00
#include <QPainter>
#include <QDir>
2018-02-20 23:37:19 +01:00
#include "common/rectc.h"
#include "common/wgs84.h"
#include "common/programpaths.h"
2018-02-20 23:37:19 +01:00
#include "transform.h"
2018-04-28 19:08:21 +02:00
#include "tileloader.h"
2018-02-20 23:37:19 +01:00
#include "wmts.h"
#include "wmtsmap.h"
#define CAPABILITIES_FILE "capabilities.xml"
2020-12-02 23:58:11 +01:00
WMTSMap::WMTSMap(const QString &fileName, const QString &name,
const WMTS::Setup &setup, qreal tileRatio,
QObject *parent) : Map(fileName, parent), _name(name), _tileLoader(0),
_zoom(0), _mapRatio(1.0), _tileRatio(tileRatio)
2018-02-20 23:37:19 +01:00
{
QString tilesDir(QDir(ProgramPaths::tilesDir()).filePath(_name));
_tileLoader = new TileLoader(tilesDir, this);
_tileLoader->setAuthorization(setup.authorization());
2021-04-28 00:01:07 +02:00
connect(_tileLoader, &TileLoader::finished, this, &WMTSMap::tilesLoaded);
_wmts = new WMTS(QDir(tilesDir).filePath(CAPABILITIES_FILE), setup, this);
2021-04-28 00:01:07 +02:00
connect(_wmts, &WMTS::downloadFinished, this, &WMTSMap::wmtsReady);
if (_wmts->isReady())
init();
}
void WMTSMap::init()
{
_tileLoader->setUrl(_wmts->tileUrl());
_bounds = RectD(_wmts->bbox(), _wmts->projection());
2018-02-20 23:37:19 +01:00
updateTransform();
}
void WMTSMap::wmtsReady()
{
if (_wmts->isValid())
init();
emit mapLoaded();
}
void WMTSMap::clearCache()
{
_tileLoader->clearCache();
2018-02-20 23:37:19 +01:00
}
double WMTSMap::sd2res(double scaleDenominator) const
2018-02-20 23:37:19 +01:00
{
return scaleDenominator * 0.28e-3
* _wmts->projection().units().fromMeters(1.0);
2018-02-20 23:37:19 +01:00
}
Transform WMTSMap::transform(int zoom) const
2018-02-20 23:37:19 +01:00
{
const WMTS::Zoom &z = _wmts->zooms().at(zoom);
2018-02-20 23:37:19 +01:00
PointD topLeft = (_wmts->cs().axisOrder() == CoordinateSystem::YX)
? PointD(z.topLeft().y(), z.topLeft().x()) : z.topLeft();
double pixelSpan = sd2res(z.scaleDenominator());
if (_wmts->projection().isGeographic())
pixelSpan /= deg2rad(WGS84_RADIUS);
return Transform(ReferencePoint(PointD(0, 0), topLeft),
PointD(pixelSpan, pixelSpan));
2018-02-20 23:37:19 +01:00
}
QRectF WMTSMap::tileBounds(int zoom) const
2018-02-20 23:37:19 +01:00
{
const WMTS::Zoom &z = _wmts->zooms().at(zoom);
return (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()));
}
void WMTSMap::updateTransform()
{
_transform = transform(_zoom);
}
QRectF WMTSMap::bounds()
{
QRectF tb(tileBounds(_zoom));
QRectF lb = _bounds.isValid()
? QRectF(_transform.proj2img(_bounds.topLeft()) / coordinatesRatio(),
_transform.proj2img(_bounds.bottomRight()) / coordinatesRatio())
: QRectF();
return lb.isValid() ? lb & tb : tb;
}
RectC WMTSMap::llBounds()
{
if (_wmts->bbox().isValid())
return _wmts->bbox();
else {
int maxZoom = _wmts->zooms().size() - 1;
QRectF tb(tileBounds(maxZoom));
Transform t(transform(maxZoom));
RectD rect(t.img2proj(tb.topLeft() * coordinatesRatio()),
t.img2proj(tb.bottomRight() * coordinatesRatio()));
return rect.toRectC(_wmts->projection());
}
2018-02-20 23:37:19 +01:00
}
int WMTSMap::zoomFit(const QSize &size, const RectC &rect)
2018-02-20 23:37:19 +01:00
{
if (rect.isValid()) {
RectD prect(rect, _wmts->projection());
2019-01-14 23:47:24 +01:00
PointD sc(prect.width() / size.width(), prect.height() / size.height());
double resolution = qMax(qAbs(sc.x()), qAbs(sc.y()));
if (_wmts->projection().isGeographic())
resolution *= deg2rad(WGS84_RADIUS);
2018-02-20 23:37:19 +01:00
_zoom = 0;
for (int i = 0; i < _wmts->zooms().size(); i++) {
if (sd2res(_wmts->zooms().at(i).scaleDenominator()) < resolution
2018-08-18 21:06:36 +02:00
/ coordinatesRatio())
2018-02-20 23:37:19 +01:00
break;
_zoom = i;
}
} else
_zoom = _wmts->zooms().size() - 1;
2018-02-20 23:37:19 +01:00
updateTransform();
return _zoom;
}
2018-04-28 22:18:11 +02:00
void WMTSMap::setZoom(int zoom)
{
_zoom = zoom;
updateTransform();
}
int WMTSMap::zoomIn()
2018-02-20 23:37:19 +01:00
{
_zoom = qMin(_zoom + 1, _wmts->zooms().size() - 1);
2018-02-20 23:37:19 +01:00
updateTransform();
return _zoom;
}
int WMTSMap::zoomOut()
2018-02-20 23:37:19 +01:00
{
_zoom = qMax(_zoom - 1, 0);
updateTransform();
return _zoom;
}
2018-08-18 21:06:36 +02:00
qreal WMTSMap::coordinatesRatio() const
{
return _mapRatio > 1.0 ? _mapRatio / _tileRatio : 1.0;
2018-08-18 21:06:36 +02:00
}
qreal WMTSMap::imageRatio() const
{
return _mapRatio > 1.0 ? _mapRatio : _tileRatio;
2018-08-18 21:06:36 +02:00
}
QSizeF WMTSMap::tileSize(const WMTS::Zoom &zoom) const
{
return QSizeF(zoom.tile().width() / coordinatesRatio(),
zoom.tile().height() / coordinatesRatio());
}
2018-08-23 20:26:10 +02:00
void WMTSMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
2018-02-20 23:37:19 +01:00
{
const WMTS::Zoom &z = _wmts->zooms().at(_zoom);
2018-08-18 21:06:36 +02:00
QSizeF ts(tileSize(z));
2018-10-06 21:15:06 +02:00
QPoint tl = QPoint(qFloor(rect.left() / ts.width()),
qFloor(rect.top() / ts.height()));
QPoint br = QPoint(qCeil(rect.right() / ts.width()),
qCeil(rect.bottom() / ts.height()));
2018-02-20 23:37:19 +01:00
2018-10-05 07:10:49 +02:00
QVector<Tile> tiles;
tiles.reserve((br.x() - tl.x()) * (br.y() - tl.y()));
for (int i = tl.x(); i < br.x(); i++)
for (int j = tl.y(); j < br.y(); j++)
tiles.append(Tile(QPoint(i, j), z.id()));
2018-02-20 23:37:19 +01:00
2018-08-23 20:26:10 +02:00
if (flags & Map::Block)
_tileLoader->loadTilesSync(tiles);
2018-02-20 23:37:19 +01:00
else
_tileLoader->loadTilesAsync(tiles);
2018-02-20 23:37:19 +01:00
for (int i = 0; i < tiles.count(); i++) {
Tile &t = tiles[i];
2018-08-18 21:06:36 +02:00
QPointF tp(t.xy().x() * ts.width(), t.xy().y() * ts.height());
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-02-20 23:37:19 +01:00
}
}
2018-07-13 09:51:41 +02:00
QPointF WMTSMap::ll2xy(const Coordinates &c)
2018-02-20 23:37:19 +01:00
{
return _transform.proj2img(_wmts->projection().ll2xy(c))
/ coordinatesRatio();
2018-02-20 23:37:19 +01:00
}
2018-07-13 09:51:41 +02:00
Coordinates WMTSMap::xy2ll(const QPointF &p)
2018-02-20 23:37:19 +01:00
{
return _wmts->projection().xy2ll(_transform.img2proj(p
* coordinatesRatio()));
2018-02-20 23:37:19 +01:00
}