Skip to content
Permalink
Browse files

[Server] dxf writer refactoring

  • Loading branch information
rldhont committed Jun 9, 2017
1 parent 00b2d24 commit 8df3391fc498a5f085a093dc85505b4fc2803687
@@ -20,6 +20,7 @@ email : david dot marteau at 3liz dot com
#include "qgsmaplayer.h"
#include "qgsvectorlayer.h"
#include "qgsdxfexport.h"
#include "qgswmsrenderer.h"

namespace QgsWms
{
@@ -44,118 +45,20 @@ namespace QgsWms
return options;
}

void readDxfLayerSettings( const QgsServerRequest::Parameters &parameters, QgsWmsConfigParser *configParser,
QList< QPair<QgsVectorLayer *, int > > &layers,
const QMap<QString, QString> &options )
{
QSet<QString> wfsLayers = QSet<QString>::fromList( configParser->wfsLayerNames() );

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

//LAYERS and STYLES
QStringList layerList, styleList;
readLayersAndStyles( parameters, layerList, styleList );

for ( int i = 0; i < layerList.size(); ++i )
{
QString layerName = layerList.at( i );
QString styleName;
if ( styleList.size() > i )
{
styleName = styleList.at( i );
}

QList<QgsMapLayer *> layerList = configParser->mapLayerFromStyle( layerName, styleName );
for ( auto layerIt = layerList.constBegin(); layerIt != layerList.constEnd(); ++layerIt )
{
if ( !( *layerIt ) )
{
continue;
}

//vector layer?
if ( ( *layerIt )->type() != QgsMapLayer::VectorLayer )
{
continue;
}

QgsVectorLayer *vlayer = static_cast<QgsVectorLayer *>( *layerIt );

int layerAttribute = -1;
if ( layerAttributes.size() > i )
{
layerAttribute = vlayer->pendingFields().indexFromName( layerAttributes.at( i ) );
}

//only wfs layers are allowed to be published
if ( !wfsLayers.contains( vlayer->name() ) )
{
continue;
}

layers.append( qMakePair( vlayer, layerAttribute ) );
}
}
}

}

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

QgsWmsConfigParser *configParser = getConfigParser( serverIface );

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

QgsRectangle extent = parseBbox( params.value( QStringLiteral( "BBOX" ) ) );
dxf.setExtent( extent );
QgsRenderer renderer( serverIface, project, params, getConfigParser( serverIface ) );

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

QList< QPair<QgsVectorLayer *, int > > layers;
readDxfLayerSettings( params, configParser, layers, formatOptionsMap );
dxf.addLayers( layers );

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

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

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

//SCALE
QMap<QString, QString>::const_iterator scaleIt = formatOptionsMap.find( QStringLiteral( "SCALE" ) );
if ( scaleIt != formatOptionsMap.constEnd() )
{
dxf.setSymbologyScale( scaleIt->toDouble() );
}
QgsDxfExport dxf = renderer.getDxf( formatOptionsMap );

QString codec = QStringLiteral( "ISO-8859-1" );
QMap<QString, QString>::const_iterator codecIt = formatOptionsMap.find( QStringLiteral( "CODEC" ) );
@@ -20,7 +20,8 @@ namespace QgsWms

/** Output GetMap response in Dfx format
*/
void writeAsDxf( QgsServerInterface *serverIface, const QString &version, const QgsServerRequest &request,
void writeAsDxf( QgsServerInterface *serverIface, const QgsProject *project,
const QString &version, const QgsServerRequest &request,
QgsServerResponse &response );

} // samespace QgsWms
@@ -96,7 +96,7 @@ namespace QgsWms
QString format = params.value( QStringLiteral( "FORMAT" ) );
if QSTR_COMPARE( format, "application/dxf" )
{
writeAsDxf( mServerIface, versionString, request, response );
writeAsDxf( mServerIface, project, versionString, request, response );
}
else
{
@@ -66,6 +66,7 @@
#include "qgsvectorlayerlabeling.h"
#include "qgspallabeling.h"
#include "qgslayerrestorer.h"
#include "qgsdxfexport.h"

#include <QImage>
#include <QPainter>
@@ -900,6 +901,122 @@ namespace QgsWms
return image.release();
}

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

// set extent
QgsRectangle extent = mWmsParameters.bboxAsRectangle();
dxf.setExtent( extent );

// get layers parameters
QList<QgsMapLayer *> layers;
QList<QgsWmsParametersLayer> params = mWmsParameters.layersParameters();

// init layer restorer before doing anything
std::unique_ptr<QgsLayerRestorer> restorer;
restorer.reset( new QgsLayerRestorer( mNicknameLayers.values() ) );

// init stylized layers according to LAYERS/STYLES or SLD
QString sld = mWmsParameters.sld();
if ( !sld.isEmpty() )
{
layers = sldStylizedLayers( sld );
}
else
{
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
QList< QPair<QgsVectorLayer *, int > > dxfLayers;
int layerIdx = -1;
Q_FOREACH ( QgsMapLayer *layer, layers )
{
layerIdx++;
if ( layer->type() != QgsMapLayer::VectorLayer )
continue;
if ( !wfsLayerIds.contains( layer->id() ) )
continue;
Q_FOREACH ( QgsWmsParametersLayer param, params )
{
if ( param.mNickname == layerNickname( *layer ) )
{
checkLayerReadPermissions( layer );

setLayerOpacity( layer, param.mOpacity );

setLayerFilter( layer, param.mFilter );

setLayerAccessControlFilter( layer );

break;
}
}
// cast for dxf layers
QgsVectorLayer *vlayer = static_cast<QgsVectorLayer *>( layer );

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

dxfLayers.append( qMakePair( 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.setSymbologyScale( scaleIt->toDouble() );
}

return dxf;
}

static void infoPointToMapCoordinates( int i, int j, QgsPointXY *infoPoint, const QgsMapSettings &mapSettings )
{
//check if i, j are in the pixel range of the image
@@ -46,6 +46,7 @@ class QgsVectorLayer;
class QgsSymbol;
class QgsSymbol;
class QgsAccessControl;
class QgsDxfExport;

class QColor;
class QFile;
@@ -101,6 +102,11 @@ namespace QgsWms
QImage *getMap( QgsMapSettings &mapSettings, HitTest *hitTest = nullptr );
QImage *getMapOld( QgsMapSettings &mapSettings, HitTest *hitTest = nullptr );

/** 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 );

/** Returns printed page as binary
\param formatString out: format of the print output (e.g. pdf, svg, png, ...)

0 comments on commit 8df3391

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