2018-10-06 23:14:46 +02:00
|
|
|
#include <QtCore>
|
2018-03-30 10:25:05 +02:00
|
|
|
#include <QDir>
|
|
|
|
#include <QPainter>
|
|
|
|
#include "common/wgs84.h"
|
|
|
|
#include "common/rectc.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "downloader.h"
|
2018-04-28 19:08:21 +02:00
|
|
|
#include "tileloader.h"
|
2018-03-30 10:25:05 +02:00
|
|
|
#include "wmsmap.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define CAPABILITIES_FILE "capabilities.xml"
|
|
|
|
#define TILE_SIZE 256
|
|
|
|
|
2018-04-13 21:14:12 +02:00
|
|
|
double WMSMap::sd2res(double scaleDenominator) const
|
2018-03-30 10:25:05 +02:00
|
|
|
{
|
|
|
|
return scaleDenominator * 0.28e-3 * _projection.units().fromMeters(1.0);
|
|
|
|
}
|
|
|
|
|
2018-03-31 11:27:01 +02:00
|
|
|
QString WMSMap::tileUrl(const QString &version) const
|
2018-03-30 10:25:05 +02:00
|
|
|
{
|
2018-04-08 19:13:16 +02:00
|
|
|
QString url;
|
2018-03-31 11:27:01 +02:00
|
|
|
|
2018-09-16 12:05:11 +02:00
|
|
|
url = QString("%1%2version=%3&request=GetMap&bbox=$bbox"
|
|
|
|
"&width=%4&height=%5&layers=%6&styles=%7&format=%8&transparent=true")
|
|
|
|
.arg(_setup.url(), _setup.url().contains('?') ? "&" : "?", version,
|
|
|
|
QString::number(TILE_SIZE), QString::number(TILE_SIZE), _setup.layer(),
|
|
|
|
_setup.style(), _setup.format());
|
2018-04-08 19:13:16 +02:00
|
|
|
|
|
|
|
if (version >= "1.3.0")
|
|
|
|
url.append(QString("&CRS=%1").arg(_setup.crs()));
|
|
|
|
else
|
|
|
|
url.append(QString("&SRS=%1").arg(_setup.crs()));
|
|
|
|
|
|
|
|
for (int i = 0; i < _setup.dimensions().size(); i++) {
|
2018-09-30 12:16:41 +02:00
|
|
|
const KV &dim = _setup.dimensions().at(i);
|
|
|
|
url.append(QString("&%1=%2").arg(dim.key(), dim.value()));
|
2018-04-08 19:13:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
2018-03-30 10:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString WMSMap::tilesDir() const
|
|
|
|
{
|
|
|
|
return QString(TILES_DIR + "/" + _name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WMSMap::computeZooms(const RangeF &scaleDenominator)
|
|
|
|
{
|
|
|
|
_zooms.clear();
|
|
|
|
|
|
|
|
if (scaleDenominator.size() > 0) {
|
2018-04-13 21:14:12 +02:00
|
|
|
double ld = log2(scaleDenominator.max()) - log2(scaleDenominator.min());
|
2018-10-06 21:15:06 +02:00
|
|
|
int cld = (int)ceil(ld);
|
|
|
|
double step = ld / (double)cld;
|
|
|
|
double lmax = log2(scaleDenominator.max());
|
2018-03-30 10:25:05 +02:00
|
|
|
for (int i = 0; i <= cld; i++)
|
|
|
|
_zooms.append(pow(2.0, lmax - i * step));
|
|
|
|
} else
|
|
|
|
_zooms.append(scaleDenominator.min());
|
|
|
|
}
|
|
|
|
|
|
|
|
void WMSMap::updateTransform()
|
|
|
|
{
|
2018-04-16 20:26:10 +02:00
|
|
|
double pixelSpan = sd2res(_zooms.at(_zoom));
|
2018-03-30 10:25:05 +02:00
|
|
|
if (_projection.isGeographic())
|
|
|
|
pixelSpan /= deg2rad(WGS84_RADIUS);
|
2018-05-02 21:25:14 +02:00
|
|
|
double sx = _bbox.width() / pixelSpan;
|
|
|
|
double sy = _bbox.height() / pixelSpan;
|
2018-03-30 10:25:05 +02:00
|
|
|
|
2018-05-02 21:25:14 +02:00
|
|
|
ReferencePoint tl(PointD(0, 0), _bbox.topLeft());
|
|
|
|
ReferencePoint br(PointD(sx, sy), _bbox.bottomRight());
|
2018-04-15 16:27:47 +02:00
|
|
|
_transform = Transform(tl, br);
|
2018-03-30 10:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WMSMap::loadWMS()
|
|
|
|
{
|
|
|
|
QString file = tilesDir() + "/" + CAPABILITIES_FILE;
|
|
|
|
|
|
|
|
WMS wms(file, _setup);
|
|
|
|
if (!wms.isValid()) {
|
|
|
|
_errorString = wms.errorString();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_projection = wms.projection();
|
2018-05-02 21:25:14 +02:00
|
|
|
_bbox = RectD(_projection.ll2xy(wms.boundingBox().topLeft()),
|
|
|
|
_projection.ll2xy(wms.boundingBox().bottomRight()));
|
2018-04-27 19:31:27 +02:00
|
|
|
_tileLoader->setUrl(tileUrl(wms.version()));
|
2018-03-30 10:25:05 +02:00
|
|
|
|
2018-04-05 20:38:23 +02:00
|
|
|
if (wms.version() >= "1.3.0") {
|
2018-04-07 18:42:25 +02:00
|
|
|
if (_setup.coordinateSystem().axisOrder() == CoordinateSystem::Unknown)
|
|
|
|
_cs = _projection.coordinateSystem();
|
2018-04-05 20:38:23 +02:00
|
|
|
else
|
2018-04-07 18:42:25 +02:00
|
|
|
_cs = _setup.coordinateSystem();
|
2018-04-05 20:38:23 +02:00
|
|
|
} else
|
2018-04-07 18:42:25 +02:00
|
|
|
_cs = CoordinateSystem::XY;
|
2018-04-05 20:38:23 +02:00
|
|
|
|
2018-03-30 10:25:05 +02:00
|
|
|
computeZooms(wms.scaleDenominator());
|
|
|
|
updateTransform();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
WMSMap::WMSMap(const QString &name, const WMS::Setup &setup, QObject *parent)
|
2018-07-10 08:58:37 +02:00
|
|
|
: Map(parent), _name(name), _setup(setup), _tileLoader(0), _zoom(0),
|
2018-08-18 21:06:36 +02:00
|
|
|
_ratio(1.0), _valid(false)
|
2018-03-30 10:25:05 +02:00
|
|
|
{
|
2018-09-24 22:49:10 +02:00
|
|
|
_tileLoader = new TileLoader(tilesDir(), this);
|
2018-04-27 23:08:44 +02:00
|
|
|
_tileLoader->setAuthorization(_setup.authorization());
|
2018-04-27 19:31:27 +02:00
|
|
|
connect(_tileLoader, SIGNAL(finished()), this, SIGNAL(loaded()));
|
|
|
|
|
2018-03-30 10:25:05 +02:00
|
|
|
_valid = loadWMS();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WMSMap::clearCache()
|
|
|
|
{
|
2018-04-27 19:31:27 +02:00
|
|
|
_tileLoader->clearCache();
|
2018-03-30 10:25:05 +02:00
|
|
|
_zoom = 0;
|
|
|
|
|
|
|
|
if (!loadWMS())
|
2018-09-24 23:07:11 +02:00
|
|
|
qWarning("%s: %s", qPrintable(_name), qPrintable(_errorString));
|
2018-03-30 10:25:05 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 09:51:41 +02:00
|
|
|
QRectF WMSMap::bounds()
|
2018-05-02 21:25:14 +02:00
|
|
|
{
|
2018-08-18 21:06:36 +02:00
|
|
|
return QRectF(_transform.proj2img(_bbox.topLeft()) / _ratio,
|
|
|
|
_transform.proj2img(_bbox.bottomRight()) / _ratio);
|
2018-05-02 21:25:14 +02:00
|
|
|
}
|
|
|
|
|
2018-04-16 20:26:10 +02:00
|
|
|
int WMSMap::zoomFit(const QSize &size, const RectC &rect)
|
2018-03-30 10:25:05 +02:00
|
|
|
{
|
2018-04-16 20:26:10 +02:00
|
|
|
if (rect.isValid()) {
|
|
|
|
PointD tl(_projection.ll2xy(rect.topLeft()));
|
|
|
|
PointD br(_projection.ll2xy(rect.bottomRight()));
|
|
|
|
PointD sc((br.x() - tl.x()) / size.width(), (tl.y() - br.y())
|
|
|
|
/ size.height());
|
2018-04-13 21:14:12 +02:00
|
|
|
double resolution = qMax(qAbs(sc.x()), qAbs(sc.y()));
|
2018-03-30 10:25:05 +02:00
|
|
|
if (_projection.isGeographic())
|
|
|
|
resolution *= deg2rad(WGS84_RADIUS);
|
|
|
|
|
|
|
|
_zoom = 0;
|
|
|
|
for (int i = 0; i < _zooms.size(); i++) {
|
2018-08-18 21:06:36 +02:00
|
|
|
if (sd2res(_zooms.at(i)) < resolution / _ratio)
|
2018-03-30 10:25:05 +02:00
|
|
|
break;
|
|
|
|
_zoom = i;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
_zoom = _zooms.size() - 1;
|
|
|
|
|
|
|
|
updateTransform();
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
2018-04-28 22:18:11 +02:00
|
|
|
void WMSMap::setZoom(int zoom)
|
|
|
|
{
|
|
|
|
_zoom = zoom;
|
|
|
|
updateTransform();
|
|
|
|
}
|
|
|
|
|
2018-03-30 10:25:05 +02:00
|
|
|
int WMSMap::zoomIn()
|
|
|
|
{
|
|
|
|
_zoom = qMin(_zoom + 1, _zooms.size() - 1);
|
|
|
|
updateTransform();
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WMSMap::zoomOut()
|
|
|
|
{
|
|
|
|
_zoom = qMax(_zoom - 1, 0);
|
|
|
|
updateTransform();
|
|
|
|
return _zoom;
|
|
|
|
}
|
|
|
|
|
2018-06-30 12:14:58 +02:00
|
|
|
QPointF WMSMap::ll2xy(const Coordinates &c)
|
2018-03-30 10:25:05 +02:00
|
|
|
{
|
2018-08-18 21:06:36 +02:00
|
|
|
return _transform.proj2img(_projection.ll2xy(c)) / _ratio;
|
2018-03-30 10:25:05 +02:00
|
|
|
}
|
|
|
|
|
2018-06-30 12:14:58 +02:00
|
|
|
Coordinates WMSMap::xy2ll(const QPointF &p)
|
2018-03-30 10:25:05 +02:00
|
|
|
{
|
2018-08-18 21:06:36 +02:00
|
|
|
return _projection.xy2ll(_transform.img2proj(p * _ratio));
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal WMSMap::tileSize() const
|
|
|
|
{
|
|
|
|
return (TILE_SIZE / _ratio);
|
2018-03-30 10:25:05 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 20:26:10 +02:00
|
|
|
void WMSMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
|
2018-03-30 10:25:05 +02:00
|
|
|
{
|
2018-10-06 21:15:06 +02:00
|
|
|
QPoint tl = QPoint(qFloor(rect.left() / tileSize()),
|
|
|
|
qFloor(rect.top() / tileSize()));
|
|
|
|
QPoint br = QPoint(qCeil(rect.right() / tileSize()),
|
|
|
|
qCeil(rect.bottom() / tileSize()));
|
2018-03-30 10:25:05 +02:00
|
|
|
|
2018-10-05 07:10:49 +02:00
|
|
|
QVector<Tile> tiles;
|
|
|
|
tiles.reserve((br.x() - tl.x()) * (br.y() - tl.y()));
|
2018-03-30 10:25:05 +02:00
|
|
|
for (int i = tl.x(); i < br.x(); i++) {
|
|
|
|
for (int j = tl.y(); j < br.y(); j++) {
|
2018-04-15 17:32:25 +02:00
|
|
|
PointD ttl(_transform.img2proj(QPointF(i * TILE_SIZE,
|
2018-03-30 10:25:05 +02:00
|
|
|
j * TILE_SIZE)));
|
2018-04-15 17:32:25 +02:00
|
|
|
PointD tbr(_transform.img2proj(QPointF(i * TILE_SIZE + TILE_SIZE
|
2018-03-30 10:25:05 +02:00
|
|
|
- 1, j * TILE_SIZE + TILE_SIZE - 1)));
|
2018-05-02 21:25:14 +02:00
|
|
|
RectD bbox = (_cs.axisOrder() == CoordinateSystem::YX)
|
|
|
|
? RectD(PointD(tbr.y(), tbr.x()), PointD(ttl.y(), ttl.x()))
|
|
|
|
: RectD(ttl, tbr);
|
2018-03-30 10:25:05 +02:00
|
|
|
|
|
|
|
tiles.append(Tile(QPoint(i, j), _zoom, bbox));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 20:26:10 +02:00
|
|
|
if (flags & Map::Block)
|
2018-04-27 19:31:27 +02:00
|
|
|
_tileLoader->loadTilesSync(tiles);
|
2018-03-30 10:25:05 +02:00
|
|
|
else
|
2018-04-27 19:31:27 +02:00
|
|
|
_tileLoader->loadTilesAsync(tiles);
|
2018-03-30 10:25:05 +02: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() * tileSize(), t.xy().y() * tileSize());
|
|
|
|
if (!t.pixmap().isNull()) {
|
|
|
|
#ifdef ENABLE_HIDPI
|
|
|
|
t.pixmap().setDevicePixelRatio(_ratio);
|
|
|
|
#endif // ENABLE_HIDPI
|
2018-03-30 10:25:05 +02:00
|
|
|
painter->drawPixmap(tp, t.pixmap());
|
2018-08-18 21:06:36 +02:00
|
|
|
}
|
2018-03-30 10:25:05 +02:00
|
|
|
}
|
|
|
|
}
|