Skip to content
Permalink
Browse files

Merge pull request #9560 from pblottiere/server_dxf_params

[server] Move parsing of DXF parameters in QgsWmsParameters...
  • Loading branch information
pblottiere committed Mar 25, 2019
2 parents 9327834 + 5704f70 commit 250c62693ac72ba1d047f856a79da4a10c5b262a
@@ -32,7 +32,7 @@
* \brief Interfaces exposed by QGIS Server and made available to plugins.
* \since QGIS 2.8
*/
class QgsServerInterfaceImpl : public QgsServerInterface
class SERVER_EXPORT QgsServerInterfaceImpl : public QgsServerInterface
{
public:

@@ -21,55 +21,18 @@ email : david dot marteau at 3liz dot com

namespace QgsWms
{

namespace
{

QMap<QString, QString> parseFormatOptions( const QString &optionString )
{
QMap<QString, QString> options;

QStringList optionsList = optionString.split( ';' );
for ( auto optionsIt = optionsList.constBegin(); optionsIt != optionsList.constEnd(); ++optionsIt )
{
int equalIdx = optionsIt->indexOf( ':' );
if ( equalIdx > 0 && equalIdx < ( optionsIt->length() - 1 ) )
{
options.insert( optionsIt->left( equalIdx ).toUpper(),
optionsIt->right( optionsIt->length() - equalIdx - 1 ).toUpper() );
}
}
return options;
}

}

void writeAsDxf( QgsServerInterface *serverIface, const QgsProject *project,
const QString &version, const QgsServerRequest &request,
QgsServerResponse &response )
{
Q_UNUSED( version );

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

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

QMap<QString, QString> formatOptionsMap = parseFormatOptions( params.value( QStringLiteral( "FORMAT_OPTIONS" ) ) );

QgsDxfExport dxf = renderer.getDxf( formatOptionsMap );

QString codec = QStringLiteral( "ISO-8859-1" );
QMap<QString, QString>::const_iterator codecIt = formatOptionsMap.find( QStringLiteral( "CODEC" ) );
if ( codecIt != formatOptionsMap.constEnd() )
{
codec = formatOptionsMap.value( QStringLiteral( "CODEC" ) );
}

// Write output
QgsDxfExport dxf = renderer.getDxf();
response.setHeader( "Content-Type", "application/dxf" );
dxf.writeToFile( response.io(), codec );
dxf.writeToFile( response.io(), wmsParameters.dxfCodec() );
}


} // namespace QgsWms
@@ -504,6 +504,10 @@ namespace QgsWms
const QgsWmsParameter pAtlasPk( QgsWmsParameter::ATLAS_PK,
QVariant::StringList );
save( pAtlasPk );

const QgsWmsParameter pFormatOpts( QgsWmsParameter::FORMAT_OPTIONS,
QVariant::String );
save( pFormatOpts );
}

QgsWmsParameters::QgsWmsParameters( const QgsServerParameters &parameters )
@@ -1817,4 +1821,102 @@ namespace QgsWms
{
return name.startsWith( EXTERNAL_LAYER_PREFIX );
}

QStringList QgsWmsParameters::dxfLayerAttributes() const
{
QStringList attributes;
const QMap<DxfFormatOption, QString> options = dxfFormatOptions();

if ( options.contains( DxfFormatOption::LAYERATTRIBUTES ) )
{
attributes = options[ DxfFormatOption::LAYERATTRIBUTES ].split( ',' );
}

return attributes;
}

bool QgsWmsParameters::dxfUseLayerTitleAsName() const
{
bool use = false;
const QMap<DxfFormatOption, QString> options = dxfFormatOptions();

if ( options.contains( DxfFormatOption::USE_TITLE_AS_LAYERNAME ) )
{
use = QVariant( options[ DxfFormatOption::USE_TITLE_AS_LAYERNAME ] ).toBool();
}

return use;
}

double QgsWmsParameters::dxfScale() const
{
const QMap<DxfFormatOption, QString> options = dxfFormatOptions();

double scale = -1;
if ( options.contains( DxfFormatOption::SCALE ) )
{
scale = options[ DxfFormatOption::SCALE ].toDouble();
}

return scale;
}

