Skip to content

Commit

Permalink
Merge pull request #9560 from pblottiere/server_dxf_params
Browse files Browse the repository at this point in the history
[server] Move parsing of DXF parameters in QgsWmsParameters...
  • Loading branch information
pblottiere committed Mar 25, 2019
2 parents 9327834 + 5704f70 commit 250c626
Show file tree
Hide file tree
Showing 10 changed files with 370 additions and 86 deletions.
2 changes: 1 addition & 1 deletion src/server/qgsserverinterfaceimpl.h
Expand Up @@ -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:

Expand Down
41 changes: 2 additions & 39 deletions src/server/services/wms/qgsdxfwriter.cpp
Expand Up @@ -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
102 changes: 102 additions & 0 deletions src/server/services/wms/qgswmsparameters.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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;
}
}
51 changes: 50 additions & 1 deletion src/server/services/wms/qgswmsparameters.h
Expand Up @@ -28,6 +28,7 @@
#include "qgsprojectversion.h"
#include "qgsogcutils.h"
#include "qgsserverparameters.h"
#include "qgsdxfexport.h"

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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 );

Expand Down
51 changes: 8 additions & 43 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -892,7 +892,7 @@ namespace QgsWms
return image.release();
}

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

Expand All @@ -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 )
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/server/services/wms/qgswmsrenderer.h
Expand Up @@ -42,6 +42,7 @@ class QgsAccessControl;
class QgsDxfExport;
class QgsLayerTreeModel;
class QgsLayerTree;
class QgsServerInterface;

class QImage;
class QPaintDevice;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/src/CMakeLists.txt
Expand Up @@ -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)
1 change: 1 addition & 0 deletions tests/src/server/CMakeLists.txt
@@ -0,0 +1 @@
ADD_SUBDIRECTORY(wms)

0 comments on commit 250c626

Please sign in to comment.