Skip to content
Permalink
Browse files

[Server][Cache] Add WMS GetLegendGraphic to store in cache

  • Loading branch information
rldhont committed Sep 16, 2018
1 parent f79be97 commit 8c67e45f09f262c832983072c7a2bcc391ecf5f4
Showing with 45 additions and 1 deletion.
  1. +45 −1 src/server/services/wms/qgswmsgetlegendgraphics.cpp
@@ -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
{

0 comments on commit 8c67e45

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