1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 19:55:53 +01:00

Extended error checking

This commit is contained in:
Martin Tůma 2018-02-26 22:35:58 +01:00
parent 688e309ef7
commit fa99d01a77
2 changed files with 142 additions and 93 deletions

View File

@ -12,9 +12,9 @@
Downloader *WMTS::_downloader = 0; Downloader *WMTS::_downloader = 0;
bool WMTS::createProjection() bool WMTS::createProjection(const QString &crs)
{ {
QStringList list(_crs.split(':')); QStringList list(crs.split(':'));
QString authority, code; QString authority, code;
bool res; bool res;
int epsg; int epsg;
@ -92,24 +92,24 @@ WMTS::TileMatrix WMTS::tileMatrix(QXmlStreamReader &reader, bool yx)
return matrix; return matrix;
} }
void WMTS::tileMatrixSet(QXmlStreamReader &reader, const QString &set, bool yx) void WMTS::tileMatrixSet(CTX &ctx)
{ {
QString id, crs; QString id, crs;
QSet<TileMatrix> matrixes; QSet<TileMatrix> matrixes;
while (reader.readNextStartElement()) { while (ctx.reader.readNextStartElement()) {
if (reader.name() == "Identifier") if (ctx.reader.name() == "Identifier")
id = reader.readElementText(); id = ctx.reader.readElementText();
else if (reader.name() == "SupportedCRS") else if (ctx.reader.name() == "SupportedCRS")
crs = reader.readElementText(); crs = ctx.reader.readElementText();
else if (reader.name() == "TileMatrix") else if (ctx.reader.name() == "TileMatrix")
matrixes.insert(tileMatrix(reader, yx)); matrixes.insert(tileMatrix(ctx.reader, ctx.setup.yx));
else else
reader.skipCurrentElement(); ctx.reader.skipCurrentElement();
} }
if (id == set) { if (id == ctx.setup.set) {
_crs = crs; ctx.crs = crs;
_matrixes = matrixes; _matrixes = matrixes;
} }
} }
@ -153,23 +153,25 @@ QSet<WMTS::MatrixLimits> WMTS::tileMatrixSetLimits(QXmlStreamReader &reader)
return limits; return limits;
} }
void WMTS::tileMatrixSetLink(QXmlStreamReader &reader, const QString &set) void WMTS::tileMatrixSetLink(CTX &ctx)
{ {
QString id; QString id;
QSet<MatrixLimits> limits; QSet<MatrixLimits> limits;
while (reader.readNextStartElement()) { while (ctx.reader.readNextStartElement()) {
if (reader.name() == "TileMatrixSet") if (ctx.reader.name() == "TileMatrixSet")
id = reader.readElementText(); id = ctx.reader.readElementText();
else if (reader.name() == "TileMatrixSetLimits") else if (ctx.reader.name() == "TileMatrixSetLimits")
limits = tileMatrixSetLimits(reader); limits = tileMatrixSetLimits(ctx.reader);
else else
reader.skipCurrentElement(); ctx.reader.skipCurrentElement();
} }
if (id == set) if (id == ctx.setup.set) {
ctx.set = true;
_limits = limits; _limits = limits;
} }
}
RectC WMTS::wgs84BoundingBox(QXmlStreamReader &reader) RectC WMTS::wgs84BoundingBox(QXmlStreamReader &reader)
{ {
@ -189,83 +191,137 @@ RectC WMTS::wgs84BoundingBox(QXmlStreamReader &reader)
return RectC(topLeft, bottomRight); return RectC(topLeft, bottomRight);
} }
void WMTS::layer(QXmlStreamReader &reader, const QString &layer, QString WMTS::style(QXmlStreamReader &reader)
const QString &set)
{ {
QString id; QString id;
RectC bounds;
QString tpl;
while (reader.readNextStartElement()) { while (reader.readNextStartElement()) {
if (reader.name() == "Identifier") if (reader.name() == "Identifier")
id = reader.readElementText(); id = reader.readElementText();
else if (reader.name() == "TileMatrixSetLink") else
tileMatrixSetLink(reader, set); reader.skipCurrentElement();
else if (reader.name() == "WGS84BoundingBox") }
bounds = wgs84BoundingBox(reader);
else if (reader.name() == "ResourceURL") { return id;
const QXmlStreamAttributes &attr = reader.attributes(); }
void WMTS::layer(CTX &ctx)
{
QString id, tpl;
RectC bounds;
QStringList formats, styles;
while (ctx.reader.readNextStartElement()) {
if (ctx.reader.name() == "Identifier")
id = ctx.reader.readElementText();
else if (ctx.reader.name() == "TileMatrixSetLink")
tileMatrixSetLink(ctx);
else if (ctx.reader.name() == "WGS84BoundingBox")
bounds = wgs84BoundingBox(ctx.reader);
else if (ctx.reader.name() == "ResourceURL") {
const QXmlStreamAttributes &attr = ctx.reader.attributes();
if (attr.value("resourceType") == "tile") if (attr.value("resourceType") == "tile")
tpl = attr.value("template").toString(); tpl = attr.value("template").toString();
reader.skipCurrentElement(); ctx.reader.skipCurrentElement();
} else } else if (ctx.reader.name() == "Style")
reader.skipCurrentElement(); styles.append(style(ctx.reader));
else if (ctx.reader.name() == "Format")
formats.append(ctx.reader.readElementText());
else
ctx.reader.skipCurrentElement();
} }
if (id == layer) { if (id == ctx.setup.layer) {
ctx.layer = true;
_bounds = bounds; _bounds = bounds;
if (ctx.setup.rest)
_tileUrl = tpl; _tileUrl = tpl;
if (styles.contains(ctx.setup.style))
ctx.style = true;
if (formats.contains(ctx.setup.format))
ctx.format = true;
} }
} }
void WMTS::contents(QXmlStreamReader &reader, const QString &layer, void WMTS::contents(CTX &ctx)
const QString &set, bool yx)
{ {
while (reader.readNextStartElement()) { while (ctx.reader.readNextStartElement()) {
if (reader.name() == "TileMatrixSet") if (ctx.reader.name() == "TileMatrixSet")
tileMatrixSet(reader, set, yx); tileMatrixSet(ctx);
else if (reader.name() == "Layer") else if (ctx.reader.name() == "Layer")
WMTS::layer(reader, layer, set); layer(ctx);
else else
reader.skipCurrentElement(); ctx.reader.skipCurrentElement();
} }
} }
void WMTS::capabilities(QXmlStreamReader &reader, const QString &layer, void WMTS::capabilities(CTX &ctx)
const QString &set, bool yx)
{ {
while (reader.readNextStartElement()) { while (ctx.reader.readNextStartElement()) {
if (reader.name() == "Contents") if (ctx.reader.name() == "Contents")
contents(reader, layer, set, yx); contents(ctx);
else else
reader.skipCurrentElement(); ctx.reader.skipCurrentElement();
} }
} }
bool WMTS::parseCapabilities(const QString &path, const QString &layer, bool WMTS::parseCapabilities(const QString &path, const Setup &setup)
const QString &set, bool yx)
{ {
QFile file(path); QFile file(path);
QXmlStreamReader reader; CTX ctx(setup);
if (!file.open(QFile::ReadOnly | QFile::Text)) { if (!file.open(QFile::ReadOnly | QFile::Text)) {
_errorString = file.errorString(); _errorString = file.errorString();
return false; return false;
} }
reader.setDevice(&file); ctx.reader.setDevice(&file);
if (ctx.reader.readNextStartElement()) {
if (reader.readNextStartElement()) { if (ctx.reader.name() == "Capabilities")
if (reader.name() == "Capabilities") capabilities(ctx);
capabilities(reader, layer, set, yx);
else else
reader.raiseError("Not a Capabilities XML file"); ctx.reader.raiseError("Not a Capabilities XML file");
}
if (ctx.reader.error()) {
_errorString = QString("%1:%2: %3").arg(path).arg(
ctx.reader.lineNumber()).arg(ctx.reader.errorString());
return false;
} }
_errorString = reader.error() ? QString("%1:%2: %3").arg(path) if (!ctx.layer) {
.arg(reader.lineNumber()).arg(reader.errorString()) : QString(); _errorString = ctx.setup.layer + ": layer not provided";
return false;
}
if (!ctx.style) {
_errorString = ctx.setup.style + ": style not provided";
return false;
}
if (!ctx.setup.rest && !ctx.format) {
_errorString = ctx.setup.format + ": format not provided";
return false;
}
if (!ctx.set) {
_errorString = ctx.setup.set + ": set not provided";
return false;
}
if (ctx.crs.isNull()) {
_errorString = "Missing CRS definition";
return false;
}
if (!createProjection(ctx.crs)) {
_errorString = ctx.crs + ": unknown CRS";
return false;
}
if (_matrixes.isEmpty()) {
_errorString = "No usable tile matrix found";
return false;
}
if (ctx.setup.rest && _tileUrl.isNull()) {
_errorString = "Missing tile URL template";
return false;
}
return reader.error() ? false : true; return true;
} }
bool WMTS::getCapabilities(const QString &url, const QString &file) bool WMTS::getCapabilities(const QString &url, const QString &file)
@ -296,7 +352,7 @@ bool WMTS::load(const QString &file, const WMTS::Setup &setup)
if (!QFileInfo(file).exists()) if (!QFileInfo(file).exists())
if (!getCapabilities(capaUrl, file)) if (!getCapabilities(capaUrl, file))
return false; return false;
if (!parseCapabilities(file, setup.layer, setup.set, setup.yx)) if (!parseCapabilities(file, setup))
return false; return false;
if (!setup.rest) if (!setup.rest)
@ -312,23 +368,6 @@ bool WMTS::load(const QString &file, const WMTS::Setup &setup)
_tileUrl.replace("{TileCol}", "$x"); _tileUrl.replace("{TileCol}", "$x");
} }
if (_crs.isNull()) {
_errorString = "Missing CRS definition";
return false;
}
if (!createProjection()) {
_errorString = _crs + ": unknown CRS";
return false;
}
if (_matrixes.isEmpty()) {
_errorString = "No usable tile matrix found";
return false;
}
if (_tileUrl.isNull()) {
_errorString = "Missing tile URL";
return false;
}
return true; return true;
} }