QgsDxfExport::SymbologyExport QgsWmsParameters::dxfMode() const
{
const QMap<DxfFormatOption, QString> options = dxfFormatOptions();

QgsDxfExport::SymbologyExport symbol = QgsDxfExport::NoSymbology;

if ( ! options.contains( DxfFormatOption::MODE ) )
{
return symbol;
}

const QString mode = options[ DxfFormatOption::MODE ];
if ( mode.compare( QLatin1String( "SymbolLayerSymbology" ), Qt::CaseInsensitive ) == 0 )
{
symbol = QgsDxfExport::SymbolLayerSymbology;
}
else if ( mode.compare( QLatin1String( "FeatureSymbology" ), Qt::CaseInsensitive ) == 0 )
{
symbol = QgsDxfExport::FeatureSymbology;
}

return symbol;
}

QString QgsWmsParameters::dxfCodec() const
{
QString codec = QStringLiteral( "ISO-8859-1" );

if ( dxfFormatOptions().contains( DxfFormatOption::CODEC ) )
{
codec = dxfFormatOptions()[ DxfFormatOption::CODEC ];
}

return codec;
}

QMap<QgsWmsParameters::DxfFormatOption, QString> QgsWmsParameters::dxfFormatOptions() const
{
QMap<QgsWmsParameters::DxfFormatOption, QString> options;

const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWmsParameters::DxfFormatOption>() );
const QStringList opts = mWmsParameters[ QgsWmsParameter::FORMAT_OPTIONS ].toStringList( ';' );

for ( auto it = opts.constBegin(); it != opts.constEnd(); ++it )
{
const int equalIdx = it->indexOf( ':' );
if ( equalIdx > 0 && equalIdx < ( it->length() - 1 ) )
{
const QString name = it->left( equalIdx ).toUpper();
const QgsWmsParameters::DxfFormatOption option =
( QgsWmsParameters::DxfFormatOption ) metaEnum.keyToValue( name.toStdString().c_str() );
const QString value = it->right( it->length() - equalIdx - 1 );
options.insert( option, value );
}
}

return options;
}
}
@@ -28,6 +28,7 @@
#include "qgsprojectversion.h"
#include "qgsogcutils.h"
#include "qgsserverparameters.h"
#include "qgsdxfexport.h"

namespace QgsWms
{
@@ -174,7 +175,8 @@ namespace QgsWms
WITH_GEOMETRY,
WITH_MAPTIP,
WMTVER,
ATLAS_PK
ATLAS_PK,
FORMAT_OPTIONS
};
Q_ENUM( Name )

@@ -323,6 +325,17 @@ namespace QgsWms
};
Q_ENUM( Format )

//! Options for DXF format
enum DxfFormatOption
{
SCALE,
MODE,
LAYERATTRIBUTES,
USE_TITLE_AS_LAYERNAME,
CODEC
};
Q_ENUM( DxfFormatOption )

/**
* Constructor for WMS parameters with specific values.
* \param parameters Map of parameters where keys are parameters' names.
@@ -1177,6 +1190,42 @@ namespace QgsWms
*/
QStringList atlasPk() const;

/**
* Returns a map of DXF options defined within FORMAT_OPTIONS parameter.
* \since QGIS 3.8
*/
QMap<DxfFormatOption, QString> dxfFormatOptions() const;

/**
* Returns the DXF LAYERATTRIBUTES parameter.
* \since QGIS 3.8
*/
QStringList dxfLayerAttributes() const;

/**
* Returns the DXF USE_TITLE_AS_LAYERNAME parameter.
* \since QGIS 3.8
*/
bool dxfUseLayerTitleAsName() const;

/**
* Returns the DXF SCALE parameter.
* \since QGIS 3.8
*/
double dxfScale() const;

/**
* Returns the DXF MODE parameter.
* \since QGIS 3.8
*/
QgsDxfExport::SymbologyExport dxfMode() const;

/**
* Returns the DXF CODEC parameter.
* \since QGIS 3.8
*/
QString dxfCodec() const;

private:
static bool isExternalLayer( const QString &name );

@@ -892,7 +892,7 @@ namespace QgsWms
return image.release();
}

