Skip to content

Commit

Permalink
filter features when exporting atlas pdf and filter is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Samweli authored and github-actions[bot] committed Aug 2, 2021
1 parent bfbd7cd commit e32d811
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
20 changes: 20 additions & 0 deletions python/core/auto_generated/layout/qgslayoutatlas.sip.in
Expand Up @@ -230,6 +230,26 @@ Sets whether features should be ``filtered`` in the coverage layer.
.. seealso:: :py:func:`filterFeatures`

.. seealso:: :py:func:`setFilterExpression`
%End

void filterCoverageLayer();
%Docstring
Filters the coverage layer according to the current atlas filter expression.
Only filters when the coverage layer features are set to be filtered by the atlas.

.. seealso:: :py:func:`unfilterCoverageLayer`

.. versionadded:: 3.22
%End

void unFilterCoverageLayer();
%Docstring
Removes the filters that the atlas applied on the coverage layer, by applying the original
coverage layer subset string.

.. seealso:: :py:func:`filterCoverageLayer`

.. versionadded:: 3.22
%End

QString filterExpression() const;
Expand Down
2 changes: 2 additions & 0 deletions src/analysis/processing/qgsalgorithmlayoutatlastoimage.cpp
Expand Up @@ -226,6 +226,7 @@ QVariantMap QgsLayoutAtlasToImageAlgorithm::processAlgorithm( const QVariantMap
if ( atlas->updateFeatures() )
{
feedback->pushInfo( QObject::tr( "Exporting %n atlas feature(s)", "", atlas->count() ) );
atlas->filterCoverageLayer();
switch ( exporter.exportToImage( atlas, fileName, extension, settings, error, feedback ) )
{
case QgsLayoutExporter::Success:
Expand All @@ -251,6 +252,7 @@ QVariantMap QgsLayoutAtlasToImageAlgorithm::processAlgorithm( const QVariantMap
// no meaning for imageexports, will not be encountered
break;
}
atlas->unFilterCoverageLayer();
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions src/analysis/processing/qgsalgorithmlayoutatlastopdf.cpp
Expand Up @@ -143,6 +143,7 @@ QVariantMap QgsLayoutAtlasToPdfAlgorithm::processAlgorithm( const QVariantMap &p

expression = parameterAsString( parameters, QStringLiteral( "FILTER_EXPRESSION" ), context );
atlas->setFilterExpression( expression, error );
atlas->setFilterFeatures( !expression.isEmpty() && error.isEmpty() );
if ( !expression.isEmpty() && !error.isEmpty() )
{
throw QgsProcessingException( QObject::tr( "Error setting atlas filter expression" ) );
Expand Down Expand Up @@ -207,6 +208,7 @@ QVariantMap QgsLayoutAtlasToPdfAlgorithm::processAlgorithm( const QVariantMap &p
if ( atlas->updateFeatures() )
{
feedback->pushInfo( QObject::tr( "Exporting %n atlas feature(s)", "", atlas->count() ) );
atlas->filterCoverageLayer();
switch ( exporter.exportToPdf( atlas, dest, settings, error, feedback ) )
{
case QgsLayoutExporter::Success:
Expand Down Expand Up @@ -234,6 +236,7 @@ QVariantMap QgsLayoutAtlasToPdfAlgorithm::processAlgorithm( const QVariantMap &p
// no meaning for imageexports, will not be encountered
break;
}
atlas->unFilterCoverageLayer();
}
else
{
Expand Down
20 changes: 15 additions & 5 deletions src/core/layout/qgslayoutatlas.cpp
Expand Up @@ -384,22 +384,32 @@ bool QgsLayoutAtlas::beginRender()
//no matching features found
return false;
}
if ( !mFilterExpression.isEmpty() )
mCoverageLayer->setSubsetString( mFilterExpression );

return true;
}

bool QgsLayoutAtlas::endRender()
{
if ( mCoverageLayer && !mFilterExpression.isEmpty() )
mCoverageLayer->setSubsetString( QString() );

emit featureChanged( QgsFeature() );
emit renderEnded();
return true;
}

void QgsLayoutAtlas::filterCoverageLayer()
{
if ( mFilterFeatures && !mFilterExpression.isEmpty() )
{
mOriginalFilterExpression = mCoverageLayer->subsetString();
mCoverageLayer->setSubsetString( mFilterExpression );
}
}

void QgsLayoutAtlas::unFilterCoverageLayer()
{
if ( mCoverageLayer && mFilterFeatures && !mFilterExpression.isEmpty() )
mCoverageLayer->setSubsetString( mOriginalFilterExpression );
}

int QgsLayoutAtlas::count() const
{
return mFeatureIds.size();
Expand Down
20 changes: 20 additions & 0 deletions src/core/layout/qgslayoutatlas.h
Expand Up @@ -214,6 +214,24 @@ class CORE_EXPORT QgsLayoutAtlas : public QObject, public QgsAbstractLayoutItera
*/
void setFilterFeatures( bool filtered );

/**
* Filters the coverage layer according to the current atlas filter expression.
* Only filters when the coverage layer features are set to be filtered by the atlas.
*
* \see unfilterCoverageLayer()
* \since QGIS 3.22
*/
void filterCoverageLayer();

/**
* Removes the filters that the atlas applied on the coverage layer, by applying the original
* coverage layer subset string.
*
* \see filterCoverageLayer()
* \since QGIS 3.22
*/
void unFilterCoverageLayer();

/**
* Returns the expression used for filtering features in the coverage layer.
*
Expand Down Expand Up @@ -369,6 +387,8 @@ class CORE_EXPORT QgsLayoutAtlas : public QObject, public QgsAbstractLayoutItera

QgsExpression mFilenameExpression;
QgsVectorLayerRef mCoverageLayer;
// The original value of the coverage layer subset string before filtering the layer.
QString mOriginalFilterExpression;

QString mCurrentFilename;
bool mSortFeatures = false;
Expand Down

0 comments on commit e32d811

Please sign in to comment.