Skip to content

Commit

Permalink
Merge pull request #9242 from pblottiere/getprint_svg
Browse files Browse the repository at this point in the history
Some cleaning [server]
  • Loading branch information
pblottiere committed Feb 26, 2019
2 parents 0e8757b + 0a76d7c commit 6e39543
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 128 deletions.
5 changes: 3 additions & 2 deletions python/server/auto_generated/qgsserverparameters.sip.in
Expand Up @@ -43,9 +43,10 @@ Returns the type of the parameter as a string.
Returns true if the parameter is valid, false otherwise.
%End

QString toString() const;
QString toString( bool defaultValue = false ) const;
%Docstring
Converts the parameter into a string.
Converts the parameter into a string. If ``defaultValue`` is true
and current value is empty, then the default value is returned.
%End

QStringList toStringList( char delimiter = ',' ) const;
Expand Down
9 changes: 7 additions & 2 deletions src/server/qgsserverparameters.cpp
Expand Up @@ -62,9 +62,14 @@ QColor QgsServerParameterDefinition::toColor( bool &ok ) const
return color;
}

QString QgsServerParameterDefinition::toString() const
QString QgsServerParameterDefinition::toString( const bool defaultValue ) const
{
return mValue.toString();
QString value = mValue.toString();

if ( value.isEmpty() && defaultValue )
value = mDefaultValue.toString();

return value;
}

QStringList QgsServerParameterDefinition::toStringList( const char delimiter ) const
Expand Down
5 changes: 3 additions & 2 deletions src/server/qgsserverparameters.h
Expand Up @@ -59,9 +59,10 @@ class SERVER_EXPORT QgsServerParameterDefinition
virtual bool isValid() const;

/**
* Converts the parameter into a string.
* Converts the parameter into a string. If \a defaultValue is true
* and current value is empty, then the default value is returned.
*/
QString toString() const;
QString toString( bool defaultValue = false ) const;

