Skip to content

Commit 69de8f9

Browse files
committed
Show atlas layers, atlas coverage layers and primary key attributes in getProjectSettings response
1 parent 21e3adf commit 69de8f9

File tree

2 files changed

+100
-66
lines changed

2 files changed

+100
-66
lines changed

src/server/services/wms/qgswmsgetcapabilities.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgsserverprojectutils.h"
2424

2525
#include "qgslayoutmanager.h"
26+
#include "qgslayoutatlas.h"
2627
#include "qgsprintlayout.h"
2728
#include "qgslayoutitemmap.h"
2829
#include "qgslayoutitemlabel.h"
@@ -680,6 +681,23 @@ namespace QgsWms
680681
composerTemplateElem.setAttribute( QStringLiteral( "width" ), width.length() );
681682
composerTemplateElem.setAttribute( QStringLiteral( "height" ), height.length() );
682683

684+
//atlas enabled and atlas covering layer
685+
QgsLayoutAtlas *atlas = layout->atlas();
686+
if ( atlas && atlas->enabled() )
687+
{
688+
composerTemplateElem.setAttribute( QStringLiteral( "atlasEnabled" ), QStringLiteral( "1" ) );
689+
QgsVectorLayer *cLayer = atlas->coverageLayer();
690+
if ( cLayer )
691+
{
692+
QString layerName = cLayer->shortName();
693+
if ( layerName.isEmpty() )
694+
{
695+
layerName = cLayer->name();
696+
}
697+
composerTemplateElem.setAttribute( QStringLiteral( "atlasCoverageLayer" ), layerName );
698+
}
699+
}
700+
683701
//add available composer maps and their size in mm
684702
QList<QgsLayoutItemMap *> layoutMapList;
685703
layout->layoutItems<QgsLayoutItemMap>( layoutMapList );
@@ -1739,6 +1757,22 @@ namespace QgsWms
17391757
//displayfield
17401758
layerElem.setAttribute( QStringLiteral( "displayField" ), displayField );
17411759

1760+
//primary key
1761+
QgsAttributeList pkAttributes = vLayer->primaryKeyAttributes();
1762+
if ( pkAttributes.size() > 0 )
1763+
{
1764+
QDomElement pkElem = doc.createElement( QStringLiteral( "PrimaryKey" ) );
1765+
QgsAttributeList::const_iterator pkIt = pkAttributes.constBegin();
1766+
for ( ; pkIt != pkAttributes.constEnd(); ++pkIt )
1767+
{
1768+
QDomElement pkAttributeElem = doc.createElement( QStringLiteral( "PrimaryKeyAttribute" ) );
1769+
QDomText pkAttName = doc.createTextNode( layerFields.at( *pkIt ).name() );
1770+
pkAttributeElem.appendChild( pkAttName );
1771+
pkElem.appendChild( pkAttributeElem );
1772+
}
1773+
layerElem.appendChild( pkElem );
1774+
}
1775+
17421776
//geometry type
17431777
layerElem.setAttribute( QStringLiteral( "geometryType" ), QgsWkbTypes::displayString( vLayer->wkbType() ) );
17441778

src/server/services/wms/qgswmsrenderer.cpp

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -377,82 +377,82 @@ namespace QgsWms
377377
std::unique_ptr<QgsPrintLayout> layout( sourceLayout->clone() );
378378

