1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-06-28 03:59:15 +02:00

Asynchronous WMS/WMTS map loading

(also fixes crash on OS X)
This commit is contained in:
2020-03-17 21:06:51 +01:00
parent 9ce6e16b60
commit 82c0c1f8a7
19 changed files with 520 additions and 445 deletions

View File

@ -50,12 +50,15 @@ public:
virtual void setProjection(const Projection &) {}
virtual bool isValid() const {return true;}
virtual bool isReady() const {return true;}
virtual QString errorString() const {return QString();}
signals:
void loaded();
void tilesLoaded();
void mapLoaded();
};
Q_DECLARE_METATYPE(Map*)
Q_DECLARE_OPERATORS_FOR_FLAGS(Map::Flags)
#endif // MAP_H

View File

@ -1,5 +1,6 @@
#include <QFileInfo>
#include <QDir>
#include <QApplication>
#include "atlas.h"
#include "ozimap.h"
#include "jnxmap.h"
@ -12,31 +13,8 @@
#include "maplist.h"
bool MapList::loadMap(Map *map, const QString &path)
{
if (map && map->isValid()) {
_maps.append(map);
return true;
} else {
_errorPath = path;
_errorString = (map) ? map->errorString() : "Unknown file format";
return false;
}
}
Map *MapList::loadSource(const QString &path)
{
Map *map = MapSource::loadMap(path, _errorString);
if (!map)
_errorPath = path;
else
map->setParent(this);
return map;
}
bool MapList::loadFile(const QString &path, bool *terminate)
Map *MapList::loadFile(const QString &path, QString &errorString,
bool *terminate)
{
QFileInfo fi(path);
QString suffix = fi.suffix().toLower();
@ -45,75 +23,94 @@ bool MapList::loadFile(const QString &path, bool *terminate)
if (Atlas::isAtlas(path)) {
if (terminate)
*terminate = true;
map = new Atlas(path, this);
map = new Atlas(path);
} else if (suffix == "xml") {
if (MapSource::isMap(path) && !(map = loadSource(path)))
return false;
else if (GMAP::isGMAP(path)) {
if (MapSource::isMap(path)) {
if (!(map = MapSource::loadMap(path, errorString)))
return 0;
} else if (GMAP::isGMAP(path)) {
if (terminate)
*terminate = true;
map = new IMGMap(path, this);
map = new IMGMap(path);
}
} else if (suffix == "jnx")
map = new JNXMap(path, this);
map = new JNXMap(path);
else if (suffix == "tif" || suffix == "tiff")
map = new GeoTIFFMap(path, this);
map = new GeoTIFFMap(path);
else if (suffix == "mbtiles")
map = new MBTilesMap(path, this);
map = new MBTilesMap(path);
else if (suffix == "rmap" || suffix == "rtmap")
map = new RMap(path, this);
map = new RMap(path);
else if (suffix == "img")
map = new IMGMap(path, this);
map = new IMGMap(path);
else if (suffix == "map" || suffix == "tar")
map = new OziMap(path, this);
map = new OziMap(path);
if (!loadMap(map, path)) {
if (map && map->isValid())
return map;
else {
errorString = (map) ? map->errorString() : "Unknown file format";
delete map;
return false;
return 0;
}
return true;
}
bool MapList::loadDir(const QString &path)
QList<Map*> MapList::loadDir(const QString &path, QString &errorString)
{
QDir md(path);
md.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
md.setSorting(QDir::DirsLast);
QFileInfoList ml = md.entryInfoList();
bool ret = true;
QList<Map*> list;
for (int i = 0; i < ml.size(); i++) {
const QFileInfo &fi = ml.at(i);
QString suffix = fi.suffix().toLower();
bool terminate = false;
if (fi.isDir() && fi.fileName() != "set") {
if (!loadDir(fi.absoluteFilePath()))
ret = false;
} else if (filter().contains("*." + suffix)) {
if (!loadFile(fi.absoluteFilePath(), &terminate))
ret = false;
if (fi.isDir() && fi.fileName() != "set")
list.append(loadDir(fi.absoluteFilePath(), errorString));
else if (filter().contains("*." + suffix)) {
Map *map = loadFile(fi.absoluteFilePath(), errorString, &terminate);
if (map)
list.append(map);
else
qWarning(qPrintable(path + ": " + errorString));
if (terminate)
break;
}
}
return ret;
return list;
}
QList<Map*> MapList::loadMaps(const QString &path, QString &errorString)
{
if (QFileInfo(path).isDir())
return loadDir(path, errorString);
else {
QList<Map*> list;
Map *map = loadFile(path, errorString, 0);
if (map)
list.append(map);
return list;
}
}
QString MapList::formats()
{
return
tr("Supported files") + " (" + filter().join(" ") + ");;"
+ tr("Garmin IMG maps") + " (*.gmap *.gmapi *.img *.xml);;"
+ tr("Garmin JNX maps") + " (*.jnx);;"
+ tr("OziExplorer maps") + " (*.map);;"
+ tr("MBTiles maps") + " (*.mbtiles);;"
+ tr("TrekBuddy maps/atlases") + " (*.tar *.tba);;"
+ tr("GeoTIFF images") + " (*.tif *.tiff);;"
+ tr("TwoNav maps") + " (*.rmap *.rtmap);;"
+ tr("Online map sources") + " (*.xml)";
qApp->translate("MapList", "Supported files")
+ " (" + filter().join(" ") + ");;"
+ qApp->translate("MapList", "Garmin IMG maps")
+ " (*.gmap *.gmapi *.img *.xml);;"
+ qApp->translate("MapList", "Garmin JNX maps") + " (*.jnx);;"
+ qApp->translate("MapList", "OziExplorer maps") + " (*.map);;"
+ qApp->translate("MapList", "MBTiles maps") + " (*.mbtiles);;"
+ qApp->translate("MapList", "TrekBuddy maps/atlases") + " (*.tar *.tba);;"
+ qApp->translate("MapList", "GeoTIFF images") + " (*.tif *.tiff);;"
+ qApp->translate("MapList", "TwoNav maps") + " (*.rmap *.rtmap);;"
+ qApp->translate("MapList", "Online map sources") + " (*.xml)";
}
QStringList MapList::filter()

View File

@ -1,36 +1,21 @@
#ifndef MAPLIST_H
#define MAPLIST_H
#include <QObject>
#include <QString>
class Map;
class MapList : public QObject
class MapList
{
Q_OBJECT
public:
MapList(QObject *parent = 0) : QObject(parent) {}
bool loadFile(const QString &path, bool *terminate = 0);
bool loadDir(const QString &path);
const QList<Map*> &maps() const {return _maps;}
const QString &errorString() const {return _errorString;}
const QString &errorPath() const {return _errorPath;}
static QList<Map*> loadMaps(const QString &path, QString &errorString);
static QString formats();
static QStringList filter();
private:
Map *loadSource(const QString &path);
bool loadMap(Map *map, const QString &path);
QList<Map*> _maps;
QString _errorString;
QString _errorPath;
static Map *loadFile(const QString &path, QString &errorString,
bool *terminate);
static QList<Map*> loadDir(const QString &path, QString &errorString);
};
#endif // MAPLIST_H

View File

@ -21,7 +21,7 @@ OnlineMap::OnlineMap(const QString &name, const QString &url,
_tileLoader->setUrl(url);
_tileLoader->setAuthorization(authorization);
_tileLoader->setQuadTiles(quadTiles);
connect(_tileLoader, SIGNAL(finished()), this, SIGNAL(loaded()));
connect(_tileLoader, SIGNAL(finished()), this, SIGNAL(tilesLoaded()));
}
QRectF OnlineMap::bounds()

View File

@ -218,10 +218,10 @@ void WMS::capabilities(QXmlStreamReader &reader, CTX &ctx)
}
}
bool WMS::parseCapabilities(const QString &path, const Setup &setup)
bool WMS::parseCapabilities()
{
QFile file(path);
CTX ctx(setup);
QFile file(_path);
CTX ctx(_setup);
QXmlStreamReader reader;
@ -244,7 +244,7 @@ bool WMS::parseCapabilities(const QString &path, const Setup &setup)
reader.raiseError("Not a WMS Capabilities XML file");
}
if (reader.error()) {
_errorString = QString("%1:%2: %3").arg(path).arg(reader.lineNumber())
_errorString = QString("%1:%2: %3").arg(_path).arg(reader.lineNumber())
.arg(reader.errorString());
return false;
}
@ -290,10 +290,10 @@ bool WMS::parseCapabilities(const QString &path, const Setup &setup)
return false;
}
_boundingBox = ctx.layers.first().boundingBox;
_bbox = ctx.layers.first().boundingBox;
for (int i = 1; i < ctx.layers.size(); i++)
_boundingBox &= ctx.layers.at(i).boundingBox;
if (_boundingBox.isNull()) {
_bbox &= ctx.layers.at(i).boundingBox;
if (_bbox.isNull()) {
_errorString = "Empty layers bounding box join";
return false;
}
@ -306,42 +306,57 @@ bool WMS::parseCapabilities(const QString &path, const Setup &setup)
return false;
}
_tileUrl = ctx.url.isEmpty() ? setup.url() : ctx.url;
if (_version >= "1.3.0") {
if (_setup.coordinateSystem().axisOrder() == CoordinateSystem::Unknown)
_cs = _projection.coordinateSystem();
else
_cs = _setup.coordinateSystem();
} else
_cs = CoordinateSystem::XY;
_getMapUrl = ctx.url.isEmpty() ? _setup.url() : ctx.url;
return true;
}
bool WMS::getCapabilities(const QString &url, const QString &file,
const Authorization &authorization)
bool WMS::downloadCapabilities(const QString &url)
{
Downloader d;
QList<Download> dl;
dl.append(Download(url, file));
QEventLoop wait;
QObject::connect(&d, SIGNAL(finished()), &wait, SLOT(quit()));
if (d.get(dl, authorization))
wait.exec();
if (!QFileInfo(file).exists()) {
_errorString = "Error downloading capabilities XML file";
return false;
if (!_downloader) {
_downloader = new Downloader(this);
connect(_downloader, SIGNAL(finished()), this,
SLOT(capabilitiesReady()));
}
return true;
QList<Download> dl;
dl.append(Download(url, _path));
return _downloader->get(dl, _setup.authorization());
}
WMS::WMS(const QString &file, const WMS::Setup &setup) : _valid(false)
void WMS::capabilitiesReady()
{
QString capaUrl = QString("%1%2service=WMS&request=GetCapabilities")
if (!QFileInfo(_path).exists()) {
_errorString = "Error downloading capabilities XML file";
_valid = false;
} else {
_ready = true;
_valid = parseCapabilities();
}
emit downloadFinished();
}
WMS::WMS(const QString &file, const WMS::Setup &setup, QObject *parent)
: QObject(parent), _setup(setup), _path(file), _downloader(0), _valid(false),
_ready(false)
{
QString url = QString("%1%2service=WMS&request=GetCapabilities")
.arg(setup.url(), setup.url().contains('?') ? "&" : "?");
if (!QFileInfo(file).exists())
if (!getCapabilities(capaUrl, file, setup.authorization()))
return;
if (!parseCapabilities(file, setup))
return;
_valid = true;
_valid = downloadCapabilities(url);
else {
_ready = true;
_valid = parseCapabilities();
}
}

View File

@ -12,8 +12,10 @@
class QXmlStreamReader;
class WMS
class WMS : public QObject
{
Q_OBJECT
public:
class Setup
{
@ -48,17 +50,26 @@ public:
};
WMS(const QString &path, const Setup &setup);
WMS(const QString &path, const Setup &setup, QObject *parent = 0);
const RectC &bbox() const {return _bbox;}
const Projection &projection() const {return _projection;}
CoordinateSystem cs() const {return _cs;}
const RangeF &scaleDenominator() const {return _scaleDenominator;}
const RectC &boundingBox() const {return _boundingBox;}
const QString &version() const {return _version;}
const QString &tileUrl() const {return _tileUrl;}
const QString &getMapUrl() const {return _getMapUrl;}
const WMS::Setup &setup() const {return _setup;}
bool isReady() const {return _valid && _ready;}
bool isValid() const {return _valid;}
const QString &errorString() const {return _errorString;}
signals:
void downloadFinished();
private slots:
void capabilitiesReady();
private:
struct Layer {
QString name;
@ -97,20 +108,21 @@ private:
RectC &pBoundingBox);
void capability(QXmlStreamReader &reader, CTX &ctx);
void capabilities(QXmlStreamReader &reader, CTX &ctx);
bool parseCapabilities(const QString &path, const Setup &setup);
bool getCapabilities(const QString &url, const QString &file,
const Authorization &authorization);
bool parseCapabilities();
bool downloadCapabilities(const QString &url);
WMS::Setup _setup;
QString _path;
Downloader *_downloader;
Projection _projection;
RangeF _scaleDenominator;
RectC _boundingBox;
RectC _bbox;
QString _version;
QString _tileUrl;
QString _getMapUrl;
CoordinateSystem _cs;
bool _valid;
bool _valid, _ready;
QString _errorString;
static Downloader *_downloader;
};
#endif // WMS_H

View File

@ -14,109 +14,94 @@
double WMSMap::sd2res(double scaleDenominator) const
{
return scaleDenominator * 0.28e-3 * _projection.units().fromMeters(1.0);
return scaleDenominator * _wms->projection().units().fromMeters(1.0)
* 0.28e-3;
}
QString WMSMap::tileUrl(const QString &baseUrl, const QString &version) const
QString WMSMap::tileUrl() const
{
QString url;
const WMS::Setup &setup = _wms->setup();
url = QString("%1%2service=WMS&version=%3&request=GetMap&bbox=$bbox"
QString url = QString("%1%2service=WMS&version=%3&request=GetMap&bbox=$bbox"
"&width=%4&height=%5&layers=%6&styles=%7&format=%8&transparent=true")
.arg(baseUrl, baseUrl.contains('?') ? "&" : "?", version,
QString::number(_tileSize), QString::number(_tileSize), _setup.layer(),
_setup.style(), _setup.format());
.arg(_wms->getMapUrl(), _wms->getMapUrl().contains('?') ? "&" : "?",
_wms->version(), QString::number(_tileSize), QString::number(_tileSize),
setup.layer(), setup.style(), setup.format());
if (version >= "1.3.0")
url.append(QString("&CRS=%1").arg(_setup.crs()));
if (_wms->version() >= "1.3.0")
url.append(QString("&CRS=%1").arg(setup.crs()));
else
url.append(QString("&SRS=%1").arg(_setup.crs()));
url.append(QString("&SRS=%1").arg(setup.crs()));
for (int i = 0; i < _setup.dimensions().size(); i++) {
const KV<QString, QString> &dim = _setup.dimensions().at(i);
for (int i = 0; i < setup.dimensions().size(); i++) {
const KV<QString, QString> &dim = setup.dimensions().at(i);
url.append(QString("&%1=%2").arg(dim.key(), dim.value()));
}
return url;
}
QString WMSMap::tilesDir() const
{
return QString(QDir(ProgramPaths::tilesDir()).filePath(_name));
}
void WMSMap::computeZooms(const RangeF &scaleDenominator)
void WMSMap::computeZooms()
{
_zooms.clear();
if (scaleDenominator.size() > 0) {
double ld = log2(scaleDenominator.max() - EPSILON)
- log2(scaleDenominator.min() + EPSILON);
const RangeF &sd = _wms->scaleDenominator();
if (sd.size() > 0) {
double ld = log2(sd.max() - EPSILON) - log2(sd.min() + EPSILON);
int cld = (int)ceil(ld);
double step = ld / (double)cld;
double lmax = log2(scaleDenominator.max() - EPSILON);
double lmax = log2(sd.max() - EPSILON);
for (int i = 0; i <= cld; i++)
_zooms.append(pow(2.0, lmax - i * step));
} else
_zooms.append(scaleDenominator.min() + EPSILON);
_zooms.append(sd.min() + EPSILON);
}
void WMSMap::updateTransform()
{
double pixelSpan = sd2res(_zooms.at(_zoom));
if (_projection.isGeographic())
if (_wms->projection().isGeographic())
pixelSpan /= deg2rad(WGS84_RADIUS);
_transform = Transform(ReferencePoint(PointD(0, 0),
_projection.ll2xy(_bbox.topLeft())), PointD(pixelSpan, pixelSpan));
}
bool WMSMap::loadWMS()
{
QString file = tilesDir() + "/" + CAPABILITIES_FILE;
WMS wms(file, _setup);
if (!wms.isValid()) {
_errorString = wms.errorString();
return false;
}
_projection = wms.projection();
_bbox = wms.boundingBox();
_bounds = RectD(_bbox, _projection);
_tileLoader->setUrl(tileUrl(wms.tileUrl(), wms.version()));
if (wms.version() >= "1.3.0") {
if (_setup.coordinateSystem().axisOrder() == CoordinateSystem::Unknown)
_cs = _projection.coordinateSystem();
else
_cs = _setup.coordinateSystem();
} else
_cs = CoordinateSystem::XY;
computeZooms(wms.scaleDenominator());
updateTransform();
return true;
_wms->projection().ll2xy(_wms->bbox().topLeft())),
PointD(pixelSpan, pixelSpan));
}
WMSMap::WMSMap(const QString &name, const WMS::Setup &setup, int tileSize,
QObject *parent) : Map(parent), _name(name), _setup(setup), _tileLoader(0),
_zoom(0), _tileSize(tileSize), _mapRatio(1.0), _valid(false)
QObject *parent) : Map(parent), _name(name), _tileLoader(0), _zoom(0),
_tileSize(tileSize), _mapRatio(1.0)
{
_tileLoader = new TileLoader(tilesDir(), this);
_tileLoader->setAuthorization(_setup.authorization());
connect(_tileLoader, SIGNAL(finished()), this, SIGNAL(loaded()));
QString tilesDir(QDir(ProgramPaths::tilesDir()).filePath(_name));
_valid = loadWMS();
_tileLoader = new TileLoader(tilesDir, this);
_tileLoader->setAuthorization(setup.authorization());
connect(_tileLoader, SIGNAL(finished()), this, SIGNAL(tilesLoaded()));
_wms = new WMS(QDir(tilesDir).filePath(CAPABILITIES_FILE), setup, this);
connect(_wms, SIGNAL(downloadFinished()), this, SLOT(wmsReady()));
if (_wms->isReady())
init();
}
void WMSMap::init()
{
_tileLoader->setUrl(tileUrl());
_bounds = RectD(_wms->bbox(), _wms->projection());
computeZooms();
updateTransform();
}
void WMSMap::wmsReady()
{
if (_wms->isValid())
init();
emit mapLoaded();
}
void WMSMap::clearCache()
{
_tileLoader->clearCache();
_zoom = 0;
if (!loadWMS())
qWarning("%s: %s", qPrintable(_name), qPrintable(_errorString));
}
QRectF WMSMap::bounds()
@ -128,10 +113,10 @@ QRectF WMSMap::bounds()
int WMSMap::zoomFit(const QSize &size, const RectC &rect)
{
if (rect.isValid()) {
RectD prect(rect, _projection);
RectD prect(rect, _wms->projection());
PointD sc(prect.width() / size.width(), prect.height() / size.height());
double resolution = qMax(qAbs(sc.x()), qAbs(sc.y()));
if (_projection.isGeographic())
if (_wms->projection().isGeographic())
resolution *= deg2rad(WGS84_RADIUS);
_zoom = 0;
@ -169,12 +154,12 @@ int WMSMap::zoomOut()
QPointF WMSMap::ll2xy(const Coordinates &c)
{
return _transform.proj2img(_projection.ll2xy(c)) / _mapRatio;
return _transform.proj2img(_wms->projection().ll2xy(c)) / _mapRatio;
}
Coordinates WMSMap::xy2ll(const QPointF &p)
{
return _projection.xy2ll(_transform.img2proj(p * _mapRatio));
return _wms->projection().xy2ll(_transform.img2proj(p * _mapRatio));
}
qreal WMSMap::tileSize() const
@ -197,7 +182,7 @@ void WMSMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
j * _tileSize)));
PointD tbr(_transform.img2proj(QPointF(i * _tileSize + _tileSize,
j * _tileSize + _tileSize)));
RectD bbox = (_cs.axisOrder() == CoordinateSystem::YX)
RectD bbox = (_wms->cs().axisOrder() == CoordinateSystem::YX)
? RectD(PointD(tbr.y(), tbr.x()), PointD(ttl.y(), ttl.x()))
: RectD(ttl, tbr);

