Skip to content

Commit

Permalink
Change signature of processFeature so that features are no longer mod…
Browse files Browse the repository at this point in the history
…ified in place
  • Loading branch information
nyalldawson committed Jul 18, 2017
1 parent c2cad99 commit 29855b3
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 26 deletions.
7 changes: 3 additions & 4 deletions python/core/processing/qgsprocessingalgorithm.sip
Expand Up @@ -820,15 +820,14 @@ class QgsProcessingFeatureBasedAlgorithm : QgsProcessingAlgorithm
:rtype: QgsCoordinateReferenceSystem
%End

virtual bool processFeature( QgsFeature &feature, QgsProcessingFeedback *feedback ) = 0;
virtual QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) = 0;
%Docstring
Processes an individual input ``feature`` from the source. Algorithms should implement their
logic in this method for performing the algorithm's operation (e.g. replacing the feature's
geometry with the centroid of the original feature geometry for a 'centroid' type
algorithm).

Implementations should return true if the feature should be kept and added to the algorithm's
output sink, or false if the feature should be skipped and omitted from the output.
Implementations should return the modified feature.

The provided ``feedback`` object can be used to push messages to the log and for giving feedback
to users. Note that handling of progress reports and algorithm cancelation is handled by
Expand All @@ -838,7 +837,7 @@ class QgsProcessingFeatureBasedAlgorithm : QgsProcessingAlgorithm
prevent the algorithm execution from continuing. This can be annoying for users though as it
can break valid model execution - so use with extreme caution, and consider using
``feedback`` to instead report non-fatal processing failures for features instead.
:rtype: bool
:rtype: QgsFeature
%End

virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/AddTableField.py
Expand Up @@ -90,4 +90,4 @@ def processFeature(self, feature, feedback):
attributes = feature.attributes()
attributes.append(None)
feature.setAttributes(attributes)
return True
return feature
Expand Up @@ -57,4 +57,4 @@ def processFeature(self, feature, feedback):
attributes.append(self.current)
self.current += 1
feature.setAttributes(attributes)
return True
return feature
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/BoundingBox.py
Expand Up @@ -72,4 +72,4 @@ def processFeature(self, feature, feedback):

feature.setGeometry(output_geometry)

return True
return feature
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/DropMZValues.py
Expand Up @@ -91,4 +91,4 @@ def processFeature(self, feature, feedback):
new_geom.dropZValue()
feature.setGeometry(QgsGeometry(new_geom))

return True
return feature
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/SetMValue.py
Expand Up @@ -83,4 +83,4 @@ def processFeature(self, feature, feedback):

feature.setGeometry(QgsGeometry(new_geom))

return True
return feature
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/SetZValue.py
Expand Up @@ -83,4 +83,4 @@ def processFeature(self, feature, feedback):

feature.setGeometry(QgsGeometry(new_geom))

return True
return feature
15 changes: 9 additions & 6 deletions src/core/processing/qgsnativealgorithms.cpp
Expand Up @@ -87,8 +87,9 @@ QgsCentroidAlgorithm *QgsCentroidAlgorithm::createInstance() const
return new QgsCentroidAlgorithm();
}

bool QgsCentroidAlgorithm::processFeature( QgsFeature &feature, QgsProcessingFeedback *feedback )
QgsFeature QgsCentroidAlgorithm::processFeature( const QgsFeature &f, QgsProcessingFeedback *feedback )
{
QgsFeature feature = f;
if ( feature.hasGeometry() )
{
feature.setGeometry( feature.geometry().centroid() );
Expand All @@ -97,7 +98,7 @@ bool QgsCentroidAlgorithm::processFeature( QgsFeature &feature, QgsProcessingFee
feedback->pushInfo( QObject::tr( "Error calculating centroid for feature %1" ).arg( feature.id() ) );
}
}
return true;
return feature;
}
//
// QgsBufferAlgorithm
Expand Down Expand Up @@ -548,8 +549,9 @@ bool QgsTransformAlgorithm::prepareAlgorithm( const QVariantMap &parameters, Qgs
return true;
}

bool QgsTransformAlgorithm::processFeature( QgsFeature &feature, QgsProcessingFeedback * )
QgsFeature QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProcessingFeedback * )
{
QgsFeature feature = f;
if ( !mCreatedTransform )
{
mCreatedTransform = true;
Expand All @@ -568,7 +570,7 @@ bool QgsTransformAlgorithm::processFeature( QgsFeature &feature, QgsProcessingFe
feature.clearGeometry();
}
}
return true;
return feature;
}


