Skip to content

Commit

Permalink
support wmts layers to browser (implements #9492)
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Feb 22, 2014
1 parent c471858 commit 0dc0483
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 35 deletions.
148 changes: 118 additions & 30 deletions src/providers/wms/qgswmsdataitems.cpp
Expand Up @@ -61,33 +61,78 @@ QVector<QgsDataItem*> QgsWMSConnectionItem::createChildren()
QgsDebugMsg( "encodedUri = " + encodedUri );

QgsWmsProvider *wmsProvider = new QgsWmsProvider( encodedUri );
if ( !wmsProvider ) return children;
if ( !wmsProvider )
return children;

// Attention: supportedLayers() gives tree leafes, not top level
if ( !wmsProvider->supportedLayers( mLayerProperties ) )
QVector<QgsWmsLayerProperty> layerProperties;
if ( wmsProvider->supportedLayers( layerProperties ) )
{
//children.append( new QgsErrorItem( this, tr( "Failed to retrieve layers" ), mPath + "/error" ) );
// TODO: show the error without adding child
return children;
}
const QgsWmsCapabilitiesProperty &capabilitiesProperty = wmsProvider->capabilitiesProperty();
const QgsWmsCapabilityProperty &capabilityProperty = capabilitiesProperty.capability;

// Top level layer is present max once
// <element name="Capability">
// <element ref="wms:Layer" minOccurs="0"/> - default maxOccurs=1
const QgsWmsLayerProperty &topLayerProperty = capabilityProperty.layer;
foreach ( const QgsWmsLayerProperty &layerProperty, topLayerProperty.layer )
{
// Attention, the name may be empty
QgsDebugMsg( QString::number( layerProperty.orderId ) + " " + layerProperty.name + " " + layerProperty.title );
QString pathName = layerProperty.name.isEmpty() ? QString::number( layerProperty.orderId ) : layerProperty.name;

QgsWmsCapabilitiesProperty mCapabilitiesProperty = wmsProvider->capabilitiesProperty();
QgsWmsCapabilityProperty capabilityProperty = mCapabilitiesProperty.capability;
QgsWMSLayerItem *layer = new QgsWMSLayerItem( this, layerProperty.title, mPath + "/" + pathName, capabilitiesProperty, uri, layerProperty );

// Top level layer is present max once
// <element name="Capability">
// <element ref="wms:Layer" minOccurs="0"/> - default maxOccurs=1
QgsWmsLayerProperty topLayerProperty = capabilityProperty.layer;
foreach ( QgsWmsLayerProperty layerProperty, topLayerProperty.layer )
{
// Attention, the name may be empty
QgsDebugMsg( QString::number( layerProperty.orderId ) + " " + layerProperty.name + " " + layerProperty.title );
QString pathName = layerProperty.name.isEmpty() ? QString::number( layerProperty.orderId ) : layerProperty.name;
children << layer;
}
}

QgsWMSLayerItem * layer = new QgsWMSLayerItem( this, layerProperty.title, mPath + "/" + pathName, mCapabilitiesProperty, uri, layerProperty );
QList<QgsWmtsTileLayer> tileLayers;
if ( wmsProvider->supportedTileLayers( tileLayers ) )
{
QHash<QString, QgsWmtsTileMatrixSet> tileMatrixSets;
wmsProvider->supportedTileMatrixSets( tileMatrixSets );

children.append( layer );
foreach ( const QgsWmtsTileLayer &l, tileLayers )
{
QString title = l.title.isEmpty() ? l.identifier : l.title;
QgsDataItem *layerItem = l.styles.size() == 1 ? this : new QgsDataCollectionItem( this, title, mPath + "/" + l.identifier );
if ( layerItem != this )
addChildItem( layerItem );

foreach ( const QgsWmtsStyle &style, l.styles )
{
QgsDataItem *styleItem = l.setLinks.size() == 1 ? layerItem : new QgsDataCollectionItem( layerItem, style.title.isEmpty() ? style.identifier : style.title, layerItem->path() + "/" + style.identifier );
if ( styleItem != layerItem )
layerItem->addChildItem( styleItem );

foreach ( const QgsWmtsTileMatrixSetLink &setLink, l.setLinks )
{
QgsDataItem *linkItem = l.formats.size() == 1 ? styleItem : new QgsDataCollectionItem( styleItem, setLink.tileMatrixSet, styleItem->path() + "/" + setLink.tileMatrixSet );
if ( linkItem != styleItem )
styleItem->addChildItem( linkItem );

foreach ( QString format, l.formats )
{
QString name;
if ( layerItem == this )
name += ( l.title.isEmpty() ? l.identifier : l.title ) + " - ";
if ( styleItem == layerItem )
name += ( style.title.isEmpty() ? style.identifier : style.title ) + " - ";
if ( linkItem == styleItem )
name += setLink.tileMatrixSet + " - ";
name += format;

QgsDataItem *layerItem = new QgsWMTSLayerItem( linkItem, name, linkItem->path() + "/" + name, uri,
l.identifier, format, style.identifier, setLink.tileMatrixSet, tileMatrixSets[ setLink.tileMatrixSet ].crs, title );

linkItem->addChildItem( layerItem );
}
}
}
}
}

return children;
}

