|
18 | 18 | #include "qgswmsserver.h"
|
19 | 19 | #include "qgscapabilitiescache.h"
|
20 | 20 | #include "qgscrscache.h"
|
| 21 | +#include "qgsdxfexport.h" |
21 | 22 | #include "qgsfield.h"
|
22 | 23 | #include "qgsgeometry.h"
|
23 | 24 | #include "qgslayertree.h"
|
|
46 | 47 | #include "qgsogcutils.h"
|
47 | 48 | #include "qgsfeature.h"
|
48 | 49 | #include "qgseditorwidgetregistry.h"
|
| 50 | +#include "qgsserverstreamingdevice.h" |
49 | 51 |
|
50 | 52 | #include <QImage>
|
51 | 53 | #include <QPainter>
|
@@ -158,6 +160,25 @@ void QgsWMSServer::executeRequest()
|
158 | 160 | //GetMap
|
159 | 161 | else if ( request.compare( "GetMap", Qt::CaseInsensitive ) == 0 )
|
160 | 162 | {
|
| 163 | + //export as dxf |
| 164 | + QString format = mParameters.value( "FORMAT" ); |
| 165 | + if ( format.compare( "application/dxf", Qt::CaseInsensitive ) == 0 ) |
| 166 | + { |
| 167 | + try |
| 168 | + { |
| 169 | + getMapAsDxf(); |
| 170 | + cleanupAfterRequest(); |
| 171 | + return; |
| 172 | + } |
| 173 | + catch ( QgsMapServiceException& ex ) |
| 174 | + { |
| 175 | + QgsDebugMsg( "Caught exception during GetMap request" ); |
| 176 | + mRequestHandler->setServiceException( ex ); |
| 177 | + cleanupAfterRequest(); |
| 178 | + return; |
| 179 | + } |
| 180 | + } |
| 181 | + |
161 | 182 | QImage* result = 0;
|
162 | 183 | try
|
163 | 184 | {
|
@@ -446,7 +467,7 @@ QDomDocument QgsWMSServer::getCapabilities( QString version, bool fullProjectInf
|
446 | 467 |
|
447 | 468 | //wms:GetMap
|
448 | 469 | elem = doc.createElement( "GetMap"/*wms:GetMap*/ );
|
449 |
| - appendFormats( doc, elem, QStringList() << "image/jpeg" << "image/png" << "image/png; mode=16bit" << "image/png; mode=8bit" << "image/png; mode=1bit" ); |
| 470 | + appendFormats( doc, elem, QStringList() << "image/jpeg" << "image/png" << "image/png; mode=16bit" << "image/png; mode=8bit" << "image/png; mode=1bit" << "application/dxf" ); |
450 | 471 | elem.appendChild( dcpTypeElement.cloneNode().toElement() ); //this is the same as for 'GetCapabilities'
|
451 | 472 | requestElement.appendChild( elem );
|
452 | 473 |
|
@@ -1311,6 +1332,76 @@ QImage* QgsWMSServer::getMap( HitTest* hitTest )
|
1311 | 1332 | return theImage;
|
1312 | 1333 | }
|
1313 | 1334 |
|
| 1335 | +void QgsWMSServer::getMapAsDxf() |
| 1336 | +{ |
| 1337 | + QgsServerStreamingDevice d( "application/dxf" , mRequestHandler ); |
| 1338 | + if ( !d.open( QIODevice::WriteOnly ) ) |
| 1339 | + { |
| 1340 | + throw QgsMapServiceException( "Internal server error", "Error opening output device for writing" ); |
| 1341 | + } |
| 1342 | + |
| 1343 | + QgsDxfExport dxf; |
| 1344 | + |
| 1345 | + //BBOX |
| 1346 | + bool bboxOk; |
| 1347 | + QgsRectangle extent = _parseBBOX( mParameters.value( "BBOX", "0,0,0,0" ), bboxOk ); |
| 1348 | + if ( !bboxOk ) |
| 1349 | + { |
| 1350 | + extent = QgsRectangle(); |
| 1351 | + } |
| 1352 | + dxf.setExtent( extent ); |
| 1353 | + |
| 1354 | + //get format options (for MODE,SCALE, LAYERATTRIBUTES ) |
| 1355 | + QMap<QString, QString > formatOptionsMap; |
| 1356 | + readFormatOptions( formatOptionsMap ); |
| 1357 | + |
| 1358 | + QList< QPair<QgsVectorLayer *, int > > layers; |
| 1359 | + readDxfLayerSettings( layers, formatOptionsMap ); |
| 1360 | + dxf.addLayers( layers ); |
| 1361 | + |
| 1362 | + //MODE |
| 1363 | + QMap<QString, QString>::const_iterator modeIt = formatOptionsMap.find( "MODE" ); |
| 1364 | + |
| 1365 | + QgsDxfExport::SymbologyExport se; |
| 1366 | + if ( modeIt == formatOptionsMap.constEnd() ) |
| 1367 | + { |
| 1368 | + se = QgsDxfExport::NoSymbology; |
| 1369 | + } |
| 1370 | + else |
| 1371 | + { |
| 1372 | + if ( modeIt->compare( "SymbolLayerSymbology", Qt::CaseInsensitive ) == 0 ) |
| 1373 | + { |
| 1374 | + se = QgsDxfExport::SymbolLayerSymbology; |
| 1375 | + } |
| 1376 | + else if ( modeIt->compare( "FeatureSymbology", Qt::CaseInsensitive ) == 0 ) |
| 1377 | + { |
| 1378 | + se = QgsDxfExport::FeatureSymbology; |
| 1379 | + } |
| 1380 | + else |
| 1381 | + { |
| 1382 | + se = QgsDxfExport::NoSymbology; |
| 1383 | + } |
| 1384 | + } |
| 1385 | + dxf.setSymbologyExport( se ); |
| 1386 | + |
| 1387 | + //SCALE |
| 1388 | + QMap<QString, QString>::const_iterator scaleIt = formatOptionsMap.find( "SCALE" ); |
| 1389 | + if ( scaleIt != formatOptionsMap.constEnd() ) |
| 1390 | + { |
| 1391 | + dxf.setSymbologyScaleDenominator( scaleIt->toDouble() ); |
| 1392 | + } |
| 1393 | + |
| 1394 | + QString codec = "ISO-8859-1"; |
| 1395 | + QMap<QString, QString>::const_iterator codecIt = formatOptionsMap.find( "CODEC" ); |
| 1396 | + if ( codecIt != formatOptionsMap.constEnd() ) |
| 1397 | + { |
| 1398 | + codec = formatOptionsMap.value( "CODEC" ); |
| 1399 | + } |
| 1400 | + |
| 1401 | + dxf.writeToFile( &d, codec ); |
| 1402 | + d.close(); |
| 1403 | +} |
| 1404 | + |
1314 | 1405 | int QgsWMSServer::getFeatureInfo( QDomDocument& result, QString version )
|
1315 | 1406 | {
|
1316 | 1407 | if ( !mMapRenderer || !mConfigParser )
|
@@ -3114,3 +3205,82 @@ QgsRectangle QgsWMSServer::featureInfoSearchRect( QgsVectorLayer* ml, QgsMapRend
|
3114 | 3205 | infoPoint.x() + mapUnitTolerance, infoPoint.y() + mapUnitTolerance );
|
3115 | 3206 | return( mr->mapToLayerCoordinates( ml, mapRectangle ) );
|
3116 | 3207 | }
|
| 3208 | + |
| 3209 | +void QgsWMSServer::readFormatOptions( QMap<QString, QString>& formatOptions ) const |
| 3210 | +{ |
| 3211 | + formatOptions.clear(); |
| 3212 | + QString fo = mParameters.value( "FORMAT_OPTIONS" ); |
| 3213 | + QStringList formatOptionsList = fo.split( ";" ); |
| 3214 | + QStringList::const_iterator optionsIt = formatOptionsList.constBegin(); |
| 3215 | + for ( ; optionsIt != formatOptionsList.constEnd(); ++optionsIt ) |
| 3216 | + { |
| 3217 | + int equalIdx = optionsIt->indexOf( ":" ); |
| 3218 | + if ( equalIdx > 0 && equalIdx < ( optionsIt->length() - 1 ) ) |
| 3219 | + { |
| 3220 | + formatOptions.insert( optionsIt->left( equalIdx ).toUpper(), optionsIt->right( optionsIt->length() - equalIdx - 1 ).toUpper() ); |
| 3221 | + } |
| 3222 | + } |
| 3223 | +} |
| 3224 | + |
| 3225 | +void QgsWMSServer::readDxfLayerSettings( QList< QPair<QgsVectorLayer *, int > >& layers, const QMap<QString, QString>& formatOptionsMap ) const |
| 3226 | +{ |
| 3227 | + layers.clear(); |
| 3228 | + |
| 3229 | + QSet<QString> wfsLayers = QSet<QString>::fromList( mConfigParser->wfsLayerNames() ); |
| 3230 | + |
| 3231 | + QStringList layerAttributes; |
| 3232 | + QMap<QString, QString>::const_iterator layerAttributesIt = formatOptionsMap.find( "LAYERATTRIBUTES" ); |
| 3233 | + if ( layerAttributesIt != formatOptionsMap.constEnd() ) |
| 3234 | + { |
| 3235 | + layerAttributes = formatOptionsMap.value( "LAYERATTRIBUTES" ).split( "," ); |
| 3236 | + } |
| 3237 | + |
| 3238 | + //LAYERS and STYLES |
| 3239 | + QStringList layerList, styleList; |
| 3240 | + if ( readLayersAndStyles( layerList, styleList ) != 0 ) |
| 3241 | + { |
| 3242 | + return; |
| 3243 | + } |
| 3244 | + |
| 3245 | + for ( int i = 0; i < layerList.size(); ++i ) |
| 3246 | + { |
| 3247 | + QString layerName = layerList.at( i ); |
| 3248 | + QString styleName; |
| 3249 | + if ( styleList.size() > i ) |
| 3250 | + { |
| 3251 | + styleName = styleList.at( i ); |
| 3252 | + } |
| 3253 | + |
| 3254 | + QList<QgsMapLayer*> layerList = mConfigParser->mapLayerFromStyle( layerName, styleName ); |
| 3255 | + QList<QgsMapLayer*>::const_iterator layerIt = layerList.constBegin(); |
| 3256 | + for ( ; layerIt != layerList.constEnd(); ++layerIt ) |
| 3257 | + { |
| 3258 | + if ( !( *layerIt ) ) |
| 3259 | + { |
| 3260 | + continue; |
| 3261 | + } |
| 3262 | + |
| 3263 | + //vector layer? |
| 3264 | + if (( *layerIt )->type() != QgsMapLayer::VectorLayer ) |
| 3265 | + { |
| 3266 | + continue; |
| 3267 | + } |
| 3268 | + |
| 3269 | + QgsVectorLayer* vlayer = static_cast<QgsVectorLayer*>( *layerIt ); |
| 3270 | + |
| 3271 | + int layerAttribute = -1; |
| 3272 | + if ( layerAttributes.size() > i ) |
| 3273 | + { |
| 3274 | + layerAttribute = vlayer->pendingFields().indexFromName( layerAttributes.at( i ) ); |
| 3275 | + } |
| 3276 | + |
| 3277 | + //only wfs layers are allowed to be published |
| 3278 | + if ( !wfsLayers.contains( vlayer->name() ) ) |
| 3279 | + { |
| 3280 | + continue; |
| 3281 | + } |
| 3282 | + |
| 3283 | + layers.append( qMakePair( vlayer, layerAttribute ) ); |
| 3284 | + } |
| 3285 | + } |
| 3286 | +} |
0 commit comments