From 46cefada94ea6493867ff18d6829baf57ad1172f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Tue, 27 Feb 2018 21:50:29 +0100 Subject: [PATCH] Code cleanup --- src/map/mapsource.cpp | 72 +++++++++++----------------- src/map/mapsource.h | 4 +- src/map/wmts.cpp | 106 +++++++++++++++++++++--------------------- src/map/wmts.h | 14 +++--- 4 files changed, 90 insertions(+), 106 deletions(-) diff --git a/src/map/mapsource.cpp b/src/map/mapsource.cpp index 07bc0b27..398c959f 100644 --- a/src/map/mapsource.cpp +++ b/src/map/mapsource.cpp @@ -16,7 +16,7 @@ MapSource::TMSConfig::TMSConfig() Coordinates(BOUNDS_RIGHT, BOUNDS_BOTTOM)) {} -void MapSource::zooms(QXmlStreamReader &reader, Range &val) +Range MapSource::zooms(QXmlStreamReader &reader) { const QXmlStreamAttributes &attr = reader.attributes(); int min, max; @@ -30,7 +30,7 @@ void MapSource::zooms(QXmlStreamReader &reader, Range &val) #endif // QT_VERSION < 5 if (!res || (min < ZOOM_MIN || min > ZOOM_MAX)) { reader.raiseError("Invalid minimal zoom level"); - return; + return Range(); } } else min = ZOOM_MIN; @@ -43,17 +43,20 @@ void MapSource::zooms(QXmlStreamReader &reader, Range &val) #endif // QT_VERSION < 5 if (!res || (max < ZOOM_MIN || max > ZOOM_MAX)) { reader.raiseError("Invalid maximal zoom level"); - return; + return Range(); } } else max = ZOOM_MAX; - val = Range(min, max); - if (!val.isValid()) + if (min > max) { reader.raiseError("Invalid maximal/minimal zoom level combination"); + return Range(); + } + + return Range(min, max); } -void MapSource::bounds(QXmlStreamReader &reader, RectC &val) +RectC MapSource::bounds(QXmlStreamReader &reader) { const QXmlStreamAttributes &attr = reader.attributes(); double top, left, bottom, right; @@ -67,7 +70,7 @@ void MapSource::bounds(QXmlStreamReader &reader, RectC &val) #endif // QT_VERSION < 5 if (!res || (top < BOUNDS_BOTTOM || top > BOUNDS_TOP)) { reader.raiseError("Invalid bounds top value"); - return; + return RectC(); } } else top = BOUNDS_TOP; @@ -80,7 +83,7 @@ void MapSource::bounds(QXmlStreamReader &reader, RectC &val) #endif // QT_VERSION < 5 if (!res || (bottom < BOUNDS_BOTTOM || bottom > BOUNDS_TOP)) { reader.raiseError("Invalid bounds bottom value"); - return; + return RectC(); } } else bottom = BOUNDS_BOTTOM; @@ -93,7 +96,7 @@ void MapSource::bounds(QXmlStreamReader &reader, RectC &val) #endif // QT_VERSION < 5 if (!res || (left < BOUNDS_LEFT || left > BOUNDS_RIGHT)) { reader.raiseError("Invalid bounds left value"); - return; + return RectC(); } } else left = BOUNDS_LEFT; @@ -106,21 +109,21 @@ void MapSource::bounds(QXmlStreamReader &reader, RectC &val) #endif // QT_VERSION < 5 if (!res || (right < BOUNDS_LEFT || right > BOUNDS_RIGHT)) { reader.raiseError("Invalid bounds right value"); - return; + return RectC(); } } else right = BOUNDS_RIGHT; - if (bottom > top || top < bottom) { + if (bottom >= top) { reader.raiseError("Invalid bottom/top bounds combination"); - return; + return RectC(); } - if (left > right || right < left) { + if (left >= right) { reader.raiseError("Invalid left/right bounds combination"); - return; + return RectC(); } - val = RectC(Coordinates(left, top), Coordinates(right, bottom)); + return RectC(Coordinates(left, top), Coordinates(right, bottom)); } void MapSource::map(QXmlStreamReader &reader, Config &config) @@ -131,44 +134,25 @@ void MapSource::map(QXmlStreamReader &reader, Config &config) if (reader.name() == "name") config.name = reader.readElementText(); else if (reader.name() == "url") { - if (config.type == WMTS) - config.wmts.rest = (reader.attributes().value("type") == "REST") - ? true : false; + config.wmts.rest = (reader.attributes().value("type") == "REST") + ? true : false; config.url = reader.readElementText(); } else if (reader.name() == "zoom") { - if (config.type == TMS) - zooms(reader, config.tms.zooms); - else - reader.raiseError("Illegal zoom element"); + config.tms.zooms = zooms(reader); reader.skipCurrentElement(); } else if (reader.name() == "bounds") { - if (config.type == TMS) - bounds(reader, config.tms.bounds); - else - reader.raiseError("Illegal bounds element"); + config.tms.bounds = bounds(reader); reader.skipCurrentElement(); } else if (reader.name() == "format") { - if (config.type == WMTS) - config.wmts.format = reader.readElementText(); - else - reader.raiseError("Illegal format element"); + config.wmts.format = reader.readElementText(); } else if (reader.name() == "layer") - if (config.type == WMTS) - config.wmts.layer = reader.readElementText(); - else - reader.raiseError("Illegal layer element"); + config.wmts.layer = reader.readElementText(); else if (reader.name() == "style") - if (config.type == WMTS) - config.wmts.style = reader.readElementText(); - else - reader.raiseError("Illegal style element"); + config.wmts.style = reader.readElementText(); else if (reader.name() == "set") { - if (config.type == WMTS) { - config.wmts.yx = (reader.attributes().value("axis") == "yx") - ? true : false; - config.wmts.set = reader.readElementText(); - } else - reader.raiseError("Illegal set element"); + config.wmts.yx = (reader.attributes().value("axis") == "yx") + ? true : false; + config.wmts.set = reader.readElementText(); } else reader.skipCurrentElement(); } diff --git a/src/map/mapsource.h b/src/map/mapsource.h index f24e7829..7797615d 100644 --- a/src/map/mapsource.h +++ b/src/map/mapsource.h @@ -48,8 +48,8 @@ private: Config() : type(TMS) {} }; - void bounds(QXmlStreamReader &reader, RectC &val); - void zooms(QXmlStreamReader &reader, Range &val); + RectC bounds(QXmlStreamReader &reader); + Range zooms(QXmlStreamReader &reader); void map(QXmlStreamReader &reader, Config &config); QString _errorString; diff --git a/src/map/wmts.cpp b/src/map/wmts.cpp index 919d22f4..e8723709 100644 --- a/src/map/wmts.cpp +++ b/src/map/wmts.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "downloader.h" #include "pcs.h" #include "wmts.h" @@ -92,20 +93,20 @@ WMTS::TileMatrix WMTS::tileMatrix(QXmlStreamReader &reader, bool yx) return matrix; } -void WMTS::tileMatrixSet(CTX &ctx) +void WMTS::tileMatrixSet(QXmlStreamReader &reader, CTX &ctx) { QString id, crs; QSet matrixes; - while (ctx.reader.readNextStartElement()) { - if (ctx.reader.name() == "Identifier") - id = ctx.reader.readElementText(); - else if (ctx.reader.name() == "SupportedCRS") - crs = ctx.reader.readElementText(); - else if (ctx.reader.name() == "TileMatrix") - matrixes.insert(tileMatrix(ctx.reader, ctx.setup.yx)); + while (reader.readNextStartElement()) { + if (reader.name() == "Identifier") + id = reader.readElementText(); + else if (reader.name() == "SupportedCRS") + crs = reader.readElementText(); + else if (reader.name() == "TileMatrix") + matrixes.insert(tileMatrix(reader, ctx.setup.yx)); else - ctx.reader.skipCurrentElement(); + reader.skipCurrentElement(); } if (id == ctx.setup.set) { @@ -153,18 +154,18 @@ QSet WMTS::tileMatrixSetLimits(QXmlStreamReader &reader) return limits; } -void WMTS::tileMatrixSetLink(CTX &ctx) +void WMTS::tileMatrixSetLink(QXmlStreamReader &reader, CTX &ctx) { QString id; QSet limits; - while (ctx.reader.readNextStartElement()) { - if (ctx.reader.name() == "TileMatrixSet") - id = ctx.reader.readElementText(); - else if (ctx.reader.name() == "TileMatrixSetLimits") - limits = tileMatrixSetLimits(ctx.reader); + while (reader.readNextStartElement()) { + if (reader.name() == "TileMatrixSet") + id = reader.readElementText(); + else if (reader.name() == "TileMatrixSetLimits") + limits = tileMatrixSetLimits(reader); else - ctx.reader.skipCurrentElement(); + reader.skipCurrentElement(); } if (id == ctx.setup.set) { @@ -205,30 +206,30 @@ QString WMTS::style(QXmlStreamReader &reader) return id; } -void WMTS::layer(CTX &ctx) +void WMTS::layer(QXmlStreamReader &reader, 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(); + while (reader.readNextStartElement()) { + if (reader.name() == "Identifier") + id = reader.readElementText(); + else if (reader.name() == "TileMatrixSetLink") + tileMatrixSetLink(reader, ctx); + else if (reader.name() == "WGS84BoundingBox") + bounds = wgs84BoundingBox(reader); + else if (reader.name() == "ResourceURL") { + const QXmlStreamAttributes &attr = reader.attributes(); if (attr.value("resourceType") == "tile") tpl = attr.value("template").toString(); - ctx.reader.skipCurrentElement(); - } else if (ctx.reader.name() == "Style") - styles.append(style(ctx.reader)); - else if (ctx.reader.name() == "Format") - formats.append(ctx.reader.readElementText()); + reader.skipCurrentElement(); + } else if (reader.name() == "Style") + styles.append(style(reader)); + else if (reader.name() == "Format") + formats.append(reader.readElementText()); else - ctx.reader.skipCurrentElement(); + reader.skipCurrentElement(); } if (id == ctx.setup.layer) { @@ -243,25 +244,25 @@ void WMTS::layer(CTX &ctx) } } -void WMTS::contents(CTX &ctx) +void WMTS::contents(QXmlStreamReader &reader, CTX &ctx) { - while (ctx.reader.readNextStartElement()) { - if (ctx.reader.name() == "TileMatrixSet") - tileMatrixSet(ctx); - else if (ctx.reader.name() == "Layer") - layer(ctx); + while (reader.readNextStartElement()) { + if (reader.name() == "TileMatrixSet") + tileMatrixSet(reader, ctx); + else if (reader.name() == "Layer") + layer(reader, ctx); else - ctx.reader.skipCurrentElement(); + reader.skipCurrentElement(); } } -void WMTS::capabilities(CTX &ctx) +void WMTS::capabilities(QXmlStreamReader &reader, CTX &ctx) { - while (ctx.reader.readNextStartElement()) { - if (ctx.reader.name() == "Contents") - contents(ctx); + while (reader.readNextStartElement()) { + if (reader.name() == "Contents") + contents(reader, ctx); else - ctx.reader.skipCurrentElement(); + reader.skipCurrentElement(); } } @@ -269,22 +270,23 @@ bool WMTS::parseCapabilities(const QString &path, const Setup &setup) { QFile file(path); CTX ctx(setup); + QXmlStreamReader reader; if (!file.open(QFile::ReadOnly | QFile::Text)) { _errorString = file.errorString(); return false; } - ctx.reader.setDevice(&file); - if (ctx.reader.readNextStartElement()) { - if (ctx.reader.name() == "Capabilities") - capabilities(ctx); + reader.setDevice(&file); + if (reader.readNextStartElement()) { + if (reader.name() == "Capabilities") + capabilities(reader, ctx); else - ctx.reader.raiseError("Not a Capabilities XML file"); + 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()); + if (reader.error()) { + _errorString = QString("%1:%2: %3").arg(path).arg(reader.lineNumber()) + .arg(reader.errorString()); return false; } diff --git a/src/map/wmts.h b/src/map/wmts.h index a7c3ed1d..0e0c5d52 100644 --- a/src/map/wmts.h +++ b/src/map/wmts.h @@ -6,11 +6,11 @@ #include #include #include -#include #include "common/rectc.h" #include "projection.h" class Downloader; +class QXmlStreamReader; class WMTS { @@ -89,8 +89,6 @@ private: struct CTX { const Setup &setup; - QXmlStreamReader reader; - QString crs; bool layer; bool style; @@ -106,11 +104,11 @@ private: TileMatrix tileMatrix(QXmlStreamReader &reader, bool yx); QSet tileMatrixSetLimits(QXmlStreamReader &reader); QString style(QXmlStreamReader &reader); - void tileMatrixSet(CTX &ctx); - void tileMatrixSetLink(CTX &ctx); - void layer(CTX &ctx); - void contents(CTX &ctx); - void capabilities(CTX &ctx); + void tileMatrixSet(QXmlStreamReader &reader, CTX &ctx); + void tileMatrixSetLink(QXmlStreamReader &reader, CTX &ctx); + void layer(QXmlStreamReader &reader, CTX &ctx); + void contents(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); bool createProjection(const QString &crs);