/**
* Converts the parameter into a list of strings.
Expand Down
2 changes: 1 addition & 1 deletion src/server/services/wms/qgswmsgetlegendgraphics.cpp
Expand Up @@ -85,7 +85,7 @@ namespace QgsWms

if ( result )
{
writeImage( response, *result, format, renderer.getImageQuality() );
writeImage( response, *result, format, renderer.imageQuality() );
if ( cacheManager )
{
QByteArray content = response.data();
Expand Down
2 changes: 1 addition & 1 deletion src/server/services/wms/qgswmsgetmap.cpp
Expand Up @@ -42,7 +42,7 @@ namespace QgsWms
if ( result )
{
QString format = params.value( QStringLiteral( "FORMAT" ), QStringLiteral( "PNG" ) );
writeImage( response, *result, format, renderer.getImageQuality() );
writeImage( response, *result, format, renderer.imageQuality() );
}
else
{
Expand Down
31 changes: 8 additions & 23 deletions src/server/services/wms/qgswmsgetprint.cpp
Expand Up @@ -25,51 +25,36 @@
namespace QgsWms
{
void writeGetPrint( QgsServerInterface *serverIface, const QgsProject *project,
const QString &version, const QgsServerRequest &request,
const QString &, const QgsServerRequest &request,
QgsServerResponse &response )
{
QgsServerRequest::Parameters params = request.parameters();

Q_UNUSED( version );

QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) );
const QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) );
QgsRenderer renderer( serverIface, project, wmsParameters );

QString format = params.value( "FORMAT" );
const QgsWmsParameters::Format format = wmsParameters.format();
QString contentType;

// GetPrint supports svg/png/pdf
if ( format.compare( QLatin1String( "image/png" ), Qt::CaseInsensitive ) == 0 ||
format.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 )
if ( format == QgsWmsParameters::PNG )
{
format = "png";
contentType = "image/png";
}
else if ( format.compare( QLatin1String( "image/svg" ), Qt::CaseInsensitive ) == 0 ||
format.compare( QLatin1String( "image/svg+xml" ), Qt::CaseInsensitive ) == 0 ||
format.compare( QLatin1String( "svg" ), Qt::CaseInsensitive ) == 0 )
else if ( format == QgsWmsParameters::SVG )
{
format = "svg";
contentType = "image/svg+xml";
}
else if ( format.compare( QLatin1String( "application/pdf" ), Qt::CaseInsensitive ) == 0 ||
format.compare( QLatin1String( "pdf" ), Qt::CaseInsensitive ) == 0 )
else if ( format == QgsWmsParameters::PDF )
{
format = "pdf";
contentType = "application/pdf";
}
else
{
throw QgsServiceException( QStringLiteral( "InvalidFormat" ),
QString( "Output format %1 is not supported by the GetPrint request" ).arg( format ) );
QString( "Output format %1 is not supported by the GetPrint request" ).arg( wmsParameters.formatAsString() ) );
}

response.setHeader( QStringLiteral( "Content-Type" ), contentType );
response.write( renderer.getPrint( format ) );
response.write( renderer.getPrint() );
}

} // namespace QgsWms




43 changes: 33 additions & 10 deletions src/server/services/wms/qgswmsparameters.cpp
Expand Up @@ -310,7 +310,9 @@ namespace QgsWms
const QgsWmsParameter pSRS( QgsWmsParameter::SRS );
save( pSRS );

const QgsWmsParameter pFormat( QgsWmsParameter::FORMAT );
const QgsWmsParameter pFormat( QgsWmsParameter::FORMAT,
QVariant::String,
QVariant( "png" ) );
save( pFormat );

const QgsWmsParameter pInfoFormat( QgsWmsParameter::INFO_FORMAT );
Expand Down Expand Up @@ -713,21 +715,42 @@ namespace QgsWms

QString QgsWmsParameters::formatAsString() const
{
return mWmsParameters[ QgsWmsParameter::FORMAT ].toString();
return mWmsParameters[ QgsWmsParameter::FORMAT ].toString( true );
}

QgsWmsParameters::Format QgsWmsParameters::format() const
QString QgsWmsParameters::formatAsString( const QgsWmsParameters::Format format )
{
QString fStr = formatAsString();
const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWmsParameters::Format>() );
return metaEnum.valueToKey( format );
}

if ( fStr.isEmpty() )
return Format::NONE;
QgsWmsParameters::Format QgsWmsParameters::format() const
{
const QString fStr = formatAsString();

Format f = Format::PNG;
if ( fStr.compare( QLatin1String( "jpg" ), Qt::CaseInsensitive ) == 0
|| fStr.compare( QLatin1String( "jpeg" ), Qt::CaseInsensitive ) == 0
|| fStr.compare( QLatin1String( "image/jpeg" ), Qt::CaseInsensitive ) == 0 )
Format f = Format::NONE;
if ( fStr.compare( QLatin1String( "image/png" ), Qt::CaseInsensitive ) == 0 ||
fStr.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 )
{
f = Format::PNG;
}
else if ( fStr.compare( QLatin1String( "jpg" ), Qt::CaseInsensitive ) == 0
|| fStr.compare( QLatin1String( "jpeg" ), Qt::CaseInsensitive ) == 0
|| fStr.compare( QLatin1String( "image/jpeg" ), Qt::CaseInsensitive ) == 0 )
{
f = Format::JPG;
}
else if ( fStr.compare( QLatin1String( "image/svg" ), Qt::CaseInsensitive ) == 0 ||
fStr.compare( QLatin1String( "image/svg+xml" ), Qt::CaseInsensitive ) == 0 ||
fStr.compare( QLatin1String( "svg" ), Qt::CaseInsensitive ) == 0 )
{
f = Format::SVG;
}
else if ( fStr.compare( QLatin1String( "application/pdf" ), Qt::CaseInsensitive ) == 0 ||
fStr.compare( QLatin1String( "pdf" ), Qt::CaseInsensitive ) == 0 )
{
f = Format::PDF;
}

return f;
}
Expand Down
9 changes: 9 additions & 0 deletions src/server/services/wms/qgswmsparameters.h
Expand Up @@ -306,12 +306,15 @@ namespace QgsWms
NONE,
JPG,
PNG,
SVG,
PDF,
TEXT,
XML,
HTML,
GML,
JSON
};
Q_ENUM( Format )

/**
* Constructor for WMS parameters with specific values.
Expand Down Expand Up @@ -504,6 +507,12 @@ namespace QgsWms
*/
QString formatAsString() const;

/**
* Returns format parameter as a string.
* \since QGIS 3.8
*/
static QString formatAsString( Format format );

