Skip to content

Commit 792e6bd

Browse files
committed
Always output algorithm results, even when input source has no
features Allows more versatile models which can handle empty layers
1 parent dd0f183 commit 792e6bd

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

src/core/processing/qgsnativealgorithms.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,11 @@ QVariantMap QgsBufferAlgorithm::processAlgorithm( const QVariantMap &parameters,
153153
const QgsProcessingParameterDefinition *distanceParamDef = parameterDefinition( QStringLiteral( "DISTANCE" ) );
154154

155155
long count = source->featureCount();
156-
if ( count <= 0 )
157-
return QVariantMap();
158156

159157
QgsFeature f;
160158
QgsFeatureIterator it = source->getFeatures();
161159

162-
double step = 100.0 / count;
160+
double step = count > 0 ? 100.0 / count : 1;
163161
int current = 0;
164162

165163
QList< QgsGeometry > bufferedGeometriesForDissolve;
@@ -254,13 +252,11 @@ QVariantMap QgsDissolveAlgorithm::processAlgorithm( const QVariantMap &parameter
254252
QStringList fields = parameterAsFields( parameters, QStringLiteral( "FIELD" ), context );
255253

256254
long count = source->featureCount();
257-
if ( count <= 0 )
258-
return QVariantMap();
259255

260256
QgsFeature f;
261257
QgsFeatureIterator it = source->getFeatures();
262258

263-
double step = 100.0 / count;
259+
double step = count > 0 ? 100.0 / count : 1;
264260
int current = 0;
265261

266262
if ( fields.isEmpty() )
@@ -421,8 +417,11 @@ QVariantMap QgsClipAlgorithm::processAlgorithm( const QVariantMap &parameters, Q
421417
clipGeoms << f.geometry();
422418
}
423419

420+
QVariantMap outputs;
421+
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
422+
424423
if ( clipGeoms.isEmpty() )
425-
return QVariantMap();
424+
return outputs;
426425

427426
// are we clipping against a single feature? if so, we can show finer progress reports
428427
bool singleClipFeature = false;
@@ -522,8 +521,6 @@ QVariantMap QgsClipAlgorithm::processAlgorithm( const QVariantMap &parameters, Q
522521
}
523522
}
524523

525-
QVariantMap outputs;
526-
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
527524
return outputs;
528525
}
529526

@@ -655,13 +652,11 @@ QVariantMap QgsMultipartToSinglepartAlgorithm::processAlgorithm( const QVariantM
655652
return QVariantMap();
656653

657654
long count = source->featureCount();
658-
if ( count <= 0 )
659-
return QVariantMap();
660655

661656
QgsFeature f;
662657
QgsFeatureIterator it = source->getFeatures();
663658

664-
double step = 100.0 / count;
659+
double step = count > 0 ? 100.0 / count : 1;
665660
int current = 0;
666661
while ( it.nextFeature( f ) )
667662
{
@@ -754,10 +749,8 @@ QVariantMap QgsExtractByExpressionAlgorithm::processAlgorithm( const QVariantMap
754749
QgsExpressionContext expressionContext = createExpressionContext( parameters, context );
755750

756751
long count = source->featureCount();
757-
if ( count <= 0 )
758-
return QVariantMap();
759752

760-
double step = 100.0 / count;
753+
double step = count > 0 ? 100.0 / count : 1;
761754
int current = 0;
762755

763756
if ( !nonMatchingSink )
@@ -953,10 +946,8 @@ QVariantMap QgsExtractByAttributeAlgorithm::processAlgorithm( const QVariantMap
953946
QgsExpressionContext expressionContext = createExpressionContext( parameters, context );
954947

955948
long count = source->featureCount();
956-
if ( count <= 0 )
957-
return QVariantMap();
958949

959-
double step = 100.0 / count;
950+
double step = count > 0 ? 100.0 / count : 1;
960951
int current = 0;
961952

962953
if ( !nonMatchingSink )
@@ -1058,10 +1049,8 @@ QVariantMap QgsRemoveNullGeometryAlgorithm::processAlgorithm( const QVariantMap
10581049
std::unique_ptr< QgsFeatureSink > nullSink( parameterAsSink( parameters, QStringLiteral( "NULL_OUTPUT" ), context, nullSinkId, source->fields() ) );
10591050

10601051
long count = source->featureCount();
1061-
if ( count <= 0 )
1062-
return QVariantMap();
10631052

1064-
double step = 100.0 / count;
1053+
double step = count > 0 ? 100.0 / count : 1;
10651054
int current = 0;
10661055

10671056
QgsFeature f;

src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,13 +642,11 @@ QVariantMap QgsProcessingFeatureBasedAlgorithm::processAlgorithm( const QVariant
642642
return QVariantMap();
643643

644644
long count = mSource->featureCount();
645-
if ( count <= 0 )
646-
return QVariantMap();
647645

648646
QgsFeature f;
649647
QgsFeatureIterator it = mSource->getFeatures();
650648

651-
double step = 100.0 / count;
649+
double step = count > 0 ? 100.0 / count : 1;
652650
int current = 0;
653651
while ( it.nextFeature( f ) )
654652
{

0 commit comments

Comments
 (0)