Expand Down Expand Up @@ -598,8 +600,9 @@ QgsWkbTypes::Type QgsSubdivideAlgorithm::outputWkbType( QgsWkbTypes::Type inputW
return QgsWkbTypes::multiType( inputWkbType );
}

bool QgsSubdivideAlgorithm::processFeature( QgsFeature &feature, QgsProcessingFeedback *feedback )
QgsFeature QgsSubdivideAlgorithm::processFeature( const QgsFeature &f, QgsProcessingFeedback *feedback )
{
QgsFeature feature = f;
if ( feature.hasGeometry() )
{
feature.setGeometry( feature.geometry().subdivide( mMaxNodes ) );
Expand All @@ -608,7 +611,7 @@ bool QgsSubdivideAlgorithm::processFeature( QgsFeature &feature, QgsProcessingFe
feedback->reportError( QObject::tr( "Error calculating subdivision for feature %1" ).arg( feature.id() ) );
}
}
return true;
return feature;
}

bool QgsSubdivideAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
Expand Down
6 changes: 3 additions & 3 deletions src/core/processing/qgsnativealgorithms.h
Expand Up @@ -68,7 +68,7 @@ class QgsCentroidAlgorithm : public QgsProcessingFeatureBasedAlgorithm
QgsProcessing::LayerType outputLayerType() const override { return QgsProcessing::TypeVectorPoint; }
QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const override { Q_UNUSED( inputWkbType ); return QgsWkbTypes::Point; }

bool processFeature( QgsFeature &feature, QgsProcessingFeedback *feedback ) override;
QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) override;
};

/**
Expand All @@ -94,7 +94,7 @@ class QgsTransformAlgorithm : public QgsProcessingFeatureBasedAlgorithm
QString outputName() const override { return QObject::tr( "Reprojected" ); }

bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
bool processFeature( QgsFeature &feature, QgsProcessingFeedback *feedback ) override;
QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) override;

private:

Expand Down Expand Up @@ -262,7 +262,7 @@ class QgsSubdivideAlgorithm : public QgsProcessingFeatureBasedAlgorithm
QString outputName() const override { return QObject::tr( "Subdivided" ); }

QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const override;
bool processFeature( QgsFeature &feature, QgsProcessingFeedback *feedback ) override;
QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) override;

bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;

Expand Down
6 changes: 2 additions & 4 deletions src/core/processing/qgsprocessingalgorithm.cpp
Expand Up @@ -655,10 +655,8 @@ QVariantMap QgsProcessingFeatureBasedAlgorithm::processAlgorithm( const QVariant
break;
}

if ( processFeature( f, feedback ) )
{
sink->addFeature( f, QgsFeatureSink::FastInsert );
}
QgsFeature transformed = processFeature( f, feedback );
sink->addFeature( transformed, QgsFeatureSink::FastInsert );

feedback->setProgress( current * step );
current++;
Expand Down
5 changes: 2 additions & 3 deletions src/core/processing/qgsprocessingalgorithm.h
Expand Up @@ -798,8 +798,7 @@ class CORE_EXPORT QgsProcessingFeatureBasedAlgorithm : public QgsProcessingAlgor
* geometry with the centroid of the original feature geometry for a 'centroid' type
* algorithm).
*
* Implementations should return true if the feature should be kept and added to the algorithm's
* output sink, or false if the feature should be skipped and omitted from the output.
* Implementations should return the modified feature.
*
* The provided \a feedback object can be used to push messages to the log and for giving feedback
* to users. Note that handling of progress reports and algorithm cancelation is handled by
Expand All @@ -810,7 +809,7 @@ class CORE_EXPORT QgsProcessingFeatureBasedAlgorithm : public QgsProcessingAlgor
* can break valid model execution - so use with extreme caution, and consider using
* \a feedback to instead report non-fatal processing failures for features instead.
*/
virtual bool processFeature( QgsFeature &feature, QgsProcessingFeedback *feedback ) = 0;
virtual QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) = 0;

virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
Expand Down

0 comments on commit 29855b3

Please sign in to comment.