Skip to content

Commit

Permalink
Manage external layers like highlight layers for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Mar 18, 2019
1 parent 1ab7925 commit d6c7c93
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 32 deletions.
51 changes: 49 additions & 2 deletions src/server/services/wms/qgswmsparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,10 @@ namespace QgsWms
for ( int i = 0; i < layers.size(); i++ )
{
QString layer = layers[i];

if ( layer.startsWith( QStringLiteral( "EXTERNAL_WMS:" ) ) )
continue;

QgsWmsParametersLayer param;
param.mNickname = layer;

Expand Down Expand Up @@ -1492,6 +1496,27 @@ namespace QgsWms
return params;
}

QList<QgsWmsParametersExternalLayer> QgsWmsParameters::externalLayersParameters() const
{
QList<QgsWmsParametersExternalLayer> externalLayers;

const QStringList layers = allLayersNickname();
for ( const QString &layer : allLayersNickname() )
{
if ( ! layer.startsWith( QStringLiteral( "EXTERNAL_WMS:" ) ) )
continue;

QgsWmsParametersExternalLayer externalLayer;
externalLayer.mName = layer;
externalLayer.mName.remove( 0, 13 );
externalLayer.mUri = externalWMSUri( externalLayer.mName );

externalLayers << externalLayer;
}

return externalLayers;
}

QString QgsWmsParameters::backgroundColor() const
{
return mWmsParameters[ QgsWmsParameter::BGCOLOR ].toString();
Expand Down Expand Up @@ -1577,12 +1602,34 @@ namespace QgsWms
}

//layers
QStringList layers;
QStringList allLayers;
wmsParam = idParameter( QgsWmsParameter::LAYERS, mapId );
if ( wmsParam.isValid() )
{
layers = wmsParam.toStringList();
allLayers = wmsParam.toStringList();
}

// external layers
QStringList layers;
QList<QgsWmsParametersExternalLayer> eParams;

for ( const auto &layer : allLayers )
{
if ( layer.startsWith( QStringLiteral( "EXTERNAL_WMS:" ) ) )
{
QgsWmsParametersExternalLayer externalParam;
externalParam.mName = layer;
externalParam.mName.remove( 0, 13 );
externalParam.mUri = externalWMSUri( externalParam.mName );

eParams << externalParam;
}
else
{
layers << layer;
}
}
param.mExternalLayers = eParams;

QStringList styles;
wmsParam = idParameter( QgsWmsParameter::STYLES, mapId );
Expand Down
9 changes: 9 additions & 0 deletions src/server/services/wms/qgswmsparameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ namespace QgsWms
QString mStyle;
};

struct QgsWmsParametersExternalLayer
{
QString mName;
QString mUri;
};

struct QgsWmsParametersHighlightLayer
{
QString mName;
Expand All @@ -79,6 +85,7 @@ namespace QgsWms
float mGridX = 0;
float mGridY = 0;
QList<QgsWmsParametersLayer> mLayers; // list of layers for this composer map
QList<QgsWmsParametersExternalLayer> mExternalLayers; // list of external layers for this composer map
QList<QgsWmsParametersHighlightLayer> mHighlightLayers; // list of highlight layers for this composer map
};

Expand Down Expand Up @@ -944,6 +951,8 @@ namespace QgsWms
*/
QList<QgsWmsParametersHighlightLayer> highlightLayersParameters() const;

QList<QgsWmsParametersExternalLayer> externalLayersParameters() const;

