Skip to content

Commit

Permalink
Allow to request all atlas features with 'ATLAS_PK=*' if the number i…
Browse files Browse the repository at this point in the history
…s lower than the feature limit
  • Loading branch information
mhugent committed Jan 5, 2019
1 parent 2e7bf12 commit 492ee5f
Showing 1 changed file with 59 additions and 41 deletions.
100 changes: 59 additions & 41 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -396,61 +396,79 @@ namespace QgsWms
QStringLiteral( "The atlas has no coverage layer" ) );
}

QgsAttributeList pkIndexes = cLayer->primaryKeyAttributes();
if ( pkIndexes.size() < 1 )
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "An error occurred during the Atlas print" ) );
}
QStringList pkAttributeNames;
for ( int i = 0; i < pkIndexes.size(); ++i )
int maxAtlasFeatures = QgsServerProjectUtils::wmsMaxAtlasFeatures( *mProject );
if ( atlasPk.size() == 1 && atlasPk.at( 0 ) == QStringLiteral( "*" ) )
{
pkAttributeNames.append( cLayer->fields()[pkIndexes.at( i )].name() );
atlas->setFilterFeatures( false );
atlas->updateFeatures();
if ( atlas->count() > maxAtlasFeatures )
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QString( "The project configuration allows to print maximum %1 atlas features at a time" ).arg( maxAtlasFeatures ) );
}
}

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
else
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "Wrong number of ATLAS_PK parameters" ) );
}

//number of atlas features might be restricted
int maxAtlasFeatures = QgsServerProjectUtils::wmsMaxAtlasFeatures( *mProject );
nAtlasFeatures = std::min( nAtlasFeatures, maxAtlasFeatures );
QgsAttributeList pkIndexes = cLayer->primaryKeyAttributes();
if ( pkIndexes.size() < 1 )
{
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QStringLiteral( "An error occurred during the Atlas print" ) );
}
QStringList pkAttributeNames;
for ( int i = 0; i < pkIndexes.size(); ++i )
{
pkAttributeNames.append( cLayer->fields()[pkIndexes.at( i )].name() );
}

QString filterString;
int currentAtlasPk = 0;
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" ) );
}

for ( int i = 0; i < nAtlasFeatures; ++i )
{
if ( i > 0 )
//number of atlas features might be restricted
if ( nAtlasFeatures > maxAtlasFeatures )
{
filterString.append( " OR " );
throw QgsBadRequestException( QStringLiteral( "AtlasPrintError" ),
QString( "%1 atlas features have been requestet, but the project configuration only allows to print %2 atlas features at a time" )
.arg( nAtlasFeatures ).arg( maxAtlasFeatures ) );
}

filterString.append( "( " );
QString filterString;
int currentAtlasPk = 0;

for ( int j = 0; j < pkIndexes.size(); ++j )
for ( int i = 0; i < nAtlasFeatures; ++i )
{
if ( j > 0 )
if ( i > 0 )
{
filterString.append( " AND " );
filterString.append( " OR " );
}
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 occurred during the Atlas print" ) );
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( " )" );
}

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

Expand Down

0 comments on commit 492ee5f

Please sign in to comment.