/**
* Returns format. If the FORMAT parameter is not used, then the
* default value is PNG.
Expand Down
38 changes: 21 additions & 17 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -285,7 +285,7 @@ namespace QgsWms
}


QByteArray QgsRenderer::getPrint( const QString &formatString )
QByteArray QgsRenderer::getPrint()
{
//GetPrint request needs a template parameter
QString templateName = mWmsParameters.composerTemplate();
Expand Down Expand Up @@ -474,15 +474,18 @@ namespace QgsWms
configurePrintLayout( layout.get(), mapSettings, atlas );

// Get the temporary output file
QTemporaryFile tempOutputFile( QDir::tempPath() + '/' + QStringLiteral( "XXXXXX.%1" ).arg( formatString.toLower() ) );
const QgsWmsParameters::Format format = mWmsParameters.format();
const QString extension = QgsWmsParameters::formatAsString( format ).toLower();

QTemporaryFile tempOutputFile( QDir::tempPath() + '/' + QStringLiteral( "XXXXXX.%1" ).arg( extension ) );
if ( !tempOutputFile.open() )
{
throw QgsServerException( QStringLiteral( "Could not open temporary file for the GetPrint request." ) );

}

QString exportError;
if ( formatString.compare( QLatin1String( "svg" ), Qt::CaseInsensitive ) == 0 )
if ( format == QgsWmsParameters::SVG )
{
// Settings for the layout exporter
QgsLayoutExporter::SvgExportSettings exportSettings;
Expand Down Expand Up @@ -511,10 +514,11 @@ namespace QgsWms
exporter.exportToSvg( tempOutputFile.fileName(), exportSettings );
}
}
else if ( formatString.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 || formatString.compare( QLatin1String( "jpg" ), Qt::CaseInsensitive ) == 0 )
else if ( format == QgsWmsParameters::PNG )
{
// Settings for the layout exporter
QgsLayoutExporter::ImageExportSettings exportSettings;

// Get the dpi from input or use the default
double dpi( layout->renderContext().dpi( ) );
if ( !mWmsParameters.dpi().isEmpty() )
Expand Down Expand Up @@ -550,7 +554,7 @@ namespace QgsWms
exporter.exportToImage( tempOutputFile.fileName(), exportSettings );
}
}
else if ( formatString.compare( QLatin1String( "pdf" ), Qt::CaseInsensitive ) == 0 )
else if ( format == QgsWmsParameters::PDF )
{
// Settings for the layout exporter
QgsLayoutExporter::PdfExportSettings exportSettings;
Expand Down Expand Up @@ -581,7 +585,7 @@ namespace QgsWms
else //unknown format
{
throw QgsBadRequestException( QStringLiteral( "InvalidFormat" ),
QStringLiteral( "Output format '%1' is not supported in the GetPrint request" ).arg( formatString ) );
QStringLiteral( "Output format '%1' is not supported in the GetPrint request" ).arg( mWmsParameters.formatAsString() ) );
}

if ( atlas )
Expand Down Expand Up @@ -1759,10 +1763,10 @@ namespace QgsWms
{
QDomElement bBoxElem = infoDocument.createElement( QStringLiteral( "BoundingBox" ) );
bBoxElem.setAttribute( version == QLatin1String( "1.1.1" ) ? "SRS" : "CRS", outputCrs.authid() );
bBoxElem.setAttribute( QStringLiteral( "minx" ), qgsDoubleToString( box.xMinimum(), getWMSPrecision() ) );
bBoxElem.setAttribute( QStringLiteral( "maxx" ), qgsDoubleToString( box.xMaximum(), getWMSPrecision() ) );
bBoxElem.setAttribute( QStringLiteral( "miny" ), qgsDoubleToString( box.yMinimum(), getWMSPrecision() ) );
bBoxElem.setAttribute( QStringLiteral( "maxy" ), qgsDoubleToString( box.yMaximum(), getWMSPrecision() ) );
bBoxElem.setAttribute( QStringLiteral( "minx" ), qgsDoubleToString( box.xMinimum(), wmsPrecision() ) );
bBoxElem.setAttribute( QStringLiteral( "maxx" ), qgsDoubleToString( box.xMaximum(), wmsPrecision() ) );
bBoxElem.setAttribute( QStringLiteral( "miny" ), qgsDoubleToString( box.yMinimum(), wmsPrecision() ) );
bBoxElem.setAttribute( QStringLiteral( "maxy" ), qgsDoubleToString( box.yMaximum(), wmsPrecision() ) );
featureElement.appendChild( bBoxElem );
}

Expand Down Expand Up @@ -1793,7 +1797,7 @@ namespace QgsWms
}
QDomElement geometryElement = infoDocument.createElement( QStringLiteral( "Attribute" ) );
geometryElement.setAttribute( QStringLiteral( "name" ), QStringLiteral( "geometry" ) );
geometryElement.setAttribute( QStringLiteral( "value" ), geom.asWkt( getWMSPrecision() ) );
geometryElement.setAttribute( QStringLiteral( "value" ), geom.asWkt( wmsPrecision() ) );
geometryElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "derived" ) );
featureElement.appendChild( geometryElement );
}
Expand Down Expand Up @@ -2465,11 +2469,11 @@ namespace QgsWms
QDomElement boxElem;
if ( version < 3 )
{
boxElem = QgsOgcUtils::rectangleToGMLBox( &box, doc, getWMSPrecision() );
boxElem = QgsOgcUtils::rectangleToGMLBox( &box, doc, wmsPrecision() );
}
else
{
boxElem = QgsOgcUtils::rectangleToGMLEnvelope( &box, doc, getWMSPrecision() );
boxElem = QgsOgcUtils::rectangleToGMLEnvelope( &box, doc, wmsPrecision() );
}

if ( crs.isValid() )
Expand All @@ -2493,11 +2497,11 @@ namespace QgsWms
QDomElement gmlElem;
if ( version < 3 )
{
gmlElem = QgsOgcUtils::geometryToGML( geom, doc, getWMSPrecision() );
gmlElem = QgsOgcUtils::geometryToGML( geom, doc, wmsPrecision() );
}
else
{
gmlElem = QgsOgcUtils::geometryToGML( geom, doc, QStringLiteral( "GML3" ), getWMSPrecision() );
gmlElem = QgsOgcUtils::geometryToGML( geom, doc, QStringLiteral( "GML3" ), wmsPrecision() );
}

if ( !gmlElem.isNull() )
Expand Down Expand Up @@ -2570,7 +2574,7 @@ namespace QgsWms
return value;
}

int QgsRenderer::getImageQuality() const
int QgsRenderer::imageQuality() const
{
// First taken from QGIS project
int imageQuality = QgsServerProjectUtils::wmsImageQuality( *mProject );
Expand All @@ -2584,7 +2588,7 @@ namespace QgsWms
return imageQuality;
}

int QgsRenderer::getWMSPrecision() const
int QgsRenderer::wmsPrecision() const
{
// First taken from QGIS project and the default value is 6
int WMSPrecision = QgsServerProjectUtils::wmsFeatureInfoPrecision( *mProject );
Expand Down
20 changes: 7 additions & 13 deletions src/server/services/wms/qgswmsrenderer.h
Expand Up @@ -98,16 +98,21 @@ namespace QgsWms

/**
* Returns printed page as binary
\param formatString out: format of the print output (e.g. pdf, svg, png, ...)
\returns printed page as binary or 0 in case of error*/
QByteArray getPrint( const QString &formatString );
QByteArray getPrint();

/**
* Creates an xml document that describes the result of the getFeatureInfo request.
* May throw an exception
*/
QByteArray getFeatureInfo( const QString &version = "1.3.0" );

//! Returns the image quality to use for getMap request
int imageQuality() const;

//! Returns the precision to use for GetFeatureInfo request
int wmsPrecision() const;

private:

// Init the restricted layers with nicknames
Expand Down Expand Up @@ -290,8 +295,6 @@ namespace QgsWms

void handlePrintErrors( const QgsLayout *layout ) const;

private:

const QgsWmsParameters &mWmsParameters;

#ifdef HAVE_SERVER_PYTHON_PLUGINS
Expand All @@ -306,15 +309,6 @@ namespace QgsWms
QMap<QString, QgsMapLayer *> mNicknameLayers;
QMap<QString, QList<QgsMapLayer *> > mLayerGroups;
QList<QgsMapLayer *> mTemporaryLayers;

public:

//! Returns the image quality to use for getMap request
int getImageQuality() const;

//! Returns the precision to use for GetFeatureInfo request
int getWMSPrecision() const;

};

} // namespace QgsWms
Expand Down

0 comments on commit 6e39543

Please sign in to comment.