View File

@ -36,34 +36,30 @@ public:
{_mapRatio = mapRatio;}
void clearCache();
bool isValid() const {return _valid;}
QString errorString() const {return _errorString;}
bool isReady() const {return _wms->isReady();}
bool isValid() const {return _wms->isValid();}
QString errorString() const {return _wms->errorString();}
private slots:
void wmsReady();
private:
QString tileUrl(const QString &baseUrl, const QString &version) const;
QString tileUrl() const;
double sd2res(double scaleDenominator) const;
QString tilesDir() const;
void computeZooms(const RangeF &scaleDenominator);
void computeZooms();
void updateTransform();
bool loadWMS();
qreal tileSize() const;
void init();
QString _name;
WMS::Setup _setup;
WMS *_wms;
TileLoader *_tileLoader;
Projection _projection;
Transform _transform;
CoordinateSystem _cs;
QVector<double> _zooms;
RectC _bbox;
RectD _bounds;
Transform _transform;
QVector<double> _zooms;
int _zoom;
int _tileSize;
qreal _mapRatio;
bool _valid;
QString _errorString;
};
#endif // WMSMAP_H

View File

@ -58,7 +58,7 @@ void WMTS::tileMatrixSet(QXmlStreamReader &reader, CTX &ctx)
{
while (reader.readNextStartElement()) {
if (reader.name() == "Identifier") {
if (reader.readElementText() != ctx.setup.set()) {
if (reader.readElementText() != _setup.set()) {
skipParentElement(reader);
return;
}
@ -114,7 +114,7 @@ void WMTS::tileMatrixSetLink(QXmlStreamReader &reader, CTX &ctx)
{
while (reader.readNextStartElement()) {
if (reader.name() == "TileMatrixSet") {
if (reader.readElementText() == ctx.setup.set())
if (reader.readElementText() == _setup.set())
ctx.hasSet = true;
else {
skipParentElement(reader);
@ -163,7 +163,7 @@ void WMTS::layer(QXmlStreamReader &reader, CTX &ctx)
{
while (reader.readNextStartElement()) {
if (reader.name() == "Identifier") {
if (reader.readElementText() == ctx.setup.layer())
if (reader.readElementText() == _setup.layer())
ctx.hasLayer = true;
else {
skipParentElement(reader);
@ -172,10 +172,10 @@ void WMTS::layer(QXmlStreamReader &reader, CTX &ctx)
} else if (reader.name() == "TileMatrixSetLink")
tileMatrixSetLink(reader, ctx);
else if (reader.name() == "WGS84BoundingBox")
_bounds = wgs84BoundingBox(reader);
ctx.bbox = wgs84BoundingBox(reader);
else if (reader.name() == "ResourceURL") {
const QXmlStreamAttributes &attr = reader.attributes();
if (attr.value("resourceType") == "tile" && ctx.setup.rest())
if (attr.value("resourceType") == "tile" && _setup.rest())
_tileUrl = attr.value("template").toString();
reader.skipCurrentElement();
} else if (reader.name() == "Style") {
@ -184,11 +184,11 @@ void WMTS::layer(QXmlStreamReader &reader, CTX &ctx)
QString s = style(reader);
if (isDefault)
ctx.defaultStyle = s;
if (s == ctx.setup.style())
if (s == _setup.style())
ctx.hasStyle = true;
} else if (reader.name() == "Format") {
QString format(reader.readElementText());
if (bareFormat(format) == bareFormat(ctx.setup.format()))
if (bareFormat(format) == bareFormat(_setup.format()))
ctx.hasFormat = true;
} else
reader.skipCurrentElement();
@ -232,9 +232,9 @@ void WMTS::createZooms(const CTX &ctx)
qSort(_zooms);
}
bool WMTS::parseCapabilities(const QString &path, CTX &ctx)
bool WMTS::parseCapabilities(CTX &ctx)
{
QFile file(path);
QFile file(_path);
QXmlStreamReader reader;
if (!file.open(QFile::ReadOnly | QFile::Text)) {
@ -250,30 +250,30 @@ bool WMTS::parseCapabilities(const QString &path, CTX &ctx)
reader.raiseError("Not a Capabilities XML file");
}
if (reader.error()) {
_errorString = QString("%1:%2: %3").arg(path).arg(reader.lineNumber())
_errorString = QString("%1:%2: %3").arg(_path).arg(reader.lineNumber())
.arg(reader.errorString());
return false;
}
if (!ctx.hasLayer) {
_errorString = ctx.setup.layer() + ": layer not provided";
_errorString = _setup.layer() + ": layer not provided";
return false;
}
if (!ctx.hasStyle && !ctx.setup.style().isEmpty()) {
_errorString = ctx.setup.style() + ": style not provided";
if (!ctx.hasStyle && !_setup.style().isEmpty()) {
_errorString = _setup.style() + ": style not provided";
return false;
}
if (!ctx.hasStyle && ctx.setup.style().isEmpty()
if (!ctx.hasStyle && _setup.style().isEmpty()
&& ctx.defaultStyle.isEmpty()) {
_errorString = "Default style not provided";
return false;
}
if (!ctx.setup.rest() && !ctx.hasFormat) {
_errorString = ctx.setup.format() + ": format not provided";
if (!_setup.rest() && !ctx.hasFormat) {
_errorString = _setup.format() + ": format not provided";
return false;
}
if (!ctx.hasSet) {
_errorString = ctx.setup.set() + ": set not provided";
_errorString = _setup.set() + ": set not provided";
return false;
}
if (ctx.crs.isNull()) {
@ -290,74 +290,92 @@ bool WMTS::parseCapabilities(const QString &path, CTX &ctx)
_errorString = "No usable tile matrix found";
return false;
}
if (ctx.setup.rest() && _tileUrl.isNull()) {
if (_setup.rest() && _tileUrl.isNull()) {
_errorString = "Missing tile URL template";
return false;
}
_bbox = ctx.bbox;
_cs = (_setup.coordinateSystem().axisOrder() == CoordinateSystem::Unknown)
? _projection.coordinateSystem() : _setup.coordinateSystem();
return true;
}
bool WMTS::downloadCapabilities(const QString &url, const QString &file,
const Authorization &authorization)
bool WMTS::downloadCapabilities(const QString &url)
{
Downloader d;
QList<Download> dl;
dl.append(Download(url, file));
QEventLoop wait;
QObject::connect(&d, SIGNAL(finished()), &wait, SLOT(quit()));
if (d.get(dl, authorization))
wait.exec();
if (!QFileInfo(file).exists()) {
_errorString = "Error downloading capabilities XML file";
return false;
if (!_downloader) {
_downloader = new Downloader(this);
connect(_downloader, SIGNAL(finished()), this,
SLOT(capabilitiesReady()));
}
return true;
QList<Download> dl;
dl.append(Download(url, _path));
return _downloader->get(dl, _setup.authorization());
}
WMTS::WMTS(const QString &file, const WMTS::Setup &setup) : _valid(false)
void WMTS::capabilitiesReady()
{
QUrl url(setup.rest() ? setup.url() : QString(
"%1%2service=WMTS&Version=1.0.0&request=GetCapabilities").arg(setup.url(),
setup.url().contains('?') ? "&" : "?"));
if (!QFileInfo(_path).exists()) {
_errorString = "Error downloading capabilities XML file";
_valid = false;
} else {
_ready = true;
_valid = init();
}
if (!url.isLocalFile() && !QFileInfo(file).exists())
if (!downloadCapabilities(url.toString(), file, setup.authorization()))
return;
emit downloadFinished();
}
CTX ctx(setup);
if (!parseCapabilities(url.isLocalFile() ? url.toLocalFile() : file, ctx))
return;
bool WMTS::init()
{
CTX ctx;
if (!parseCapabilities(ctx))
return false;
QString style = setup.style().isEmpty() ? ctx.defaultStyle : setup.style();
if (!setup.rest()) {
QString style = _setup.style().isEmpty() ? ctx.defaultStyle : _setup.style();
if (!_setup.rest()) {
_tileUrl = QString("%1%2service=WMTS&Version=1.0.0&request=GetTile"
"&Format=%3&Layer=%4&Style=%5&TileMatrixSet=%6&TileMatrix=$z"
"&TileRow=$y&TileCol=$x").arg(setup.url(),
setup.url().contains('?') ? "&" : "?" , setup.format(),
setup.layer(), style, setup.set());
for (int i = 0; i < setup.dimensions().size(); i++) {
const KV<QString, QString> &dim = setup.dimensions().at(i);
"&TileRow=$y&TileCol=$x").arg(_setup.url(),
_setup.url().contains('?') ? "&" : "?" , _setup.format(),
_setup.layer(), style, _setup.set());
for (int i = 0; i < _setup.dimensions().size(); i++) {
const KV<QString, QString> &dim = _setup.dimensions().at(i);
_tileUrl.append(QString("&%1=%2").arg(dim.key(), dim.value()));
}
} else {
_tileUrl.replace("{Style}", style, Qt::CaseInsensitive);
_tileUrl.replace("{TileMatrixSet}", setup.set(), Qt::CaseInsensitive);
_tileUrl.replace("{TileMatrixSet}", _setup.set(), Qt::CaseInsensitive);
_tileUrl.replace("{TileMatrix}", "$z", Qt::CaseInsensitive);
_tileUrl.replace("{TileRow}", "$y", Qt::CaseInsensitive);
_tileUrl.replace("{TileCol}", "$x", Qt::CaseInsensitive);
for (int i = 0; i < setup.dimensions().size(); i++) {
const KV<QString, QString> &dim = setup.dimensions().at(i);
for (int i = 0; i < _setup.dimensions().size(); i++) {
const KV<QString, QString> &dim = _setup.dimensions().at(i);
_tileUrl.replace(QString("{%1}").arg(dim.key()), dim.value(),
Qt::CaseInsensitive);
}
}
_valid = true;
return true;
}
WMTS::WMTS(const QString &file, const WMTS::Setup &setup, QObject *parent)
: QObject(parent), _setup(setup), _downloader(0), _valid(false), _ready(false)
{
QUrl url(setup.rest() ? setup.url() : QString(
"%1%2service=WMTS&Version=1.0.0&request=GetCapabilities").arg(setup.url(),
setup.url().contains('?') ? "&" : "?"));
_path = url.isLocalFile() ? url.toLocalFile() : file;
if (!url.isLocalFile() && !QFileInfo(file).exists())
_valid = downloadCapabilities(url.toString());
else {
_ready = true;
_valid = init();
}
}
#ifndef QT_NO_DEBUG

View File

@ -14,8 +14,10 @@
class QXmlStreamReader;
class WMTS
class WMTS : public QObject
{
Q_OBJECT
public:
class Setup
{
@ -79,16 +81,24 @@ public:
};
WMTS(const QString &path, const Setup &setup);
WMTS(const QString &path, const Setup &setup, QObject *parent = 0);
const RectC &bounds() const {return _bounds;}
const RectC &bbox() const {return _bbox;}
const QList<Zoom> &zooms() const {return _zooms;}
const Projection &projection() const {return _projection;}
const QString &tileUrl() const {return _tileUrl;}
CoordinateSystem cs() const {return _cs;}
bool isReady() const {return _valid && _ready;}
bool isValid() const {return _valid;}
const QString &errorString() const {return _errorString;}
signals:
void downloadFinished();
private slots:
void capabilitiesReady();
private:
struct TileMatrix {
QString id;
@ -118,18 +128,18 @@ private:
};
struct CTX {
const Setup &setup;
QSet<TileMatrix> matrixes;
QSet<MatrixLimits> limits;
QString crs;
QString defaultStyle;
RectC bbox;
bool hasLayer;
bool hasStyle;
bool hasFormat;
bool hasSet;
CTX(const Setup &setup) : setup(setup), hasLayer(false), hasStyle(false),
hasFormat(false), hasSet(false) {}
CTX() : hasLayer(false), hasStyle(false), hasFormat(false), hasSet(false)
{}
};
RectC wgs84BoundingBox(QXmlStreamReader &reader);
@ -142,17 +152,21 @@ private:
void layer(QXmlStreamReader &reader, CTX &ctx);
void contents(QXmlStreamReader &reader, CTX &ctx);
void capabilities(QXmlStreamReader &reader, CTX &ctx);
bool parseCapabilities(const QString &path, CTX &ctx);
bool downloadCapabilities(const QString &url, const QString &file,
const Authorization &authorization);
bool parseCapabilities(CTX &ctx);
bool downloadCapabilities(const QString &url);
void createZooms(const CTX &ctx);
bool init();
WMTS::Setup _setup;
QString _path;
Downloader *_downloader;
RectC _bbox;
QList<Zoom> _zooms;
RectC _bounds;
Projection _projection;
QString _tileUrl;
CoordinateSystem _cs;
bool _valid;
bool _valid, _ready;
QString _errorString;
friend uint qHash(const WMTS::TileMatrix &key);

View File

@ -12,70 +12,57 @@
#define CAPABILITIES_FILE "capabilities.xml"
bool WMTSMap::loadWMTS()
WMTSMap::WMTSMap(const QString &name, const WMTS::Setup &setup, qreal tileRatio,
QObject *parent) : Map(parent), _name(name), _tileLoader(0), _zoom(0),
_mapRatio(1.0), _tileRatio(tileRatio)
{
QString file = tilesDir() + "/" + CAPABILITIES_FILE;
QString tilesDir(QDir(ProgramPaths::tilesDir()).filePath(_name));
WMTS wmts(file, _setup);
if (!wmts.isValid()) {
_errorString = wmts.errorString();
return false;
}
_tileLoader = new TileLoader(tilesDir, this);
_tileLoader->setAuthorization(setup.authorization());
connect(_tileLoader, SIGNAL(finished()), this, SIGNAL(tilesLoaded()));
_zooms = wmts.zooms();
_projection = wmts.projection();
_tileLoader->setUrl(wmts.tileUrl());
_bounds = RectD(wmts.bounds(), _projection);
if (_setup.coordinateSystem().axisOrder() == CoordinateSystem::Unknown)
_cs = _projection.coordinateSystem();
else
_cs = _setup.coordinateSystem();
updateTransform();
return true;
_wmts = new WMTS(QDir(tilesDir).filePath(CAPABILITIES_FILE), setup, this);
connect(_wmts, SIGNAL(downloadFinished()), this, SLOT(wmtsReady()));
if (_wmts->isReady())
init();
}
WMTSMap::WMTSMap(const QString &name, const WMTS::Setup &setup, qreal tileRatio,
QObject *parent) : Map(parent), _name(name), _setup(setup), _tileLoader(0),
_zoom(0), _mapRatio(1.0), _tileRatio(tileRatio), _valid(false)
void WMTSMap::init()
{
_tileLoader = new TileLoader(tilesDir(), this);
_tileLoader->setAuthorization(_setup.authorization());
connect(_tileLoader, SIGNAL(finished()), this, SIGNAL(loaded()));
_tileLoader->setUrl(_wmts->tileUrl());
_bounds = RectD(_wmts->bbox(), _wmts->projection());
updateTransform();
}
_valid = loadWMTS();
void WMTSMap::wmtsReady()
{
if (_wmts->isValid())
init();
emit mapLoaded();
}
void WMTSMap::clearCache()
{
_tileLoader->clearCache();
_zoom = 0;
if (!loadWMTS())
qWarning("%s: %s", qPrintable(_name), qPrintable(_errorString));
}
QString WMTSMap::tilesDir() const
{
return QString(QDir(ProgramPaths::tilesDir()).filePath(_name));
}
double WMTSMap::sd2res(double scaleDenominator) const
{
return scaleDenominator * 0.28e-3 * _projection.units().fromMeters(1.0);
return scaleDenominator * 0.28e-3
* _wmts->projection().units().fromMeters(1.0);
}
void WMTSMap::updateTransform()
{
const WMTS::Zoom &z = _zooms.at(_zoom);
const WMTS::Zoom &z = _wmts->zooms().at(_zoom);
PointD topLeft = (_cs.axisOrder() == CoordinateSystem::YX)
PointD topLeft = (_wmts->cs().axisOrder() == CoordinateSystem::YX)
? PointD(z.topLeft().y(), z.topLeft().x()) : z.topLeft();
double pixelSpan = sd2res(z.scaleDenominator());
if (_projection.isGeographic())
if (_wmts->projection().isGeographic())
pixelSpan /= deg2rad(WGS84_RADIUS);
_transform = Transform(ReferencePoint(PointD(0, 0), topLeft),
PointD(pixelSpan, pixelSpan));
@ -83,7 +70,7 @@ void WMTSMap::updateTransform()
QRectF WMTSMap::bounds()
{
const WMTS::Zoom &z = _zooms.at(_zoom);
const WMTS::Zoom &z = _wmts->zooms().at(_zoom);
QRectF tileBounds, bounds;
tileBounds = (z.limits().isNull()) ?
@ -95,29 +82,29 @@ QRectF WMTSMap::bounds()
if (_bounds.isValid())
bounds = QRectF(_transform.proj2img(_bounds.topLeft())
/ coordinatesRatio(), _transform.proj2img(_bounds.bottomRight())
/ coordinatesRatio());
/ coordinatesRatio(), _transform.proj2img(
_bounds.bottomRight()) / coordinatesRatio());
return bounds.isValid() ? tileBounds.intersected(bounds) : tileBounds;
}
int WMTSMap::zoomFit(const QSize &size, const RectC &rect)
{
if (rect.isValid()) {
RectD prect(rect, _projection);
RectD prect(rect, _wmts->projection());
PointD sc(prect.width() / size.width(), prect.height() / size.height());
double resolution = qMax(qAbs(sc.x()), qAbs(sc.y()));
if (_projection.isGeographic())
if (_wmts->projection().isGeographic())
resolution *= deg2rad(WGS84_RADIUS);
_zoom = 0;
for (int i = 0; i < _zooms.size(); i++) {
if (sd2res(_zooms.at(i).scaleDenominator()) < resolution
for (int i = 0; i < _wmts->zooms().size(); i++) {
if (sd2res(_wmts->zooms().at(i).scaleDenominator()) < resolution
/ coordinatesRatio())
break;
_zoom = i;
}
} else
_zoom = _zooms.size() - 1;
_zoom = _wmts->zooms().size() - 1;
updateTransform();
return _zoom;
@ -131,7 +118,7 @@ void WMTSMap::setZoom(int zoom)
int WMTSMap::zoomIn()
{
_zoom = qMin(_zoom + 1, _zooms.size() - 1);
_zoom = qMin(_zoom + 1, _wmts->zooms().size() - 1);
updateTransform();
return _zoom;
}
@ -161,7 +148,7 @@ QSizeF WMTSMap::tileSize(const WMTS::Zoom &zoom) const
void WMTSMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
{
const WMTS::Zoom &z = _zooms.at(_zoom);
const WMTS::Zoom &z = _wmts->zooms().at(_zoom);
QSizeF ts(tileSize(z));
QPoint tl = QPoint(qFloor(rect.left() / ts.width()),
@ -194,10 +181,12 @@ void WMTSMap::draw(QPainter *painter, const QRectF &rect, Flags flags)
QPointF WMTSMap::ll2xy(const Coordinates &c)
{
return _transform.proj2img(_projection.ll2xy(c)) / coordinatesRatio();
return _transform.proj2img(_wmts->projection().ll2xy(c))
/ coordinatesRatio();
}
Coordinates WMTSMap::xy2ll(const QPointF &p)
{
return _projection.xy2ll(_transform.img2proj(p * coordinatesRatio()));
return _wmts->projection().xy2ll(_transform.img2proj(p
* coordinatesRatio()));
}

View File

@ -36,31 +36,28 @@ public:
{_mapRatio = mapRatio;}
void clearCache();
bool isValid() const {return _valid;}
QString errorString() const {return _errorString;}
bool isReady() const {return _wmts->isReady();}
bool isValid() const {return _wmts->isValid();}
QString errorString() const {return _wmts->errorString();}
private slots:
void wmtsReady();
private:
bool loadWMTS();
double sd2res(double scaleDenominator) const;
QString tilesDir() const;
void updateTransform();
QSizeF tileSize(const WMTS::Zoom &zoom) const;
qreal coordinatesRatio() const;
qreal imageRatio() const;
void init();
QString _name;
WMTS::Setup _setup;
WMTS *_wmts;
TileLoader *_tileLoader;
RectD _bounds;
QList<WMTS::Zoom> _zooms;
Projection _projection;
Transform _transform;
CoordinateSystem _cs;
RectD _bounds;
int _zoom;
qreal _mapRatio, _tileRatio;
bool _valid;
QString _errorString;
};
#endif // WMTSMAP_H