379379
//atlas print?
380-
QgsLayoutAtlas* atlas = 0;
380+
QgsLayoutAtlas *atlas = 0;
381381
QStringList atlasPk = mWmsParameters.atlasPk();
382382
if ( mWmsParameters.atlasPrint() )
383383
{
384-
atlas = layout->atlas();
385-
if ( !atlas || !atlas->enabled() )
384+
atlas = layout->atlas();
385+
if ( !atlas || !atlas->enabled() )
386+
{
387+
//error
388+
throw QgsBadRequestException( QStringLiteral( "NoAtlas" ),
389+
QStringLiteral( "The template has no atlas enabled" ) );
390+
}
391+
392+
QgsVectorLayer *cLayer = atlas->coverageLayer();
393+
if ( !cLayer )
394+
{
395+
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
396+
QStringLiteral( "The atlas has no coverage layer" ) );
397+
}
398+
QgsVectorDataProvider *cProvider = cLayer->dataProvider();
399+
if ( !cProvider )
400+
{
401+
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
402+
QStringLiteral( "An error occured during the Atlas print" ) );
403+
}
404+
405+
QgsAttributeList pkIndexes = cProvider->pkAttributeIndexes();
406+
if ( pkIndexes.size() < 1 )
407+
{
408+
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
409+
QStringLiteral( "An error occured during the Atlas print" ) );
410+
}
411+
QStringList pkAttributeNames;
412+
for ( int i = 0; i < pkIndexes.size(); ++i )
413+
{
414+
pkAttributeNames.append( cProvider->fields()[pkIndexes.at( i )].name() );
415+
}
416+
417+
int nAtlasFeatures = atlasPk.size() / pkIndexes.size();
418+
if ( nAtlasFeatures * pkIndexes.size() != atlasPk.size() ) //Test is atlasPk.size() is a multiple of pkIndexes.size(). Bail out if not
419+
{
420+
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
421+
QStringLiteral( "Wrong number of ATLAS_PK parameters" ) );
422+
}
423+
424+
QString filterString;
425+
int currentAtlasPk = 0;
426+
for ( int i = 0; i < nAtlasFeatures; ++i )
427+
{
428+
if ( i > 0 )
386429
{
387-
//error
388-
throw QgsBadRequestException( QStringLiteral( "NoAtlas" ),
389-
QStringLiteral( "The template has no atlas enabled" ) );
430+
filterString.append( " OR " );
390431
}
391432

392-
QgsVectorLayer* cLayer = atlas->coverageLayer();
393-
if( !cLayer )
433+
filterString.append( "( " );
434+
435+
for ( int j = 0; j < pkIndexes.size(); ++j )
394436
{
395-
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
396-
QStringLiteral( "The atlas has no covering layer" ) );
437+
if ( j > 0 )
438+
{
439+
filterString.append( " AND " );
440+
}
441+
filterString.append( QString( "\"%1\" = %2" ).arg( pkAttributeNames.at( j ) ).arg( atlasPk.at( currentAtlasPk ) ) );
442+
++currentAtlasPk;
397443
}
398-
QgsVectorDataProvider* cProvider = cLayer->dataProvider();
399-
if( !cProvider )
400-
{
401-
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
402-
QStringLiteral( "An error occured during the Atlas print" ) );
403-
}
404-
405-
QgsAttributeList pkIndexes = cProvider->pkAttributeIndexes();
406-
if( pkIndexes.size() < 1 )
407-
{
408-
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
409-
QStringLiteral( "An error occured during the Atlas print" ) );
410-
}
411-
QStringList pkAttributeNames;
412-
for( int i = 0; i < pkIndexes.size(); ++i )
413-
{
414-
pkAttributeNames.append( cProvider->fields()[pkIndexes.at( i )].name() );
415-
}
416-
417-
int nAtlasFeatures = atlasPk.size() / pkIndexes.size();
418-
if( nAtlasFeatures * pkIndexes.size() != atlasPk.size() )//Test is atlasPk.size() is a multiple of pkIndexes.size(). Bail out if not
419-
{
420-
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
421-
QStringLiteral( "Wrong number of ATLAS_PK parameters" ) );
422-
}
423-
424-
QString filterString;
425-
int currentAtlasPk = 0;
426-
for( int i = 0; i < nAtlasFeatures; ++i )
427-
{
428-
if( i > 0 )
429-
{
430-
filterString.append( " OR " );
431-
}
432-
433-
filterString.append( "( " );
434444

435-
for( int j = 0; j < pkIndexes.size(); ++j )
436-
{
437-
if( j > 0 )
438-
{
439-
filterString.append( " AND " );
440-
}
441-
filterString.append( QString( "\"%1\" = %2" ).arg( pkAttributeNames.at( j ) ).arg( atlasPk.at( currentAtlasPk ) ) );
442-
++currentAtlasPk;
443-
}
445+
filterString.append( " )" );
446+
}
444447

445-
filterString.append( " )" );
446-
}
447-
448-
atlas->setFilterFeatures( true );
449-
QString errorString;
450-
atlas->setFilterExpression( filterString, errorString );
451-
if( !errorString.isEmpty() )
452-
{
453-
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
454-
QStringLiteral( "An error occured during the Atlas print" ) );
455-
}
448+
atlas->setFilterFeatures( true );
449+
QString errorString;
450+
atlas->setFilterExpression( filterString, errorString );
451+
if ( !errorString.isEmpty() )
452+
{
453+
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
454+
QStringLiteral( "An error occured during the Atlas print" ) );
455+
}
456456
}
457457

458458
configurePrintLayout( layout.get(), mapSettings, atlas );

0 commit comments

Comments
 (0)