Skip to content

Commit

Permalink
Show atlas layers, atlas coverage layers and primary key attributes i…
Browse files Browse the repository at this point in the history
…n getProjectSettings response
  • Loading branch information
mhugent committed Jan 5, 2019
1 parent 21e3adf commit 69de8f9
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 66 deletions.
34 changes: 34 additions & 0 deletions src/server/services/wms/qgswmsgetcapabilities.cpp
Expand Up @@ -23,6 +23,7 @@
#include "qgsserverprojectutils.h"

#include "qgslayoutmanager.h"
#include "qgslayoutatlas.h"
#include "qgsprintlayout.h"
#include "qgslayoutitemmap.h"
#include "qgslayoutitemlabel.h"
Expand Down Expand Up @@ -680,6 +681,23 @@ namespace QgsWms
composerTemplateElem.setAttribute( QStringLiteral( "width" ), width.length() );
composerTemplateElem.setAttribute( QStringLiteral( "height" ), height.length() );

//atlas enabled and atlas covering layer
QgsLayoutAtlas *atlas = layout->atlas();
if ( atlas && atlas->enabled() )
{
composerTemplateElem.setAttribute( QStringLiteral( "atlasEnabled" ), QStringLiteral( "1" ) );
QgsVectorLayer *cLayer = atlas->coverageLayer();
if ( cLayer )
{
QString layerName = cLayer->shortName();
if ( layerName.isEmpty() )
{
layerName = cLayer->name();
}
composerTemplateElem.setAttribute( QStringLiteral( "atlasCoverageLayer" ), layerName );
}
}

//add available composer maps and their size in mm
QList<QgsLayoutItemMap *> layoutMapList;
layout->layoutItems<QgsLayoutItemMap>( layoutMapList );
Expand Down Expand Up @@ -1739,6 +1757,22 @@ namespace QgsWms
//displayfield
layerElem.setAttribute( QStringLiteral( "displayField" ), displayField );

//primary key
QgsAttributeList pkAttributes = vLayer->primaryKeyAttributes();
if ( pkAttributes.size() > 0 )
{
QDomElement pkElem = doc.createElement( QStringLiteral( "PrimaryKey" ) );
QgsAttributeList::const_iterator pkIt = pkAttributes.constBegin();
for ( ; pkIt != pkAttributes.constEnd(); ++pkIt )
{
QDomElement pkAttributeElem = doc.createElement( QStringLiteral( "PrimaryKeyAttribute" ) );
QDomText pkAttName = doc.createTextNode( layerFields.at( *pkIt ).name() );
pkAttributeElem.appendChild( pkAttName );
pkElem.appendChild( pkAttributeElem );
}
layerElem.appendChild( pkElem );
}

//geometry type
layerElem.setAttribute( QStringLiteral( "geometryType" ), QgsWkbTypes::displayString( vLayer->wkbType() ) );

Expand Down
132 changes: 66 additions & 66 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -377,82 +377,82 @@ namespace QgsWms
std::unique_ptr<QgsPrintLayout> layout( sourceLayout->clone() );

//atlas print?
QgsLayoutAtlas* atlas = 0;
QgsLayoutAtlas *atlas = 0;
QStringList atlasPk = mWmsParameters.atlasPk();
if ( mWmsParameters.atlasPrint() )
{
atlas = layout->atlas();
if ( !atlas || !atlas->enabled() )
atlas = layout->atlas();
if ( !atlas || !atlas->enabled() )
{
//error
throw QgsBadRequestException( QStringLiteral( "NoAtlas" ),
QStringLiteral( "The template has no atlas enabled" ) );
}

QgsVectorLayer *cLayer = atlas->coverageLayer();
if ( !cLayer )
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "The atlas has no coverage layer" ) );
}
QgsVectorDataProvider *cProvider = cLayer->dataProvider();
if ( !cProvider )
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "An error occured during the Atlas print" ) );
}

QgsAttributeList pkIndexes = cProvider->pkAttributeIndexes();
if ( pkIndexes.size() < 1 )
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "An error occured during the Atlas print" ) );
}
QStringList pkAttributeNames;
for ( int i = 0; i < pkIndexes.size(); ++i )
{
pkAttributeNames.append( cProvider->fields()[pkIndexes.at( i )].name() );
}

int nAtlasFeatures = atlasPk.size() / pkIndexes.size();
if ( nAtlasFeatures * pkIndexes.size() != atlasPk.size() ) //Test is atlasPk.size() is a multiple of pkIndexes.size(). Bail out if not
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "Wrong number of ATLAS_PK parameters" ) );
}

QString filterString;
int currentAtlasPk = 0;
for ( int i = 0; i < nAtlasFeatures; ++i )
{
if ( i > 0 )
{
//error
throw QgsBadRequestException( QStringLiteral( "NoAtlas" ),
QStringLiteral( "The template has no atlas enabled" ) );
filterString.append( " OR " );
}

QgsVectorLayer* cLayer = atlas->coverageLayer();
if( !cLayer )
filterString.append( "( " );

for ( int j = 0; j < pkIndexes.size(); ++j )
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "The atlas has no covering layer" ) );
if ( j > 0 )
{
filterString.append( " AND " );
}
filterString.append( QString( "\"%1\" = %2" ).arg( pkAttributeNames.at( j ) ).arg( atlasPk.at( currentAtlasPk ) ) );
++currentAtlasPk;
}
QgsVectorDataProvider* cProvider = cLayer->dataProvider();
if( !cProvider )
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "An error occured during the Atlas print" ) );
}

QgsAttributeList pkIndexes = cProvider->pkAttributeIndexes();
if( pkIndexes.size() < 1 )
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "An error occured during the Atlas print" ) );
}
QStringList pkAttributeNames;
for( int i = 0; i < pkIndexes.size(); ++i )
{
pkAttributeNames.append( cProvider->fields()[pkIndexes.at( i )].name() );
}

int nAtlasFeatures = atlasPk.size() / pkIndexes.size();
if( nAtlasFeatures * pkIndexes.size() != atlasPk.size() )//Test is atlasPk.size() is a multiple of pkIndexes.size(). Bail out if not
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "Wrong number of ATLAS_PK parameters" ) );
}

QString filterString;
int currentAtlasPk = 0;
for( int i = 0; i < nAtlasFeatures; ++i )
{
if( i > 0 )
{
filterString.append( " OR " );
}

filterString.append( "( " );

for( int j = 0; j < pkIndexes.size(); ++j )
{
if( j > 0 )
{
filterString.append( " AND " );
}
filterString.append( QString( "\"%1\" = %2" ).arg( pkAttributeNames.at( j ) ).arg( atlasPk.at( currentAtlasPk ) ) );
++currentAtlasPk;
}
filterString.append( " )" );
}

filterString.append( " )" );
}

atlas->setFilterFeatures( true );
QString errorString;
atlas->setFilterExpression( filterString, errorString );
if( !errorString.isEmpty() )
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "An error occured during the Atlas print" ) );
}
atlas->setFilterFeatures( true );
QString errorString;
atlas->setFilterExpression( filterString, errorString );
if ( !errorString.isEmpty() )
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "An error occured during the Atlas print" ) );
}
}

configurePrintLayout( layout.get(), mapSettings, atlas );
Expand Down

0 comments on commit 69de8f9

Please sign in to comment.