Skip to content
Permalink
Browse files

Manage external layers like highlight layers for consistency

  • Loading branch information
pblottiere committed Mar 5, 2019
1 parent 1ab7925 commit d6c7c93a1c8e0e0bbb278802c8a6f7990ad573ce
@@ -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;

@@ -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();
@@ -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 );
@@ -55,6 +55,12 @@ namespace QgsWms
QString mStyle;
};

struct QgsWmsParametersExternalLayer
{
QString mName;
QString mUri;
};

struct QgsWmsParametersHighlightLayer
{
QString mName;
@@ -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
};

@@ -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
@@ -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 );
@@ -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() );

@@ -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;
@@ -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() )
{
@@ -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 );
@@ -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();

@@ -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;

0 comments on commit d6c7c93

Please sign in to comment.
You can’t perform that action at this time.