Skip to content

Commit

Permalink
Add support for displaying WMTS legend graphics in layer tree
Browse files Browse the repository at this point in the history
  • Loading branch information
manisandro authored and nyalldawson committed Jan 17, 2020
1 parent 8410ea7 commit 7b916aa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/providers/wms/qgswmscapabilities.cpp
Expand Up @@ -1569,6 +1569,20 @@ void QgsWmsCapabilities::parseWMTSContents( const QDomElement &element )

style.legendURLs << legendURL;
}
QDomElement thirdChildElement = secondChildElement.firstChildElement( QStringLiteral( "LegendURL" ) );
if ( !thirdChildElement.isNull() )
{
QgsWmtsLegendURL legendURL;

legendURL.format = thirdChildElement.attribute( QStringLiteral( "format" ) );
legendURL.minScale = thirdChildElement.attribute( QStringLiteral( "minScaleDenominator" ) ).toDouble();
legendURL.maxScale = thirdChildElement.attribute( QStringLiteral( "maxScaleDenominator" ) ).toDouble();
legendURL.href = thirdChildElement.attribute( QStringLiteral( "xlink:href" ) );
legendURL.width = thirdChildElement.attribute( QStringLiteral( "width" ) ).toInt();
legendURL.height = thirdChildElement.attribute( QStringLiteral( "height" ) ).toInt();

style.legendURLs << legendURL;
}

style.isDefault = secondChildElement.attribute( QStringLiteral( "isDefault" ) ) == QLatin1String( "true" );

Expand Down
31 changes: 31 additions & 0 deletions src/providers/wms/qgswmsprovider.cpp
Expand Up @@ -303,6 +303,31 @@ QString QgsWmsProvider::getLegendGraphicUrl() const
url = mCaps.mCapabilities.capability.request.getLegendGraphic.dcpType.front().http.get.onlineResource.xlinkHref;
}

if ( url.isEmpty() )
{
for ( const QgsWmtsTileLayer &l : mCaps.mTileLayersSupported )
{
if ( l.identifier != mSettings.mActiveSubLayers[0] )
continue;

QHash<QString, QgsWmtsStyle>::const_iterator it = l.styles.constFind( mSettings.mActiveSubStyles[0] );
if ( it == l.styles.constEnd() )
if ( it == l.styles.end() )
continue;

for ( const QgsWmtsLegendURL &u : it.value().legendURLs )
{
if ( u.format == mSettings.mImageMimeType )
url = u.href;
}
if ( url.isEmpty() && !it.value().legendURLs.isEmpty() )
url = it.value().legendURLs.front().href;

if ( !url.isEmpty() )
break;
}
}

return url.isEmpty() ? url : prepareUri( url );
}

Expand Down Expand Up @@ -3382,6 +3407,12 @@ QUrl QgsWmsProvider::getLegendGraphicFullURL( double scale, const QgsRectangle &

QUrl url( lurl );

if ( dataSourceUri().contains( "SERVICE=WMTS" ) || dataSourceUri().contains( "/WMTSCapabilities.xml" ) )
{
QgsDebugMsg( QString( "getlegendgraphicrequest: %1" ).arg( url.toString() ) );
return url;
}

// query names are NOT case-sensitive, so make an uppercase list for proper comparison
QStringList qnames = QStringList();
for ( int i = 0; i < url.queryItems().size(); i++ )
Expand Down

0 comments on commit 7b916aa

Please sign in to comment.