Expand Down Expand Up @@ -142,18 +187,18 @@ void QgsWMSConnectionItem::deleteConnection()

// ---------------------------------------------------------------------------

QgsWMSLayerItem::QgsWMSLayerItem( QgsDataItem* parent, QString name, QString path, QgsWmsCapabilitiesProperty capabilitiesProperty, QgsDataSourceURI dataSourceUri, QgsWmsLayerProperty layerProperty )
: QgsLayerItem( parent, name, path, QString(), QgsLayerItem::Raster, "wms" ),
mCapabilitiesProperty( capabilitiesProperty ),
mDataSourceUri( dataSourceUri ),
mLayerProperty( layerProperty )
//mProviderKey ("wms"),
//mLayerType ( QgsLayerItem::Raster )
QgsWMSLayerItem::QgsWMSLayerItem( QgsDataItem* parent, QString name, QString path, const QgsWmsCapabilitiesProperty &capabilitiesProperty, QgsDataSourceURI dataSourceUri, const QgsWmsLayerProperty &layerProperty )
: QgsLayerItem( parent, name, path, QString(), QgsLayerItem::Raster, "wms" )
, mCapabilitiesProperty( capabilitiesProperty )
, mDataSourceUri( dataSourceUri )
, mLayerProperty( layerProperty )
{
QgsDebugMsg( "uri = " + mDataSourceUri.encodedUri() );

mUri = createUri();

// Populate everything, it costs nothing, all info about layers is collected
foreach ( QgsWmsLayerProperty layerProperty, mLayerProperty.layer )
foreach ( const QgsWmsLayerProperty &layerProperty, mLayerProperty.layer )
{
// Attention, the name may be empty
QgsDebugMsg( QString::number( layerProperty.orderId ) + " " + layerProperty.name + " " + layerProperty.title );
Expand All @@ -162,11 +207,12 @@ QgsWMSLayerItem::QgsWMSLayerItem( QgsDataItem* parent, QString name, QString pat
mChildren.append( layer );
}

if ( mChildren.size() == 0 )
if ( mChildren.isEmpty() )
{
//mIcon = iconRaster();
mIcon = QgsApplication::getThemeIcon( "mIconWms.svg" );
}

mPopulated = true;
}

Expand All @@ -186,8 +232,8 @@ QString QgsWMSLayerItem::createUri()

QString format;
// get first supported by qt and server
QVector<QgsWmsSupportedFormat> formats = QgsWmsProvider::supportedFormats();
foreach ( QgsWmsSupportedFormat f, formats )
QVector<QgsWmsSupportedFormat> formats( QgsWmsProvider::supportedFormats() );
foreach ( const QgsWmsSupportedFormat &f, formats )
{
if ( mCapabilitiesProperty.capability.request.getMap.format.indexOf( f.format ) >= 0 )
{
Expand Down Expand Up @@ -219,6 +265,48 @@ QString QgsWMSLayerItem::createUri()
return mDataSourceUri.encodedUri();
}

// ---------------------------------------------------------------------------

QgsWMTSLayerItem::QgsWMTSLayerItem( QgsDataItem *parent,
const QString &name,
const QString &path,
const QgsDataSourceURI &uri,
const QString &id,
const QString &format,
const QString &style,
const QString &tileMatrixSet,
const QString &crs,
const QString &title )
: QgsLayerItem( parent, name, path, QString(), QgsLayerItem::Raster, "wms" )
, mDataSourceUri( uri )
, mId( id )
, mFormat( format )
, mStyle( style )
, mTileMatrixSet( tileMatrixSet )
, mCrs( crs )
, mTitle( title )
{
mUri = createUri();
mPopulated = true;
}

QgsWMTSLayerItem::~QgsWMTSLayerItem()
{
}

QString QgsWMTSLayerItem::createUri()
{
// TODO dimensions

QgsDataSourceURI uri( mDataSourceUri );
uri.setParam( "layers", mId );
uri.setParam( "styles", mStyle );
uri.setParam( "format", mFormat );
uri.setParam( "crs", mCrs );
uri.setParam( "tileMatrixSet", mTileMatrixSet );
return uri.encodedUri();
}

// ---------------------------------------------------------------------------
QgsWMSRootItem::QgsWMSRootItem( QgsDataItem* parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path )
Expand Down
32 changes: 27 additions & 5 deletions src/providers/wms/qgswmsdataitems.h
Expand Up @@ -31,10 +31,6 @@ class QgsWMSConnectionItem : public QgsDataCollectionItem

virtual QList<QAction*> actions();

QgsWmsCapabilitiesProperty mCapabilitiesProperty;
QString mConnInfo;
QVector<QgsWmsLayerProperty> mLayerProperties;

public slots:
void editConnection();
void deleteConnection();
Expand All @@ -47,7 +43,9 @@ class QgsWMSLayerItem : public QgsLayerItem
Q_OBJECT
public:
QgsWMSLayerItem( QgsDataItem* parent, QString name, QString path,
QgsWmsCapabilitiesProperty capabilitiesProperty, QgsDataSourceURI dataSourceUri, QgsWmsLayerProperty layerProperties );
const QgsWmsCapabilitiesProperty &capabilitiesProperty,
QgsDataSourceURI dataSourceUri,
const QgsWmsLayerProperty &layerProperty );
~QgsWMSLayerItem();

QString createUri();
Expand All @@ -57,6 +55,30 @@ class QgsWMSLayerItem : public QgsLayerItem
QgsWmsLayerProperty mLayerProperty;
};

class QgsWMTSLayerItem : public QgsLayerItem
{
Q_OBJECT
public:
QgsWMTSLayerItem( QgsDataItem* parent,
const QString &name,
const QString &path,
const QgsDataSourceURI &dataSourceUri,
const QString &id,
const QString &format,
const QString &style,
const QString &tileMatrixSet,
const QString &crs,
const QString &title );
~QgsWMTSLayerItem();

QString createUri();
QString layerName() const { return mTitle; }

private:
QgsDataSourceURI mDataSourceUri;
QString mId, mFormat, mStyle, mTileMatrixSet, mCrs, mTitle;
};

class QgsWMSRootItem : public QgsDataCollectionItem
{
Q_OBJECT
Expand Down

1 comment on commit 0dc0483

@palmerj
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jef-n great feature thanks. I've noticed a little issue where a few of out WMTS layers get a parent node using the Style property as the label ('Automatic Style' in this case) rather than the layer name. See screenshot below:

wmts_browser

Some of the layers do however follow the correct logic:

wmts_browser_2

But maybe this because the layer has multiple CRS tilesets and multiple styles.

Please sign in to comment.