@@ -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