Add prepareSource method

This commit is contained in:
Alessandro Pasotti 2018-09-11 13:02:24 +02:00 committed by Nyall Dawson
parent c7ac4fe7fd
commit 0380c06003
5 changed files with 49 additions and 4 deletions

View File

@ -984,6 +984,13 @@ checks based on the geometry type of the layer.
:return: true if the algorithm supports in-place editing
.. versionadded:: 3.4
%End
void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );
%Docstring
Read the source from ``parameters`` and ``context`` and set it
.. versionadded:: 3.4
%End

View File

@ -79,6 +79,7 @@ QgsTransformAlgorithm *QgsTransformAlgorithm::createInstance() const
bool QgsTransformAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
prepareSource( parameters, context );
mDestCrs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );
mTransformContext = context.project() ? context.project()->transformContext() : QgsCoordinateTransformContext();
return true;

View File

@ -768,6 +768,7 @@ bool QgsProcessingAlgorithm::supportInPlaceEdit( const QgsVectorLayer *layer ) c
return false;
}
bool QgsProcessingAlgorithm::createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter )
{
if ( !parameter->isDestination() )
@ -852,10 +853,7 @@ QgsCoordinateReferenceSystem QgsProcessingFeatureBasedAlgorithm::sourceCrs() con
QVariantMap QgsProcessingFeatureBasedAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
mSource.reset( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !mSource )
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
prepareSource( parameters, context );
QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest,
outputFields( mSource->fields() ),
@ -933,3 +931,13 @@ bool QgsProcessingFeatureBasedAlgorithm::supportInPlaceEdit( const QgsVectorLaye
return true;
}
void QgsProcessingFeatureBasedAlgorithm::prepareSource( const QVariantMap &parameters, QgsProcessingContext &context )
{
if ( ! mSource )
{
mSource.reset( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !mSource )
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
}
}

View File

@ -826,6 +826,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
bool createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter );
friend class QgsProcessingProvider;
friend class TestQgsProcessing;
friend class QgsProcessingModelAlgorithm;
@ -990,6 +991,13 @@ class CORE_EXPORT QgsProcessingFeatureBasedAlgorithm : public QgsProcessingAlgor
*/
virtual bool supportInPlaceEdit( const QgsVectorLayer *layer ) const override;
/**
* Read the source from \a parameters and \a context and set it
*
* \since QGIS 3.4
*/
void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );
private:
std::unique_ptr< QgsProcessingFeatureSource > mSource;

View File

@ -88,6 +88,7 @@ class TestQgsProcessingInPlace(unittest.TestCase):
context.setProject(QgsProject.instance())
feedback = QgsProcessingFeedback()
input_layer.rollBack()
with self.assertRaises(QgsProcessingException) as cm:
execute_in_place_run(
alg, input_layer, parameters, context=context, feedback=feedback, raise_exceptions=True)
@ -199,6 +200,26 @@ class TestQgsProcessingInPlace(unittest.TestCase):
# Check selected
self.assertEqual(len(self.vl.selectedFeatureIds()), 3)
def test_reprojectlayer(self):
"""Check that this runs correctly"""
old_count = self.vl.featureCount()
old_features, new_features = self._alg_tester(
'native:reprojectlayer',
self.vl,
{
'TARGET_CRS': 'EPSG:3857',
}
)
g = [f.geometry() for f in new_features][0]
self.assertAlmostEqual(g.get().x(), 1001875.4, 1)
self.assertAlmostEqual(g.get().y(), 5621521.5, 1)
# Check selected
self.assertEqual(self.vl.selectedFeatureIds(), [1])
def _make_compatible_tester(self, feature_wkt, layer_wkb_name, attrs=[1], old_feature=None):
fields = QgsFields()
wkb_type = getattr(QgsWkbTypes, layer_wkb_name)