View File

@ -6,10 +6,10 @@
#include <QSet> #include <QSet>
#include <QList> #include <QList>
#include <QHash> #include <QHash>
#include <QXmlStreamReader>
#include "common/rectc.h" #include "common/rectc.h"
#include "projection.h" #include "projection.h"
class QXmlStreamReader;
class Downloader; class Downloader;
class WMTS class WMTS
@ -87,27 +87,37 @@ private:
{return !id.isEmpty() && rect.isValid();} {return !id.isEmpty() && rect.isValid();}
}; };
bool createProjection(); struct CTX {
const Setup &setup;
QXmlStreamReader reader;
QString crs;
bool layer;
bool style;
bool format;
bool set;
CTX(const Setup &setup) : setup(setup), layer(false), style(false),
format(false), set(false) {}
};
RectC wgs84BoundingBox(QXmlStreamReader &reader); RectC wgs84BoundingBox(QXmlStreamReader &reader);
MatrixLimits tileMatrixLimits(QXmlStreamReader &reader); MatrixLimits tileMatrixLimits(QXmlStreamReader &reader);
TileMatrix tileMatrix(QXmlStreamReader &reader, bool yx); TileMatrix tileMatrix(QXmlStreamReader &reader, bool yx);
QSet<MatrixLimits> tileMatrixSetLimits(QXmlStreamReader &reader); QSet<MatrixLimits> tileMatrixSetLimits(QXmlStreamReader &reader);
void tileMatrixSet(QXmlStreamReader &reader, const QString &set, bool yx); QString style(QXmlStreamReader &reader);
void tileMatrixSetLink(QXmlStreamReader &reader, const QString &set); void tileMatrixSet(CTX &ctx);
void layer(QXmlStreamReader &reader, const QString &layer, void tileMatrixSetLink(CTX &ctx);
const QString &set); void layer(CTX &ctx);
void contents(QXmlStreamReader &reader, const QString &layer, void contents(CTX &ctx);
const QString &set, bool yx); void capabilities(CTX &ctx);
void capabilities(QXmlStreamReader &reader, const QString &layer, bool parseCapabilities(const QString &path, const Setup &setup);
const QString &set, bool yx);
bool parseCapabilities(const QString &path, const QString &layer,
const QString &set, bool yx);
bool getCapabilities(const QString &url, const QString &file); bool getCapabilities(const QString &url, const QString &file);
bool createProjection(const QString &crs);
QSet<TileMatrix> _matrixes; QSet<TileMatrix> _matrixes;
QSet<MatrixLimits> _limits; QSet<MatrixLimits> _limits;
RectC _bounds; RectC _bounds;
QString _crs;
Projection _projection; Projection _projection;
QString _tileUrl; QString _tileUrl;