Skip to content

Commit

Permalink
Merge pull request #5396 from mhugent/server_wms_print_layers
Browse files Browse the repository at this point in the history
Server wms print layers
  • Loading branch information
mhugent authored Oct 24, 2017
2 parents bb21395 + c993888 commit 628a60b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/server/services/wms/qgswmsparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgswmsparameters.h"
#include "qgsdatasourceuri.h"
#include "qgsmessagelog.h"
#include <iostream>

Expand Down Expand Up @@ -512,6 +513,16 @@ namespace QgsWms
raiseError( name );
}
}
else //maybe an external wms parameter?
{
int separator = key.indexOf( ":" );
if ( separator >= 1 )
{
QString id = key.left( separator );
QString param = key.right( key.length() - separator - 1 );
mExternalWMSParameters[id].insert( param, parameters[key] );
}
}
}
}
}
Expand Down Expand Up @@ -1880,6 +1891,23 @@ namespace QgsWms
return param;
}

QString QgsWmsParameters::externalWMSUri( const QString &id ) const
{
if ( !mExternalWMSParameters.contains( id ) )
{
return QString();
}

QgsDataSourceUri wmsUri;
const QMap<QString, QString> &paramMap = mExternalWMSParameters[ id ];
QMap<QString, QString>::const_iterator paramIt = paramMap.constBegin();
for ( ; paramIt != paramMap.constEnd(); ++paramIt )
{
wmsUri.setParam( paramIt.key().toLower(), paramIt.value() );
}
return wmsUri.encodedUri();
}

QString QgsWmsParameters::name( ParameterName name ) const
{
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
Expand Down
8 changes: 8 additions & 0 deletions src/server/services/wms/qgswmsparameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,13 @@ namespace QgsWms
*/
QgsWmsParametersComposerMap composerMapParameters( int mapId ) const;

/**
* @brief externalWMSUri
* @param id the id of the external wms
* @return uri string or an empty string if the external wms id does not exist
*/
QString externalWMSUri( const QString &id ) const;

private:
QString name( ParameterName name ) const;
void raiseError( ParameterName name ) const;
Expand Down Expand Up @@ -955,6 +962,7 @@ namespace QgsWms
QgsServerRequest::Parameters mRequestParameters;
QMap<ParameterName, Parameter> mParameters;
QMap<int, QMap<ParameterName, Parameter>> mComposerParameters;
QMap<QString, QMap<QString, QString> > mExternalWMSParameters;
QList<QgsProjectVersion> mVersions;
};
}
Expand Down
41 changes: 39 additions & 2 deletions src/server/services/wms/qgswmsrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ namespace QgsWms
initNicknameLayers();
}

QgsRenderer::~QgsRenderer()
{
removeTemporaryLayers();
}


QImage *QgsRenderer::getLegendGraphics()
{
Expand Down Expand Up @@ -2557,6 +2562,7 @@ namespace QgsWms
}
}

mTemporaryLayers.append( highlightLayers );
return highlightLayers;
}

Expand Down Expand Up @@ -2602,15 +2608,27 @@ namespace QgsWms
return layers;
}

QList<QgsMapLayer *> QgsRenderer::stylizedLayers( const QList<QgsWmsParametersLayer> &params ) const
QList<QgsMapLayer *> QgsRenderer::stylizedLayers( const QList<QgsWmsParametersLayer> &params )
{
QList<QgsMapLayer *> layers;

Q_FOREACH ( QgsWmsParametersLayer param, params )
{
QString nickname = param.mNickname;
QString style = param.mStyle;
if ( mNicknameLayers.contains( nickname ) && !mRestrictedLayers.contains( nickname ) )
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 ( !style.isEmpty() )
{
Expand All @@ -2633,6 +2651,25 @@ 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 0;
}

return wmsLayer;
}

void QgsRenderer::removeTemporaryLayers()
{
qDeleteAll( mTemporaryLayers );
mTemporaryLayers.clear();
}

QPainter *QgsRenderer::layersRendering( const QgsMapSettings &mapSettings, QImage &image, HitTest *hitTest ) const
{
QPainter *painter = nullptr;
Expand Down
10 changes: 9 additions & 1 deletion src/server/services/wms/qgswmsrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ namespace QgsWms
const QgsProject *project,
const QgsServerRequest::Parameters &parameters );

~QgsRenderer();

/**
* Returns the map legend as an image (or a null pointer in case of error). The caller takes ownership
of the image object*/
Expand Down Expand Up @@ -154,7 +156,7 @@ namespace QgsWms
void annotationsRendering( QPainter *painter ) const;

// Return a list of layers stylized with LAYERS/STYLES parameters
QList<QgsMapLayer *> stylizedLayers( const QList<QgsWmsParametersLayer> &params ) const;
QList<QgsMapLayer *> stylizedLayers( const QList<QgsWmsParametersLayer> &params );

// Return a list of layers stylized with SLD parameter
QList<QgsMapLayer *> sldStylizedLayers( const QString &sld ) const;
Expand Down Expand Up @@ -274,6 +276,11 @@ namespace QgsWms
//! configure the composition for the GetPrint request
bool configureComposition( QgsComposition *c, const QgsMapSettings &mapSettings );

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

void removeTemporaryLayers();

private:

const QgsServerRequest::Parameters &mParameters;
Expand All @@ -286,6 +293,7 @@ namespace QgsWms
QgsWmsParameters mWmsParameters;
QStringList mRestrictedLayers;
QMap<QString, QgsMapLayer *> mNicknameLayers;
QList<QgsMapLayer *> mTemporaryLayers;

public:

Expand Down

0 comments on commit 628a60b

Please sign in to comment.