Skip to content
Permalink
Browse files

Change signature of processFeature so that features are no longer mod…

…ified in place
  • Loading branch information
nyalldawson committed Jul 18, 2017
1 parent c2cad99 commit 29855b39423087dd847839cb31577953c2896489
@@ -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
@@ -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,
@@ -90,4 +90,4 @@ def processFeature(self, feature, feedback):
attributes = feature.attributes()
attributes.append(None)
feature.setAttributes(attributes)
return True
return feature
@@ -57,4 +57,4 @@ def processFeature(self, feature, feedback):
attributes.append(self.current)
self.current += 1
feature.setAttributes(attributes)
return True
return feature
@@ -72,4 +72,4 @@ def processFeature(self, feature, feedback):

feature.setGeometry(output_geometry)

return True
return feature
@@ -91,4 +91,4 @@ def processFeature(self, feature, feedback):
new_geom.dropZValue()
feature.setGeometry(QgsGeometry(new_geom))

return True
return feature
@@ -83,4 +83,4 @@ def processFeature(self, feature, feedback):

feature.setGeometry(QgsGeometry(new_geom))

return True
return feature
@@ -83,4 +83,4 @@ def processFeature(self, feature, feedback):

feature.setGeometry(QgsGeometry(new_geom))

return True
return feature
@@ -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() );
@@ -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
@@ -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;
@@ -568,7 +570,7 @@ bool QgsTransformAlgorithm::processFeature( QgsFeature &feature, QgsProcessingFe
feature.clearGeometry();
}
}
return true;
return feature;
}


@@ -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 ) );
@@ -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 * )
@@ -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;
};

/**
@@ -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:

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

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

0 comments on commit 29855b3

Please sign in to comment.
You can’t perform that action at this time.