|
| 1 | +/*************************************************************************** |
| 2 | + qgsdxfexport.cpp |
| 3 | + ------------------------------------------------------------------- |
| 4 | +Date : 20 December 2016 |
| 5 | +Copyright : (C) 2015 by |
| 6 | +email : marco.hugentobler at sourcepole dot com (original code) |
| 7 | +Copyright : (C) 2016 by |
| 8 | +email : david dot marteau at 3liz dot com |
| 9 | + *************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | +#include "qgsmodule.h" |
| 18 | +#include "qgsdxfwriter.h" |
| 19 | +#include "qgswmsutils.h" |
| 20 | +#include "qgswmsservertransitional.h" |
| 21 | +#include "qgsmaplayer.h" |
| 22 | +#include "qgsvectorlayer.h" |
| 23 | +#include "qgsdxfexport.h" |
| 24 | + |
| 25 | +namespace QgsWms |
| 26 | +{ |
| 27 | + |
| 28 | + namespace |
| 29 | + { |
| 30 | + |
| 31 | + QMap<QString, QString> parseFormatOptions( const QString& optionString ) |
| 32 | + { |
| 33 | + QMap<QString, QString> options; |
| 34 | + |
| 35 | + QStringList optionsList = optionString.split( QStringLiteral( ";" ) ); |
| 36 | + QStringList::const_iterator optionsIt = optionsList.constBegin(); |
| 37 | + for ( ; optionsIt != optionsList.constEnd(); ++optionsIt ) |
| 38 | + { |
| 39 | + int equalIdx = optionsIt->indexOf( QLatin1String( ":" ) ); |
| 40 | + if ( equalIdx > 0 && equalIdx < ( optionsIt->length() - 1 ) ) |
| 41 | + { |
| 42 | + options.insert( optionsIt->left( equalIdx ).toUpper(), |
| 43 | + optionsIt->right( optionsIt->length() - equalIdx - 1 ).toUpper() ); |
| 44 | + } |
| 45 | + } |
| 46 | + return options; |
| 47 | + } |
| 48 | + |
| 49 | + void readDxfLayerSettings( QgsWmsServer& server, QgsWmsConfigParser* configParser, |
| 50 | + QList< QPair<QgsVectorLayer *, int > >& layers, |
| 51 | + const QMap<QString, QString>& options ) |
| 52 | + { |
| 53 | + QSet<QString> wfsLayers = QSet<QString>::fromList( configParser->wfsLayerNames() ); |
| 54 | + |
| 55 | + QStringList layerAttributes; |
| 56 | + QMap<QString, QString>::const_iterator layerAttributesIt = options.find( QStringLiteral( "LAYERATTRIBUTES" ) ); |
| 57 | + if ( layerAttributesIt != options.constEnd() ) |
| 58 | + { |
| 59 | + layerAttributes = options.value( QStringLiteral( "LAYERATTRIBUTES" ) ).split( QStringLiteral( "," ) ); |
| 60 | + } |
| 61 | + |
| 62 | + //LAYERS and STYLES |
| 63 | + QStringList layerList, styleList; |
| 64 | + server.readLayersAndStyles( layerList, styleList ); |
| 65 | + |
| 66 | + for ( int i = 0; i < layerList.size(); ++i ) |
| 67 | + { |
| 68 | + QString layerName = layerList.at( i ); |
| 69 | + QString styleName; |
| 70 | + if ( styleList.size() > i ) |
| 71 | + { |
| 72 | + styleName = styleList.at( i ); |
| 73 | + } |
| 74 | + |
| 75 | + QList<QgsMapLayer*> layerList = configParser->mapLayerFromStyle( layerName, styleName ); |
| 76 | + QList<QgsMapLayer*>::const_iterator layerIt = layerList.constBegin(); |
| 77 | + for ( ; layerIt != layerList.constEnd(); ++layerIt ) |
| 78 | + { |
| 79 | + if ( !( *layerIt ) ) |
| 80 | + { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + //vector layer? |
| 85 | + if (( *layerIt )->type() != QgsMapLayer::VectorLayer ) |
| 86 | + { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + QgsVectorLayer* vlayer = static_cast<QgsVectorLayer*>( *layerIt ); |
| 91 | + |
| 92 | + int layerAttribute = -1; |
| 93 | + if ( layerAttributes.size() > i ) |
| 94 | + { |
| 95 | + layerAttribute = vlayer->pendingFields().indexFromName( layerAttributes.at( i ) ); |
| 96 | + } |
| 97 | + |
| 98 | + //only wfs layers are allowed to be published |
| 99 | + if ( !wfsLayers.contains( vlayer->name() ) ) |
| 100 | + { |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + layers.append( qMakePair( vlayer, layerAttribute ) ); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + void writeAsDxf( QgsServerInterface* serverIface, const QString& version, const QgsServerRequest& request, QgsServerResponse& response ) |
| 112 | + { |
| 113 | + Q_UNUSED( version ); |
| 114 | + |
| 115 | + QgsWmsConfigParser* configParser = getConfigParser( serverIface ); |
| 116 | + |
| 117 | + QgsDxfExport dxf; |
| 118 | + QgsServerRequest::Parameters params = request.parameters(); |
| 119 | + |
| 120 | + QgsRectangle extent = parseBbox( params.value( QStringLiteral( "BBOX" ) ) ); |
| 121 | + dxf.setExtent( extent ); |
| 122 | + |
| 123 | + QMap<QString, QString> formatOptionsMap = parseFormatOptions( params.value( QStringLiteral( "FORMAT_OPTIONS" ) ) ); |
| 124 | + |
| 125 | + QgsWmsServer server( serverIface->configFilePath(), params, |
| 126 | + configParser, |
| 127 | + serverIface->accessControls() ); |
| 128 | + |
| 129 | + QList< QPair<QgsVectorLayer *, int > > layers; |
| 130 | + readDxfLayerSettings( server, configParser, layers, formatOptionsMap ); |
| 131 | + dxf.addLayers( layers ); |
| 132 | + |
| 133 | + dxf.setLayerTitleAsName( formatOptionsMap.contains( QStringLiteral( "USE_TITLE_AS_LAYERNAME" ) ) ); |
| 134 | + |
| 135 | + //MODE |
| 136 | + QMap<QString, QString>::const_iterator modeIt = formatOptionsMap.find( QStringLiteral( "MODE" ) ); |
| 137 | + |
| 138 | + QgsDxfExport::SymbologyExport se; |
| 139 | + if ( modeIt == formatOptionsMap.constEnd() ) |
| 140 | + { |
| 141 | + se = QgsDxfExport::NoSymbology; |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + if ( modeIt->compare( QLatin1String( "SymbolLayerSymbology" ), Qt::CaseInsensitive ) == 0 ) |
| 146 | + { |
| 147 | + se = QgsDxfExport::SymbolLayerSymbology; |
| 148 | + } |
| 149 | + else if ( modeIt->compare( QLatin1String( "FeatureSymbology" ), Qt::CaseInsensitive ) == 0 ) |
| 150 | + { |
| 151 | + se = QgsDxfExport::FeatureSymbology; |
| 152 | + } |
| 153 | + else |
| 154 | + { |
| 155 | + se = QgsDxfExport::NoSymbology; |
| 156 | + } |
| 157 | + } |
| 158 | + dxf.setSymbologyExport( se ); |
| 159 | + |
| 160 | + //SCALE |
| 161 | + QMap<QString, QString>::const_iterator scaleIt = formatOptionsMap.find( QStringLiteral( "SCALE" ) ); |
| 162 | + if ( scaleIt != formatOptionsMap.constEnd() ) |
| 163 | + { |
| 164 | + dxf.setSymbologyScaleDenominator( scaleIt->toDouble() ); |
| 165 | + } |
| 166 | + |
| 167 | + QString codec = QStringLiteral( "ISO-8859-1" ); |
| 168 | + QMap<QString, QString>::const_iterator codecIt = formatOptionsMap.find( QStringLiteral( "CODEC" ) ); |
| 169 | + if ( codecIt != formatOptionsMap.constEnd() ) |
| 170 | + { |
| 171 | + codec = formatOptionsMap.value( QStringLiteral( "CODEC" ) ); |
| 172 | + } |
| 173 | + |
| 174 | + // Write output |
| 175 | + response.setHeader( "Content-Type", "application/dxf" ); |
| 176 | + dxf.writeToFile( response.io(), codec ); |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | +} // samespace QgsWms |
0 commit comments