Skip to content
Permalink
Browse files

Merge pull request #9242 from pblottiere/getprint_svg

Some cleaning [server]
  • Loading branch information
pblottiere committed Feb 26, 2019
2 parents 0e8757b + 0a76d7c commit 6e39543983a92ec63d1aa6c6c23151bfe53790c3
@@ -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;
@@ -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
@@ -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.
@@ -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();
@@ -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
{
@@ -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




@@ -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 );
@@ -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;
}
@@ -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.
@@ -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.
@@ -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();
@@ -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;
@@ -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() )
@@ -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;
@@ -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 )
@@ -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 );
}

@@ -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 );
}
@@ -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() )
@@ -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() )
@@ -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 );
@@ -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 );
@@ -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
@@ -290,8 +295,6 @@ namespace QgsWms

void handlePrintErrors( const QgsLayout *layout ) const;

private:

const QgsWmsParameters &mWmsParameters;

#ifdef HAVE_SERVER_PYTHON_PLUGINS
@@ -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

0 comments on commit 6e39543

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