QgsDxfExport QgsRenderer::getDxf( const QMap<QString, QString> &options )
QgsDxfExport QgsRenderer::getDxf()
{
QgsDxfExport dxf;

@@ -919,18 +919,11 @@ namespace QgsWms
layers = stylizedLayers( params );
}

// layer attributes options
QStringList layerAttributes;
QMap<QString, QString>::const_iterator layerAttributesIt = options.find( QStringLiteral( "LAYERATTRIBUTES" ) );
if ( layerAttributesIt != options.constEnd() )
{
layerAttributes = options.value( QStringLiteral( "LAYERATTRIBUTES" ) ).split( ',' );
}

// only wfs layers are allowed to be published
QStringList wfsLayerIds = QgsServerProjectUtils::wfsLayerIds( *mProject );

// get dxf layers
const QStringList attributes = mWmsParameters.dxfLayerAttributes();
QList< QgsDxfExport::DxfLayer > dxfLayers;
int layerIdx = -1;
for ( QgsMapLayer *layer : layers )
@@ -962,49 +955,21 @@ namespace QgsWms

// get the layer attribute used in dxf
int layerAttribute = -1;
if ( layerAttributes.size() > layerIdx )
if ( attributes.size() > layerIdx )
{
layerAttribute = vlayer->fields().indexFromName( layerAttributes.at( layerIdx ) );
layerAttribute = vlayer->fields().indexFromName( attributes[ layerIdx ] );
}

dxfLayers.append( QgsDxfExport::DxfLayer( vlayer, layerAttribute ) );
}

// add layers to dxf
dxf.addLayers( dxfLayers );

dxf.setLayerTitleAsName( options.contains( QStringLiteral( "USE_TITLE_AS_LAYERNAME" ) ) );

//MODE
QMap<QString, QString>::const_iterator modeIt = options.find( QStringLiteral( "MODE" ) );

QgsDxfExport::SymbologyExport se;
if ( modeIt == options.constEnd() )
{
se = QgsDxfExport::NoSymbology;
}
else
{
if ( modeIt->compare( QStringLiteral( "SymbolLayerSymbology" ), Qt::CaseInsensitive ) == 0 )
{
se = QgsDxfExport::SymbolLayerSymbology;
}
else if ( modeIt->compare( QStringLiteral( "FeatureSymbology" ), Qt::CaseInsensitive ) == 0 )
{
se = QgsDxfExport::FeatureSymbology;
}
else
{
se = QgsDxfExport::NoSymbology;
}
}
dxf.setSymbologyExport( se );

//SCALE
QMap<QString, QString>::const_iterator scaleIt = options.find( QStringLiteral( "SCALE" ) );
if ( scaleIt != options.constEnd() )
dxf.setLayerTitleAsName( mWmsParameters.dxfUseLayerTitleAsName() );
dxf.setSymbologyExport( mWmsParameters.dxfMode() );
if ( mWmsParameters.dxfFormatOptions().contains( QgsWmsParameters::DxfFormatOption::SCALE ) )
{
dxf.setSymbologyScale( scaleIt->toDouble() );
dxf.setSymbologyScale( mWmsParameters.dxfScale() );
}

return dxf;
@@ -42,6 +42,7 @@ class QgsAccessControl;
class QgsDxfExport;
class QgsLayerTreeModel;
class QgsLayerTree;
class QgsServerInterface;

class QImage;
class QPaintDevice;
@@ -91,11 +92,10 @@ namespace QgsWms

/**
* Returns the map as DXF data
* \param options extracted from the FORMAT_OPTIONS parameter
* \returns the map as DXF data
* \since QGIS 3.0
*/
QgsDxfExport getDxf( const QMap<QString, QString> &options );
QgsDxfExport getDxf();

/**
* Returns printed page as binary
@@ -50,4 +50,7 @@ IF (ENABLE_TESTS)
IF (WITH_QUICK)
ADD_SUBDIRECTORY(quickgui)
ENDIF (WITH_QUICK)
IF (WITH_SERVER)
ADD_SUBDIRECTORY(server)
ENDIF (WITH_SERVER)
ENDIF (ENABLE_TESTS)
@@ -0,0 +1 @@
ADD_SUBDIRECTORY(wms)

0 comments on commit 250c626

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