Skip to content

Commit

Permalink
Update getPrint with render context
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Mar 27, 2019
1 parent 8a46e2f commit 24f3f97
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 69 deletions.
19 changes: 15 additions & 4 deletions src/server/services/wms/qgswmsgetprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ namespace QgsWms
const QString &, const QgsServerRequest &request,
QgsServerResponse &response )
{
// get wms parameters from query
const QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) );
QgsRenderer renderer( serverIface, project, wmsParameters );

// GetPrint supports svg/png/pdf
const QgsWmsParameters::Format format = wmsParameters.format();
QString contentType;

// GetPrint supports svg/png/pdf
switch ( format )
{
case QgsWmsParameters::PNG:
Expand All @@ -55,8 +54,20 @@ namespace QgsWms
break;
}

// prepare render context
QgsWmsRenderContext context( project, serverIface );
context.setFlag( QgsWmsRenderContext::UpdateExtent );
context.setFlag( QgsWmsRenderContext::UseOpacity );
context.setFlag( QgsWmsRenderContext::UseFilter );
context.setFlag( QgsWmsRenderContext::UseSelection );
context.setFlag( QgsWmsRenderContext::SetAccessControl );
context.setFlag( QgsWmsRenderContext::AddHighlightLayers );
context.setFlag( QgsWmsRenderContext::AddExternalLayers );
context.setParameters( wmsParameters );

// rendering
QgsRenderer renderer( context );
response.setHeader( QStringLiteral( "Content-Type" ), contentType );
response.write( renderer.getPrint() );
}

} // namespace QgsWms
85 changes: 20 additions & 65 deletions src/server/services/wms/qgswmsrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,77 +286,19 @@ namespace QgsWms

QByteArray QgsRenderer::getPrint()
{
//GetPrint request needs a template parameter
QString templateName = mWmsParameters.composerTemplate();
if ( templateName.isEmpty() )
{
throw QgsBadRequestException( QStringLiteral( "ParameterMissing" ),
QStringLiteral( "The TEMPLATE parameter is required for the GetPrint request" ) );
}

// get layers parameters
QList<QgsMapLayer *> layers;
QList<QgsWmsParametersLayer> params = mWmsParameters.layersParameters();

// create the output image (this is not really used but configureMapSettings
// needs it)
std::unique_ptr<QImage> image( new QImage() );

// configure map settings (background, DPI, ...)
QgsMapSettings mapSettings;
configureMapSettings( image.get(), mapSettings );

// init layer restorer before doing anything
std::unique_ptr<QgsLayerRestorer> restorer;
restorer.reset( new QgsLayerRestorer( mNicknameLayers.values() ) );

// init stylized layers according to LAYERS/STYLES or SLD
QString sld = mWmsParameters.sldBody();
if ( !sld.isEmpty() )
{
layers = sldStylizedLayers( sld );
}
else
{
layers = stylizedLayers( params );
}

// remove unwanted layers (restricted layers, ...)
removeUnwantedLayers( layers );
restorer.reset( new QgsLayerRestorer( mContext.layers() ) );

// configure each layer with opacity, selection filter, ...
bool updateMapExtent = mWmsParameters.bbox().isEmpty();
for ( QgsMapLayer *layer : layers )
// GetPrint request needs a template parameter
const QString templateName = mWmsParameters.composerTemplate();
if ( templateName.isEmpty() )
{
checkLayerReadPermissions( layer );

for ( const QgsWmsParametersLayer &param : params )
{
if ( param.mNickname == layerNickname( *layer ) )
{
setLayerOpacity( layer, param.mOpacity );

setLayerFilter( layer, param.mFilter );

setLayerSelection( layer, param.mSelection );

if ( updateMapExtent )
updateExtent( layer, mapSettings );

break;
}
}

setLayerAccessControlFilter( layer );
throw QgsBadRequestException( QStringLiteral( "ParameterMissing" ),
QStringLiteral( "The TEMPLATE parameter is required for the GetPrint request" ) );
}

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

// add layers to map settings (revert order for the rendering)
std::reverse( layers.begin(), layers.end() );
mapSettings.setLayers( layers );

// check template
const QgsLayoutManager *lManager = mProject->layoutManager();
QgsPrintLayout *sourceLayout( dynamic_cast<QgsPrintLayout *>( lManager->layoutByName( templateName ) ) );
if ( !sourceLayout )
Expand Down Expand Up @@ -470,6 +412,19 @@ namespace QgsWms
}
}

// configure layers
QgsMapSettings mapSettings;
QList<QgsMapLayer *> layers = mContext.layersToRender();
configureLayers( layers, &mapSettings );

// configure map settings (background, DPI, ...)
std::unique_ptr<QImage> image( new QImage() );
configureMapSettings( image.get(), mapSettings );

// add layers to map settings (revert order for the rendering)
mapSettings.setLayers( layers );

// configure layout
configurePrintLayout( layout.get(), mapSettings, atlas );

// Get the temporary output file
Expand Down

0 comments on commit 24f3f97

Please sign in to comment.