Skip to content

Commit

Permalink
[Server][Feature][needs-docs] Update WMTS service to use cache manage…
Browse files Browse the repository at this point in the history
…r for tiles
  • Loading branch information
rldhont committed Aug 20, 2018
1 parent 059232a commit 05e9ab9
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/server/services/wmts/qgswmtsgettile.cpp
Expand Up @@ -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
Expand Down

0 comments on commit 05e9ab9

Please sign in to comment.