From 4096d87a6ad2d1c3c6dcde30bf019e05e9bca160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Tue, 3 Apr 2018 22:35:13 +0200 Subject: [PATCH] Improved default WMS/WMTS style handling --- src/map/mapsource.cpp | 7 +------ src/map/wms.cpp | 21 ++++++++++++--------- src/map/wms.h | 2 +- src/map/wmts.cpp | 31 ++++++++++++++++--------------- src/map/wmts.h | 12 ++++++------ 5 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/map/mapsource.cpp b/src/map/mapsource.cpp index 8b569f94..becc94e9 100644 --- a/src/map/mapsource.cpp +++ b/src/map/mapsource.cpp @@ -15,8 +15,7 @@ MapSource::Config::Config() : type(TMS), zooms(ZOOM_MIN, ZOOM_MAX), bounds(Coordinates(BOUNDS_LEFT, BOUNDS_TOP), Coordinates(BOUNDS_RIGHT, - BOUNDS_BOTTOM)), style("default"), format("image/png"), rest(false), - yx(false) {} + BOUNDS_BOTTOM)), format("image/png"), rest(false), yx(false) {} Range MapSource::zooms(QXmlStreamReader &reader) @@ -195,10 +194,6 @@ Map *MapSource::loadFile(const QString &path) _errorString = "Missing layer definition"; return 0; } - if (config.style.isEmpty()) { - _errorString = "Missing style definiton"; - return 0; - } if (config.format.isEmpty()) { _errorString = "Missing format definition"; return 0; diff --git a/src/map/wms.cpp b/src/map/wms.cpp index 15cca735..eba77907 100644 --- a/src/map/wms.cpp +++ b/src/map/wms.cpp @@ -12,13 +12,18 @@ Downloader *WMS::_downloader = 0; WMS::CTX::CTX(const Setup &setup) : setup(setup), formatSupported(false) { QStringList ll = setup.layer().split(','); - QStringList sl = setup.style().split(','); - if (ll.size() != sl.size()) - return; + if (setup.style().isEmpty()) { + for (int i = 0; i < ll.size(); i++) + layers.append(Layer(ll.at(i))); + } else { + QStringList sl = setup.style().split(','); + if (ll.size() != sl.size()) + return; - for (int i = 0; i < ll.size(); i++) - layers.append(Layer(ll.at(i), sl.at(i))); + for (int i = 0; i < ll.size(); i++) + layers.append(Layer(ll.at(i), sl.at(i))); + } } void WMS::getMap(QXmlStreamReader &reader, CTX &ctx) @@ -123,7 +128,7 @@ void WMS::layer(QXmlStreamReader &reader, CTX &ctx, layer.scaleDenominator = scaleDenominator; layer.boundingBox = boundingBox; layer.isDefined = true; - layer.hasStyle = styles.contains(layer.style); + layer.hasStyle = styles.contains(layer.style) || layer.style.isEmpty(); layer.hasCRS = CRSs.contains(ctx.setup.crs()); } } @@ -135,8 +140,6 @@ void WMS::capability(QXmlStreamReader &reader, CTX &ctx) RangeF scaleDenominator(2132.729583849784, 559082264.0287178); RectC boundingBox; - styles.append("default"); - while (reader.readNextStartElement()) { if (reader.name() == "Layer") layer(reader, ctx, CRSs, styles, scaleDenominator, boundingBox); @@ -167,7 +170,7 @@ bool WMS::parseCapabilities(const QString &path, const Setup &setup) if (ctx.layers.isEmpty()) { - _errorString = "Invalid layers definition"; + _errorString = "Invalid layers/styles list definition"; return false; } diff --git a/src/map/wms.h b/src/map/wms.h index 4968e0be..2db287a0 100644 --- a/src/map/wms.h +++ b/src/map/wms.h @@ -64,7 +64,7 @@ private: bool hasStyle; bool hasCRS; - Layer(const QString &name, const QString &style = "default") + Layer(const QString &name, const QString &style = QString()) : name(name), style(style), isDefined(false), hasStyle(false), hasCRS(false) {} bool operator==(const Layer &other) const diff --git a/src/map/wmts.cpp b/src/map/wmts.cpp index fd61c4db..afe75893 100644 --- a/src/map/wmts.cpp +++ b/src/map/wmts.cpp @@ -124,7 +124,7 @@ void WMTS::tileMatrixSetLink(QXmlStreamReader &reader, CTX &ctx) } if (id == ctx.setup.set()) { - ctx.set = true; + ctx.hasSet = true; _limits = limits; } } @@ -188,14 +188,14 @@ void WMTS::layer(QXmlStreamReader &reader, CTX &ctx) } if (id == ctx.setup.layer()) { - ctx.layer = true; + ctx.hasLayer = true; _bounds = bounds; if (ctx.setup.rest()) _tileUrl = tpl; - if (styles.contains(ctx.setup.style())) - ctx.style = true; + if (styles.contains(ctx.setup.style()) || ctx.setup.style().isEmpty()) + ctx.hasStyle = true; if (formats.contains(ctx.setup.format())) - ctx.format = true; + ctx.hasFormat = true; } } @@ -245,19 +245,19 @@ bool WMTS::parseCapabilities(const QString &path, const Setup &setup) return false; } - if (!ctx.layer) { + if (!ctx.hasLayer) { _errorString = ctx.setup.layer() + ": layer not provided"; return false; } - if (!ctx.style) { + if (!ctx.hasStyle) { _errorString = ctx.setup.style() + ": style not provided"; return false; } - if (!ctx.setup.rest() && !ctx.format) { + if (!ctx.setup.rest() && !ctx.hasFormat) { _errorString = ctx.setup.format() + ": format not provided"; return false; } - if (!ctx.set) { + if (!ctx.hasSet) { _errorString = ctx.setup.set() + ": set not provided"; return false; } @@ -314,17 +314,18 @@ WMTS::WMTS(const QString &file, const WMTS::Setup &setup) : _valid(false) if (!parseCapabilities(file, setup)) return; + QString style = setup.style().isEmpty() ? "default" : setup.style(); if (!setup.rest()) { _tileUrl = QString("%1?service=WMTS&Version=1.0.0&request=GetTile" "&Format=%2&Layer=%3&Style=%4&TileMatrixSet=%5&TileMatrix=$z" - "&TileRow=$y&TileCol=$x").arg(setup.url()).arg(setup.format()) - .arg(setup.layer()).arg(setup.style()).arg(setup.set()); + "&TileRow=$y&TileCol=$x").arg(setup.url(), setup.format(), + setup.layer(), style, setup.set()); for (int i = 0; i < setup.dimensions().size(); i++) { const QPair &dim = setup.dimensions().at(i); - _tileUrl.append(QString("&%1=%2").arg(dim.first).arg(dim.second)); + _tileUrl.append(QString("&%1=%2").arg(dim.first, dim.second)); } } else { - _tileUrl.replace("{Style}", setup.style(), Qt::CaseInsensitive); + _tileUrl.replace("{Style}", style, Qt::CaseInsensitive); _tileUrl.replace("{TileMatrixSet}", setup.set(), Qt::CaseInsensitive); _tileUrl.replace("{TileMatrix}", "$z", Qt::CaseInsensitive); _tileUrl.replace("{TileRow}", "$y", Qt::CaseInsensitive); @@ -350,8 +351,8 @@ QList WMTS::zooms() const zooms.append(Zoom(mi->id, mi->scaleDenominator, mi->topLeft, mi->tile, mi->matrix, QRect())); else - zooms.append(Zoom(Zoom(mi->id, mi->scaleDenominator, mi->topLeft, - mi->tile, mi->matrix, li->rect))); + zooms.append(Zoom(mi->id, mi->scaleDenominator, mi->topLeft, + mi->tile, mi->matrix, li->rect)); } qSort(zooms); diff --git a/src/map/wmts.h b/src/map/wmts.h index b0cc986c..624e173b 100644 --- a/src/map/wmts.h +++ b/src/map/wmts.h @@ -120,13 +120,13 @@ private: struct CTX { const Setup &setup; QString crs; - bool layer; - bool style; - bool format; - bool set; + bool hasLayer; + bool hasStyle; + bool hasFormat; + bool hasSet; - CTX(const Setup &setup) : setup(setup), layer(false), style(false), - format(false), set(false) {} + CTX(const Setup &setup) : setup(setup), hasLayer(false), hasStyle(false), + hasFormat(false), hasSet(false) {} }; RectC wgs84BoundingBox(QXmlStreamReader &reader);