/**
* Returns HIGHLIGHT_GEOM as a list of string in WKT.
* \returns highlight geometries
Expand Down
53 changes: 26 additions & 27 deletions src/server/services/wms/qgswmsrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,13 +653,14 @@ namespace QgsWms

if ( !map->keepLayerSet() )
{
if ( cMapParams.mLayers.isEmpty() )
if ( cMapParams.mLayers.isEmpty() && cMapParams.mExternalLayers.isEmpty() )
{
map->setLayers( mapSettings.layers() );
}
else
{
QList<QgsMapLayer *> layerSet = stylizedLayers( cMapParams.mLayers );
layerSet << externalLayers( cMapParams.mExternalLayers );
layerSet << highlightLayers( cMapParams.mHighlightLayers );
std::reverse( layerSet.begin(), layerSet.end() );
map->setLayers( layerSet );
Expand Down Expand Up @@ -856,6 +857,9 @@ namespace QgsWms
setLayerAccessControlFilter( layer );
}

// add external layers
layers = layers << externalLayers( mWmsParameters.externalLayersParameters() );

// add highlight layers above others
layers = layers << highlightLayers( mWmsParameters.highlightLayersParameters() );

Expand Down Expand Up @@ -2914,6 +2918,26 @@ namespace QgsWms
return highlightLayers;
}

QList<QgsMapLayer *> QgsRenderer::externalLayers( const QList<QgsWmsParametersExternalLayer> &params )
{
QList<QgsMapLayer *> layers;

for ( const QgsWmsParametersExternalLayer &param : params )
{
std::unique_ptr<QgsMapLayer> layer;
layer.reset( new QgsRasterLayer( param.mUri, param.mName, QStringLiteral( "wms" ) ) );

if ( layer->isValid() )
{
// to delete later
mTemporaryLayers.append( layer.release() );
layers << mTemporaryLayers.last();
}
}

return layers;
}

QList<QgsMapLayer *> QgsRenderer::sldStylizedLayers( const QString &sld ) const
{
QList<QgsMapLayer *> layers;
Expand Down Expand Up @@ -2976,19 +3000,7 @@ namespace QgsWms
{
QString nickname = param.mNickname;
QString style = param.mStyle;
if ( nickname.startsWith( "EXTERNAL_WMS:" ) )
{
QString externalLayerId = nickname;
externalLayerId.remove( 0, 13 );
QgsMapLayer *externalWMSLayer = createExternalWMSLayer( externalLayerId );
if ( externalWMSLayer )
{
layers.append( externalWMSLayer );
mNicknameLayers[nickname] = externalWMSLayer; //might be used later in GetPrint request
mTemporaryLayers.append( externalWMSLayer );
}
}
else if ( mNicknameLayers.contains( nickname ) && !mRestrictedLayers.contains( nickname ) )
if ( mNicknameLayers.contains( nickname ) && !mRestrictedLayers.contains( nickname ) )
{
if ( !style.isEmpty() )
{
Expand Down Expand Up @@ -3032,19 +3044,6 @@ namespace QgsWms
return layers;
}

QgsMapLayer *QgsRenderer::createExternalWMSLayer( const QString &externalLayerId ) const
{
QString wmsUri = mWmsParameters.externalWMSUri( externalLayerId.toUpper() );
QgsMapLayer *wmsLayer = new QgsRasterLayer( wmsUri, externalLayerId, QStringLiteral( "wms" ) );
if ( !wmsLayer->isValid() )
{
delete wmsLayer;
return nullptr;
}

return wmsLayer;
}

void QgsRenderer::removeTemporaryLayers()
{
qDeleteAll( mTemporaryLayers );
Expand Down
6 changes: 3 additions & 3 deletions src/server/services/wms/qgswmsrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ namespace QgsWms
// Build and returns highlight layers
QList<QgsMapLayer *> highlightLayers( QList<QgsWmsParametersHighlightLayer> params );

// Build and returns external layers
QList<QgsMapLayer *> externalLayers( const QList<QgsWmsParametersExternalLayer> &params );

// Init a map with nickname for layers' project
void initNicknameLayers();

Expand Down Expand Up @@ -289,9 +292,6 @@ namespace QgsWms
* */
bool configurePrintLayout( QgsPrintLayout *c, const QgsMapSettings &mapSettings, bool atlasPrint = false );

//! Creates external WMS layer. Caller takes ownership
QgsMapLayer *createExternalWMSLayer( const QString &externalLayerId ) const;

void removeTemporaryLayers();

void handlePrintErrors( const QgsLayout *layout ) const;
Expand Down

3 comments on commit d6c7c93

@mhugent
Copy link
Contributor

@mhugent mhugent commented on d6c7c93 Aug 5, 2020

Choose a reason for hiding this comment

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

This commit adds the external WMS layers on top of the normal layers (similar to the highlight layers). Right?
But the external WMS layers can be at any position between the normal layers. In most cases, they are even at the bottom of the map (as a background). Is it possible to revert this commit? As is, it is impossible for me to use the external WMS layers any more.

@mhugent
Copy link
Contributor

Choose a reason for hiding this comment

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

@pblottiere : did you see my comment?

@pblottiere
Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @mhugent,

I'll take a look in the week. Thanks for raising this!

Please sign in to comment.