Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Server][Cache] Add WMS GetLegendGraphic to store in cache
  • Loading branch information
rldhont committed Oct 6, 2018
1 parent f79be97 commit 8c67e45
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/server/services/wms/qgswmsgetlegendgraphics.cpp
Expand Up @@ -34,16 +34,60 @@ namespace QgsWms
Q_UNUSED( version );

QgsServerRequest::Parameters params = request.parameters();
QString format = params.value( QStringLiteral( "FORMAT" ), QStringLiteral( "PNG" ) );

QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) );

// Get cached image
QgsAccessControl *accessControl = serverIface->accessControls();
QgsServerCacheManager *cacheManager = serverIface->cacheManager();
if ( cacheManager )
{
ImageOutputFormat outputFormat = parseImageFormat( format );
QString saveFormat;
QString contentType;
switch ( outputFormat )
{
case PNG:
case PNG8:
case PNG16:
case PNG1:
contentType = "image/png";
saveFormat = "PNG";
break;
case JPEG:
contentType = "image/jpeg";
saveFormat = "JPEG";
break;
default:
throw QgsServiceException( "InvalidFormat",
QString( "Output format '%1' is not supported in the GetLegendGraphic request" ).arg( format ) );
break;
}

QImage image;
QByteArray content = cacheManager->getCachedImage( project, request, accessControl );
if ( !content.isEmpty() && image.loadFromData( content ) )
{
response.setHeader( QStringLiteral( "Content-Type" ), contentType );
image.save( response.io(), qPrintable( saveFormat ) );
return;
}
}

QgsRenderer renderer( serverIface, project, wmsParameters );

std::unique_ptr<QImage> result( renderer.getLegendGraphics() );

if ( result )
{
QString format = params.value( QStringLiteral( "FORMAT" ), QStringLiteral( "PNG" ) );
writeImage( response, *result, format, renderer.getImageQuality() );
if ( cacheManager )
{
QByteArray content = response.data();
if ( !content.isEmpty() )
cacheManager->setCachedImage( &content, project, request, accessControl );
}
}
else
{
Expand Down

0 comments on commit 8c67e45

Please sign in to comment.