Skip to content
Permalink
Browse files

[Server] WMS GetPrint refactoring

  • Loading branch information
rldhont committed Aug 1, 2017
1 parent bb61db2 commit 7c1d3972885d9efeeb4e899d4e9b12e48dbbc1da
@@ -1271,8 +1271,8 @@ namespace QgsWms

QStringList QgsWmsParameters::allStyles() const
{
QStringList style = value( ParameterName::STYLE ).toString().split( ",", QString::SkipEmptyParts );
QStringList styles = value( ParameterName::STYLES ).toString().split( "," );
QStringList style = toStringList( ParameterName::STYLE );
QStringList styles = toStringList( ParameterName::STYLES );
return style << styles;
}

@@ -1417,6 +1417,134 @@ namespace QgsWms
return toColor( ParameterName::BGCOLOR );
}

QgsWmsParametersComposerMap QgsWmsParameters::composerMapParameters( int mapId ) const
{
QgsWmsParametersComposerMap param;
param.mId = mapId;

QString pMapId = "MAP" + QString::number( mapId );

//map extent is mandatory
if ( !mRequestParameters.contains( pMapId + ":EXTENT" ) )
return param;

QVariant extentValue( mRequestParameters[pMapId + ":EXTENT"] );
QString extentStr = extentValue.toString();

if ( extentStr.isEmpty() )
return param;

QgsRectangle extent;
QStringList corners = extentStr.split( "," );

if ( corners.size() == 4 )
{
double d[4];
bool ok;

for ( int i = 0; i < 4; i++ )
{
corners[i].replace( QLatin1String( " " ), QLatin1String( "+" ) );
d[i] = corners[i].toDouble( &ok );
if ( !ok )
{
raiseError( pMapId + "BBOX ('" + extentStr + "') cannot be converted into a rectangle" );
}
}

extent = QgsRectangle( d[0], d[1], d[2], d[3] );
}
else
{
raiseError( pMapId + "BBOX ('" + extentStr + "') cannot be converted into a rectangle" );
}

param.mHasExtent = true;
param.mExtent = extent;

// scale
if ( mRequestParameters.contains( pMapId + ":SCALE" ) )
{
param.mScale = composerMapParamToDouble( mapId, "SCALE", param.mScale );
}

// rotation
if ( mRequestParameters.contains( pMapId + ":ROTATION" ) )
{
param.mRotation = composerMapParamToDouble( mapId, "ROTATION", param.mRotation );
}

//grid space x / y
if ( mRequestParameters.contains( pMapId + ":GRID_INTERVAL_X" ) && mRequestParameters.contains( pMapId + ":GRID_INTERVAL_Y" ) )
{
param.mGridX = composerMapParamToDouble( mapId, "GRID_INTERVAL_X", param.mGridX );
param.mGridY = composerMapParamToDouble( mapId, "GRID_INTERVAL_Y", param.mGridY );
}

//layers
QList<QgsWmsParametersLayer> parameters;
QStringList layers = composerMapParamToStringList( mapId, "LAYERS", ',' );
QStringList styles = composerMapParamToStringList( mapId, "STYLES", ',' );
for ( int i = 0; i < layers.size(); i++ )
{
QString layer = layers[i];
QgsWmsParametersLayer param;
param.mNickname = layer;

if ( i < styles.count() )
param.mStyle = styles[i];

parameters.append( param );
}
param.mLayers = parameters;

return param;
}

double QgsWmsParameters::composerMapParamToDouble( int mapId, QString name, double defaultVal ) const
{
QString pMapId = "MAP" + QString::number( mapId );
QString paramName = pMapId + ":" + name;
double val = defaultVal;

if ( mRequestParameters.contains( paramName ) )
{
QVariant value( mRequestParameters[paramName] );
QString valStr = value.toString();

if ( ! valStr.isEmpty() && value.canConvert( QVariant::Double ) )
{
bool ok;
val = value.toDouble( &ok );

if ( !ok )
{
QString msg = paramName + " ('" + valStr + "') cannot be converted into a double";
raiseError( msg );
}

}
}

return val;
}

QStringList QgsWmsParameters::composerMapParamToStringList( int mapId, QString name, char delimiter ) const
{
QString pMapId = "MAP" + QString::number( mapId );
QString paramName = pMapId + ":" + name;

QString valStr;

if ( mRequestParameters.contains( paramName ) )
{
QVariant value( mRequestParameters[paramName] );
valStr = value.toString();
}

return valStr.split( delimiter, QString::SkipEmptyParts );
}

QString QgsWmsParameters::name( ParameterName name ) const
{
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
@@ -59,6 +59,18 @@ namespace QgsWms
QColor mBufferColor;
};

struct QgsWmsParametersComposerMap
{
int mId; // composer map id
bool mHasExtent; // does the request contains extent for this composer map
QgsRectangle mExtent; // the request extent for this composer map
float mScale = -1;
float mRotation;
float mGridX = 0;
float mGridY = 0;
QList<QgsWmsParametersLayer> mLayers; // list of layers fr this composer map
};

class QgsWmsParameters
{
Q_GADGET
@@ -767,6 +779,38 @@ namespace QgsWms
*/
int dpiAsInt() const;

/** Returns the requested parameters for a composer map parameter.
* An exception is raised if parameters are defined and cannot be
* converted like EXTENT, SCALE, ROTATION, GRID_INTERVAL_X and
* GRID_INTERVAL_Y.
* \param mapId the composer map id.
* \returns parameters for the composer map.
* \throws QgsBadRequestException
*/
QgsWmsParametersComposerMap composerMapParameters( int mapId ) const;

/** Returns a composer map parameter as a double or the defaultVal.
* An exception is raised if the parameter is defined and cannot
* be converted. Made for specific composer map SCALE, ROTATION,
* GRID_INTERVAL_X and GRID_INTERVAL_Y parameter.
* \param mapId the composer map id.
* \param name the parameter's name.
* \param defaultVal the default value for this parameter.
* \returns parameter as double
* \throws QgsBadRequestException
*/
double composerMapParamToDouble( int mapId, QString name, double defaultVal ) const;

/** Returns a composer map parameter as a string list or an empty
* list. Made for specific composer map LAYERS and STYLES
* parameter.
* \param mapId the composer map id.
* \param name the parameter's name.
* \param delimiter the delimiter to split the list.
* \returns parameter as a string list
*/
QStringList composerMapParamToStringList( int mapId, QString name, char delimiter ) const;

private:
QString name( ParameterName name ) const;
void raiseError( ParameterName name ) const;

0 comments on commit 7c1d397

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