Skip to content
Permalink
Browse files

[Server][Feature][needs-docs] Update WMTS service to use cache manage…

…r for tiles
  • Loading branch information
rldhont committed Aug 1, 2018
1 parent 059232a commit 05e9ab9e559179e04ad5e4ff15c2747ed52f5120
Showing with 42 additions and 1 deletion.
  1. +42 −1 src/server/services/wmts/qgswmtsgettile.cpp
@@ -27,16 +27,57 @@ namespace QgsWmts
QgsServerResponse &response )
{
Q_UNUSED( version );

QgsServerRequest::Parameters params = request.parameters();

// WMS query
QUrlQuery query = translateWmtsParamToWmsQueryItem( QStringLiteral( "GetMap" ), params, project, serverIface );

// Get cached image
QStringList cacheKeyList;
bool cache = true;

QgsAccessControl *accessControl = serverIface->accessControls();
if ( accessControl )
cache = accessControl->fillCacheKey( cacheKeyList );

QString cacheKey = cacheKeyList.join( QStringLiteral( "-" ) );
QgsServerCacheManager *cacheManager = serverIface->cacheManager();
if ( cacheManager && cache )
{
QString contentType = params.value( QStringLiteral( "FORMAT" ) );
QString saveFormat;
std::unique_ptr<QImage> image;
if ( contentType == "image/jpeg" )
{
saveFormat = "JPEG";
image = qgis::make_unique<QImage>( 256, 256, QImage::Format_RGB32 );
}
else
{
saveFormat = "PNG";
image = qgis::make_unique<QImage>( 256, 256, QImage::Format_ARGB32_Premultiplied );
}

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


QgsServerParameters wmsParams( query );
QgsServerRequest wmsRequest( "?" + query.query( QUrl::FullyDecoded ) );
QgsService *service = serverIface->serviceRegistry()->getService( wmsParams.service(), wmsParams.version() );
service->executeRequest( wmsRequest, response, project );
if ( cache && cacheManager )
{
QByteArray content = response.data();
if ( !content.isEmpty() )
cacheManager->setCachedImage( &content, project, request, cacheKey );
}
}

} // namespace QgsWmts

0 comments on commit 05e9ab9

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